Skip to content

Commit

Permalink
feat: zero effort typings for reroute (#2252)
Browse files Browse the repository at this point in the history
Related to sveltejs/kit#11537
Also turns the error on unknown exports into a warning - people could be stuck on old versions of language tools while new features are added, and SvelteKit will throw a dev time error anyway. Also commented out that never worked.
  • Loading branch information
dummdidumm authored Jan 11, 2024
1 parent f5ec504 commit 5c08ff6
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ export class JSOrTSDocumentSnapshot extends IdentityMapper implements DocumentSn
private paramsPath = 'src/params';
private serverHooksPath = 'src/hooks.server';
private clientHooksPath = 'src/hooks.client';
private universalHooksPath = 'src/hooks';

private openedByClient = false;

Expand Down Expand Up @@ -578,7 +579,8 @@ export class JSOrTSDocumentSnapshot extends IdentityMapper implements DocumentSn
{
clientHooksPath: this.clientHooksPath,
paramsPath: this.paramsPath,
serverHooksPath: this.serverHooksPath
serverHooksPath: this.serverHooksPath,
universalHooksPath: this.universalHooksPath
},
() => this.createSource(),
surroundWithIgnoreComments
Expand All @@ -596,6 +598,7 @@ export class JSOrTSDocumentSnapshot extends IdentityMapper implements DocumentSn
this.paramsPath ||= files.params;
this.serverHooksPath ||= files.hooks?.server;
this.clientHooksPath ||= files.hooks?.client;
this.universalHooksPath ||= files.hooks?.universal;
}
}

Expand Down
12 changes: 4 additions & 8 deletions packages/svelte2tsx/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,11 @@ export const internalHelpers: {
options: InternalHelpers.KitFilesSettings
) => boolean;
isKitRouteFile: (basename: string) => boolean,
isClientHooksFile: (
isHooksFile: (
fileName: string,
basename: string,
clientHooksPath: string
) =>boolean,
isServerHooksFile: (
fileName: string,
basename: string,
serverHooksPath: string
)=> boolean,
hooksPath: string
) => boolean,
isParamsFile: (fileName: string, basename: string, paramsPath: string) =>boolean,
upsertKitFile: (
_ts: typeof ts,
Expand Down Expand Up @@ -185,6 +180,7 @@ export namespace InternalHelpers {
export interface KitFilesSettings {
serverHooksPath: string;
clientHooksPath: string;
universalHooksPath: string;
paramsPath: string;
}
}
2 changes: 1 addition & 1 deletion packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte2tsx",
"version": "0.6.8",
"version": "0.7.0",
"description": "Convert Svelte components to TSX for type checking",
"author": "David Pershouse",
"license": "MIT",
Expand Down
6 changes: 2 additions & 4 deletions packages/svelte2tsx/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {
isClientHooksFile,
isHooksFile,
isKitFile,
isKitRouteFile,
isParamsFile,
isServerHooksFile,
toOriginalPos,
toVirtualPos,
upsertKitFile
Expand All @@ -19,8 +18,7 @@ import { findExports } from './typescript';
export const internalHelpers = {
isKitFile,
isKitRouteFile,
isClientHooksFile,
isServerHooksFile,
isHooksFile,
isParamsFile,
upsertKitFile,
toVirtualPos,
Expand Down
87 changes: 53 additions & 34 deletions packages/svelte2tsx/src/helpers/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface AddedCode {
export interface KitFilesSettings {
serverHooksPath: string;
clientHooksPath: string;
universalHooksPath: string;
paramsPath: string;
}

Expand All @@ -27,8 +28,9 @@ export function isKitFile(fileName: string, options: KitFilesSettings): boolean
const basename = path.basename(fileName);
return (
isKitRouteFile(basename) ||
isServerHooksFile(fileName, basename, options.serverHooksPath) ||
isClientHooksFile(fileName, basename, options.clientHooksPath) ||
isHooksFile(fileName, basename, options.serverHooksPath) ||
isHooksFile(fileName, basename, options.clientHooksPath) ||
isHooksFile(fileName, basename, options.universalHooksPath) ||
isParamsFile(fileName, basename, options.paramsPath)
);
}
Expand All @@ -50,30 +52,11 @@ export function isKitRouteFile(basename: string): boolean {
/**
* Determines whether or not a given file is a SvelteKit-specific hooks file
*/
export function isServerHooksFile(
fileName: string,
basename: string,
serverHooksPath: string
): boolean {
export function isHooksFile(fileName: string, basename: string, hooksPath: string): boolean {
return (
((basename === 'index.ts' || basename === 'index.js') &&
fileName.slice(0, -basename.length - 1).endsWith(serverHooksPath)) ||
fileName.slice(0, -path.extname(basename).length).endsWith(serverHooksPath)
);
}

/**
* Determines whether or not a given file is a SvelteKit-specific hooks file
*/
export function isClientHooksFile(
fileName: string,
basename: string,
clientHooksPath: string
): boolean {
return (
((basename === 'index.ts' || basename === 'index.js') &&
fileName.slice(0, -basename.length - 1).endsWith(clientHooksPath)) ||
fileName.slice(0, -path.extname(basename).length).endsWith(clientHooksPath)
fileName.slice(0, -basename.length - 1).endsWith(hooksPath)) ||
fileName.slice(0, -path.extname(basename).length).endsWith(hooksPath)
);
}

Expand All @@ -97,24 +80,32 @@ export function upsertKitFile(
): { text: string; addedCode: AddedCode[] } {
let basename = path.basename(fileName);
const result =
upserKitRouteFile(ts, basename, getSource, surround) ??
upserKitServerHooksFile(
upsertKitRouteFile(ts, basename, getSource, surround) ??
upsertKitServerHooksFile(
ts,
fileName,
basename,
kitFilesSettings.serverHooksPath,
getSource,
surround
) ??
upserKitClientHooksFile(
upsertKitClientHooksFile(
ts,
fileName,
basename,
kitFilesSettings.clientHooksPath,
getSource,
surround
) ??
upserKitParamsFile(
upsertKitUniversalHooksFile(
ts,
fileName,
basename,
kitFilesSettings.universalHooksPath,
getSource,
surround
) ??
upsertKitParamsFile(
ts,
fileName,
basename,
Expand All @@ -139,7 +130,7 @@ export function upsertKitFile(
return { text, addedCode };
}

function upserKitRouteFile(
function upsertKitRouteFile(
ts: _ts,
basename: string,
getSource: () => ts.SourceFile | undefined,
Expand Down Expand Up @@ -225,7 +216,7 @@ function upserKitRouteFile(
return { addedCode, originalText: source.getFullText() };
}

function upserKitParamsFile(
function upsertKitParamsFile(
ts: _ts,
fileName: string,
basename: string,
Expand Down Expand Up @@ -253,15 +244,15 @@ function upserKitParamsFile(
return { addedCode, originalText: source.getFullText() };
}

function upserKitClientHooksFile(
function upsertKitClientHooksFile(
ts: _ts,
fileName: string,
basename: string,
clientHooksPath: string,
getSource: () => ts.SourceFile | undefined,
surround: (text: string) => string
) {
if (!isClientHooksFile(fileName, basename, clientHooksPath)) {
if (!isHooksFile(fileName, basename, clientHooksPath)) {
return;
}

Expand All @@ -288,15 +279,15 @@ function upserKitClientHooksFile(
return { addedCode, originalText: source.getFullText() };
}

function upserKitServerHooksFile(
function upsertKitServerHooksFile(
ts: _ts,
fileName: string,
basename: string,
serverHooksPath: string,
getSource: () => ts.SourceFile | undefined,
surround: (text: string) => string
) {
if (!isServerHooksFile(fileName, basename, serverHooksPath)) {
if (!isHooksFile(fileName, basename, serverHooksPath)) {
return;
}

Expand All @@ -322,6 +313,34 @@ function upserKitServerHooksFile(
return { addedCode, originalText: source.getFullText() };
}

function upsertKitUniversalHooksFile(
ts: _ts,
fileName: string,
basename: string,
universalHooksPath: string,
getSource: () => ts.SourceFile | undefined,
surround: (text: string) => string
) {
if (!isHooksFile(fileName, basename, universalHooksPath)) {
return;
}

const source = getSource();
if (!source) return;

const addedCode: AddedCode[] = [];
const insert = (pos: number, inserted: string) => {
insertCode(addedCode, pos, inserted);
};

const isTsFile = basename.endsWith('.ts');
const exports = findExports(ts, source, isTsFile);

addTypeToFunction(ts, exports, surround, insert, 'reroute', `import('@sveltejs/kit').Reroute`);

return { addedCode, originalText: source.getFullText() };
}

function addTypeToVariable(
exports: Map<
string,
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte2tsx/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('Internal Helpers - upsertKitFile', () => {
{
clientHooksPath: 'hooks.client',
paramsPath: 'params',
serverHooksPath: 'hooks.server'
serverHooksPath: 'hooks.server',
universalHooksPath: 'hooks'
},
() => sourceFile
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ function getKitDiagnostics<
messageText: `Invalid export '${exportName}' (valid exports are ${validExports.join(
', '
)}, or anything with a '_' prefix)`,
category: ts.DiagnosticCategory.Error,
// make it a warning in case people are stuck on old versions and new exports are added to SvelteKit
category: ts.DiagnosticCategory.Warning,
code: 71001 // arbitrary
});
}
Expand Down
6 changes: 2 additions & 4 deletions packages/typescript-plugin/src/language-service/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ export function decorateHover(
const node = source && findNodeAtPosition(source, virtualPos);
if (node && isTopLevelExport(ts, node, source) && ts.isIdentifier(node)) {
const name = node.text;
if (name in kitExports) {
quickInfo.documentation = !quickInfo.documentation?.length
? kitExports[name].documentation
: quickInfo.documentation;
if (name in kitExports && !quickInfo.documentation?.length) {
quickInfo.documentation = kitExports[name].documentation;
}
}

Expand Down
Loading

0 comments on commit 5c08ff6

Please sign in to comment.