Skip to content

Commit

Permalink
(chore) TypeScript 5.0 (#1919)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: Lyu, Wei Da <b00605009@ntu.edu.tw>
  • Loading branch information
dummdidumm and jasonlyu123 committed Mar 31, 2023
1 parent 65a23ac commit 66fbb9a
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lint": "prettier --check ."
},
"dependencies": {
"typescript": "^4.9.3"
"typescript": "^5.0.3"
},
"devDependencies": {
"prettier": "2.8.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,15 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
normalizePath(virtualDocInfo.virtualDoc.getFilePath()!)
);

console.log(JSON.stringify(fix.changes, null, 2));
for (const change of fix.changes) {
if (getCanonicalFileName(normalizePath(change.fileName)) === virtualDocPath) {
change.fileName = tsDoc.filePath;

this.removeDuplicatedComponentImport(virtualDocInfo.insertedNames, change);
}
}
console.log(JSON.stringify(fix.changes, null, 2));

await this.lsAndTsDocResolver.deleteSnapshot(virtualDocPath);
}
Expand Down Expand Up @@ -342,7 +344,13 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
change.textChanges = change.textChanges
.map((textChange) => ({
...textChange,
newText: textChange.newText.replace(importRegex, '')
newText: textChange.newText.replace(importRegex, (match) => {
if (match.split('\n').length > 2) {
return '\n';
} else {
return '';
}
})
}))
// in case there are replacements
.filter((change) => change.span.length || change.newText);
Expand Down Expand Up @@ -1003,6 +1011,8 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const getGlobalCompletion = memoize(() =>
lang.getCompletionsAtPosition(tsDoc.filePath, 0, userPreferences, formatCodeSettings)
);
const [tsMajorStr] = ts.version.split('.');
const tsSupportHandlerQuickFix = parseInt(tsMajorStr) >= 5;

for (const diagnostic of cannotFindNameDiagnostics) {
const identifier = this.findIdentifierForDiagnostic(tsDoc, diagnostic, sourceFile);
Expand All @@ -1012,10 +1022,6 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
}

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

const fixes: ts.CodeFixAction[] = [];
if (isQuickFixTargetTargetStore) {
Expand All @@ -1031,16 +1037,22 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
);
}

if (isQuickFixTargetEventHandler) {
fixes.push(
...this.getEventHandlerQuickFixes(
identifier,
tsDoc,
typeChecker,
quote,
formatCodeBasis
)
if (!tsSupportHandlerQuickFix) {
const isQuickFixTargetEventHandler = this.isQuickFixForEventHandler(
document,
diagnostic
);
if (isQuickFixTargetEventHandler) {
fixes.push(
...this.getEventHandlerQuickFixes(
identifier,
tsDoc,
typeChecker,
quote,
formatCodeBasis
)
);
}
}

if (!fixes.length) {
Expand Down Expand Up @@ -1077,6 +1089,8 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
return identifier;
}

// TODO: Remove this in late 2023
// when most users have upgraded to TS 5.0+
private getSvelteStoreQuickFixes(
identifier: ts.Identifier,
lang: ts.LanguageService,
Expand Down Expand Up @@ -1180,7 +1194,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
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class ImpliedNodeFormatResolver {
export function createSvelteModuleLoader(
getSnapshot: (fileName: string) => DocumentSnapshot,
compilerOptions: ts.CompilerOptions,
tsSystem: ts.System
tsSystem: ts.System,
tsResolveModuleName: typeof ts.resolveModuleName
) {
const svelteSys = createSvelteSys(getSnapshot, tsSystem);
const moduleCache = new ModuleResolutionCache();
Expand Down Expand Up @@ -186,7 +187,7 @@ export function createSvelteModuleLoader(
// Delegate to the TS resolver first.
// If that does not bring up anything, try the Svelte Module loader
// which is able to deal with .svelte files.
const tsResolvedModule = ts.resolveModuleName(
const tsResolvedModule = tsResolveModuleName(
name,
containingFile,
compilerOptions,
Expand All @@ -199,7 +200,7 @@ export function createSvelteModuleLoader(
return tsResolvedModule;
}

const svelteResolvedModule = ts.resolveModuleName(
const svelteResolvedModule = tsResolveModuleName(
name,
containingFile,
compilerOptions,
Expand Down
11 changes: 9 additions & 2 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ async function createLanguageService(
// by the time they need to be accessed synchronously by DocumentSnapshots.
await configLoader.loadConfigs(workspacePath);

const svelteModuleLoader = createSvelteModuleLoader(getSnapshot, compilerOptions, tsSystem);
const svelteModuleLoader = createSvelteModuleLoader(
getSnapshot,
compilerOptions,
tsSystem,
ts.resolveModuleName
);

let svelteTsPath: string;
try {
Expand Down Expand Up @@ -442,7 +447,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 @@ -264,7 +264,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 Expand Up @@ -988,8 +988,8 @@ describe('CodeActionsProvider', () => {
edits: [
{
newText:
`\n${indent}import { FixAllImported3 } from \"./importing/c\";\n` +
`${indent}import FixAllImported2 from \"./importing/FixAllImported2.svelte\";\n`,
`\n${indent}import FixAllImported2 from \"./importing/FixAllImported2.svelte\";\n` +
`${indent}import { FixAllImported3 } from \"./importing/c\";\n`,
range: {
start: {
character: 18,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe('createSvelteModuleLoader', () => {
const resolveStub = sinon.stub().returns(<ts.ResolvedModuleWithFailedLookupLocations>{
resolvedModule
});
sinon.replace(ts, 'resolveModuleName', resolveStub);

const svelteSys = <any>'svelteSys';
sinon.stub(svS, 'createSvelteSys').returns(svelteSys);
Expand All @@ -27,7 +26,8 @@ describe('createSvelteModuleLoader', () => {
const moduleResolver = createSvelteModuleLoader(
getSvelteSnapshotStub,
compilerOptions,
ts.sys
ts.sys,
resolveStub
);

return {
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
2 changes: 1 addition & 1 deletion packages/svelte-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"import-fresh": "^3.2.1",
"sade": "^1.7.4",
"svelte-preprocess": "^5.0.3",
"typescript": "^4.9.4"
"typescript": "^5.0.3"
},
"peerDependencies": {
"svelte": "^3.55.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"svelte": "~3.57.0",
"tiny-glob": "^0.2.6",
"tslib": "^2.4.0",
"typescript": "^4.9.3"
"typescript": "^5.0.3"
},
"peerDependencies": {
"svelte": "^3.55",
Expand Down
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -176,7 +178,6 @@ async function createTsCompilerHost(options: any, svelteMap: SvelteMap) {
return resolveModuleName(moduleName, containingFile, compilerOptions);
});
};
// @ts-expect-error remove once we bump dev dep to TS 5
host.resolveModuleNameLiterals = (
moduleLiterals,
containingFile,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1632,10 +1632,10 @@ type-detect@4.0.8, type-detect@^4.0.8:
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==

typescript@*, typescript@^4.9.3, typescript@^4.9.4:
version "4.9.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.4.tgz#a2a3d2756c079abda241d75f149df9d561091e78"
integrity sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==
typescript@*,typescript@^5.0.3:
version "5.0.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.0.3.tgz#fe976f0c826a88d0a382007681cbb2da44afdedf"
integrity sha512-xv8mOEDnigb/tN9PSMTwSEqAnUvkoXMQlicOb0IUVDBSQCgBSaAAROUZYy2IcUy5qU6XajK5jjjO7TMWqBTKZA==

unist-util-stringify-position@^3.0.0:
version "3.0.2"
Expand Down

0 comments on commit 66fbb9a

Please sign in to comment.