Skip to content

Commit 7f93aad

Browse files
chuckjazmhevery
authored andcommitted
fix(compiler-cli): do not lower expressions in non-modules (#21649)
Fixes: #21651 PR Close #21649
1 parent 879756d commit 7f93aad

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/compiler-cli/src/transformers/lower_expressions.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,14 @@ export class LowerMetadataCache implements RequestsMap {
325325
};
326326

327327
// Do not validate or lower metadata in a declaration file. Declaration files are requested
328-
// when we need to update the version of the metadata to add informatoin that might be missing
328+
// when we need to update the version of the metadata to add information that might be missing
329329
// in the out-of-date version that can be recovered from the .d.ts file.
330330
const declarationFile = sourceFile.isDeclarationFile;
331+
const moduleFile = ts.isExternalModule(sourceFile);
331332

332333
const metadata = this.collector.getMetadata(
333334
sourceFile, this.strict && !declarationFile,
334-
declarationFile ? undefined : substituteExpression);
335+
moduleFile && !declarationFile ? substituteExpression : undefined);
335336

336337
return {metadata, requests};
337338
}

packages/compiler-cli/test/transformers/lower_expressions_spec.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,17 @@ describe('Expression lowering', () => {
9999
.toBeTruthy('did not find the data field');
100100
});
101101

102-
it('should throw a validation execption for invalid files', () => {
102+
it('should not lower a non-module', () => {
103+
const collected = collect(`
104+
declare const global: any;
105+
const ngDevMode: boolean = (function(global: any) {
106+
return global.ngDevMode = true;
107+
})(typeof window != 'undefined' && window || typeof self != 'undefined' && self || typeof global != 'undefined' && global);
108+
`);
109+
expect(collected.requests.size).toBe(0, 'unexpected rewriting');
110+
});
111+
112+
it('should throw a validation exception for invalid files', () => {
103113
const cache = new LowerMetadataCache({}, /* strict */ true);
104114
const sourceFile = ts.createSourceFile(
105115
'foo.ts', `

0 commit comments

Comments
 (0)