Build: improve specificity of eslint config; add ecma versions
[jquery.git] / eslint.config.js
blobd4e57930efc4c3ada46012d052ede4b9a6aa37e6
1 import jqueryConfig from "eslint-config-jquery";
2 import importPlugin from "eslint-plugin-import";
3 import globals from "globals";
5 export default [
6         {
8                 // Only global ignores will bypass the parser
9                 // and avoid JS parsing errors
10                 // See https://github.com/eslint/eslint/discussions/17412
11                 ignores: [
12                         "external",
13                         "**/tmp",
14                         "test/data/json_obj.js"
15                 ]
16         },
18         // Source
19         {
20                 files: [ "src/**" ],
21                 plugins: {
22                         import: importPlugin
23                 },
24                 languageOptions: {
25                         ecmaVersion: 2015,
27                         // The browser env is not enabled on purpose so that code takes
28                         // all browser-only globals from window instead of assuming
29                         // they're available as globals. This makes it possible to use
30                         // jQuery with tools like jsdom which provide a custom window
31                         // implementation.
32                         globals: {
33                                 window: false
34                         }
35                 },
36                 rules: {
37                         ...jqueryConfig.rules,
38                         "import/extensions": [ "error", "always" ],
39                         "import/no-cycle": "error",
41                         // TODO: Enable this rule when eslint-plugin-import supports
42                         // it when using flat config.
43                         // See https://github.com/import-js/eslint-plugin-import/issues/2556
45                         // "import/no-unused-modules": [
46                         //      "error",
47                         //      {
48                         //              unusedExports: true,
50                         //              // When run via WebStorm, the root path against which these paths
51                         //              // are resolved is the path where this ESLint config file lies,
52                         //              // i.e. `src`. When run via the command line, it's usually the root
53                         //              // folder of the jQuery repository. This pattern intends to catch both.
54                         //              // Note that we cannot specify two patterns here:
55                         //              //     [ "src/*.js", "*.js" ]
56                         //              // as they're analyzed individually and the rule crashes if a pattern
57                         //              // cannot be matched.
58                         //              ignoreExports: [ "{src/,}*.js" ]
59                         //      }
60                         // ],
61                         indent: [
62                                 "error",
63                                 "tab",
64                                 {
65                                         outerIIFEBody: 0
66                                 }
67                         ],
68                         "no-implicit-globals": "error",
69                         "one-var": [ "error", { var: "always" } ],
70                         strict: [ "error", "function" ]
71                 }
72         },
74         {
75                 files: [
76                         "src/wrapper.js",
77                         "src/wrapper-esm.js",
78                         "src/wrapper-factory.js",
79                         "src/wrapper-factory-esm.js"
80                 ],
81                 languageOptions: {
82                         globals: {
83                                 jQuery: false
84                         }
85                 },
86                 rules: {
87                         "no-unused-vars": "off",
88                         indent: [
89                                 "error",
90                                 "tab",
91                                 {
93                                         // This makes it so code within the wrapper is not indented.
94                                         ignoredNodes: [
95                                                 "Program > FunctionDeclaration > *"
96                                         ]
97                                 }
98                         ]
99                 }
100         },
102         {
103                 files: [
104                         "src/wrapper.js",
105                         "src/wrapper-factory.js"
106                 ],
107                 languageOptions: {
108                         sourceType: "script",
109                         globals: {
110                                 module: false
111                         }
112                 }
113         },
115         {
116                 files: [ "src/wrapper.js" ],
117                 rules: {
118                         indent: [
119                                 "error",
120                                 "tab",
121                                 {
123                                         // This makes it so code within the wrapper is not indented.
124                                         ignoredNodes: [
125                                                 "Program > ExpressionStatement > CallExpression > :last-child > *"
126                                         ]
127                                 }
128                         ]
129                 }
130         },
132         {
133                 files: [ "src/exports/amd.js" ],
134                 languageOptions: {
135                         globals: {
136                                 define: false
137                         }
138                 }
139         },
141         // Tests
142         {
143                 files: [
144                         "test/*",
145                         "test/data/**",
146                         "test/integration/**",
147                         "test/unit/**"
148                 ],
149                 ignores: [
150                         "test/data/jquery-3.7.1.js",
151                         "test/data/badcall.js",
152                         "test/data/badjson.js",
153                         "test/data/support/csp.js",
154                         "test/data/support/getComputedSupport.js",
155                         "test/data/core/jquery-iterability-transpiled.js"
156                 ],
157                 languageOptions: {
158                         ecmaVersion: 2015,
159                         sourceType: "script",
160                         globals: {
161                                 ...globals.browser,
162                                 require: false,
163                                 trustedTypes: false,
164                                 QUnit: false,
165                                 ajaxTest: false,
166                                 testIframe: false,
167                                 createDashboardXML: false,
168                                 createWithFriesXML: false,
169                                 createXMLFragment: false,
170                                 includesModule: false,
171                                 moduleTeardown: false,
172                                 url: false,
173                                 q: false,
174                                 jQuery: false,
175                                 $: false,
176                                 sinon: false,
177                                 amdDefined: false,
178                                 fireNative: false,
179                                 Globals: false,
180                                 hasPHP: false,
181                                 isLocal: false,
182                                 supportjQuery: false,
183                                 originaljQuery: false,
184                                 original$: false,
185                                 baseURL: false,
186                                 externalHost: false
187                         }
188                 },
189                 rules: {
190                         ...jqueryConfig.rules,
192                         "no-unused-vars": [
193                                 "error",
194                                 { args: "after-used", argsIgnorePattern: "^_" }
195                         ],
197                         // Too many errors
198                         "max-len": "off",
199                         camelcase: "off"
200                 }
201         },
203         {
204                 files: [
205                         "test/unit/core.js"
206                 ],
207                 rules: {
209                         // Core has several cases where unused vars are expected
210                         "no-unused-vars": "off"
211                 }
212         },
214         {
215                 files: [
216                         "test/runner/**/*.js"
217                 ],
218                 languageOptions: {
219                         ecmaVersion: "latest",
220                         globals: {
221                                 ...globals.node
222                         }
223                 },
224                 rules: {
225                         ...jqueryConfig.rules
226                 }
227         },
229         {
230                 files: [ "test/runner/listeners.js" ],
231                 languageOptions: {
232                         ecmaVersion: 5,
233                         sourceType: "script",
234                         globals: {
235                                 ...globals.browser,
236                                 QUnit: false,
237                                 Symbol: false
238                         }
239                 }
240         },
242         {
243                 files: [
244                         "test/data/testinit.js",
245                         "test/data/testrunner.js",
246                         "test/data/core/jquery-iterability-transpiled-es6.js"
247                 ],
248                 languageOptions: {
249                         ecmaVersion: 2015,
250                         sourceType: "script",
251                         globals: {
252                                 ...globals.browser
253                         }
254                 },
255                 rules: {
256                         ...jqueryConfig.rules,
257                         strict: [ "error", "function" ]
258                 }
259         },
261         {
262                 files: [
263                         "test/data/testinit.js"
264                 ],
265                 rules: {
266                         strict: [ "error", "global" ]
267                 }
268         },
270         {
271                 files: [
272                         "test/unit/deferred.js"
273                 ],
274                 rules: {
276                         // Deferred tests set strict mode for certain tests
277                         strict: "off"
278                 }
279         },
281         {
282                 files: [
283                         "build/**",
284                         "eslint.config.js",
285                         "test/node_smoke_tests/**",
286                         "test/bundler_smoke_tests/**/*",
287                         "test/promises_aplus_adapters/**",
288                         "test/middleware-mockserver.cjs"
289                 ],
290                 languageOptions: {
291                         ecmaVersion: "latest",
292                         globals: {
293                                 ...globals.browser,
294                                 ...globals.node
295                         }
296                 },
297                 rules: {
298                         ...jqueryConfig.rules,
299                         "no-implicit-globals": "error",
300                         strict: [ "error", "global" ]
301                 }
302         },
304         {
305                 files: [
306                         "build/**/*.js"
307                 ],
308                 languageOptions: {
309                         sourceType: "commonjs"
310                 }
311         },
313         {
314                 files: [
315                         "dist/jquery.js",
316                         "dist/jquery.slim.js",
317                         "dist/jquery.factory.js",
318                         "dist/jquery.factory.slim.js",
319                         "dist-module/jquery.module.js",
320                         "dist-module/jquery.slim.module.js",
321                         "dist-module/jquery.factory.module.js",
322                         "dist-module/jquery.factory.slim.module.js",
323                         "dist/jquery.bundler-require-wrapper.js",
324                         "dist/jquery.bundler-require-wrapper.slim.js",
325                         "dist-module/jquery.node-module-wrapper.js",
326                         "dist-module/jquery.node-module-wrapper.slim.js"
327                 ],
328                 languageOptions: {
329                         ecmaVersion: 2015,
330                         globals: {
331                                 define: false,
332                                 module: false,
333                                 Symbol: false,
334                                 window: false
335                         }
336                 },
337                 rules: {
338                         ...jqueryConfig.rules,
340                         "no-implicit-globals": "error",
342                         // That is okay for the built version
343                         "no-multiple-empty-lines": "off",
345                         // When custom compilation is used, the version string
346                         // can get large. Accept that in the built version.
347                         "max-len": "off",
348                         "one-var": "off"
349                 }
350         },
352         {
353                 files: [
354                         "src/wrapper.js",
355                         "src/wrapper-factory.js",
356                         "dist/jquery.factory.js",
357                         "dist/jquery.factory.slim.js",
358                         "test/middleware-mockserver.cjs"
359                 ],
360                 rules: {
361                         "no-implicit-globals": "off"
362                 }
363         },
365         {
366                 files: [
367                         "dist/**"
368                 ],
369                 languageOptions: {
370                         ecmaVersion: 5,
371                         sourceType: "script"
372                 }
373         },
375         {
376                 files: [
377                         "dist-module/**"
378                 ],
379                 languageOptions: {
380                         ecmaVersion: 2015,
381                         sourceType: "module"
382                 }
383         },
385         {
386                 files: [
387                         "dist/jquery.bundler-require-wrapper.js",
388                         "dist/jquery.bundler-require-wrapper.slim.js"
389                 ],
390                 languageOptions: {
391                         ecmaVersion: 2015,
392                         sourceType: "commonjs"
393                 }
394         }