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

feat: zero effort typings for reroute #2252

Merged
merged 3 commits into from
Jan 11, 2024
Merged
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
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