diff --git a/.eslintrc.js b/.eslintrc.js index 65d6a289..1d0efec0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,6 +13,17 @@ module.exports = { }, ignorePatterns: [ "test-packages/**" ], rules: { + "node/no-unpublished-import": "off", "node/no-unpublished-require": "off", + "node/no-unsupported-features/es-syntax": "off", }, + overrides: [ + { + files: [ "examples/flat-config/**/*" ], + parserOptions: { + sourceType: "module", + project: "./examples/flat-config/tsconfig.json", + }, + }, + ], }; diff --git a/README.md b/README.md index bb7cdbb3..7566e81d 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,6 @@ An ESLint plugin to enforce EditorConfig rules $ npm install --save-dev eslint@8 eslint-plugin-editorconfig ``` -or - -```bash -$ yarn add --dev eslint@8 eslint-plugin-editorconfig -``` - If you use [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint), you need to install `@typescript-eslint/eslint-plugin` too. ```bash @@ -78,11 +72,7 @@ It is recommended to disable them. - `linebreak-style` - `no-trailing-spaces` - `unicode-bom` -- `@typescript-eslint/eol-last` - `@typescript-eslint/indent` -- `@typescript-eslint/linebreak-style` -- `@typescript-eslint/no-trailing-spaces` -- `@typescript-eslint/unicode-bom` If above rules are specified in your .eslintrc, just remove them. If they are specified in the extended config, consider adding `plugin:editorconfig/noconflict` to your `extends`. diff --git a/examples/flat-config/eslint.config.js b/examples/flat-config/eslint.config.js index 6738b945..7e6e5795 100644 --- a/examples/flat-config/eslint.config.js +++ b/examples/flat-config/eslint.config.js @@ -16,6 +16,6 @@ export default [ plugins: [ "editorconfig" ], }).map(config => ({ ...config, - ignores: [ join(__dirname, "src/invalid.ts") ] + ignores: [ join(__dirname, "src/invalid.ts") ], })), ]; diff --git a/examples/flat-config/package.json b/examples/flat-config/package.json index b62109c4..a596e692 100644 --- a/examples/flat-config/package.json +++ b/examples/flat-config/package.json @@ -7,10 +7,14 @@ "scripts": { "lint": "eslint ." }, + "dependencies": { + "undici": "^6.2.1" + }, "devDependencies": { + "@eslint/eslintrc": "^3.1.0", + "@phanect/configs": "latest", "@types/node": "^20.10.6", "eslint": "^8.56.0", - "eslint-plugin-editorconfig": "^4.0.3", - "undici": "^6.2.1" + "eslint-plugin-editorconfig": "^4.0.3" } } diff --git a/examples/flat-config/src/invalid.ts b/examples/flat-config/src/invalid.ts index f8ced376..bc8f3a6b 100644 --- a/examples/flat-config/src/invalid.ts +++ b/examples/flat-config/src/invalid.ts @@ -3,6 +3,7 @@ import { fetch } from "undici"; const res = await fetch("https://example.com"); try { + // eslint-disable-next-line editorconfig/indent const data = await res.json(); // This line should be warned by this plugin console.log(data); } catch (err) { diff --git a/examples/flat-config/tsconfig.json b/examples/flat-config/tsconfig.json new file mode 100644 index 00000000..6837c24c --- /dev/null +++ b/examples/flat-config/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "@phanect/configs/tsconfig/nodejs", + "include": [ "**/*.ts" ] +} diff --git a/lib/base.js b/lib/base.js index 14e89cb6..cdc676a4 100644 --- a/lib/base.js +++ b/lib/base.js @@ -25,16 +25,15 @@ module.exports.buildRule = ({ baseRuleName, description, omitFirstOption, getESL }, create: function(context) { - const filename = context.getFilename(); - const ecParams = editorconfig.parseSync(context.getFilename(filename)); + const ecParams = editorconfig.parseSync(context.filename); const { enabled, eslintOption } = getESLintOption(ecParams); let baseRule; - if (filename.endsWith(".ts")) { + if (context.filename.endsWith(".ts")) { try { const { rules } = require("@typescript-eslint/eslint-plugin"); - baseRule = rules[baseRuleName] ? klona(rules[baseRuleName]) : jsBaseRule; + baseRule = rules?.[baseRuleName] ? klona(rules[baseRuleName]) : jsBaseRule; } catch (err) { if (err.code === "MODULE_NOT_FOUND") { throw new Error("eslint-plugin-editorconfig requires typescript and @typescript-eslint/eslint-plugin to lint *.ts files. Run `npm install typescript @typescript-eslint/eslint-plugin`."); diff --git a/main.js b/main.js index 44b0eef4..10d4629c 100644 --- a/main.js +++ b/main.js @@ -16,11 +16,7 @@ module.exports = { "linebreak-style": "off", "no-trailing-spaces": "off", "unicode-bom": "off", - "@typescript-eslint/eol-last": "off", "@typescript-eslint/indent": "off", - "@typescript-eslint/linebreak-style": "off", - "@typescript-eslint/no-trailing-spaces": "off", - "@typescript-eslint/unicode-bom": "off", }, }, all: { @@ -30,11 +26,7 @@ module.exports = { "linebreak-style": "off", "no-trailing-spaces": "off", "unicode-bom": "off", - "@typescript-eslint/eol-last": "off", "@typescript-eslint/indent": "off", - "@typescript-eslint/linebreak-style": "off", - "@typescript-eslint/no-trailing-spaces": "off", - "@typescript-eslint/unicode-bom": "off", "editorconfig/charset": "error", "editorconfig/eol-last": "error", diff --git a/package.json b/package.json index 4c2d052f..a5dec437 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,9 @@ "url": "https://github.com/phanect/eslint-plugin-editorconfig/issues" }, "homepage": "https://github.com/phanect/eslint-plugin-editorconfig", + "workspaces": [ + "examples/*" + ], "dependencies": { "editorconfig": "^1.0.2", "eslint": "^8.40.0", diff --git a/test-packages/success/base-rules-conflict/.editorconfig b/test-packages/success/base-rules-conflict/.editorconfig new file mode 100644 index 00000000..8c52ff93 --- /dev/null +++ b/test-packages/success/base-rules-conflict/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/test-packages/success/js/.editorconfig b/test-packages/success/js/.editorconfig new file mode 100644 index 00000000..8c52ff93 --- /dev/null +++ b/test-packages/success/js/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/test-packages/success/ts/.editorconfig b/test-packages/success/ts/.editorconfig new file mode 100644 index 00000000..8c52ff93 --- /dev/null +++ b/test-packages/success/ts/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/tests/lib/rules/editorconfig-ts.js b/tests/lib/rules/editorconfig-ts.js index c6e38d23..9069f4fb 100644 --- a/tests/lib/rules/editorconfig-ts.js +++ b/tests/lib/rules/editorconfig-ts.js @@ -1,17 +1,8 @@ "use strict"; -// ----------------------------------------------------------------------------- -// Requirements -// ----------------------------------------------------------------------------- - const path = require("path"); const RuleTester = require("eslint").RuleTester; - -// ----------------------------------------------------------------------------- -// Tests -// ----------------------------------------------------------------------------- - const ruleTester = new RuleTester({ parser: require.resolve("@typescript-eslint/parser"), parserOptions: { diff --git a/tests/lib/rules/editorconfig.js b/tests/lib/rules/editorconfig.js index 537244a7..49505d13 100644 --- a/tests/lib/rules/editorconfig.js +++ b/tests/lib/rules/editorconfig.js @@ -1,17 +1,8 @@ "use strict"; -// ----------------------------------------------------------------------------- -// Requirements -// ----------------------------------------------------------------------------- - const path = require("path"); const RuleTester = require("eslint").RuleTester; - -// ----------------------------------------------------------------------------- -// Tests -// ----------------------------------------------------------------------------- - const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2019 }}); const commonValidTests = [