Skip to content

Commit ebb495d

Browse files
committed
fix(@ngtools/webpack): add parent nodes and keep program
Technically that program should always be the valid one, and is needed in some cases (e.g. diagnostics). Adding parent nodes get rid of the getChildAt error that happens, because the prop.parent is not set. The issue happened because people are using "files": ["main.ts"] or something similar, and when we load another file than main we dont set the parent nodes. Fixes #5143 Fixes #4817
1 parent 82ddc5c commit ebb495d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/@ngtools/webpack/src/loader.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ describe('@ngtools/webpack', () => {
3434
expect(refactor2.sourceText).toMatch(/\(\{\s*otherValue2: 2\s*,\s*otherValue3: 3\s*}\)/);
3535
expect(refactor2.sourceText).toMatch(/\(\{\s*otherValue4: 4\s*}\)/);
3636
});
37+
38+
it('should work without a root name', () => {
39+
const host = new WebpackCompilerHost({}, '');
40+
host.writeFile('/file.ts', `
41+
import './file2.ts';
42+
`, false);
43+
host.writeFile('/file2.ts', `
44+
@SomeDecorator({ moduleId: 123 }) class CLS {}
45+
@SomeDecorator({ moduleId: 123, otherValue1: 1 }) class CLS2 {}
46+
@SomeDecorator({ otherValue2: 2, moduleId: 123, otherValue3: 3 }) class CLS3 {}
47+
@SomeDecorator({ otherValue4: 4, moduleId: 123 }) class CLS4 {}
48+
`, false);
49+
50+
const program = ts.createProgram(['/file.ts'], {}, host);
51+
const refactor = new TypeScriptFileRefactor('/file2.ts', host, program);
52+
removeModuleIdOnlyForTesting(refactor);
53+
expect(refactor.sourceText).toMatch(/\(\{\s+}\)/);
54+
expect(refactor.sourceText).toMatch(/\(\{\s*otherValue1: 1\s*}\)/);
55+
expect(refactor.sourceText).toMatch(/\(\{\s*otherValue2: 2\s*,\s*otherValue3: 3\s*}\)/);
56+
expect(refactor.sourceText).toMatch(/\(\{\s*otherValue4: 4\s*}\)/);
57+
});
3758
});
3859
});
3960
});

packages/@ngtools/webpack/src/refactor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ export class TypeScriptFileRefactor {
4545
this._sourceFile = _program.getSourceFile(fileName);
4646
}
4747
if (!this._sourceFile) {
48-
this._program = null;
4948
this._sourceFile = ts.createSourceFile(fileName, _host.readFile(fileName),
50-
ts.ScriptTarget.Latest);
49+
ts.ScriptTarget.Latest, true);
5150
}
5251
this._sourceText = this._sourceFile.getFullText(this._sourceFile);
5352
this._sourceString = new MagicString(this._sourceText);

0 commit comments

Comments
 (0)