Skip to content

Commit

Permalink
fix: add files globs to be linted (#130)
Browse files Browse the repository at this point in the history
* fix: add files globs to be linted

* react: **/*.jsx
* vue: **/*.vue
* ts: **/*.ts
* react+ts: **/*.tsx

* fix: add languageOptions
  • Loading branch information
aladdin-add authored Jun 18, 2024
1 parent 6a1d4f2 commit 19d7d6f
Show file tree
Hide file tree
Showing 28 changed files with 51 additions and 12 deletions.
19 changes: 16 additions & 3 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class ConfigGenerator {
this.answers.config = typeof this.answers.config === "string"
? { packageName: this.answers.config, type: "flat" }
: this.answers.config;
const extensions = []; // file extensions to lint, the default is ["js", "mjs", "cjs"]

let importContent = "";
const helperContent = `import path from "path";
Expand Down Expand Up @@ -166,22 +167,29 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug
}

if (this.answers.language === "typescript") {
extensions.push("ts");
this.result.devDependencies.push("typescript-eslint");
this.result.installFlags.push("--force"); // tseslint does not support eslint 9.x yet
importContent += "import tseslint from \"typescript-eslint\";\n";
exportContent += " ...tseslint.configs.recommended,\n";
}

if (this.answers.framework === "vue") {

extensions.push("vue");
this.result.devDependencies.push("eslint-plugin-vue");

importContent += "import pluginVue from \"eslint-plugin-vue\";\n";
exportContent += " ...pluginVue.configs[\"flat/essential\"],\n";
}

if (this.answers.framework === "react") {
exportContent += " { files: [\"**/*.jsx\"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },\n";
extensions.push("jsx");

if (this.answers.language === "typescript") {
extensions.push("tsx");
}
// eslint-plugin-react's flat configs have languageOptions as a non-enumerable property
exportContent = ` { languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },\n${exportContent}`;

if (this.answers.eslintVersion === "9.x") {
this.result.devDependencies.push("eslint-plugin-react", "@eslint/compat");
if (!this.result.installFlags.includes("--force")) {
Expand All @@ -197,6 +205,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug
exportContent += " pluginReactConfig,\n";
}
}

if (this.answers.config) {
const config = this.answers.config;

Expand Down Expand Up @@ -237,6 +246,10 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug
this.result.devDependencies.push("@eslint/eslintrc", "@eslint/js");
}

const lintFilesConfig = extensions.length > 0 ? ` {files: ["**/*.{${["js", "mjs", "cjs", ...extensions]}}"]},\n` : "";

exportContent = `${lintFilesConfig}${exportContent}`;

this.result.configContent = `${importContent}
${needCompatHelper ? helperContent : ""}
export default [\n${exportContent}];`;
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/config@eslint-config-airbnb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export default [
"devDependencies": [
"eslint@^7.32.0 || ^8.2.0",
"eslint-config-airbnb",
"eslint-plugin-react@^7.28.0",
"eslint-plugin-import@^2.25.3",
"eslint-plugin-jsx-a11y@^6.5.1",
"eslint-plugin-react@^7.28.0",
"eslint-plugin-react-hooks@^4.3.0",
"@eslint/eslintrc",
"@eslint/js",
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-commonjs-none-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tseslint from "typescript-eslint";


export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-commonjs-vue-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-commonjs-vue-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,ts,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-esm-none-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tseslint from "typescript-eslint";


export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/problems-esm-react-eslint8.x-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import pluginReactConfig from "eslint-plugin-react/configs/recommended.js";


export default [
{files: ["**/*.{js,mjs,cjs,jsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
pluginReactConfig,
];",
"configFilename": "eslint.config.js",
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/problems-esm-react-eslint8.x-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import pluginReactConfig from "eslint-plugin-react/configs/recommended.js";


export default [
{files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
pluginReactConfig,
];",
"configFilename": "eslint.config.js",
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/problems-esm-react-eslint9.x-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { fixupConfigRules } from "@eslint/compat";


export default [
{files: ["**/*.{js,mjs,cjs,jsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
...fixupConfigRules(pluginReactConfig),
];",
"configFilename": "eslint.config.js",
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/problems-esm-react-eslint9.x-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { fixupConfigRules } from "@eslint/compat";


export default [
{files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
...fixupConfigRules(pluginReactConfig),
];",
"configFilename": "eslint.config.js",
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-esm-vue-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,vue}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
...pluginVue.configs["flat/essential"],
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-esm-vue-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,ts,vue}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-script-none-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tseslint from "typescript-eslint";


export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "script"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-script-vue-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "script"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/problems-script-vue-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,ts,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "script"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
pluginJs.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-commonjs-none-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import tseslint from "typescript-eslint";


export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...tseslint.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-commonjs-vue-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...pluginVue.configs["flat/essential"],
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-commonjs-vue-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,ts,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "commonjs"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...tseslint.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-esm-none-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import tseslint from "typescript-eslint";


export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...tseslint.configs.recommended,
];",
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/syntax-esm-react-eslint8.x-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import pluginReactConfig from "eslint-plugin-react/configs/recommended.js";


export default [
{files: ["**/*.{js,mjs,cjs,jsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
pluginReactConfig,
];",
"configFilename": "eslint.config.js",
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/syntax-esm-react-eslint8.x-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import pluginReactConfig from "eslint-plugin-react/configs/recommended.js";


export default [
{files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
...tseslint.configs.recommended,
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
pluginReactConfig,
];",
"configFilename": "eslint.config.js",
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/syntax-esm-react-eslint9.x-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { fixupConfigRules } from "@eslint/compat";


export default [
{files: ["**/*.{js,mjs,cjs,jsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
...fixupConfigRules(pluginReactConfig),
];",
"configFilename": "eslint.config.js",
Expand Down
3 changes: 2 additions & 1 deletion tests/__snapshots__/syntax-esm-react-eslint9.x-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { fixupConfigRules } from "@eslint/compat";


export default [
{files: ["**/*.{js,mjs,cjs,ts,jsx,tsx}"]},
{ languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
{languageOptions: { globals: globals.browser }},
...tseslint.configs.recommended,
{ files: ["**/*.jsx"], languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } } },
...fixupConfigRules(pluginReactConfig),
];",
"configFilename": "eslint.config.js",
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-esm-vue-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,vue}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...pluginVue.configs["flat/essential"],
];",
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-esm-vue-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,ts,vue}"]},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...tseslint.configs.recommended,
...pluginVue.configs["flat/essential"],
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-script-none-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import tseslint from "typescript-eslint";


export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "script"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...tseslint.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-script-vue-javascript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "script"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...pluginVue.configs["flat/essential"],
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/syntax-script-vue-typescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pluginVue from "eslint-plugin-vue";


export default [
{files: ["**/*.{js,mjs,cjs,ts,vue}"]},
{files: ["**/*.js"], languageOptions: {sourceType: "script"}},
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
...tseslint.configs.recommended,
Expand Down

0 comments on commit 19d7d6f

Please sign in to comment.