Skip to content

Commit 2c48a41

Browse files
chore: eslint use typescript-eslint helper (#66)
Co-authored-by: AdrianGonz97 <[email protected]>
1 parent 72b9ee7 commit 2c48a41

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

packages/adders/common.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function addEslintConfigPrettier({ ast }: ScriptFileEditor<Record<string,
2525

2626
const fallbackConfig = common.expressionFromString('[]');
2727
const defaultExport = exports.defaultExport(ast, fallbackConfig);
28-
const array = defaultExport.value;
29-
if (array.type !== 'ArrayExpression') return;
28+
const eslintConfig = defaultExport.value;
29+
if (eslintConfig.type !== 'ArrayExpression' && eslintConfig.type !== 'CallExpression') return;
3030

3131
const prettier = common.expressionFromString('prettier');
3232
const sveltePrettierConfig = common.expressionFromString(
@@ -35,11 +35,13 @@ export function addEslintConfigPrettier({ ast }: ScriptFileEditor<Record<string,
3535
const configSpread = common.createSpreadElement(sveltePrettierConfig);
3636

3737
const nodesToInsert = [];
38-
if (!common.hasNode(array, prettier)) nodesToInsert.push(prettier);
39-
if (!common.hasNode(array, configSpread)) nodesToInsert.push(configSpread);
38+
if (!common.hasNode(eslintConfig, prettier)) nodesToInsert.push(prettier);
39+
if (!common.hasNode(eslintConfig, configSpread)) nodesToInsert.push(configSpread);
4040

41+
const elements =
42+
eslintConfig.type == 'ArrayExpression' ? eslintConfig.elements : eslintConfig.arguments;
4143
// finds index of `...svelte.configs["..."]`
42-
const idx = array.elements.findIndex(
44+
const idx = elements.findIndex(
4345
(el) =>
4446
el?.type === 'SpreadElement' &&
4547
el.argument.type === 'MemberExpression' &&
@@ -51,9 +53,9 @@ export function addEslintConfigPrettier({ ast }: ScriptFileEditor<Record<string,
5153
);
5254

5355
if (idx !== -1) {
54-
array.elements.splice(idx + 1, 0, ...nodesToInsert);
56+
elements.splice(idx + 1, 0, ...nodesToInsert);
5557
} else {
5658
// append to the end as a fallback
57-
array.elements.push(...nodesToInsert);
59+
elements.push(...nodesToInsert);
5860
}
5961
}

packages/adders/eslint/config/adder.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { options } from './options.ts';
44
import { addEslintConfigPrettier } from '../../common.ts';
5-
import { defineAdderConfig, log } from '@svelte-cli/core';
6-
import { array, common, exports, imports, object } from '@svelte-cli/core/js';
5+
import { defineAdderConfig, log, type AstKinds, type AstTypes } from '@svelte-cli/core';
6+
import { array, common, exports, functions, imports, object } from '@svelte-cli/core/js';
77

88
export const adder = defineAdderConfig({
99
metadata: {
@@ -65,18 +65,20 @@ export const adder = defineAdderConfig({
6565
name: () => 'eslint.config.js',
6666
contentType: 'script',
6767
content: ({ ast, typescript }) => {
68-
const eslintConfigs = array.createEmpty();
68+
const eslintConfigs: Array<
69+
AstKinds.ExpressionKind | AstTypes.SpreadElement | AstTypes.ObjectExpression
70+
> = [];
6971

7072
const jsConfig = common.expressionFromString('js.configs.recommended');
71-
array.push(eslintConfigs, jsConfig);
73+
eslintConfigs.push(jsConfig);
7274

7375
if (typescript) {
7476
const tsConfig = common.expressionFromString('ts.configs.recommended');
75-
array.push(eslintConfigs, common.createSpreadElement(tsConfig));
77+
eslintConfigs.push(common.createSpreadElement(tsConfig));
7678
}
7779

7880
const svelteConfig = common.expressionFromString('svelte.configs["flat/recommended"]');
79-
array.push(eslintConfigs, common.createSpreadElement(svelteConfig));
81+
eslintConfigs.push(common.createSpreadElement(svelteConfig));
8082

8183
const globalsBrowser = common.createSpreadElement(
8284
common.expressionFromString('globals.browser')
@@ -89,7 +91,7 @@ export const adder = defineAdderConfig({
8991
globals: globalsObjLiteral
9092
})
9193
});
92-
array.push(eslintConfigs, globalsConfig);
94+
eslintConfigs.push(globalsConfig);
9395

9496
if (typescript) {
9597
const svelteTSParserConfig = object.create({
@@ -100,23 +102,35 @@ export const adder = defineAdderConfig({
100102
})
101103
})
102104
});
103-
array.push(eslintConfigs, svelteTSParserConfig);
105+
eslintConfigs.push(svelteTSParserConfig);
104106
}
105107

106108
const ignoresConfig = object.create({
107109
ignores: common.expressionFromString('["build/", ".svelte-kit/", "dist/"]')
108110
});
109-
array.push(eslintConfigs, ignoresConfig);
111+
eslintConfigs.push(ignoresConfig);
110112

111-
const defaultExport = exports.defaultExport(ast, eslintConfigs);
113+
let exportExpression: AstTypes.ArrayExpression | AstTypes.CallExpression;
114+
if (typescript) {
115+
const tsConfigCall = functions.call('ts.config', []);
116+
tsConfigCall.arguments.push(...eslintConfigs);
117+
exportExpression = tsConfigCall;
118+
} else {
119+
const eslintArray = array.createEmpty();
120+
eslintConfigs.map((x) => array.push(eslintArray, x));
121+
exportExpression = eslintArray;
122+
}
123+
124+
const defaultExport = exports.defaultExport(ast, exportExpression);
112125
// if it's not the config we created, then we'll leave it alone and exit out
113-
if (defaultExport.value !== eslintConfigs) {
126+
if (defaultExport.value !== exportExpression) {
114127
log.warn('An eslint config is already defined. Skipping initialization.');
115128
return;
116129
}
117130

118131
// type annotate config
119-
common.addJsDocTypeComment(defaultExport.astNode, "import('eslint').Linter.Config[]");
132+
if (!typescript)
133+
common.addJsDocTypeComment(defaultExport.astNode, "import('eslint').Linter.Config[]");
120134

121135
// imports
122136
if (typescript) imports.addDefault(ast, 'typescript-eslint', 'ts');

0 commit comments

Comments
 (0)