Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Typescript 5+ support #106

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
!test/compile/index.js
npm-debug.log*
node_modules
.vscode
2 changes: 1 addition & 1 deletion examples/rollup/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "es2015",
"moduleResolution": "node",
"noEmitOnError": true,
"suppressImplicitAnyIndexErrors": true,
"noUncheckedIndexedAccess": false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember why I added suppressImplicitAnyIndexErrors in the tsconfig.json files in the examples folder, but it seems ts files compile without this option. Can you just remove the option without adding noUncheckedIndexedAccess? It seems unnecessary.

"target": "ES5"
},
"exclude": [
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-jest/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"strict": true,
"noEmitOnError": true,
"suppressImplicitAnyIndexErrors": true,
"noUncheckedIndexedAccess": false,
"target": "ES5"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion examples/ttypescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"strict": true,
"noEmitOnError": true,
"suppressImplicitAnyIndexErrors": true,
"noUncheckedIndexedAccess": false,
"target": "ES5",
"plugins": [
{ "transform": "ts-transformer-keys/transformer" }
Expand Down
2 changes: 1 addition & 1 deletion examples/webpack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"strict": true,
"noEmitOnError": true,
"suppressImplicitAnyIndexErrors": true,
"noUncheckedIndexedAccess": false,
"target": "ES5"
},
"exclude": [
Expand Down
16 changes: 15 additions & 1 deletion test/compile/compile.ts
Original file line number Diff line number Diff line change
@@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems unnecessary.

esModuleInterop: true,
moduleResolution: getDefaultModuleResolution(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? I confirmed npm test succeeds without this.

target
});
const transformers: ts.CustomTransformers = {
Expand Down
35 changes: 33 additions & 2 deletions transformer.ts
Original file line number Diff line number Diff line change
@@ -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");
})();
Comment on lines +4 to +36
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following should be much easier:

Suggested change
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");
})();
declare module "typescript" {
const createArrayLiteral: typeof ts.factory.createArrayLiteralExpression;
const createLiteral: typeof ts.factory.createStringLiteral;
}
const createArrayExpression = ts.factory ? ts.factory.createArrayLiteralExpression : ts.createArrayLiteral;
const createStringLiteral = ts.factory ? ts.factory.createStringLiteral : ts.createLiteral;

What do you think?


export default function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext) => (file: ts.SourceFile) => visitNodeAndChildren(file, program, context);
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"noEmitOnError": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
"esModuleInterop": true,
"noUncheckedIndexedAccess": false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems unnecessary.

},
"include": [
"transformer.ts",
Expand Down
Loading