Skip to content

Commit

Permalink
chore: use @sveltejs/eslint-config (#9977)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored May 21, 2023
1 parent ac8123e commit 1c1ddd5
Show file tree
Hide file tree
Showing 64 changed files with 556 additions and 302 deletions.
10 changes: 3 additions & 7 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"root": true,
"extends": "@sveltejs",
"env": {
"es2022": true
},
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["unicorn"],
"root": true,
"ignorePatterns": ["packages/create-svelte/shared/"],
"rules": {
"unicorn/prefer-node-protocol": "error"
"no-undef": "off"
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@sveltejs/eslint-config": "^6.0.2",
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"eslint": "^8.33.0",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-svelte": "^2.28.0",
"eslint-plugin-unicorn": "^47.0.0",
"playwright": "1.30.0",
"prettier": "^2.8.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ export default function (options = {}) {
*/
function get_routes_json(builder, assets, { include = ['/*'], exclude = ['<all>'] }) {
if (!Array.isArray(include) || !Array.isArray(exclude)) {
throw new Error(`routes.include and routes.exclude must be arrays`);
throw new Error('routes.include and routes.exclude must be arrays');
}

if (include.length === 0) {
throw new Error(`routes.include must contain at least one route`);
throw new Error('routes.include must contain at least one route');
}

if (include.length > 100) {
throw new Error(`routes.include must contain 100 or fewer routes`);
throw new Error('routes.include must contain 100 or fewer routes');
}

exclude = exclude
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function (opts = {}) {
dir: `${out}/server`,
format: 'esm',
sourcemap: true,
chunkFileNames: `chunks/[name]-[hash].js`
chunkFileNames: 'chunks/[name]-[hash].js'
});

builder.copy(files, out, {
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const plugin = function (defaults = {}) {

if (ignored_isr.size) {
builder.log.warn(
`\nWarning: The following routes have an ISR config which is ignored because the route is prerendered:`
'\nWarning: The following routes have an ISR config which is ignored because the route is prerendered:'
);

for (const ignored of ignored_isr) {
Expand Down Expand Up @@ -586,7 +586,7 @@ function validate_vercel_json(builder, vercel_config) {

if (unmatched_paths.length) {
builder.log.warn(
`\nWarning: vercel.json defines cron tasks that use paths that do not correspond to an API route with a GET handler (ignore this if the request is handled in your \`handle\` hook):`
'\nWarning: vercel.json defines cron tasks that use paths that do not correspond to an API route with a GET handler (ignore this if the request is handled in your `handle` hook):'
);

for (const path of unmatched_paths) {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ await create(cwd, {
vitest: options.features.includes('vitest')
});

p.outro(`Your project is ready!`);
p.outro('Your project is ready!');

if (options.types === 'typescript') {
console.log(bold('✔ Typescript'));
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/scripts/build-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function strip_jsdoc(content) {
return '';
}

return `/**${description.replace(/\*\ $/, '')}*/${whitespace}`;
return `/**${description.replace(/\* $/, '')}*/${whitespace}`;
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/create-svelte/test/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ for (const pkg_path of glob(resolve_path('../../../packages/*/package.json'))) {

try {
const kit_dir = resolve_path('../../../packages/kit');
const ls_vite_result = execSync(`pnpm ls --json vite`, { cwd: kit_dir });
const ls_vite_result = execSync('pnpm ls --json vite', { cwd: kit_dir });
const vite_version = JSON.parse(ls_vite_result.toString())[0].devDependencies.vite.version;
overrides.vite = vite_version;
} catch (e) {
Expand Down Expand Up @@ -61,7 +61,7 @@ fs.writeFileSync(path.join(test_workspace_dir, 'pnpm-workspace.yaml'), 'packages
const exec_async = promisify(exec);

beforeAll(async () => {
await exec_async(`pnpm install --no-frozen-lockfile`, {
await exec_async('pnpm install --no-frozen-lockfile', {
cwd: test_workspace_dir
});
}, 60000);
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ const options = object(
(/** @type {any} */ { message }) => {
throw new Error(
message +
`\nTo suppress or handle this error, implement \`handleHttpError\` in https://kit.svelte.dev/docs/configuration#prerender`
'\nTo suppress or handle this error, implement `handleHttpError` in https://kit.svelte.dev/docs/configuration#prerender'
);
},
(input, keypath) => {
Expand All @@ -224,7 +224,7 @@ const options = object(
(/** @type {any} */ { message }) => {
throw new Error(
message +
`\nTo suppress or handle this error, implement \`handleMissingId\` in https://kit.svelte.dev/docs/configuration#prerender`
'\nTo suppress or handle this error, implement `handleMissingId` in https://kit.svelte.dev/docs/configuration#prerender'
);
},
(input, keypath) => {
Expand All @@ -238,7 +238,7 @@ const options = object(
(/** @type {any} */ { message }) => {
throw new Error(
message +
`\nTo suppress or handle this error, implement \`handleEntryGeneratorMismatch\` in https://kit.svelte.dev/docs/configuration#prerender`
'\nTo suppress or handle this error, implement `handleEntryGeneratorMismatch` in https://kit.svelte.dev/docs/configuration#prerender'
);
},
(input, keypath) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function create_dynamic_types(id, env, prefix) {

if (id === 'private') {
properties.push(`${prefixed}: undefined;`);
properties.push(`[key: string]: string | undefined;`);
properties.push('[key: string]: string | undefined;');
} else {
properties.push(`${prefixed}: string | undefined;`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/generate_manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function generate_manifest({ build_data, relative_path, routes }) {

/** @param {Array<number | undefined>} indexes */
function get_nodes(indexes) {
let string = indexes.map((n) => reindexed.get(n) ?? '').join(',');
const string = indexes.map((n) => reindexed.get(n) ?? '').join(',');

// since JavaScript ignores trailing commas, we need to insert a dummy
// comma so that the array has the correct length if the last item
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/sync/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ function analyze(project_relative, file, component_extensions, module_extensions
);
}

const kind = !!(match[1] || match[4] || match[7]) ? 'server' : 'universal';
const kind = match[1] || match[4] || match[7] ? 'server' : 'universal';

return {
kind,
Expand Down Expand Up @@ -513,7 +513,7 @@ function prevent_conflicts(routes) {
const normalized = normalize_route_id(route.id);

// find all permutations created by optional parameters
const split = normalized.split(/<\?(.+?)\>/g);
const split = normalized.split(/<\?(.+?)>/g);

let permutations = [/** @type {string} */ (split[0])];

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_client_manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function write_client_manifest(kit, manifest_data, output, metadata) {
if (node.universal) {
declarations.push(
`import * as universal from ${s(relative_path(`${output}/nodes`, node.universal))};`,
`export { universal };`
'export { universal };'
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export { set_assets, set_building, set_private_env, set_public_env };
* @param {string} output
*/
export function write_server(config, output) {
// TODO the casting shouldn't be necessary — investigate
// TODO the casting shouldn't be necessary — investigate
const hooks_file = /** @type {string} */ (resolve_entry(config.kit.files.hooks.server));

/** @param {string} file */
Expand Down
48 changes: 24 additions & 24 deletions packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function write_all_types(config, manifest_data) {
// it could be invoked by another process in the meantime.
const meta_data_file = `${types_dir}/route_meta_data.json`;
const has_meta_data = fs.existsSync(meta_data_file);
let meta_data = has_meta_data
const meta_data = has_meta_data
? /** @type {Record<string, string[]>} */ (JSON.parse(fs.readFileSync(meta_data_file, 'utf-8')))
: {};
const routes_map = create_routes_map(manifest_data);
Expand Down Expand Up @@ -177,7 +177,7 @@ function update_types(config, routes, route, to_delete = new Set()) {
const outdir = path.join(config.kit.outDir, 'types', routes_dir, route.id);

// now generate new types
const imports = [`import type * as Kit from '@sveltejs/kit';`];
const imports = ["import type * as Kit from '@sveltejs/kit';"];

/** @type {string[]} */
const declarations = [];
Expand All @@ -196,7 +196,7 @@ function update_types(config, routes, route, to_delete = new Set()) {

if (route.params.length > 0) {
exports.push(
`export type EntryGenerator = () => Promise<Array<RouteParams>> | Array<RouteParams>;`
'export type EntryGenerator = () => Promise<Array<RouteParams>> | Array<RouteParams>;'
);
}

Expand All @@ -206,24 +206,24 @@ function update_types(config, routes, route, to_delete = new Set()) {
if (route.layout || route.leaf) {
declarations.push(
// If T extends the empty object, void is also allowed as a return type
`type MaybeWithVoid<T> = {} extends T ? T | void : T;`,
'type MaybeWithVoid<T> = {} extends T ? T | void : T;',

// Returns the key of the object whose values are required.
`export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K; }[keyof T];`,
'export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K; }[keyof T];',

// Helper type to get the correct output type for load functions. It should be passed the parent type to check what types from App.PageData are still required.
// If none, void is also allowed as a return type.
`type OutputDataShape<T> = MaybeWithVoid<Omit<App.PageData, RequiredKeys<T>> & Partial<Pick<App.PageData, keyof T & keyof App.PageData>> & Record<string, any>>`,
'type OutputDataShape<T> = MaybeWithVoid<Omit<App.PageData, RequiredKeys<T>> & Partial<Pick<App.PageData, keyof T & keyof App.PageData>> & Record<string, any>>',

// null & {} == null, we need to prevent that in some situations
`type EnsureDefined<T> = T extends null | undefined ? {} : T;`,
'type EnsureDefined<T> = T extends null | undefined ? {} : T;',

// Takes a union type and returns a union type where each type also has all properties
// of all possible types (typed as undefined), making accessing them more ergonomic
`type OptionalUnion<U extends Record<string, any>, A extends keyof U = U extends U ? keyof U : never> = U extends unknown ? { [P in Exclude<A, keyof U>]?: never } & U : never;`,
'type OptionalUnion<U extends Record<string, any>, A extends keyof U = U extends U ? keyof U : never> = U extends unknown ? { [P in Exclude<A, keyof U>]?: never } & U : never;',

// Re-export `Snapshot` from @sveltejs/kit — in future we could use this to infer <T> from the return type of `snapshot.capture`
`export type Snapshot<T = any> = Kit.Snapshot<T>;`
'export type Snapshot<T = any> = Kit.Snapshot<T>;'
);
}

Expand Down Expand Up @@ -255,10 +255,10 @@ function update_types(config, routes, route, to_delete = new Set()) {

if (route.leaf.server) {
exports.push(
`export type Action<OutputData extends Record<string, any> | void = Record<string, any> | void> = Kit.Action<RouteParams, OutputData, RouteId>`
'export type Action<OutputData extends Record<string, any> | void = Record<string, any> | void> = Kit.Action<RouteParams, OutputData, RouteId>'
);
exports.push(
`export type Actions<OutputData extends Record<string, any> | void = Record<string, any> | void> = Kit.Actions<RouteParams, OutputData, RouteId>`
'export type Actions<OutputData extends Record<string, any> | void = Record<string, any> | void> = Kit.Actions<RouteParams, OutputData, RouteId>'
);
}
}
Expand Down Expand Up @@ -326,11 +326,11 @@ function update_types(config, routes, route, to_delete = new Set()) {
}

if (route.endpoint) {
exports.push(`export type RequestHandler = Kit.RequestHandler<RouteParams, RouteId>;`);
exports.push('export type RequestHandler = Kit.RequestHandler<RouteParams, RouteId>;');
}

if (route.leaf?.server || route.layout?.server || route.endpoint) {
exports.push(`export type RequestEvent = Kit.RequestEvent<RouteParams, RouteId>;`);
exports.push('export type RequestEvent = Kit.RequestEvent<RouteParams, RouteId>;');
}

const output = [imports.join('\n'), declarations.join('\n'), exports.join('\n')]
Expand Down Expand Up @@ -386,7 +386,7 @@ function process_node(node, outdir, is_page, proxies, all_pages_have_load = true
// +page.js load present -> server can return all-optional data
const output_data_shape =
node.universal || (!is_page && all_pages_have_load)
? `Partial<App.PageData> & Record<string, any> | void`
? 'Partial<App.PageData> & Record<string, any> | void'
: `OutputDataShape<${parent_type}>`;
exports.push(
`export type ${prefix}ServerLoad<OutputData extends ${output_data_shape} = ${output_data_shape}> = Kit.ServerLoad<${params}, ${parent_type}, OutputData, ${route_id}>;`
Expand All @@ -404,15 +404,15 @@ function process_node(node, outdir, is_page, proxies, all_pages_have_load = true
: path_to_original(outdir, node.server);

exports.push(
`type ExcludeActionFailure<T> = T extends Kit.ActionFailure<any> ? never : T extends void ? never : T;`,
`type ActionsSuccess<T extends Record<string, (...args: any) => any>> = { [Key in keyof T]: ExcludeActionFailure<Awaited<ReturnType<T[Key]>>>; }[keyof T];`,
`type ExtractActionFailure<T> = T extends Kit.ActionFailure<infer X> ? X extends void ? never : X : never;`,
`type ActionsFailure<T extends Record<string, (...args: any) => any>> = { [Key in keyof T]: Exclude<ExtractActionFailure<Awaited<ReturnType<T[Key]>>>, void>; }[keyof T];`,
'type ExcludeActionFailure<T> = T extends Kit.ActionFailure<any> ? never : T extends void ? never : T;',
'type ActionsSuccess<T extends Record<string, (...args: any) => any>> = { [Key in keyof T]: ExcludeActionFailure<Awaited<ReturnType<T[Key]>>>; }[keyof T];',
'type ExtractActionFailure<T> = T extends Kit.ActionFailure<infer X> ? X extends void ? never : X : never;',
'type ActionsFailure<T extends Record<string, (...args: any) => any>> = { [Key in keyof T]: Exclude<ExtractActionFailure<Awaited<ReturnType<T[Key]>>>, void>; }[keyof T];',
`type ActionsExport = typeof import('${from}').actions`,
`export type SubmitFunction = Kit.SubmitFunction<Expand<ActionsSuccess<ActionsExport>>, Expand<ActionsFailure<ActionsExport>>>`
'export type SubmitFunction = Kit.SubmitFunction<Expand<ActionsSuccess<ActionsExport>>, Expand<ActionsFailure<ActionsExport>>>'
);

type = `Expand<Kit.AwaitedActions<ActionsExport>> | null`;
type = 'Expand<Kit.AwaitedActions<ActionsExport>> | null';
}
exports.push(`export type ActionData = ${type};`);
}
Expand Down Expand Up @@ -440,7 +440,7 @@ function process_node(node, outdir, is_page, proxies, all_pages_have_load = true

const output_data_shape =
!is_page && all_pages_have_load
? `Partial<App.PageData> & Record<string, any> | void`
? 'Partial<App.PageData> & Record<string, any> | void'
: `OutputDataShape<${parent_type}>`;
exports.push(
`export type ${prefix}Load<OutputData extends ${output_data_shape} = ${output_data_shape}> = Kit.Load<${params}, ${prefix}ServerData, ${parent_type}, OutputData, ${route_id}>;`
Expand Down Expand Up @@ -688,7 +688,7 @@ export function tweak_types(content, is_server) {
// remove type from `export const load: Load ...`
if (declaration.type) {
let a = declaration.type.pos;
let b = declaration.type.end;
const b = declaration.type.end;
while (/\s/.test(content[a])) a += 1;

const type = content.slice(a, b);
Expand Down Expand Up @@ -760,7 +760,7 @@ export function tweak_types(content, is_server) {
// remove type from `export const actions: Actions ...`
if (declaration.type) {
let a = declaration.type.pos;
let b = declaration.type.end;
const b = declaration.type.end;
while (/\s/.test(content[a])) a += 1;

const type = content.slice(a, b);
Expand Down Expand Up @@ -788,7 +788,7 @@ export function tweak_types(content, is_server) {
if (arg && !arg.type) {
code.appendLeft(
arg.name.end,
`: import('./$types').RequestEvent` + (add_parens ? ')' : '')
": import('./$types').RequestEvent" + (add_parens ? ')' : '')
);
}
}
Expand Down
Loading

0 comments on commit 1c1ddd5

Please sign in to comment.