diff --git a/README.md b/README.md index c5e6b86..219af53 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,39 @@ The available configuration presets to choose from: It's important to note that you can still override any of the preset rule definitions in your configuration. Think of these presets as convenience "defaults" that can still be customized. +### Flat Config + +If you're using ESLint's newer flat config format, you can use the plugin's flat configuration preset: + +```js +// eslint.config.js +import properArrows from "@getify/eslint-plugin-proper-arrows"; + +export default [ + properArrows.flatConfigs["CONFIG-PRESET-NAME"], // i.e., "getify-says", etc + // ... other configs +]; +``` + +This will automatically load the plugin and apply the recommended preset rules configuration. +The preset includes the same rules configuration as the traditional `"extends": ["plugin:@getify/proper-arrows/CONFIG-PRESET-NAME"]` format. + +You can still override any of the preset rules in your configuration after including the flat config: + +```js +import properArrows from "@getify/eslint-plugin-proper-arrows"; + +export default [ + properArrows.flatConfigs["getify-says"], + { + rules: { + "@getify/proper-arrows/params": ["error", {"unused": "none"}], + // ... other rule overrides + } + } +]; +``` + ### `.eslintrc.json` To load the plugin and enable its rules via a local or global `.eslintrc.json` configuration file: @@ -97,6 +130,25 @@ To load the plugin and enable its rules via a local or global `.eslintrc.json` c } ``` +For users of ESLint's newer flat config format, the configuration would look like this: + +```js +import properArrows from "@getify/eslint-plugin-proper-arrows"; + +{ + plugins: { + "@getify/proper-arrows": properArrows + }, + rules: { + "@getify/proper-arrows/params": ["error",{"unused":"trailing"}], + "@getify/proper-arrows/name": ["error",{"trivial":false}], + "@getify/proper-arrows/where": ["error",{"global":true}], + "@getify/proper-arrows/return": ["error",{"object":true}], + "@getify/proper-arrows/this": ["error","always",{"no-global":true}] + } +} +``` + ### `package.json` To load the plugin and enable its rules via a project's `package.json`: diff --git a/lib/index.js b/lib/index.js index 678a5b5..a2c89c7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,17 +1,21 @@ "use strict"; -module.exports = { +var presetRulesConfig = { + "getify-says": { + "@getify/proper-arrows/params": [ "error", { "unused": "trailing", "count": 2, "length": 3, "allowed": [ "e", "v", "cb", "fn", "pr", ], }, ], + "@getify/proper-arrows/name": "error", + "@getify/proper-arrows/return": [ "error", { "ternary": 1, }, ], + "@getify/proper-arrows/where": "error", + "@getify/proper-arrows/this": [ "error", "nested", { "no-global": true, }, ], + } +}; + +var plugin = { configs: { "getify-says": { plugins: [ "@getify/proper-arrows", ], - rules: { - "@getify/proper-arrows/params": [ "error", { "unused": "trailing", "count": 2, "length": 3, "allowed": [ "e", "v", "cb", "fn", "pr", ], }, ], - "@getify/proper-arrows/name": "error", - "@getify/proper-arrows/return": [ "error", { "ternary": 1, }, ], - "@getify/proper-arrows/where": "error", - "@getify/proper-arrows/this": [ "error", "nested", { "no-global": true, }, ], - }, - }, + rules: presetRulesConfig["getify-says"], + } }, rules: { "params": { @@ -110,7 +114,8 @@ module.exports = { // handle "unused" mode if (!noneUnusedMode) { - let scope = context.getScope(); + let sourceCode = getSourceCode(context); + let scope = getScope(context,sourceCode,node); let checkParamNames = checkParamIds.map(function getName(paramId){ return paramId.name; }); @@ -301,7 +306,9 @@ module.exports = { return; } - var globalArrow = currentlyInGlobalScope(context.parserOptions,context.getScope()); + var sourceCode = getSourceCode(context); + var scope = getScope(context,sourceCode,node); + var globalArrow = currentlyInGlobalScope(context.parserOptions,scope); var globalArrowDeclaration = ( globalArrow && node.parent.type == "VariableDeclarator" @@ -414,12 +421,13 @@ module.exports = { var sequenceMode = defaultsOnly || !("sequence" in extraOptions) || extraOptions.sequence === true; var ignoreTrivial = !(extraOptions && extraOptions.trivial === true); - var sourceCode = context.getSourceCode(); + var sourceCode = getSourceCode(context); var ternaryBodyStack = new Map(); return { "ConditionalExpression:exit": function exit(node) { - var parentArrow = getParentArrowFunction(context.getAncestors(),/*onlyFromBody=*/true); + var ancestors = getAncestors(context,sourceCode,node); + var parentArrow = getParentArrowFunction(ancestors,/*onlyFromBody=*/true); if (parentArrow) { if (!ternaryBodyStack.has(parentArrow)) { ternaryBodyStack.set(parentArrow,[]); @@ -562,7 +570,9 @@ module.exports = { return { "ThisExpression": function enter(node) { - var parentArrow = getParentArrowFunction(context.getAncestors()); + var sourceCode = getSourceCode(context); + var ancestors = getAncestors(context, sourceCode,node); + var parentArrow = getParentArrowFunction(ancestors); thisFoundIn.add(parentArrow); }, "ArrowFunctionExpression:exit": function exit(node) { @@ -571,7 +581,9 @@ module.exports = { return; } - var globalArrow = currentlyInGlobalScope(context.parserOptions,context.getScope()); + var sourceCode = getSourceCode(context); + var scope = getScope(context, sourceCode, node); + var globalArrow = currentlyInGlobalScope(context.parserOptions,scope); var foundThis = thisFoundIn.has(node); // `this` found in arrow function? @@ -605,7 +617,8 @@ module.exports = { // need to track nested `this`? if (nestedThis || neverGlobalThis) { - let parentArrow = getParentArrowFunction(context.getAncestors()); + let ancestors = getAncestors(context, sourceCode,node); + let parentArrow = getParentArrowFunction(ancestors); if (parentArrow) { thisFoundIn.add(parentArrow); } @@ -627,6 +640,16 @@ module.exports = { }, }; +plugin.flatConfigs = { + "getify-says": { + name: "getify-says", + plugins: { + "@getify/proper-arrows": plugin + }, + rules: presetRulesConfig["getify-says"], + } +} + // *************************** @@ -801,3 +824,32 @@ function currentlyInGlobalScope(parserOptions,scope) { ) ); } + +var getSourceCode = context => { + getSourceCode = ( + context.sourceCode ? + context => context.sourceCode : + context => context.getSourceCode() + ); + return getSourceCode(context); +}; + +var getScope = (context, sourceCode, node) => { + getScope = ( + sourceCode.getScope ? + (context, sourceCode, node) => sourceCode.getScope(node) : + (context, sourceCode, node) => context.getScope() + ); + return getScope(context, sourceCode, node); +}; + +var getAncestors = (context, sourceCode, node) => { + getAncestors = ( + sourceCode.getAncestors ? + (context, sourceCode, node) => sourceCode.getAncestors(node) : + (context, sourceCode, node) => context.getAncestors() + ); + return getAncestors(context, sourceCode, node); +}; + +module.exports = plugin; diff --git a/package.json b/package.json index 2caf7d7..22a0f8a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getify/eslint-plugin-proper-arrows", - "version": "11.0.3", + "version": "12.0.0", "description": "ESLint rules to ensure proper arrow function definitions", "main": "./lib/index.js", "scripts": { @@ -18,12 +18,12 @@ }, "devDependencies": { "coveralls": "~3.1.0", - "eslint": "~7.25.0", + "eslint": "~9.17.0", "qunit": "~2.15.0", "terser": "~5.7.0" }, "peerDependencies": { - "eslint": ">= 7.25.0" + "eslint": ">= 9.17.0" }, "repository": "getify/eslint-plugin-proper-arrows", "keywords": [ diff --git a/scripts/node-tests.js b/scripts/node-tests.js index aa76bed..ec7d0e9 100755 --- a/scripts/node-tests.js +++ b/scripts/node-tests.js @@ -5,7 +5,7 @@ var path = require("path"); var Linter = require("eslint").Linter; -var eslinter = global.eslinter = new Linter(); +var eslinter = global.eslinter = new Linter({ configType: "eslintrc"}); var properArrows; /* istanbul ignore next */