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

Conversation

ZettZet
Copy link

@ZettZet ZettZet commented Nov 14, 2023

Many deprecated parts of tsAPI were removed from the ts5 release, so updating to ts5 breaks projects.
Added support to the new API with the help of dynamic checks of what API version is used right now.

Copy link
Owner

@kimamula kimamula left a comment

Choose a reason for hiding this comment

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

Thank you for raising this PR! Can you take a look at my comments?

@@ -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.

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.

Comment on lines +4 to +36
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");
})();
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?

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants