Skip to content

Commit d6e49a9

Browse files
committed
Don’t emit closure type casts for Polymer behaviors
when using a call expression. E.g. ``` Polymer({behaviors: [(Polymer as any).NeonAnimationRunnerBehavior]) ``` This type cast was never emitted in non transformer mode due to microsoft/TypeScript#9873. This is a temporary workaround, the real fix is to change the Polymer Closure plugin (see #529).
1 parent 4c7fd40 commit d6e49a9

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

src/tsickle.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,17 @@ class Annotator extends ClosureRewriter {
785785
case ts.SyntaxKind.AsExpression:
786786
// Both of these cases are AssertionExpressions.
787787
const typeAssertion = node as ts.AssertionExpression;
788+
if (this.polymerBehaviorStackCount > 0) {
789+
// Don't emit type casts for Polymer behaviors that are declared
790+
// by calling the Polymer function
791+
// as the Polymer closure plugin does not work when emitting them.
792+
// Note: This only matters in the transformer version of tsickle,
793+
// as the non transformer version never emitted type casts due to
794+
// https://github.com/Microsoft/TypeScript/issues/9873 (see below).
795+
// TODO(tbosch): file an issue with Polymer, tracked in
796+
// https://github.com/angular/tsickle/issues/529.
797+
return false;
798+
}
788799
// When using a type casts in template expressions,
789800
// closure requires another pair of parens, otherwise it will
790801
// complain with "Misplaced type annotation. Type annotations are not allowed here."
@@ -852,6 +863,15 @@ class Annotator extends ClosureRewriter {
852863
return true;
853864
}
854865
break;
866+
case ts.SyntaxKind.PropertyAssignment:
867+
const pa = node as ts.PropertyAssignment;
868+
if (isPolymerBehaviorPropertyInCallExpression(pa)) {
869+
this.polymerBehaviorStackCount++;
870+
this.writeNodeFrom(node, node.getStart());
871+
this.polymerBehaviorStackCount--;
872+
return true;
873+
}
874+
return false;
855875
case ts.SyntaxKind.ElementAccessExpression:
856876
// Warn for quoted accesses to properties that have a symbol declared.
857877
// Mixing quoted and non-quoted access to a symbol (x['foo'] and x.foo) risks breaking
@@ -1870,6 +1890,18 @@ class ExternsWriter extends ClosureRewriter {
18701890
}
18711891
}
18721892

1893+
function isPolymerBehaviorPropertyInCallExpression(pa: ts.PropertyAssignment): boolean {
1894+
const parentParent = pa.parent && pa.parent.parent;
1895+
if (pa.name.kind !== ts.SyntaxKind.Identifier ||
1896+
(pa.name as ts.Identifier).text !== 'behaviors' || !pa.parent || !pa.parent.parent ||
1897+
pa.parent.parent.kind !== ts.SyntaxKind.CallExpression) {
1898+
return false;
1899+
}
1900+
1901+
const expr = (parentParent as ts.CallExpression).expression;
1902+
return expr.kind === ts.SyntaxKind.Identifier && (expr as ts.Identifier).text === 'Polymer';
1903+
}
1904+
18731905
export function annotate(
18741906
typeChecker: ts.TypeChecker, file: ts.SourceFile, host: AnnotatorHost,
18751907
options: AnnotatorOptions = {}, tsHost?: ts.ModuleResolutionHost, tsOpts?: ts.CompilerOptions,

test_files/jsdoc/jsdoc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,9 @@ const /** @type {string} */ c = 'c';
126126
* @polymerBehavior
127127
*/
128128
const somePolymerBehavior = {};
129+
/**
130+
* Don't emit type comments for Polymer behaviors
131+
* if they are declared via the Polymer function.
132+
*/
133+
let /** @type {?} */ Polymer;
134+
Polymer({ behaviors: ['test'] });

test_files/jsdoc/jsdoc.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,10 @@ const c = 'c';
109109
* @polymerBehavior
110110
*/
111111
const somePolymerBehavior = {};
112+
113+
/**
114+
* Don't emit type comments for Polymer behaviors
115+
* if they are declared via the Polymer function.
116+
*/
117+
let Polymer: any;
118+
Polymer({ behaviors: [ 'test' as any ] });

test_files/jsdoc/jsdoc.tsickle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,9 @@ const /** @type {string} */ c = 'c';
162162
* @polymerBehavior
163163
*/
164164
const somePolymerBehavior = {};
165+
/**
166+
* Don't emit type comments for Polymer behaviors
167+
* if they are declared via the Polymer function.
168+
*/
169+
let /** @type {?} */ Polymer: any;
170+
Polymer({behaviors: [ 'test' as any ] });

0 commit comments

Comments
 (0)