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

(chore) TypeScript 5.0 #1919

Merged
merged 11 commits into from
Mar 31, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const results: ts.CodeFixAction[] = [];
const quote = getQuotePreference(sourceFile, userPreferences);

const [tsMajorStr] = ts.version.split('.');
const tsSupportHandlerQuickFix = parseInt(tsMajorStr) >= 5;

for (const diagnostic of diagnostics) {
const start = tsDoc.offsetAt(tsDoc.getGeneratedPosition(diagnostic.range.start));
const end = tsDoc.offsetAt(tsDoc.getGeneratedPosition(diagnostic.range.end));
Expand All @@ -563,10 +566,6 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {

const isQuickFixTargetTargetStore =
identifier?.escapedText.toString().startsWith('$') && diagnostic.code === 2304;
const isQuickFixTargetEventHandler = this.isQuickFixForEventHandler(
document,
diagnostic
);

if (isQuickFixTargetTargetStore) {
results.push(
Expand All @@ -580,6 +579,15 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
);
}

if (tsSupportHandlerQuickFix) {
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

const isQuickFixTargetEventHandler = this.isQuickFixForEventHandler(
document,
diagnostic
);

if (isQuickFixTargetEventHandler) {
results.push(
...this.getEventHandlerQuickFixes(
Expand All @@ -596,6 +604,8 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
return results;
}

// TODO: Remove this in late 2023
// when most users have upgraded to TS 5.0+
private async getSvelteStoreQuickFixes(
identifier: ts.Identifier,
lang: ts.LanguageService,
Expand Down Expand Up @@ -705,7 +715,9 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {

const newText = [
...jsDoc,
`function ${identifier.text}(${parametersText})${useJsDoc ? '' : ': ' + returnType} {`,
`function ${identifier.text}(${parametersText})${
useJsDoc || returnType === 'any' ? '' : ': ' + returnType
} {`,
formatCodeBasis.indent +
`throw new Error(${quote}Function not implemented.${quote})` +
formatCodeBasis.semi,
Expand Down
4 changes: 3 additions & 1 deletion packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ async function createLanguageService(
!compilerOptions.moduleResolution ||
compilerOptions.moduleResolution === ts.ModuleResolutionKind.Classic
) {
compilerOptions.moduleResolution = ts.ModuleResolutionKind.NodeJs;
compilerOptions.moduleResolution =
// NodeJS: up to 4.9, Node10: since 5.0
(ts.ModuleResolutionKind as any).NodeJs ?? ts.ModuleResolutionKind.Node10;
}
if (
!compilerOptions.module ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe('CodeActionsProvider', () => {
edits: [
{
newText:
`\n\n${indent}function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLButtonElement; }): any {\n` +
`\n\n${indent}function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLButtonElement; }) {\n` +
`${indent}${indent}throw new Error("Function not implemented.");\n` +
`${indent}}\n`,
range: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('service', () => {
strict: true,
declaration: false,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
moduleResolution: ts.ModuleResolutionKind.Node10,
noEmit: true,
skipLibCheck: true,
target: ts.ScriptTarget.ESNext
Expand Down
4 changes: 3 additions & 1 deletion packages/svelte2tsx/src/emitDts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function loadTsconfig(config: EmitDtsConfig, svelteMap: SvelteMap) {
options: {
...options,
noEmit: false, // Set to true in case of jsconfig, force false, else nothing is emitted
moduleResolution: ts.ModuleResolutionKind.NodeJs, // Classic if not set, which gives wrong results
moduleResolution:
// NodeJS: up to 4.9, Node10: since 5.0
(ts.ModuleResolutionKind as any).NodeJs ?? ts.ModuleResolutionKind.Node10, // Classic if not set, which gives wrong results
declaration: true, // Needed for d.ts file generation
emitDeclarationOnly: true, // We only want d.ts file generation
declarationDir: config.declarationDir, // Where to put the declarations
Expand Down