fix(ngcc): prevent reflected decorators from being clobbered (#33362)

ngcc has an internal cache of computed decorator information for
reflected classes, which could previously be mutated by consumers of the
reflection host. With the ability to inject synthesized decorators, such
decorators would inadvertently be added into the array of decorators
that was owned by the internal cache of the reflection host, incorrectly
resulting in synthesized decorators to be considered real decorators on
a class. This commit fixes the issue by cloning the cached array before
returning it.

PR Close #33362
This commit is contained in:
JoostK 2019-10-22 20:32:38 +02:00 committed by Andrew Kushnir
parent dcdb433b7d
commit 0de2dbfec1
2 changed files with 24 additions and 2 deletions

View File

@ -344,10 +344,18 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
return superDeclaration; return superDeclaration;
} }
/** Gets all decorators of the given class symbol. */ /**
* Gets all decorators of the given class symbol. Any decorator that have been synthetically
* injected by a migration will not be present in the returned collection.
*/
getDecoratorsOfSymbol(symbol: NgccClassSymbol): Decorator[]|null { getDecoratorsOfSymbol(symbol: NgccClassSymbol): Decorator[]|null {
const {classDecorators} = this.acquireDecoratorInfo(symbol); const {classDecorators} = this.acquireDecoratorInfo(symbol);
return classDecorators; if (classDecorators === null) {
return null;
}
// Return a clone of the array to prevent consumers from mutating the cache.
return Array.from(classDecorators);
} }
/** /**

View File

@ -1931,6 +1931,20 @@ runInEachFileSystem(() => {
expect(classDecoratorsSecondary.length).toEqual(1); expect(classDecoratorsSecondary.length).toEqual(1);
expect(classDecoratorsSecondary[0] !.map(d => d.name)).toEqual(['Directive']); expect(classDecoratorsSecondary[0] !.map(d => d.name)).toEqual(['Directive']);
}); });
it('should return a cloned array on each invocation', () => {
loadTestFiles(DECORATED_FILES);
const {program} = makeTestBundleProgram(getRootFiles(DECORATED_FILES)[0]);
const host = new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classDecl =
getDeclaration(program, DECORATED_FILES[0].name, 'A', ts.isClassDeclaration) !;
const classSymbol = host.getClassSymbol(classDecl) !;
const firstResult = host.getDecoratorsOfSymbol(classSymbol);
const secondResult = host.getDecoratorsOfSymbol(classSymbol);
expect(firstResult).not.toBe(secondResult);
});
}); });
describe('getDtsDeclarationsOfClass()', () => { describe('getDtsDeclarationsOfClass()', () => {