Skip to content

Commit

Permalink
fixup! fix(ngcc): handle presence of both ctorParameters and `__dec…
Browse files Browse the repository at this point in the history
…orate`
  • Loading branch information
JoostK committed Sep 30, 2019
1 parent a8d7d90 commit 9d35d51
Showing 1 changed file with 15 additions and 38 deletions.
53 changes: 15 additions & 38 deletions packages/compiler-cli/ngcc/src/host/esm2015_host.ts
Expand Up @@ -655,32 +655,16 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
return this.decoratorCache.get(decl) !;
}

// First attempt extracting decorators from static properties.
let decoratorInfo = this.computeDecoratorInfoFromStaticProperties(classSymbol);
if (decoratorInfo === null) {
// If none were present, use the `__decorate` helper calls instead.
decoratorInfo = this.computeDecoratorInfoFromHelperCalls(classSymbol);
} else {
// If there was decorator information in static properties, it may be the case that only
// constructor parameters (`ctorParameters`) were present as static property and additional
// decorator info is represented using `__decorate` helper calls. Therefore, also inspect
// `__decorate` helpers and attach the class and member decorators it may contain to the
// computed decorator info.
//
// Note that the helper calls may also provide information about constructor parameters, if
// `emitDecoratorMetadata` was enabled for the published library. We can safely disregard this
// information from the helper calls, as the extracted information from the `ctorParameters`
// static property is fully complete by itself.
//
// Refer to https://github.com/ng-packagr/ng-packagr/pull/1401 where the static property
// transform of constructor parameters was ported from tsickle to ng-packagr.
if (decoratorInfo.classDecorators === null && decoratorInfo.memberDecorators.size === 0) {
const helperCallInfo = this.computeDecoratorInfoFromHelperCalls(classSymbol);

decoratorInfo.classDecorators = helperCallInfo.classDecorators;
decoratorInfo.memberDecorators = helperCallInfo.memberDecorators;
}
}
// Extract decorators from static properties and `__decorate` helper calls, then merging them
// together where the information from the static properties is preferred.
const staticProps = this.computeDecoratorInfoFromStaticProperties(classSymbol);
const helperCalls = this.computeDecoratorInfoFromHelperCalls(classSymbol);

const decoratorInfo: DecoratorInfo = {
classDecorators: staticProps.classDecorators || helperCalls.classDecorators,
memberDecorators: staticProps.memberDecorators || helperCalls.memberDecorators,
constructorParamInfo: staticProps.constructorParamInfo || helperCalls.constructorParamInfo,
};

this.decoratorCache.set(decl, decoratorInfo);
return decoratorInfo;
Expand All @@ -696,8 +680,10 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
* @returns All information on the decorators as extracted from static properties, or `null` if
* none of the static properties exist.
*/
protected computeDecoratorInfoFromStaticProperties(classSymbol: NgccClassSymbol): DecoratorInfo
|null {
protected computeDecoratorInfoFromStaticProperties(classSymbol: NgccClassSymbol): {
classDecorators: Decorator[] | null; memberDecorators: Map<string, Decorator[]>| null;
constructorParamInfo: ParamInfo[] | null;
} {
let classDecorators: Decorator[]|null = null;
let memberDecorators: Map<string, Decorator[]>|null = null;
let constructorParamInfo: ParamInfo[]|null = null;
Expand All @@ -717,16 +703,7 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
constructorParamInfo = this.getParamInfoFromStaticProperty(constructorParamsProperty);
}

// If none of the static properties were present, no decorator info could be computed.
if (classDecorators === null && memberDecorators === null && constructorParamInfo === null) {
return null;
}

return {
classDecorators,
memberDecorators: memberDecorators || new Map<string, Decorator[]>(),
constructorParamInfo: constructorParamInfo || [],
};
return {classDecorators, memberDecorators, constructorParamInfo};
}

/**
Expand Down

0 comments on commit 9d35d51

Please sign in to comment.