1
+ import type { Linter } from 'eslint'
2
+
1
3
import globals from 'globals'
2
4
3
5
import type { OptionsNodeJs } from '../options.js'
4
6
import type { TypedFlatConfigItem } from '../types.js'
5
7
6
- import { GLOB_JS } from '../globs.js'
8
+ import { GLOB_JS , GLOB_TS } from '../globs.js'
7
9
import plugins from '../plugins.js'
8
10
import { getFlatConfigName , getPackageJson } from '../utils/index.js'
9
11
10
12
const name = getFlatConfigName ( 'node-js' )
11
13
const isModule = getPackageJson ( ) ?. type === 'module'
12
14
15
+ const globalsCommonJs : Linter . Globals = {
16
+ ...globals . es2025 ,
17
+ ...globals . node ,
18
+ ...globals . commonjs ,
19
+ __dirname : 'readonly' ,
20
+ __filename : 'readonly' ,
21
+ }
22
+
23
+ const globalsModule : Linter . Globals = {
24
+ ...globals . es2025 ,
25
+ ...globals . node ,
26
+ __dirname : 'off' ,
27
+ __filename : 'off' ,
28
+ exports : 'off' ,
29
+ module : 'off' ,
30
+ require : 'off' ,
31
+ }
32
+
13
33
export function nodeJs ( options : OptionsNodeJs = { } ) : TypedFlatConfigItem [ ] {
14
34
const { module = isModule , extraFiles = [ ] } = options
15
35
16
- const files : string [ ] = [ GLOB_JS , ...extraFiles ]
36
+ const files : string [ ] = [ GLOB_JS , GLOB_TS , ...extraFiles ]
17
37
18
38
return [
19
39
{
@@ -23,30 +43,12 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
23
43
node : plugins [ 'pluginNode' ] ,
24
44
} ,
25
45
languageOptions : {
26
- sourceType : module ? 'module' : 'commonjs' ,
27
46
ecmaVersion : 'latest' ,
28
47
parserOptions : {
29
48
ecmaFeatures : {
30
49
impliedStrict : true ,
31
50
} ,
32
51
} ,
33
- globals : {
34
- ...globals . es2025 ,
35
- ...globals . node ,
36
- ...( module
37
- ? {
38
- __dirname : 'off' ,
39
- __filename : 'off' ,
40
- exports : 'off' ,
41
- module : 'off' ,
42
- require : 'off' ,
43
- }
44
- : {
45
- ...globals . commonjs ,
46
- __dirname : 'readonly' ,
47
- __filename : 'readonly' ,
48
- } ) ,
49
- } ,
50
52
} ,
51
53
} ,
52
54
{
@@ -57,11 +59,7 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
57
59
// pluginNode.configs.commons
58
60
// Ref: https://github.com/eslint-community/eslint-plugin-n/blob/ccf5f9e482c32f2fd2d5f78649d7f837a5db8870/lib/configs/_commons.js#L6
59
61
'node/no-deprecated-api' : 'error' ,
60
- 'node/no-extraneous-import' : 'error' ,
61
- 'node/no-extraneous-require' : 'error' ,
62
62
'node/no-exports-assign' : 'error' ,
63
- 'node/no-missing-import' : 'error' ,
64
- 'node/no-missing-require' : 'error' ,
65
63
'node/no-process-exit' : 'error' ,
66
64
'node/no-unpublished-bin' : 'error' ,
67
65
'node/no-unpublished-import' : 'error' ,
@@ -72,6 +70,12 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
72
70
'node/process-exit-as-throw' : 'error' ,
73
71
'node/hashbang' : 'error' ,
74
72
73
+ // Will handled by `eslint-plugin-import-x`
74
+ 'node/no-extraneous-import' : 'off' ,
75
+ 'node/no-extraneous-require' : 'off' ,
76
+ 'node/no-missing-import' : 'off' ,
77
+ 'node/no-missing-require' : 'off' ,
78
+
75
79
// Require error handling in callbacks
76
80
'node/handle-callback-err' : [ 'error' , '^error$' ] ,
77
81
// Disallow `new` operators with calls to `require`
@@ -94,21 +98,54 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
94
98
} ) ,
95
99
} ,
96
100
} ,
101
+ {
102
+ name : name . script ,
103
+ files : [ '**/*.[jt]s' ] ,
104
+ languageOptions : {
105
+ sourceType : module ? 'module' : 'commonjs' ,
106
+ parserOptions : {
107
+ ecmaFeatures : {
108
+ globalReturn : ! module ,
109
+ } ,
110
+ } ,
111
+ globals : {
112
+ ...( module ? globalsModule : globalsCommonJs ) ,
113
+ } ,
114
+ } ,
115
+ } ,
97
116
{
98
117
name : name . commonjs ,
99
- files : [ '*.c[jt]s' , '. *.c[jt]s'] ,
118
+ files : [ '**/ *.c[jt]s' ] ,
100
119
languageOptions : {
101
120
sourceType : 'commonjs' ,
121
+ parserOptions : {
122
+ ecmaFeatures : {
123
+ globalReturn : true ,
124
+ } ,
125
+ } ,
102
126
globals : {
103
- ...globals . commonjs ,
104
- __dirname : 'readonly' ,
105
- __filename : 'readonly' ,
127
+ ...globalsCommonJs ,
106
128
} ,
107
129
} ,
108
130
rules : {
109
131
strict : [ 'error' , 'global' ] ,
110
132
'node/no-unsupported-features/es-syntax' : [ 'error' , { ignores : [ ] } ] ,
111
133
} ,
112
134
} ,
135
+ {
136
+ name : name . module ,
137
+ files : [ '**/*.m[jt]s' ] ,
138
+ languageOptions : {
139
+ sourceType : 'module' ,
140
+ parserOptions : {
141
+ ecmaFeatures : {
142
+ globalReturn : false ,
143
+ } ,
144
+ } ,
145
+ globals : {
146
+ ...globalsModule ,
147
+ } ,
148
+ } ,
149
+ } ,
113
150
]
114
151
}
0 commit comments