svelte2tsx 0.6.7 → 0.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -1
- package/index.js +23 -21
- package/index.mjs +38 -36
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -128,6 +128,7 @@ export const internalHelpers: {
|
|
|
128
128
|
)=> boolean,
|
|
129
129
|
isParamsFile: (fileName: string, basename: string, paramsPath: string) =>boolean,
|
|
130
130
|
upsertKitFile: (
|
|
131
|
+
_ts: typeof ts,
|
|
131
132
|
fileName: string,
|
|
132
133
|
kitFilesSettings: InternalHelpers.KitFilesSettings,
|
|
133
134
|
getSource: () => ts.SourceFile | undefined,
|
|
@@ -135,7 +136,7 @@ export const internalHelpers: {
|
|
|
135
136
|
) => { text: string; addedCode: InternalHelpers.AddedCode[] } | undefined,
|
|
136
137
|
toVirtualPos: (pos: number, addedCode: InternalHelpers.AddedCode[]) => number,
|
|
137
138
|
toOriginalPos: (pos: number, addedCode: InternalHelpers.AddedCode[]) => {pos: number; inGenerated: boolean},
|
|
138
|
-
findExports: (source: ts.SourceFile, isTsFile: boolean) => Map<
|
|
139
|
+
findExports: (_ts: typeof ts, source: ts.SourceFile, isTsFile: boolean) => Map<
|
|
139
140
|
string,
|
|
140
141
|
| {
|
|
141
142
|
type: 'function';
|
package/index.js
CHANGED
|
@@ -5997,7 +5997,7 @@ function isSubpath(maybeParent, maybeChild) {
|
|
|
5997
5997
|
/**
|
|
5998
5998
|
* Finds the top level const/let/function exports of a source file.
|
|
5999
5999
|
*/
|
|
6000
|
-
function findExports(source, isTsFile) {
|
|
6000
|
+
function findExports(ts, source, isTsFile) {
|
|
6001
6001
|
var _a, _b, _c, _d;
|
|
6002
6002
|
const exports = new Map();
|
|
6003
6003
|
// TODO handle indirect exports?
|
|
@@ -6009,7 +6009,7 @@ function findExports(source, isTsFile) {
|
|
|
6009
6009
|
exports.set(statement.name.text, {
|
|
6010
6010
|
type: 'function',
|
|
6011
6011
|
node: statement,
|
|
6012
|
-
hasTypeDefinition: hasTypedParameter(statement, isTsFile)
|
|
6012
|
+
hasTypeDefinition: hasTypedParameter(ts, statement, isTsFile)
|
|
6013
6013
|
});
|
|
6014
6014
|
}
|
|
6015
6015
|
if (ts.isVariableStatement(statement) &&
|
|
@@ -6034,7 +6034,7 @@ function findExports(source, isTsFile) {
|
|
|
6034
6034
|
exports.set(declaration.name.getText(), {
|
|
6035
6035
|
type: 'function',
|
|
6036
6036
|
node,
|
|
6037
|
-
hasTypeDefinition: hasTypeDefinition || hasTypedParameter(node, isTsFile)
|
|
6037
|
+
hasTypeDefinition: hasTypeDefinition || hasTypedParameter(ts, node, isTsFile)
|
|
6038
6038
|
});
|
|
6039
6039
|
}
|
|
6040
6040
|
else {
|
|
@@ -6048,7 +6048,7 @@ function findExports(source, isTsFile) {
|
|
|
6048
6048
|
}
|
|
6049
6049
|
return exports;
|
|
6050
6050
|
}
|
|
6051
|
-
function hasTypedParameter(node, isTsFile) {
|
|
6051
|
+
function hasTypedParameter(ts, node, isTsFile) {
|
|
6052
6052
|
var _a;
|
|
6053
6053
|
return (!!((_a = node.parameters[0]) === null || _a === void 0 ? void 0 : _a.type) ||
|
|
6054
6054
|
(!isTsFile &&
|
|
@@ -6104,10 +6104,10 @@ function isParamsFile(fileName, basename, paramsPath) {
|
|
|
6104
6104
|
!basename.includes('.test') &&
|
|
6105
6105
|
!basename.includes('.spec'));
|
|
6106
6106
|
}
|
|
6107
|
-
function upsertKitFile(fileName, kitFilesSettings, getSource, surround = (text) => text) {
|
|
6107
|
+
function upsertKitFile(ts, fileName, kitFilesSettings, getSource, surround = (text) => text) {
|
|
6108
6108
|
var _a, _b, _c;
|
|
6109
6109
|
let basename = path.basename(fileName);
|
|
6110
|
-
const result = (_c = (_b = (_a = upserKitRouteFile(fileName, basename, getSource, surround)) !== null && _a !== void 0 ? _a : upserKitServerHooksFile(fileName, basename, kitFilesSettings.serverHooksPath, getSource, surround)) !== null && _b !== void 0 ? _b : upserKitClientHooksFile(fileName, basename, kitFilesSettings.clientHooksPath, getSource, surround)) !== null && _c !== void 0 ? _c : upserKitParamsFile(fileName, basename, kitFilesSettings.paramsPath, getSource, surround);
|
|
6110
|
+
const result = (_c = (_b = (_a = upserKitRouteFile(ts, fileName, basename, getSource, surround)) !== null && _a !== void 0 ? _a : upserKitServerHooksFile(ts, fileName, basename, kitFilesSettings.serverHooksPath, getSource, surround)) !== null && _b !== void 0 ? _b : upserKitClientHooksFile(ts, fileName, basename, kitFilesSettings.clientHooksPath, getSource, surround)) !== null && _c !== void 0 ? _c : upserKitParamsFile(ts, fileName, basename, kitFilesSettings.paramsPath, getSource, surround);
|
|
6111
6111
|
if (!result) {
|
|
6112
6112
|
return;
|
|
6113
6113
|
}
|
|
@@ -6122,7 +6122,7 @@ function upsertKitFile(fileName, kitFilesSettings, getSource, surround = (text)
|
|
|
6122
6122
|
text += originalText.slice(pos);
|
|
6123
6123
|
return { text, addedCode };
|
|
6124
6124
|
}
|
|
6125
|
-
function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
6125
|
+
function upserKitRouteFile(ts, fileName, basename, getSource, surround) {
|
|
6126
6126
|
if (!isKitRouteFile(fileName, basename))
|
|
6127
6127
|
return;
|
|
6128
6128
|
const source = getSource();
|
|
@@ -6133,7 +6133,7 @@ function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
|
6133
6133
|
insertCode(addedCode, pos, inserted);
|
|
6134
6134
|
};
|
|
6135
6135
|
const isTsFile = basename.endsWith('.ts');
|
|
6136
|
-
const exports = findExports(source, isTsFile);
|
|
6136
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6137
6137
|
// add type to load function if not explicitly typed
|
|
6138
6138
|
const load = exports.get('load');
|
|
6139
6139
|
if ((load === null || load === void 0 ? void 0 : load.type) === 'function' && load.node.parameters.length === 1 && !load.hasTypeDefinition) {
|
|
@@ -6154,7 +6154,7 @@ function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
|
6154
6154
|
addTypeToVariable(exports, surround, insert, 'csr', `boolean`);
|
|
6155
6155
|
// add types to GET/PUT/POST/PATCH/DELETE/OPTIONS if not explicitly typed
|
|
6156
6156
|
const insertApiMethod = (name) => {
|
|
6157
|
-
addTypeToFunction(exports, surround, insert, name, `import('./$types').RequestEvent`, `Response | Promise<Response>`);
|
|
6157
|
+
addTypeToFunction(ts, exports, surround, insert, name, `import('./$types').RequestEvent`, `Response | Promise<Response>`);
|
|
6158
6158
|
};
|
|
6159
6159
|
insertApiMethod('GET');
|
|
6160
6160
|
insertApiMethod('PUT');
|
|
@@ -6164,7 +6164,7 @@ function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
|
6164
6164
|
insertApiMethod('OPTIONS');
|
|
6165
6165
|
return { addedCode, originalText: source.getFullText() };
|
|
6166
6166
|
}
|
|
6167
|
-
function upserKitParamsFile(fileName, basename, paramsPath, getSource, surround) {
|
|
6167
|
+
function upserKitParamsFile(ts, fileName, basename, paramsPath, getSource, surround) {
|
|
6168
6168
|
if (!isParamsFile(fileName, basename, paramsPath)) {
|
|
6169
6169
|
return;
|
|
6170
6170
|
}
|
|
@@ -6176,11 +6176,11 @@ function upserKitParamsFile(fileName, basename, paramsPath, getSource, surround)
|
|
|
6176
6176
|
insertCode(addedCode, pos, inserted);
|
|
6177
6177
|
};
|
|
6178
6178
|
const isTsFile = basename.endsWith('.ts');
|
|
6179
|
-
const exports = findExports(source, isTsFile);
|
|
6180
|
-
addTypeToFunction(exports, surround, insert, 'match', 'string', 'boolean');
|
|
6179
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6180
|
+
addTypeToFunction(ts, exports, surround, insert, 'match', 'string', 'boolean');
|
|
6181
6181
|
return { addedCode, originalText: source.getFullText() };
|
|
6182
6182
|
}
|
|
6183
|
-
function upserKitClientHooksFile(fileName, basename, clientHooksPath, getSource, surround) {
|
|
6183
|
+
function upserKitClientHooksFile(ts, fileName, basename, clientHooksPath, getSource, surround) {
|
|
6184
6184
|
if (!isClientHooksFile(fileName, basename, clientHooksPath)) {
|
|
6185
6185
|
return;
|
|
6186
6186
|
}
|
|
@@ -6192,11 +6192,11 @@ function upserKitClientHooksFile(fileName, basename, clientHooksPath, getSource,
|
|
|
6192
6192
|
insertCode(addedCode, pos, inserted);
|
|
6193
6193
|
};
|
|
6194
6194
|
const isTsFile = basename.endsWith('.ts');
|
|
6195
|
-
const exports = findExports(source, isTsFile);
|
|
6196
|
-
addTypeToFunction(exports, surround, insert, 'handleError', `import('@sveltejs/kit').HandleClientError`);
|
|
6195
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6196
|
+
addTypeToFunction(ts, exports, surround, insert, 'handleError', `import('@sveltejs/kit').HandleClientError`);
|
|
6197
6197
|
return { addedCode, originalText: source.getFullText() };
|
|
6198
6198
|
}
|
|
6199
|
-
function upserKitServerHooksFile(fileName, basename, serverHooksPath, getSource, surround) {
|
|
6199
|
+
function upserKitServerHooksFile(ts, fileName, basename, serverHooksPath, getSource, surround) {
|
|
6200
6200
|
if (!isServerHooksFile(fileName, basename, serverHooksPath)) {
|
|
6201
6201
|
return;
|
|
6202
6202
|
}
|
|
@@ -6208,9 +6208,9 @@ function upserKitServerHooksFile(fileName, basename, serverHooksPath, getSource,
|
|
|
6208
6208
|
insertCode(addedCode, pos, inserted);
|
|
6209
6209
|
};
|
|
6210
6210
|
const isTsFile = basename.endsWith('.ts');
|
|
6211
|
-
const exports = findExports(source, isTsFile);
|
|
6211
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6212
6212
|
const addType = (name, type) => {
|
|
6213
|
-
addTypeToFunction(exports, surround, insert, name, type);
|
|
6213
|
+
addTypeToFunction(ts, exports, surround, insert, name, type);
|
|
6214
6214
|
};
|
|
6215
6215
|
addType('handleError', `import('@sveltejs/kit').HandleServerError`);
|
|
6216
6216
|
addType('handle', `import('@sveltejs/kit').Handle`);
|
|
@@ -6225,15 +6225,17 @@ function addTypeToVariable(exports, surround, insert, name, type) {
|
|
|
6225
6225
|
insert(pos, inserted);
|
|
6226
6226
|
}
|
|
6227
6227
|
}
|
|
6228
|
-
function addTypeToFunction(exports, surround, insert, name, type, returnType) {
|
|
6228
|
+
function addTypeToFunction(ts, exports, surround, insert, name, type, returnType) {
|
|
6229
6229
|
const fn = exports.get(name);
|
|
6230
6230
|
if ((fn === null || fn === void 0 ? void 0 : fn.type) === 'function' && fn.node.parameters.length === 1 && !fn.hasTypeDefinition) {
|
|
6231
6231
|
const paramPos = fn.node.parameters[0].getEnd();
|
|
6232
6232
|
const paramInsertion = surround(!returnType ? `: Parameters<${type}>[0]` : `: ${type}`);
|
|
6233
6233
|
insert(paramPos, paramInsertion);
|
|
6234
6234
|
if (!fn.node.type && fn.node.body) {
|
|
6235
|
-
const returnPos = fn.node
|
|
6236
|
-
|
|
6235
|
+
const returnPos = ts.isArrowFunction(fn.node)
|
|
6236
|
+
? fn.node.equalsGreaterThanToken.getStart()
|
|
6237
|
+
: fn.node.body.getStart();
|
|
6238
|
+
const returnInsertion = surround(!returnType ? `: ReturnType<${type}> ` : `: ${returnType} `);
|
|
6237
6239
|
insert(returnPos, returnInsertion);
|
|
6238
6240
|
}
|
|
6239
6241
|
}
|
package/index.mjs
CHANGED
|
@@ -5977,44 +5977,44 @@ function isSubpath(maybeParent, maybeChild) {
|
|
|
5977
5977
|
/**
|
|
5978
5978
|
* Finds the top level const/let/function exports of a source file.
|
|
5979
5979
|
*/
|
|
5980
|
-
function findExports(source, isTsFile) {
|
|
5980
|
+
function findExports(ts, source, isTsFile) {
|
|
5981
5981
|
var _a, _b, _c, _d;
|
|
5982
5982
|
const exports = new Map();
|
|
5983
5983
|
// TODO handle indirect exports?
|
|
5984
5984
|
for (const statement of source.statements) {
|
|
5985
|
-
if (
|
|
5985
|
+
if (ts.isFunctionDeclaration(statement) &&
|
|
5986
5986
|
statement.name &&
|
|
5987
|
-
((_b = (_a =
|
|
5987
|
+
((_b = (_a = ts.getModifiers(statement)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.kind) === ts.SyntaxKind.ExportKeyword) {
|
|
5988
5988
|
// export function x ...
|
|
5989
5989
|
exports.set(statement.name.text, {
|
|
5990
5990
|
type: 'function',
|
|
5991
5991
|
node: statement,
|
|
5992
|
-
hasTypeDefinition: hasTypedParameter(statement, isTsFile)
|
|
5992
|
+
hasTypeDefinition: hasTypedParameter(ts, statement, isTsFile)
|
|
5993
5993
|
});
|
|
5994
5994
|
}
|
|
5995
|
-
if (
|
|
5995
|
+
if (ts.isVariableStatement(statement) &&
|
|
5996
5996
|
statement.declarationList.declarations.length === 1 &&
|
|
5997
|
-
((_d = (_c =
|
|
5997
|
+
((_d = (_c = ts.getModifiers(statement)) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.kind) === ts.SyntaxKind.ExportKeyword) {
|
|
5998
5998
|
// export const x = ...
|
|
5999
5999
|
const declaration = statement.declarationList.declarations[0];
|
|
6000
6000
|
const hasTypeDefinition = !!declaration.type ||
|
|
6001
|
-
(!isTsFile && !!
|
|
6002
|
-
(!!declaration.initializer &&
|
|
6001
|
+
(!isTsFile && !!ts.getJSDocType(declaration)) ||
|
|
6002
|
+
(!!declaration.initializer && ts.isSatisfiesExpression(declaration.initializer));
|
|
6003
6003
|
if (declaration.initializer &&
|
|
6004
|
-
(
|
|
6005
|
-
|
|
6006
|
-
(
|
|
6007
|
-
|
|
6008
|
-
(
|
|
6009
|
-
|
|
6010
|
-
const node =
|
|
6004
|
+
(ts.isFunctionExpression(declaration.initializer) ||
|
|
6005
|
+
ts.isArrowFunction(declaration.initializer) ||
|
|
6006
|
+
(ts.isSatisfiesExpression(declaration.initializer) &&
|
|
6007
|
+
ts.isParenthesizedExpression(declaration.initializer.expression) &&
|
|
6008
|
+
(ts.isFunctionExpression(declaration.initializer.expression.expression) ||
|
|
6009
|
+
ts.isArrowFunction(declaration.initializer.expression.expression))))) {
|
|
6010
|
+
const node = ts.isSatisfiesExpression(declaration.initializer)
|
|
6011
6011
|
? declaration.initializer.expression
|
|
6012
6012
|
.expression
|
|
6013
6013
|
: declaration.initializer;
|
|
6014
6014
|
exports.set(declaration.name.getText(), {
|
|
6015
6015
|
type: 'function',
|
|
6016
6016
|
node,
|
|
6017
|
-
hasTypeDefinition: hasTypeDefinition || hasTypedParameter(node, isTsFile)
|
|
6017
|
+
hasTypeDefinition: hasTypeDefinition || hasTypedParameter(ts, node, isTsFile)
|
|
6018
6018
|
});
|
|
6019
6019
|
}
|
|
6020
6020
|
else {
|
|
@@ -6028,12 +6028,12 @@ function findExports(source, isTsFile) {
|
|
|
6028
6028
|
}
|
|
6029
6029
|
return exports;
|
|
6030
6030
|
}
|
|
6031
|
-
function hasTypedParameter(node, isTsFile) {
|
|
6031
|
+
function hasTypedParameter(ts, node, isTsFile) {
|
|
6032
6032
|
var _a;
|
|
6033
6033
|
return (!!((_a = node.parameters[0]) === null || _a === void 0 ? void 0 : _a.type) ||
|
|
6034
6034
|
(!isTsFile &&
|
|
6035
|
-
(!!
|
|
6036
|
-
(node.parameters[0] && !!
|
|
6035
|
+
(!!ts.getJSDocType(node) ||
|
|
6036
|
+
(node.parameters[0] && !!ts.getJSDocParameterTags(node.parameters[0]).length))));
|
|
6037
6037
|
}
|
|
6038
6038
|
|
|
6039
6039
|
const kitPageFiles = new Set(['+page', '+layout', '+page.server', '+layout.server', '+server']);
|
|
@@ -6084,10 +6084,10 @@ function isParamsFile(fileName, basename, paramsPath) {
|
|
|
6084
6084
|
!basename.includes('.test') &&
|
|
6085
6085
|
!basename.includes('.spec'));
|
|
6086
6086
|
}
|
|
6087
|
-
function upsertKitFile(fileName, kitFilesSettings, getSource, surround = (text) => text) {
|
|
6087
|
+
function upsertKitFile(ts, fileName, kitFilesSettings, getSource, surround = (text) => text) {
|
|
6088
6088
|
var _a, _b, _c;
|
|
6089
6089
|
let basename = path__default.basename(fileName);
|
|
6090
|
-
const result = (_c = (_b = (_a = upserKitRouteFile(fileName, basename, getSource, surround)) !== null && _a !== void 0 ? _a : upserKitServerHooksFile(fileName, basename, kitFilesSettings.serverHooksPath, getSource, surround)) !== null && _b !== void 0 ? _b : upserKitClientHooksFile(fileName, basename, kitFilesSettings.clientHooksPath, getSource, surround)) !== null && _c !== void 0 ? _c : upserKitParamsFile(fileName, basename, kitFilesSettings.paramsPath, getSource, surround);
|
|
6090
|
+
const result = (_c = (_b = (_a = upserKitRouteFile(ts, fileName, basename, getSource, surround)) !== null && _a !== void 0 ? _a : upserKitServerHooksFile(ts, fileName, basename, kitFilesSettings.serverHooksPath, getSource, surround)) !== null && _b !== void 0 ? _b : upserKitClientHooksFile(ts, fileName, basename, kitFilesSettings.clientHooksPath, getSource, surround)) !== null && _c !== void 0 ? _c : upserKitParamsFile(ts, fileName, basename, kitFilesSettings.paramsPath, getSource, surround);
|
|
6091
6091
|
if (!result) {
|
|
6092
6092
|
return;
|
|
6093
6093
|
}
|
|
@@ -6102,7 +6102,7 @@ function upsertKitFile(fileName, kitFilesSettings, getSource, surround = (text)
|
|
|
6102
6102
|
text += originalText.slice(pos);
|
|
6103
6103
|
return { text, addedCode };
|
|
6104
6104
|
}
|
|
6105
|
-
function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
6105
|
+
function upserKitRouteFile(ts, fileName, basename, getSource, surround) {
|
|
6106
6106
|
if (!isKitRouteFile(fileName, basename))
|
|
6107
6107
|
return;
|
|
6108
6108
|
const source = getSource();
|
|
@@ -6113,7 +6113,7 @@ function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
|
6113
6113
|
insertCode(addedCode, pos, inserted);
|
|
6114
6114
|
};
|
|
6115
6115
|
const isTsFile = basename.endsWith('.ts');
|
|
6116
|
-
const exports = findExports(source, isTsFile);
|
|
6116
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6117
6117
|
// add type to load function if not explicitly typed
|
|
6118
6118
|
const load = exports.get('load');
|
|
6119
6119
|
if ((load === null || load === void 0 ? void 0 : load.type) === 'function' && load.node.parameters.length === 1 && !load.hasTypeDefinition) {
|
|
@@ -6134,7 +6134,7 @@ function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
|
6134
6134
|
addTypeToVariable(exports, surround, insert, 'csr', `boolean`);
|
|
6135
6135
|
// add types to GET/PUT/POST/PATCH/DELETE/OPTIONS if not explicitly typed
|
|
6136
6136
|
const insertApiMethod = (name) => {
|
|
6137
|
-
addTypeToFunction(exports, surround, insert, name, `import('./$types').RequestEvent`, `Response | Promise<Response>`);
|
|
6137
|
+
addTypeToFunction(ts, exports, surround, insert, name, `import('./$types').RequestEvent`, `Response | Promise<Response>`);
|
|
6138
6138
|
};
|
|
6139
6139
|
insertApiMethod('GET');
|
|
6140
6140
|
insertApiMethod('PUT');
|
|
@@ -6144,7 +6144,7 @@ function upserKitRouteFile(fileName, basename, getSource, surround) {
|
|
|
6144
6144
|
insertApiMethod('OPTIONS');
|
|
6145
6145
|
return { addedCode, originalText: source.getFullText() };
|
|
6146
6146
|
}
|
|
6147
|
-
function upserKitParamsFile(fileName, basename, paramsPath, getSource, surround) {
|
|
6147
|
+
function upserKitParamsFile(ts, fileName, basename, paramsPath, getSource, surround) {
|
|
6148
6148
|
if (!isParamsFile(fileName, basename, paramsPath)) {
|
|
6149
6149
|
return;
|
|
6150
6150
|
}
|
|
@@ -6156,11 +6156,11 @@ function upserKitParamsFile(fileName, basename, paramsPath, getSource, surround)
|
|
|
6156
6156
|
insertCode(addedCode, pos, inserted);
|
|
6157
6157
|
};
|
|
6158
6158
|
const isTsFile = basename.endsWith('.ts');
|
|
6159
|
-
const exports = findExports(source, isTsFile);
|
|
6160
|
-
addTypeToFunction(exports, surround, insert, 'match', 'string', 'boolean');
|
|
6159
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6160
|
+
addTypeToFunction(ts, exports, surround, insert, 'match', 'string', 'boolean');
|
|
6161
6161
|
return { addedCode, originalText: source.getFullText() };
|
|
6162
6162
|
}
|
|
6163
|
-
function upserKitClientHooksFile(fileName, basename, clientHooksPath, getSource, surround) {
|
|
6163
|
+
function upserKitClientHooksFile(ts, fileName, basename, clientHooksPath, getSource, surround) {
|
|
6164
6164
|
if (!isClientHooksFile(fileName, basename, clientHooksPath)) {
|
|
6165
6165
|
return;
|
|
6166
6166
|
}
|
|
@@ -6172,11 +6172,11 @@ function upserKitClientHooksFile(fileName, basename, clientHooksPath, getSource,
|
|
|
6172
6172
|
insertCode(addedCode, pos, inserted);
|
|
6173
6173
|
};
|
|
6174
6174
|
const isTsFile = basename.endsWith('.ts');
|
|
6175
|
-
const exports = findExports(source, isTsFile);
|
|
6176
|
-
addTypeToFunction(exports, surround, insert, 'handleError', `import('@sveltejs/kit').HandleClientError`);
|
|
6175
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6176
|
+
addTypeToFunction(ts, exports, surround, insert, 'handleError', `import('@sveltejs/kit').HandleClientError`);
|
|
6177
6177
|
return { addedCode, originalText: source.getFullText() };
|
|
6178
6178
|
}
|
|
6179
|
-
function upserKitServerHooksFile(fileName, basename, serverHooksPath, getSource, surround) {
|
|
6179
|
+
function upserKitServerHooksFile(ts, fileName, basename, serverHooksPath, getSource, surround) {
|
|
6180
6180
|
if (!isServerHooksFile(fileName, basename, serverHooksPath)) {
|
|
6181
6181
|
return;
|
|
6182
6182
|
}
|
|
@@ -6188,9 +6188,9 @@ function upserKitServerHooksFile(fileName, basename, serverHooksPath, getSource,
|
|
|
6188
6188
|
insertCode(addedCode, pos, inserted);
|
|
6189
6189
|
};
|
|
6190
6190
|
const isTsFile = basename.endsWith('.ts');
|
|
6191
|
-
const exports = findExports(source, isTsFile);
|
|
6191
|
+
const exports = findExports(ts, source, isTsFile);
|
|
6192
6192
|
const addType = (name, type) => {
|
|
6193
|
-
addTypeToFunction(exports, surround, insert, name, type);
|
|
6193
|
+
addTypeToFunction(ts, exports, surround, insert, name, type);
|
|
6194
6194
|
};
|
|
6195
6195
|
addType('handleError', `import('@sveltejs/kit').HandleServerError`);
|
|
6196
6196
|
addType('handle', `import('@sveltejs/kit').Handle`);
|
|
@@ -6205,15 +6205,17 @@ function addTypeToVariable(exports, surround, insert, name, type) {
|
|
|
6205
6205
|
insert(pos, inserted);
|
|
6206
6206
|
}
|
|
6207
6207
|
}
|
|
6208
|
-
function addTypeToFunction(exports, surround, insert, name, type, returnType) {
|
|
6208
|
+
function addTypeToFunction(ts, exports, surround, insert, name, type, returnType) {
|
|
6209
6209
|
const fn = exports.get(name);
|
|
6210
6210
|
if ((fn === null || fn === void 0 ? void 0 : fn.type) === 'function' && fn.node.parameters.length === 1 && !fn.hasTypeDefinition) {
|
|
6211
6211
|
const paramPos = fn.node.parameters[0].getEnd();
|
|
6212
6212
|
const paramInsertion = surround(!returnType ? `: Parameters<${type}>[0]` : `: ${type}`);
|
|
6213
6213
|
insert(paramPos, paramInsertion);
|
|
6214
6214
|
if (!fn.node.type && fn.node.body) {
|
|
6215
|
-
const returnPos = fn.node
|
|
6216
|
-
|
|
6215
|
+
const returnPos = ts.isArrowFunction(fn.node)
|
|
6216
|
+
? fn.node.equalsGreaterThanToken.getStart()
|
|
6217
|
+
: fn.node.body.getStart();
|
|
6218
|
+
const returnInsertion = surround(!returnType ? `: ReturnType<${type}> ` : `: ${returnType} `);
|
|
6217
6219
|
insert(returnPos, returnInsertion);
|
|
6218
6220
|
}
|
|
6219
6221
|
}
|