Skip to content

Commit 0be30bb

Browse files
authored
fix: correct language options for node js config (#42)
1 parent 1df3fe1 commit 0be30bb

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
lines changed

eslint.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import config from '@biscuittin/eslint-config'
22

33
export default config(
44
{
5+
javascript: {
6+
env: {
7+
browser: false,
8+
},
9+
},
510
typescript: {
611
allowDefaultProject: [
712
'commitlint.config.mjs',
Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
1+
import type { Linter } from 'eslint'
2+
13
import globals from 'globals'
24

35
import type { OptionsNodeJs } from '../options.js'
46
import type { TypedFlatConfigItem } from '../types.js'
57

6-
import { GLOB_JS } from '../globs.js'
8+
import { GLOB_JS, GLOB_TS } from '../globs.js'
79
import plugins from '../plugins.js'
810
import { getFlatConfigName, getPackageJson } from '../utils/index.js'
911

1012
const name = getFlatConfigName('node-js')
1113
const isModule = getPackageJson()?.type === 'module'
1214

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+
1333
export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
1434
const { module = isModule, extraFiles = [] } = options
1535

16-
const files: string[] = [GLOB_JS, ...extraFiles]
36+
const files: string[] = [GLOB_JS, GLOB_TS, ...extraFiles]
1737

1838
return [
1939
{
@@ -23,30 +43,12 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
2343
node: plugins['pluginNode'],
2444
},
2545
languageOptions: {
26-
sourceType: module ? 'module' : 'commonjs',
2746
ecmaVersion: 'latest',
2847
parserOptions: {
2948
ecmaFeatures: {
3049
impliedStrict: true,
3150
},
3251
},
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-
},
5052
},
5153
},
5254
{
@@ -57,11 +59,7 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
5759
// pluginNode.configs.commons
5860
// Ref: https://github.com/eslint-community/eslint-plugin-n/blob/ccf5f9e482c32f2fd2d5f78649d7f837a5db8870/lib/configs/_commons.js#L6
5961
'node/no-deprecated-api': 'error',
60-
'node/no-extraneous-import': 'error',
61-
'node/no-extraneous-require': 'error',
6262
'node/no-exports-assign': 'error',
63-
'node/no-missing-import': 'error',
64-
'node/no-missing-require': 'error',
6563
'node/no-process-exit': 'error',
6664
'node/no-unpublished-bin': 'error',
6765
'node/no-unpublished-import': 'error',
@@ -72,6 +70,12 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
7270
'node/process-exit-as-throw': 'error',
7371
'node/hashbang': 'error',
7472

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+
7579
// Require error handling in callbacks
7680
'node/handle-callback-err': ['error', '^error$'],
7781
// Disallow `new` operators with calls to `require`
@@ -94,21 +98,54 @@ export function nodeJs(options: OptionsNodeJs = {}): TypedFlatConfigItem[] {
9498
}),
9599
},
96100
},
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+
},
97116
{
98117
name: name.commonjs,
99-
files: ['*.c[jt]s', '.*.c[jt]s'],
118+
files: ['**/*.c[jt]s'],
100119
languageOptions: {
101120
sourceType: 'commonjs',
121+
parserOptions: {
122+
ecmaFeatures: {
123+
globalReturn: true,
124+
},
125+
},
102126
globals: {
103-
...globals.commonjs,
104-
__dirname: 'readonly',
105-
__filename: 'readonly',
127+
...globalsCommonJs,
106128
},
107129
},
108130
rules: {
109131
strict: ['error', 'global'],
110132
'node/no-unsupported-features/es-syntax': ['error', { ignores: [] }],
111133
},
112134
},
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+
},
113150
]
114151
}

0 commit comments

Comments
 (0)