Skip to content

Commit 4767902

Browse files
chuckjazvicb
authored andcommitted
fix(compiler-cli): do not validate metadata from declaration files (#19324)
Upgrading metadata files causes spurious reports of metadata errors. Fixes #18867
1 parent 68078fd commit 4767902

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,14 @@ export class LowerMetadataCache implements RequestsMap {
322322
return value;
323323
};
324324

325+
// Do not validate or lower metadata in a declaration file. Declaration files are requested
326+
// when we need to update the version of the metadata to add informatoin that might be missing
327+
// in the out-of-date version that can be recovered from the .d.ts file.
328+
const declarationFile = sourceFile.isDeclarationFile;
329+
325330
const metadata = this.collector.getMetadata(
326-
sourceFile, this.strict, sourceFile.isDeclarationFile ? undefined : substituteExpression);
331+
sourceFile, this.strict && !declarationFile,
332+
declarationFile ? undefined : substituteExpression);
327333

328334
return {metadata, requests};
329335
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ describe('Expression lowering', () => {
9898
expect(collected.requests.has(collected.annotations[0].start))
9999
.toBeTruthy('did not find the data field');
100100
});
101+
102+
it('should throw a validation execption for invalid files', () => {
103+
const cache = new LowerMetadataCache({}, /* strict */ true);
104+
const sourceFile = ts.createSourceFile(
105+
'foo.ts', `
106+
import {Injectable} from '@angular/core';
107+
108+
class SomeLocalClass {}
109+
@Injectable()
110+
export class SomeClass {
111+
constructor(a: SomeLocalClass) {}
112+
}
113+
`,
114+
ts.ScriptTarget.Latest, true);
115+
expect(() => cache.getMetadata(sourceFile)).toThrow();
116+
});
117+
118+
it('should not report validation errors on a .d.ts file', () => {
119+
const cache = new LowerMetadataCache({}, /* strict */ true);
120+
const dtsFile = ts.createSourceFile(
121+
'foo.d.ts', `
122+
import {Injectable} from '@angular/core';
123+
124+
class SomeLocalClass {}
125+
@Injectable()
126+
export class SomeClass {
127+
constructor(a: SomeLocalClass) {}
128+
}
129+
`,
130+
ts.ScriptTarget.Latest, true);
131+
expect(() => cache.getMetadata(dtsFile)).not.toThrow();
132+
});
101133
});
102134
});
103135

0 commit comments

Comments
 (0)