Skip to content

Commit 7307cc4

Browse files
committed
fix eslint
1 parent f41da14 commit 7307cc4

File tree

8 files changed

+49
-27
lines changed

8 files changed

+49
-27
lines changed

packages/addons/eslint/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ export default defineAddon({
154154

155155
// type annotate config
156156
if (!typescript)
157-
common.addJsDocTypeComment(astNode, { type: "import('eslint').Linter.Config[]" });
157+
common.addJsDocTypeComment(astNode, additionalComments, {
158+
type: "import('eslint').Linter.Config[]"
159+
});
158160

159161
if (typescript) imports.addDefault(ast, { from: 'typescript-eslint', as: 'ts' });
160162
imports.addNamed(ast, { from: 'node:url', imports: ['fileURLToPath'] });
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { common, type AstTypes } from '@sveltejs/cli-core/js';
1+
import { common, type AdditionalCommentMap, type AstTypes } from '@sveltejs/cli-core/js';
22

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

6-
common.addJsDocComment(functionDeclaration, {
6+
common.addJsDocComment(functionDeclaration, additionalComments, {
77
params: { 'import("$lib/paraglide/runtime").AvailableLanguageTag': 'newLanguage' }
88
});
99
}

packages/core/tests/js/common/jsdoc-type-comment/run.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { common, variables, type AstTypes } from '@sveltejs/cli-core/js';
1+
import { common, variables, type AdditionalCommentMap, type AstTypes } from '@sveltejs/cli-core/js';
22

3-
export function run(ast: AstTypes.Program): void {
3+
export function run(ast: AstTypes.Program, additionalComments: AdditionalCommentMap): void {
44
const declaration = variables.declaration(ast, {
55
kind: 'const',
66
name: 'foo',
77
value: { type: 'Literal', value: 42 }
88
});
99

10-
common.addJsDocTypeComment(declaration, {
10+
common.addJsDocTypeComment(declaration, additionalComments, {
1111
type: 'number'
1212
});
1313

packages/core/tests/js/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ for (const categoryDirectory of categoryDirectories) {
1616

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

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

25-
let output = serializeScript(ast, comments, input);
25+
let output = serializeScript(ast, comments, input, additionalComments);
2626
if (!output.endsWith('\n')) output += '\n';
2727
await expect(output).toMatchFileSnapshot(`${testDirectoryPath}/output.ts`);
2828
});

packages/core/tooling/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import ts, { type AdditionalComment } from 'esrap/languages/ts';
1818
import * as acorn from 'acorn';
1919
import { tsPlugin } from '@sveltejs/acorn-typescript';
2020

21+
type AdditionalCommentMap = WeakMap<TsEstree.Node, AdditionalComment[]>;
22+
2123
export {
2224
// html
2325
Document as HtmlDocument,
@@ -41,6 +43,7 @@ export type {
4143

4244
// js
4345
TsEstree as AstTypes,
46+
AdditionalCommentMap,
4447

4548
//css
4649
CssChildNode
@@ -53,7 +56,7 @@ export type {
5356
export function parseScript(content: string): {
5457
ast: TsEstree.Program;
5558
comments: TsEstree.Comment[];
56-
additionalComments: WeakMap<TsEstree.Node, AdditionalComment[]>;
59+
additionalComments: AdditionalCommentMap;
5760
} {
5861
const comments: TsEstree.Comment[] = [];
5962

@@ -96,7 +99,7 @@ export function serializeScript(
9699
ast: TsEstree.Node,
97100
comments: TsEstree.Comment[],
98101
previousContent?: string,
99-
additionalComments?: WeakMap<TsEstree.Node, AdditionalComment[]>
102+
additionalComments?: AdditionalCommentMap
100103
): string {
101104
// @ts-expect-error we are still using `estree` while `esrap` is using `@typescript-eslint/types`
102105
// which is causing these errors. But they are simmilar enough to work together.

packages/core/tooling/js/common.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
import { type AstTypes, Walker, parseScript, serializeScript, stripAst } from '../index.ts';
1+
import {
2+
type AdditionalCommentMap,
3+
type AstTypes,
4+
Walker,
5+
parseScript,
6+
serializeScript,
7+
stripAst
8+
} from '../index.ts';
29
import decircular from 'decircular';
310
import dedent from 'dedent';
411

5-
export function addJsDocTypeComment(node: AstTypes.Node, options: { type: string }): void {
12+
export function addJsDocTypeComment(
13+
node: AstTypes.Node,
14+
additionalComments: AdditionalCommentMap,
15+
options: { type: string }
16+
): void {
617
const comment: AstTypes.Comment = {
718
type: 'Block',
819
value: `* @type {${options.type}} `
920
};
1021

11-
addComment(node, comment);
22+
addComment(node, additionalComments, comment);
1223
}
1324

1425
export function addJsDocComment(
1526
node: AstTypes.Node,
27+
additionalComments: AdditionalCommentMap,
1628
options: { params: Record<string, string> }
1729
): void {
1830
const commentLines: string[] = [];
@@ -25,16 +37,23 @@ export function addJsDocComment(
2537
value: `*\n * ${commentLines.join('\n * ')}\n `
2638
};
2739

28-
addComment(node, comment);
40+
addComment(node, additionalComments, comment);
2941
}
3042

31-
function addComment(node: AstTypes.Node, comment: AstTypes.Comment) {
32-
node.leadingComments ??= [];
33-
34-
const found = node.leadingComments.find(
35-
(item) => item.type === 'Block' && item.value === comment.value
36-
);
37-
if (!found) node.leadingComments.push(comment);
43+
function addComment(
44+
node: AstTypes.Node,
45+
additionalComments: AdditionalCommentMap,
46+
comment: AstTypes.Comment
47+
) {
48+
const found = additionalComments
49+
.get(node)
50+
?.find((item) => item.type === 'Block' && item.value === comment.value);
51+
52+
if (!found) {
53+
const comments = additionalComments.get(node) ?? [];
54+
comments.push({ ...comment, position: 'leading' });
55+
additionalComments.set(node, comments);
56+
}
3857
}
3958

4059
export function typeAnnotate(

packages/core/tooling/js/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export * as variables from './variables.ts';
77
export * as exports from './exports.ts';
88
export * as kit from './kit.ts';
99
export * as vite from './vite.ts';
10-
export type { AstTypes } from '../index.ts';
10+
export type { AstTypes, AdditionalCommentMap } from '../index.ts';

packages/core/tooling/parsers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as utils from './index.ts';
22
import MagicString from 'magic-string';
3-
import type { TsEstree } from './js/ts-estree.ts';
4-
import type { AdditionalComment } from 'esrap/languages/ts';
53

64
type ParseBase = {
75
source: string;
@@ -11,7 +9,7 @@ type ParseBase = {
119
export function parseScript(source: string): {
1210
ast: utils.AstTypes.Program;
1311
comments: utils.AstTypes.Comment[];
14-
additionalComments: WeakMap<TsEstree.Node, AdditionalComment[]>;
12+
additionalComments: utils.AdditionalCommentMap;
1513
} & ParseBase {
1614
const { ast, comments, additionalComments } = utils.parseScript(source);
1715
const generateCode = () => utils.serializeScript(ast, comments, source, additionalComments);

0 commit comments

Comments
 (0)