Skip to content

Commit

Permalink
fix(@ngtools/webpack): add parent nodes and keep program
Browse files Browse the repository at this point in the history
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 angular#5143
Fixes angular#4817
  • Loading branch information
hansl committed Mar 19, 2017
1 parent 864e84f commit 1d3b7f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 21 additions & 0 deletions packages/@ngtools/webpack/src/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ describe('@ngtools/webpack', () => {
expect(refactor2.sourceText).toMatch(/\(\{\s*otherValue2: 2\s*,\s*otherValue3: 3\s*}\)/);
expect(refactor2.sourceText).toMatch(/\(\{\s*otherValue4: 4\s*}\)/);
});

it('should work without a root name', () => {
const host = new WebpackCompilerHost({}, '');
host.writeFile('/file.ts', `
import './file2.ts';
`, false);
host.writeFile('/file2.ts', `
@SomeDecorator({ moduleId: 123 }) class CLS {}
@SomeDecorator({ moduleId: 123, otherValue1: 1 }) class CLS2 {}
@SomeDecorator({ otherValue2: 2, moduleId: 123, otherValue3: 3 }) class CLS3 {}
@SomeDecorator({ otherValue4: 4, moduleId: 123 }) class CLS4 {}
`, false);

const program = ts.createProgram(['/file.ts'], {}, host);
const refactor = new TypeScriptFileRefactor('/file2.ts', host, program);
removeModuleIdOnlyForTesting(refactor);
expect(refactor.sourceText).toMatch(/\(\{\s+}\)/);
expect(refactor.sourceText).toMatch(/\(\{\s*otherValue1: 1\s*}\)/);
expect(refactor.sourceText).toMatch(/\(\{\s*otherValue2: 2\s*,\s*otherValue3: 3\s*}\)/);
expect(refactor.sourceText).toMatch(/\(\{\s*otherValue4: 4\s*}\)/);
});
});
});
});
4 changes: 2 additions & 2 deletions packages/@ngtools/webpack/src/refactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export class TypeScriptFileRefactor {
this._sourceFile = _program.getSourceFile(fileName);
}
if (!this._sourceFile) {
this._program = null;
// this._program = null;
this._sourceFile = ts.createSourceFile(fileName, _host.readFile(fileName),
ts.ScriptTarget.Latest);
ts.ScriptTarget.Latest, true);
}
this._sourceText = this._sourceFile.getFullText(this._sourceFile);
this._sourceString = new MagicString(this._sourceText);
Expand Down

0 comments on commit 1d3b7f6

Please sign in to comment.