Skip to content

Instantly share code, notes, and snippets.

@arciisine
Last active August 14, 2017 12:12
Show Gist options
  • Save arciisine/2ba754f561d3e28b138aeed686a800d3 to your computer and use it in GitHub Desktop.
Save arciisine/2ba754f561d3e28b138aeed686a800d3 to your computer and use it in GitHub Desktop.
TS Compiler Issue
> node sample.js
TypeError: Cannot read property 'kind' of undefined
at nodeCanBeDecorated <path>/node_modules/typescript/lib/typescript.js:7805:35)
at nodeIsDecorated <path>/node_modules/typescript/lib/typescript.js:7825:16)
at nodeOrChildIsDecorated <path>/node_modules/typescript/lib/typescript.js:7829:16)
at Object.forEach <path>/node_modules/typescript/lib/typescript.js:1506:30)
at Object.childIsDecorated <path>/node_modules/typescript/lib/typescript.js:7835:27)
at getClassFacts <path>/node_modules/typescript/lib/typescript.js:51089:20)
at visitClassDeclaration <path>/node_modules/typescript/lib/typescript.js:51114:25)
at visitTypeScript <path>/node_modules/typescript/lib/typescript.js:50973:28)
at visitorWorker <path>/node_modules/typescript/lib/typescript.js:50786:24)
at sourceElementVisitorWorker <path>/node_modules/typescript/lib/typescript.js:50818:28)
TypeError: Cannot read property 'exports' of undefined
at resolveNameHelper <path>/node_modules/typescript/lib/typescript.js:27547:70)
at resolveName <path>/node_modules/typescript/lib/typescript.js:27491:20)
at resolveEntityName <path>/node_modules/typescript/lib/typescript.js:28153:26)
at Object.getTypeReferenceSerializationKind <path>/node_modules/typescript/lib/typescript.js:47727:31)
at serializeTypeReferenceNode <path>/node_modules/typescript/lib/typescript.js:52220:30)
at serializeTypeNode <path>/node_modules/typescript/lib/typescript.js:52168:28)
at serializeTypeOfNode <path>/node_modules/typescript/lib/typescript.js:52040:28)
at addOldTypeMetadata <path>/node_modules/typescript/lib/typescript.js:51960:92)
at addTypeMetadata <path>/node_modules/typescript/lib/typescript.js:51954:17)
at transformAllDecoratorsOfDeclaration <path>/node_modules/typescript/lib/typescript.js:51793:13)
const ts = require('typescript');
const compilerOptions = {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2015,
strictNullChecks: true,
noImplicitAny: true,
sourceMap: true,
preserveConstEnums: true,
experimentalDecorators: true,
emitDecoratorMetadata: true
}
function transpile(input, compilerOptions, fileName, transformers) {
const output = ts.transpileModule(input, { compilerOptions, fileName, reportDiagnostics: false, transformers });
// addRange correctly handles cases when wither 'from' or 'to' argument is missing
ts.addRange(undefined, output.diagnostics);
return output.outputText;
}
function visitNode(context, node, state) {
if (ts.isClassDeclaration(node)) {
let ret = ts.visitEachChild(node, c => visitNode(context, c, { inSchema: true, setParent : state.setParent }), context);
if (state.setParent) {
for (let mem of ret.members) {
mem.parent = ret;
}
}
return ret;
} else if (ts.isPropertyDeclaration(node) && state.inSchema) {
let dec = ts.createDecorator(ts.createCall(ts.createIdentifier('Auto'), [], []));
return ts.createProperty(
(node.decorators || []).concat(dec),
node.modifiers,
node.name,
node.questionToken,
node.type,
node.initializer);
} else {
return ts.visitEachChild(node, c => visitNode(context, c, state), context);
}
}
// Simple transform that changes 'change me' to 'changed'.
const changeTransform = (state) => (context) => (sourceFile) => visitNode(context, sourceFile, state)
const body = `
import {Other} from './other';
const Auto(type?: any) = type ? (target:any, property: string) => {} : (target:any) => target
@Auto
export class AppComponent {
field: Other;
}`
try {
let out = transpile(body, compilerOptions, '/example.ts', { before: [changeTransform({})] });
console.log(out);
} catch (e) {
console.log(e);
}
try {
let out = transpile(body, compilerOptions, '/example.ts', { before: [changeTransform({ setParent: true })] });
console.log(out);
} catch (e) {
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment