diff --git a/.gitignore b/.gitignore index bfae8b4..050e4f2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ !test/compile/index.js npm-debug.log* node_modules +.vscode \ No newline at end of file diff --git a/examples/rollup/tsconfig.json b/examples/rollup/tsconfig.json index d5d9f55..b31df5c 100644 --- a/examples/rollup/tsconfig.json +++ b/examples/rollup/tsconfig.json @@ -4,7 +4,7 @@ "module": "es2015", "moduleResolution": "node", "noEmitOnError": true, - "suppressImplicitAnyIndexErrors": true, + "noUncheckedIndexedAccess": false, "target": "ES5" }, "exclude": [ diff --git a/examples/ts-jest/tsconfig.json b/examples/ts-jest/tsconfig.json index 91a915d..3e62600 100644 --- a/examples/ts-jest/tsconfig.json +++ b/examples/ts-jest/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "strict": true, "noEmitOnError": true, - "suppressImplicitAnyIndexErrors": true, + "noUncheckedIndexedAccess": false, "target": "ES5" }, "files": [ diff --git a/examples/ttypescript/tsconfig.json b/examples/ttypescript/tsconfig.json index a229e8b..af43d83 100644 --- a/examples/ttypescript/tsconfig.json +++ b/examples/ttypescript/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "strict": true, "noEmitOnError": true, - "suppressImplicitAnyIndexErrors": true, + "noUncheckedIndexedAccess": false, "target": "ES5", "plugins": [ { "transform": "ts-transformer-keys/transformer" } diff --git a/examples/webpack/tsconfig.json b/examples/webpack/tsconfig.json index 92320f9..f542a66 100644 --- a/examples/webpack/tsconfig.json +++ b/examples/webpack/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "strict": true, "noEmitOnError": true, - "suppressImplicitAnyIndexErrors": true, + "noUncheckedIndexedAccess": false, "target": "ES5" }, "exclude": [ diff --git a/test/compile/compile.ts b/test/compile/compile.ts index fab715c..6570fe3 100644 --- a/test/compile/compile.ts +++ b/test/compile/compile.ts @@ -1,12 +1,26 @@ import ts from 'typescript'; import transformer from '../../transformer'; +function getDefaultModuleResolution(): ts.ModuleResolutionKind { + if('NodeJs' in ts.ModuleResolutionKind) { + return ts.ModuleResolutionKind.NodeJs; + } + + if('Node10' in ts.ModuleResolutionKind) { + // @ts-ignore forward compatibility check + return ts.ModuleResolutionKind.Node10 as ts.ModuleResolutionKind; + } + + throw new Error('Cannot choose default module resolution kind') +} + export function compile(filePaths: string[], target = ts.ScriptTarget.ES5, writeFileCallback?: ts.WriteFileCallback) { const program = ts.createProgram(filePaths, { strict: true, noEmitOnError: true, - suppressImplicitAnyIndexErrors: true, + noUncheckedIndexedAccess: false, esModuleInterop: true, + moduleResolution: getDefaultModuleResolution(), target }); const transformers: ts.CustomTransformers = { diff --git a/transformer.ts b/transformer.ts index b9640a6..8281847 100644 --- a/transformer.ts +++ b/transformer.ts @@ -1,8 +1,39 @@ import ts from 'typescript'; import path from 'path'; -const createArrayExpression = ts.factory ? ts.factory.createArrayLiteralExpression : ts.createArrayLiteral; -const createStringLiteral = ts.factory ? ts.factory.createStringLiteral : ts.createLiteral; +type ArrayFactory = ( + elements?: readonly ts.Expression[] | undefined, + multiLine?: boolean | undefined +) => ts.ArrayLiteralExpression; + +const createArrayExpression = ((): ArrayFactory => { + if (ts.factory) { + return ts.factory.createArrayLiteralExpression; + } + + if ("createArrayLiteral" in ts) { + return ts.createArrayLiteral as ArrayFactory; + } + + throw new Error("Cannot choose array literal factory"); +})(); + +type StringFactory = ( + text: string, + isSingleQuote?: boolean | undefined +) => ts.StringLiteral; + +const createStringLiteral = ((): StringFactory => { + if (ts.factory) { + return ts.factory.createStringLiteral; + } + + if ("createLiteral" in ts) { + return ts.createLiteral as StringFactory; + } + + throw new Error("Cannot choose string literal factory"); +})(); export default function transformer(program: ts.Program): ts.TransformerFactory { return (context: ts.TransformationContext) => (file: ts.SourceFile) => visitNodeAndChildren(file, program, context); diff --git a/tsconfig.json b/tsconfig.json index 8e697c3..ac84113 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,8 @@ "noEmitOnError": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, - "esModuleInterop": true + "esModuleInterop": true, + "noUncheckedIndexedAccess": false }, "include": [ "transformer.ts",