Skip to content

Commit 5c5b802

Browse files
authored
feat: Add --ext CLI option (#19405)
* feat: Add `--ext` CLI option Fixes #19361 * fix typos in test descriptions * check for empty input
1 parent dd7d930 commit 5c5b802

File tree

13 files changed

+197
-39
lines changed

13 files changed

+197
-39
lines changed

‎docs/src/use/command-line-interface.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Basic configuration:
9393
-c, --config path::String Use this configuration instead of eslint.config.js, eslint.config.mjs, or
9494
eslint.config.cjs
9595
--inspect-config Open the config inspector with the current configuration
96+
--ext [String] Specify additional file extensions to lint
9697
--global [String] Define global variables
9798
--parser String Specify the parser to be used
9899
--parser-options Object Specify parser options
@@ -219,34 +220,32 @@ Details about the global variables defined by each environment are available in
219220

220221
#### `--ext`
221222

222-
**eslintrc Mode Only.** If you are using flat config (`eslint.config.js`), please see [migration guide](./configure/migration-guide#--ext).
223-
224-
This option allows you to specify which file extensions ESLint uses when searching for target files in the directories you specify.
223+
This option allows you to specify additional file extensions to lint.
225224

226225
* **Argument Type**: String. File extension.
227226
* **Multiple Arguments**: Yes
228-
* **Default Value**: `.js` and the files that match the `overrides` entries of your configuration.
227+
* **Default Value**: By default, ESLint lints files with extensions `.js`, `.mjs`, `.cjs`, and additional extensions [specified in the configuration file](configure/configuration-files#specifying-files-with-arbitrary-extensions).
229228

230-
`--ext` is only used when the patterns to lint are directories. If you use glob patterns or file names, then `--ext` is ignored. For example, `npx eslint "lib/*" --ext .js` matches all files within the `lib/` directory, regardless of extension.
229+
This option is primarely intended for use in combination with the `--no-config-lookup` option, since in that case there is no configuration file in which the additional extensions would be specified.
231230

232231
##### `--ext` example
233232

234233
{{ npx_tabs ({
235234
package: "eslint",
236235
args: [".", "--ext", ".ts"],
237-
comment: "Use only .ts extension"
236+
comment: "Include .ts files"
238237
}) }}
239238

240239
{{ npx_tabs ({
241240
package: "eslint",
242-
args: [".", "--ext", ".js", "--ext", ".ts"],
243-
comment: "Use both .js and .ts"
241+
args: [".", "--ext", ".ts", "--ext", ".tsx"],
242+
comment: "Include .ts and .tsx files"
244243
}) }}
245244

246245
{{ npx_tabs ({
247246
package: "eslint",
248-
args: [".", "--ext", ".js,.ts"],
249-
comment: "Also use both .js and .ts"
247+
args: [".", "--ext", ".ts,.tsx"],
248+
comment: "Also include .ts and .tsx files"
250249
}) }}
251250

252251
#### `--global`

‎lib/cli.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ async function translateOptions({
192192
overrideConfig[0].plugins = await loadPlugins(importer, plugin);
193193
}
194194

195+
if (ext) {
196+
overrideConfig.push({
197+
files: ext.map(extension => `**/*${extension.startsWith(".") ? "" : "."}${extension}`)
198+
});
199+
}
200+
195201
} else {
196202
overrideConfigFile = config;
197203

@@ -489,6 +495,23 @@ const cli = {
489495
return 2;
490496
}
491497

498+
if (usingFlatConfig && options.ext) {
499+
500+
// Passing `--ext ""` results in `options.ext` being an empty array.
501+
if (options.ext.length === 0) {
502+
log.error("The --ext option value cannot be empty.");
503+
return 2;
504+
}
505+
506+
// Passing `--ext ,ts` results in an empty string at index 0. Passing `--ext ts,,tsx` results in an empty string at index 1.
507+
const emptyStringIndex = options.ext.indexOf("");
508+
509+
if (emptyStringIndex >= 0) {
510+
log.error(`The --ext option arguments cannot be empty strings. Found an empty string at index ${emptyStringIndex}.`);
511+
return 2;
512+
}
513+
}
514+
492515
const ActiveESLint = usingFlatConfig ? ESLint : LegacyESLint;
493516
const eslintOptions = await translateOptions(options, usingFlatConfig ? "flat" : "eslintrc");
494517
const engine = new ActiveESLint(eslintOptions);

‎lib/options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ module.exports = function(usingFlatConfig) {
123123
type: "[String]",
124124
description: "Specify JavaScript file extensions"
125125
};
126+
} else {
127+
extFlag = {
128+
option: "ext",
129+
type: "[String]",
130+
description: "Specify additional file extensions to lint"
131+
};
126132
}
127133

128134
let resolvePluginsFlag;

‎tests/fixtures/file-extensions/a.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

‎tests/fixtures/file-extensions/b.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

‎tests/fixtures/file-extensions/c.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

‎tests/fixtures/file-extensions/d.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [{ files: ["**/*.jsx"] }];

‎tests/fixtures/file-extensions/f.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

‎tests/fixtures/file-extensions/foots

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// this file should not be linted with --ext ts

0 commit comments

Comments
 (0)