From a81a3228be66468ff3646f4c31b653c6521ebf09 Mon Sep 17 00:00:00 2001 From: zengxiaoluan <370203400@qq.com> Date: Fri, 9 Aug 2024 23:37:05 +0800 Subject: [PATCH] fix: update recommended config --- README.md | 21 +++++++++++++-------- docs/rules/always-camel-case.md | 2 ++ docs/rules/comments-need-space.md | 2 ++ docs/rules/kebab-case.md | 2 ++ docs/rules/no-then.md | 2 ++ docs/rules/prefer-template-literal.md | 2 ++ eslint.config.mjs | 15 +++++++++++++++ lib/configs.js | 5 +++++ lib/configs/recommended.js | 10 ++++++++++ lib/index.js | 8 +++----- lib/rules/kebab-case.js | 9 +++++++-- lib/utils.js | 12 ++++++++++++ package-lock.json | 4 ++-- 13 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 lib/configs.js create mode 100644 lib/configs/recommended.js diff --git a/README.md b/README.md index 4c4ef50..2913317 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,9 @@ Then configure the rules you want to use under the rules section. -TODO: Run eslint-doc-generator to generate the configs list (or delete this section if no configs are offered). +| | Name | +| :- | :------------ | +| ✅ | `recommended` | @@ -48,12 +50,15 @@ TODO: Run eslint-doc-generator to generate the configs list (or delete this sect -| Name | Description | -| :--------------------------------------------------------------- | :-------------------------------------------- | -| [always-camel-case](docs/rules/always-camel-case.md) | Always camel case every thing | -| [comments-need-space](docs/rules/comments-need-space.md) | comments need space | -| [kebab-case](docs/rules/kebab-case.md) | kebab case your file name and directory name. | -| [no-then](docs/rules/no-then.md) | No then with promise | -| [prefer-template-literal](docs/rules/prefer-template-literal.md) | prefer template literal | +💼 Configurations enabled in.\ +✅ Set in the `recommended` configuration. + +| Name | Description | 💼 | +| :--------------------------------------------------------------- | :-------------------------------------------- | :- | +| [always-camel-case](docs/rules/always-camel-case.md) | Always camel case every thing | ✅ | +| [comments-need-space](docs/rules/comments-need-space.md) | comments need space | ✅ | +| [kebab-case](docs/rules/kebab-case.md) | kebab case your file name and directory name. | ✅ | +| [no-then](docs/rules/no-then.md) | No then with promise | ✅ | +| [prefer-template-literal](docs/rules/prefer-template-literal.md) | prefer template literal | ✅ | diff --git a/docs/rules/always-camel-case.md b/docs/rules/always-camel-case.md index d7ed2e7..2ea8d88 100644 --- a/docs/rules/always-camel-case.md +++ b/docs/rules/always-camel-case.md @@ -1,5 +1,7 @@ # Always camel case every thing (`jlc/always-camel-case`) +💼 This rule is enabled in the ✅ `recommended` config. + ## Rule Details diff --git a/docs/rules/comments-need-space.md b/docs/rules/comments-need-space.md index fe65f0d..bec621a 100644 --- a/docs/rules/comments-need-space.md +++ b/docs/rules/comments-need-space.md @@ -1,5 +1,7 @@ # Comments need space (`jlc/comments-need-space`) +💼 This rule is enabled in the ✅ `recommended` config. + Comments like below is not ok: diff --git a/docs/rules/kebab-case.md b/docs/rules/kebab-case.md index eb675e6..3e5ab5d 100644 --- a/docs/rules/kebab-case.md +++ b/docs/rules/kebab-case.md @@ -1,5 +1,7 @@ # Kebab case your file name and directory name (`jlc/kebab-case`) +💼 This rule is enabled in the ✅ `recommended` config. + If you naming filename like this, it's bad case: diff --git a/docs/rules/no-then.md b/docs/rules/no-then.md index 955fa19..dde1d7e 100644 --- a/docs/rules/no-then.md +++ b/docs/rules/no-then.md @@ -1,5 +1,7 @@ # No then with promise (`jlc/no-then`) +💼 This rule is enabled in the ✅ `recommended` config. + That is bad: diff --git a/docs/rules/prefer-template-literal.md b/docs/rules/prefer-template-literal.md index 049d408..48dce9d 100644 --- a/docs/rules/prefer-template-literal.md +++ b/docs/rules/prefer-template-literal.md @@ -1,5 +1,7 @@ # Prefer template literal (`jlc/prefer-template-literal`) +💼 This rule is enabled in the ✅ `recommended` config. + That is bad: diff --git a/eslint.config.mjs b/eslint.config.mjs index 4319e54..730a1c9 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -2,8 +2,23 @@ import pluginJs from "@eslint/js"; import pluginNode from "eslint-plugin-n"; import eslintPlugin from "eslint-plugin-eslint-plugin"; +import * as jlc from "./lib/index.js"; + export default [ pluginJs.configs.recommended, ...pluginNode.configs["flat/mixed-esm-and-cjs"], eslintPlugin.configs["flat/recommended"], + + { + files: ["**/*.js"], + languageOptions: { + sourceType: "commonjs", + ecmaVersion: "latest", + }, + // Using the eslint-plugin-example plugin defined locally + plugins: { jlc }, + rules: { + "jlc/kebab-case": "error", + }, + }, ]; diff --git a/lib/configs.js b/lib/configs.js new file mode 100644 index 0000000..60185b5 --- /dev/null +++ b/lib/configs.js @@ -0,0 +1,5 @@ +"use strict"; + +module.exports = { + recommended: require("./configs/recommended"), +}; diff --git a/lib/configs/recommended.js b/lib/configs/recommended.js new file mode 100644 index 0000000..90f786d --- /dev/null +++ b/lib/configs/recommended.js @@ -0,0 +1,10 @@ +module.exports = { + plugins: ["jlc"], + rules: { + "jlc/always-camel-case": "error", + "jlc/comments-need-space": "error", + "jlc/kebab-case": "error", + "jlc/no-then": "error", + "jlc/prefer-template-literal": "error", + }, +}; diff --git a/lib/index.js b/lib/index.js index a934c53..e9f37bf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ /** - * @fileoverview Preferences eslint rule for zeng - * @author eslint-plugin-zeng + * @fileoverview An opinionated collection of ESLint rules used by JLC. + * @author zengxiaoluan */ "use strict"; @@ -14,9 +14,7 @@ const requireIndex = require("requireindex"); // Plugin Definition //------------------------------------------------------------------------------ - // import all rules in lib/rules module.exports.rules = requireIndex(__dirname + "/rules"); - - +module.exports.configs = requireIndex(__dirname + "/configs"); diff --git a/lib/rules/kebab-case.js b/lib/rules/kebab-case.js index 01abf1d..a9f88fc 100644 --- a/lib/rules/kebab-case.js +++ b/lib/rules/kebab-case.js @@ -8,6 +8,8 @@ // Rule Definition //------------------------------------------------------------------------------ +let { url, parseFilename } = require("../utils"); + /** @type {import('eslint').Rule.RuleModule} */ module.exports = { meta: { @@ -15,7 +17,7 @@ module.exports = { docs: { description: "kebab case your file name and directory name.", recommended: false, - url: require("../utils").url(module), // URL to the documentation page for this rule + url: url(module), // URL to the documentation page for this rule }, fixable: null, // Or `code` or `whitespace` schema: [], // Add a schema if the rule has options @@ -43,8 +45,11 @@ module.exports = { return { Program: function (node) { let filename = context.filename; + let physicalFilename = context.physicalFilename; + + let parsed = parseFilename(physicalFilename); - let matchesRegex = conventionRegexp.test(filename); + let matchesRegex = conventionRegexp.test(parsed.base); if (matchesRegex) { context.report({ diff --git a/lib/utils.js b/lib/utils.js index f713f2c..80f1323 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -9,6 +9,18 @@ function url({ id }) { return url.toString(); } +function parseFilename(filename) { + let ext = path.extname(filename); + + return { + dir: path.dirname(filename), + base: path.basename(filename), + ext: ext, + name: path.basename(filename, ext), + }; +} + module.exports = { url, + parseFilename, }; diff --git a/package-lock.json b/package-lock.json index 6ed592b..a0489f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "eslint-plugin-jlc", - "version": "0.0.5", + "version": "0.0.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "eslint-plugin-jlc", - "version": "0.0.5", + "version": "0.0.7", "license": "MIT", "dependencies": { "requireindex": "^1.2.0"