Open
Description
TypeScript Version: 2.8.3 (I used this version because I was getting a completely different error when I updated to the latest @next version. Probably caused by ttypescript or some other dependency that's not updated to the latest version)
Search Terms: compiler API, transform
Code
import * as ts from 'typescript'
export default function transformer() {
return (context: ts.TransformationContext) => {
return (sourceFile: ts.SourceFile) => {
return visitNodeAndChildren(sourceFile)
function visitNodeAndChildren(node: ts.Node): ts.VisitResult<ts.Node> {
return ts.visitEachChild(visitNode(node), (childNode) => visitNodeAndChildren(childNode), context)
}
function visitNode(node: ts.Node): ts.Node {
switch (node.kind) {
case ts.SyntaxKind.JsxElement:
const element = node as ts.JsxElement
return ts.createJsxElement(ts.createJsxOpeningElement(element.openingElement.tagName, element.openingElement.attributes), element.children, element.closingElement)
default:
return node
}
}
}
}
}
I'm running this using ttsc but I don't think that's the issue.
Expected behavior:
No error.
Actual behavior:
at resolveNameHelper (node_modules\typescript\lib\typescript.js:26726:30)
at resolveName (node_modules\typescript\lib\typescript.js:26489:20)
at getReferencedValueSymbol (node_modules\typescript\lib\typescript.js:48917:20)
at Object.getReferencedDeclarationWithCollidingName (node_modules\typescript\lib\typescript.js:48672:34)
at substituteExpressionIdentifier (node_modules\typescript\lib\typescript.js:63756:44)
at substituteExpression (node_modules\typescript\lib\typescript.js:63743:28)
at onSubstituteNode (node_modules\typescript\lib\typescript.js:63696:24)
at onSubstituteNode (node_modules\typescript\lib\typescript.js:65555:20)
at onSubstituteNode (node_modules\typescript\lib\typescript.js:69452:20)
at substituteNode (node_modules\typescript\lib\typescript.js:69643:59)
at trySubstituteNode (node_modules\typescript\lib\typescript.js:73151:46)
at pipelineEmitWithComments (node_modules\typescript\lib\typescript.js:72765:20)
at emitNodeWithNotification (node_modules\typescript\lib\typescript.js:69674:21)
at pipelineEmitWithNotification (node_modules\typescript\lib\typescript.js:72758:17)
at emitExpression (node_modules\typescript\lib\typescript.js:72754:13)
at emitPropertyAccessExpression (node_modules\typescript\lib\typescript.js:73547:13)
at pipelineEmitExpression (node_modules\typescript\lib\typescript.js:73087:28)
at pipelineEmitWithHint (node_modules\typescript\lib\typescript.js:72785:49)
at emitNodeWithSourceMap (node_modules\typescript\lib\typescript.js:70051:21)
at pipelineEmitWithSourceMap (node_modules\typescript\lib\typescript.js:72775:17)
at emitNodeWithNestedComments (node_modules\typescript\lib\typescript.js:70340:17)
at emitNodeWithSynthesizedComments (node_modules\typescript\lib\typescript.js:70290:13)
at emitNodeWithComments (node_modules\typescript\lib\typescript.js:70226:21)
at pipelineEmitWithComments (node_modules\typescript\lib\typescript.js:72767:17)
at emitNodeWithNotification (node_modules\typescript\lib\typescript.js:69674:21)
at pipelineEmitWithNotification (node_modules\typescript\lib\typescript.js:72758:17)
at emitExpression (node_modules\typescript\lib\typescript.js:72754:13)
at emitCallExpression (node_modules\typescript\lib\typescript.js:73584:13)
at pipelineEmitExpression (node_modules\typescript\lib\typescript.js:73091:28)
at pipelineEmitWithHint (node_modules\typescript\lib\typescript.js:72785:49)
(node:23136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a
promise which was not handled with .catch(). (rejection id: 1)
Workaround: This seems to actually error sometimes and I can't figure out how to reproduce the fail so I don't recommend to use this workaround. To work around this, I had to remove the ts.createJsxOpeningElement call and alter the properties myself. Example:
import * as ts from 'typescript'
export default function transformer() {
return (context: ts.TransformationContext) => {
return (sourceFile: ts.SourceFile) => {
return visitNodeAndChildren(sourceFile)
function visitNodeAndChildren(node: ts.Node): ts.VisitResult<ts.Node> {
return ts.visitEachChild(visitNode(node), (childNode) => visitNodeAndChildren(childNode), context)
}
function visitNode(node: ts.Node): ts.Node {
switch (node.kind) {
case ts.SyntaxKind.JsxElement:
const element = node as ts.JsxElement
+ element.openingElement.tagName = ts.createLiteral('whatever') // I wanted to alter the tag name in my case but should be the same for altering the attributes
- return ts.createJsxElement(ts.createJsxOpeningElement(element.openingElement.tagName, element.openingElement.attributes), element.children, element.closingElement)
+ return ts.createJsxElement(element.openingElement, element.children, element.closingElement)
default:
return node
}
}
}
}
}