@@ -2,8 +2,8 @@ import fs from 'node:fs';
2
2
import path from 'node:path' ;
3
3
import { options } from './options.ts' ;
4
4
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' ;
7
7
8
8
export const adder = defineAdderConfig ( {
9
9
metadata : {
@@ -65,18 +65,20 @@ export const adder = defineAdderConfig({
65
65
name : ( ) => 'eslint.config.js' ,
66
66
contentType : 'script' ,
67
67
content : ( { ast, typescript } ) => {
68
- const eslintConfigs = array . createEmpty ( ) ;
68
+ const eslintConfigs : Array <
69
+ AstKinds . ExpressionKind | AstTypes . SpreadElement | AstTypes . ObjectExpression
70
+ > = [ ] ;
69
71
70
72
const jsConfig = common . expressionFromString ( 'js.configs.recommended' ) ;
71
- array . push ( eslintConfigs , jsConfig ) ;
73
+ eslintConfigs . push ( jsConfig ) ;
72
74
73
75
if ( typescript ) {
74
76
const tsConfig = common . expressionFromString ( 'ts.configs.recommended' ) ;
75
- array . push ( eslintConfigs , common . createSpreadElement ( tsConfig ) ) ;
77
+ eslintConfigs . push ( common . createSpreadElement ( tsConfig ) ) ;
76
78
}
77
79
78
80
const svelteConfig = common . expressionFromString ( 'svelte.configs["flat/recommended"]' ) ;
79
- array . push ( eslintConfigs , common . createSpreadElement ( svelteConfig ) ) ;
81
+ eslintConfigs . push ( common . createSpreadElement ( svelteConfig ) ) ;
80
82
81
83
const globalsBrowser = common . createSpreadElement (
82
84
common . expressionFromString ( 'globals.browser' )
@@ -89,7 +91,7 @@ export const adder = defineAdderConfig({
89
91
globals : globalsObjLiteral
90
92
} )
91
93
} ) ;
92
- array . push ( eslintConfigs , globalsConfig ) ;
94
+ eslintConfigs . push ( globalsConfig ) ;
93
95
94
96
if ( typescript ) {
95
97
const svelteTSParserConfig = object . create ( {
@@ -100,23 +102,35 @@ export const adder = defineAdderConfig({
100
102
} )
101
103
} )
102
104
} ) ;
103
- array . push ( eslintConfigs , svelteTSParserConfig ) ;
105
+ eslintConfigs . push ( svelteTSParserConfig ) ;
104
106
}
105
107
106
108
const ignoresConfig = object . create ( {
107
109
ignores : common . expressionFromString ( '["build/", ".svelte-kit/", "dist/"]' )
108
110
} ) ;
109
- array . push ( eslintConfigs , ignoresConfig ) ;
111
+ eslintConfigs . push ( ignoresConfig ) ;
110
112
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 ) ;
112
125
// 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 ) {
114
127
log . warn ( 'An eslint config is already defined. Skipping initialization.' ) ;
115
128
return ;
116
129
}
117
130
118
131
// type annotate config
119
- common . addJsDocTypeComment ( defaultExport . astNode , "import('eslint').Linter.Config[]" ) ;
132
+ if ( ! typescript )
133
+ common . addJsDocTypeComment ( defaultExport . astNode , "import('eslint').Linter.Config[]" ) ;
120
134
121
135
// imports
122
136
if ( typescript ) imports . addDefault ( ast , 'typescript-eslint' , 'ts' ) ;
0 commit comments