Skip to content
16 changes: 10 additions & 6 deletions packages/addons/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default defineAddon({
});

sv.file('eslint.config.js', (content) => {
const { ast, generateCode } = parseScript(content);
const { ast, additionalComments, generateCode } = parseScript(content);

const eslintConfigs: Array<AstTypes.Expression | AstTypes.SpreadElement> = [];
imports.addDefault(ast, { from: './svelte.config.js', as: 'svelteConfig' });
Expand Down Expand Up @@ -85,18 +85,20 @@ export default defineAddon({
if (rules.properties[0].type !== 'Property') {
throw new Error('rules.properties[0].type !== "Property"');
}
rules.properties[0].key.leadingComments = [
additionalComments.set(rules.properties[0].key, [
{
type: 'Line',
value:
' typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.'
' typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.',
position: 'leading'
},
{
type: 'Line',
value:
' see: https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors'
' see: https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors',
position: 'leading'
}
];
]);

const globalsConfig = object.create({
languageOptions: {
Expand Down Expand Up @@ -153,7 +155,9 @@ export default defineAddon({

// type annotate config
if (!typescript)
common.addJsDocTypeComment(astNode, { type: "import('eslint').Linter.Config[]" });
common.addJsDocTypeComment(astNode, additionalComments, {
type: "import('eslint').Linter.Config[]"
});

if (typescript) imports.addDefault(ast, { from: 'typescript-eslint', as: 'ts' });
imports.addDefault(ast, { from: 'globals', as: 'globals' });
Expand Down
19 changes: 16 additions & 3 deletions packages/addons/sveltekit-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default defineAddon({
sv.devDependency(adapter.package, adapter.version);

sv.file('svelte.config.js', (content) => {
const { ast, generateCode } = parseScript(content);
const { ast, comments, generateCode } = parseScript(content);

// finds any existing adapter's import declaration
const importDecls = ast.body.filter((n) => n.type === 'ImportDeclaration');
Expand Down Expand Up @@ -86,8 +86,21 @@ export default defineAddon({
if (adapter.package !== '@sveltejs/adapter-auto') {
const fallback = object.create({});
const cfgKitValue = object.property(config, { name: 'kit', fallback });
const cfgAdapter = object.propertyNode(cfgKitValue, { name: 'adapter', fallback });
cfgAdapter.leadingComments = [];

// removes any existing adapter auto comments
const adapterAutoComments = comments.filter(
(c) =>
c.loc &&
cfgKitValue.loc &&
c.loc.start.line >= cfgKitValue.loc.start.line &&
c.loc.end.line <= cfgKitValue.loc.end.line
);
// modify the array in place
comments.splice(
0,
comments.length,
...comments.filter((c) => !adapterAutoComments.includes(c))
);
}

return generateCode();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dom-serializer": "^2.0.0",
"domhandler": "^5.0.3",
"domutils": "^3.2.2",
"esrap": "^1.4.9",
"esrap": "https://pkg.pr.new/sveltejs/esrap@af12b38",
"htmlparser2": "^9.1.0",
"magic-string": "^0.30.17",
"picocolors": "^1.1.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/js/common/jsdoc-comment/run.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { common, type AstTypes } from '@sveltejs/cli-core/js';
import { common, type AdditionalCommentMap, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
export function run(ast: AstTypes.Program, additionalComments: AdditionalCommentMap): void {
const functionDeclaration = ast.body[0] as AstTypes.FunctionDeclaration;

common.addJsDocComment(functionDeclaration, {
common.addJsDocComment(functionDeclaration, additionalComments, {
params: { 'import("$lib/paraglide/runtime").AvailableLanguageTag': 'newLanguage' }
});
}
3 changes: 1 addition & 2 deletions packages/core/tests/js/common/jsdoc-type-comment/output.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/** @type {number} */
const foo = 42;
/** @type {number} */ const foo = 42;
6 changes: 3 additions & 3 deletions packages/core/tests/js/common/jsdoc-type-comment/run.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { common, variables, type AstTypes } from '@sveltejs/cli-core/js';
import { common, variables, type AdditionalCommentMap, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
export function run(ast: AstTypes.Program, additionalComments: AdditionalCommentMap): void {
const declaration = variables.declaration(ast, {
kind: 'const',
name: 'foo',
value: { type: 'Literal', value: 42 }
});

common.addJsDocTypeComment(declaration, {
common.addJsDocTypeComment(declaration, additionalComments, {
type: 'number'
});

Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ for (const categoryDirectory of categoryDirectories) {

const inputFilePath = join(testDirectoryPath, 'input.ts');
const input = fs.existsSync(inputFilePath) ? fs.readFileSync(inputFilePath, 'utf8') : '';
const ast = parseScript(input);
const { ast, comments, additionalComments } = parseScript(input);

// dynamic imports always need to provide the path inline for static analysis
const module = await import(`./${categoryDirectory}/${testName}/run.ts`);
module.run(ast);
module.run(ast, additionalComments);

let output = serializeScript(ast, input);
let output = serializeScript(ast, comments, input, additionalComments);
if (!output.endsWith('\n')) output += '\n';
await expect(output).toMatchFileSnapshot(`${testDirectoryPath}/output.ts`);
});
Expand Down
8 changes: 1 addition & 7 deletions packages/core/tests/js/object/create/output.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
const empty = {};
const created = { foo: 1, bar: 'string' };

// prettier-ignore
const created2 = {
foo: 1,
bar: 'string',
object: { foo: 'hello', nested: { bar: 'world' } },
array: [
123,
'hello',
{ foo: 'bar', bool: true },
[456, '789']
]
array: [123, 'hello', { foo: 'bar', bool: true }, [456, '789']]
};
1 change: 0 additions & 1 deletion packages/core/tests/js/object/create/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ export function run(ast: AstTypes.Program): void {
name: 'created2',
value: createdObject2
});
createdVariable2.leadingComments = [{ type: 'Line', value: ' prettier-ignore' }];
ast.body.push(createdVariable2);
}
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
const test = { a: { /** a comment */ keep: 'you', b: { c: '007' } } };
const test = {
a: {
/** a comment */
keep: 'you',

b: { c: '007' }
}
};
8 changes: 7 additions & 1 deletion packages/core/tests/js/object/override-property/output.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
const test = { /** a comment */ foo: 2, bar: 'string2', lorem: false };
const test = {
/** a comment */
foo: 2,

bar: 'string2',
lorem: false
};
7 changes: 6 additions & 1 deletion packages/core/tests/js/object/property-node/output.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
const test = { /*a comment updated*/ foo: 1, /*aka: bond, james bond*/ james: '007' };
const test = {
/** a comment */
foo: 1,

james: '007'
};
2 changes: 2 additions & 0 deletions packages/core/tests/js/vite/add-plugin-mode/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
firstPlugin(),

// a default plugin
sveltekit(),

middlePlugin(),
lastPlugin()
]
Expand Down
11 changes: 6 additions & 5 deletions packages/core/tests/js/vite/with-satisfies/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ const config = defineConfig({
plugins: [
// all plugins
examples,

tailwindcss(),
sveltekit(),
kitRoutes(),
myPlugin()
],

resolve: { alias: { $lib, $routes, $scripts, $actions } },
build: {
sourcemap: true,
target: 'esnext',
cssMinify: 'lightningcss'
},
build: { sourcemap: true, target: 'esnext', cssMinify: 'lightningcss' },

css: {
transformer: 'lightningcss',

lightningcss: {
targets: browserslistToTargets(browserslist('defaults, not ie 11'))
}
},

experimental: { enableNativePlugin: true }
}) satisfies UserConfig;

Expand Down
66 changes: 7 additions & 59 deletions packages/core/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
parseScript,
serializeScript,
guessIndentString,
guessQuoteStyle,
type AstTypes,
serializeYaml,
parseYaml
Expand Down Expand Up @@ -50,57 +49,6 @@ test('guessIndentString - eight spaces', () => {
expect(guessIndentString(code)).toBe(' ');
});

test('guessQuoteStyle - single simple', () => {
const code = dedent`
console.log('asd');
`;
const ast = parseScript(code);

expect(guessQuoteStyle(ast)).toBe('single');
});

test('guessQuoteStyle - single complex', () => {
const code = dedent`
import foo from 'bar';
console.log("bar");
const foobar = 'foo';
`;
const ast = parseScript(code);

expect(guessQuoteStyle(ast)).toBe('single');
});

test('guessQuoteStyle - double simple', () => {
const code = dedent`
console.log("asd");
`;
const ast = parseScript(code);

expect(guessQuoteStyle(ast)).toBe('double');
});

test('guessQuoteStyle - double complex', () => {
const code = dedent`
import foo from 'bar';
console.log("bar");
const foobar = "foo";
`;
const ast = parseScript(code);

expect(guessQuoteStyle(ast)).toBe('double');
});

test('guessQuoteStyle - no quotes', () => {
const code = dedent`
const foo = true;
`;
const ast = parseScript(code);

expect(guessQuoteStyle(ast)).toBe(undefined);
});

const newVariableDeclaration: AstTypes.VariableDeclaration = {
type: 'VariableDeclaration',
kind: 'const',
Expand Down Expand Up @@ -128,20 +76,20 @@ test('integration - simple', () => {
const foobar = "foo";
}
`;
const ast = parseScript(code);
const { ast, comments } = parseScript(code);
const method = ast.body[1] as AstTypes.FunctionDeclaration;

method.body.body.push(newVariableDeclaration);

// new variable is added with correct indentation and matching quotes
expect(serializeScript(ast, code)).toMatchInlineSnapshot(`
expect(serializeScript(ast, comments, code)).toMatchInlineSnapshot(`
"import foo from 'bar';
function bar() {
console.log("bar");
const foobar = "foo";
const foobar2 = "test";
const foobar2 = 'test';
}"
`);
});
Expand All @@ -155,13 +103,13 @@ test('integration - simple 2', () => {
const foobar = 'foo';
}
`;
const ast = parseScript(code);
const { ast, comments } = parseScript(code);
const method = ast.body[1] as AstTypes.FunctionDeclaration;

method.body.body.push(newVariableDeclaration);

// new variable is added with correct indentation and matching quotes
expect(serializeScript(ast, code)).toMatchInlineSnapshot(`
expect(serializeScript(ast, comments, code)).toMatchInlineSnapshot(`
"import foo from 'bar';
function bar() {
Expand All @@ -178,9 +126,9 @@ test('integration - preserves comments', () => {
/** @type {string} */
let foo = 'bar';
`;
const ast = parseScript(code);
const { ast, comments } = parseScript(code);

expect(serializeScript(ast, code)).toMatchInlineSnapshot(`
expect(serializeScript(ast, comments, code)).toMatchInlineSnapshot(`
"/** @type {string} */
let foo = 'bar';"
`);
Expand Down
Loading
Loading