Skip to content

Commit 42b9e02

Browse files
authored
feat: support eslint 9 (#38)
* feat: support eslint 9 Also increased major version * feat: support eslint 9 Add flat config preset
1 parent ccd5bfe commit 42b9e02

File tree

4 files changed

+124
-20
lines changed

4 files changed

+124
-20
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,39 @@ The available configuration presets to choose from:
8080

8181
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.
8282

83+
### Flat Config
84+
85+
If you're using ESLint's newer flat config format, you can use the plugin's flat configuration preset:
86+
87+
```js
88+
// eslint.config.js
89+
import properArrows from "@getify/eslint-plugin-proper-arrows";
90+
91+
export default [
92+
properArrows.flatConfigs["CONFIG-PRESET-NAME"], // i.e., "getify-says", etc
93+
// ... other configs
94+
];
95+
```
96+
97+
This will automatically load the plugin and apply the recommended preset rules configuration.
98+
The preset includes the same rules configuration as the traditional `"extends": ["plugin:@getify/proper-arrows/CONFIG-PRESET-NAME"]` format.
99+
100+
You can still override any of the preset rules in your configuration after including the flat config:
101+
102+
```js
103+
import properArrows from "@getify/eslint-plugin-proper-arrows";
104+
105+
export default [
106+
properArrows.flatConfigs["getify-says"],
107+
{
108+
rules: {
109+
"@getify/proper-arrows/params": ["error", {"unused": "none"}],
110+
// ... other rule overrides
111+
}
112+
}
113+
];
114+
```
115+
83116
### `.eslintrc.json`
84117

85118
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
97130
}
98131
```
99132

133+
For users of ESLint's newer flat config format, the configuration would look like this:
134+
135+
```js
136+
import properArrows from "@getify/eslint-plugin-proper-arrows";
137+
138+
{
139+
plugins: {
140+
"@getify/proper-arrows": properArrows
141+
},
142+
rules: {
143+
"@getify/proper-arrows/params": ["error",{"unused":"trailing"}],
144+
"@getify/proper-arrows/name": ["error",{"trivial":false}],
145+
"@getify/proper-arrows/where": ["error",{"global":true}],
146+
"@getify/proper-arrows/return": ["error",{"object":true}],
147+
"@getify/proper-arrows/this": ["error","always",{"no-global":true}]
148+
}
149+
}
150+
```
151+
100152
### `package.json`
101153

102154
To load the plugin and enable its rules via a project's `package.json`:

lib/index.js

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
"use strict";
22

3-
module.exports = {
3+
var presetRulesConfig = {
4+
"getify-says": {
5+
"@getify/proper-arrows/params": [ "error", { "unused": "trailing", "count": 2, "length": 3, "allowed": [ "e", "v", "cb", "fn", "pr", ], }, ],
6+
"@getify/proper-arrows/name": "error",
7+
"@getify/proper-arrows/return": [ "error", { "ternary": 1, }, ],
8+
"@getify/proper-arrows/where": "error",
9+
"@getify/proper-arrows/this": [ "error", "nested", { "no-global": true, }, ],
10+
}
11+
};
12+
13+
var plugin = {
414
configs: {
515
"getify-says": {
616
plugins: [ "@getify/proper-arrows", ],
7-
rules: {
8-
"@getify/proper-arrows/params": [ "error", { "unused": "trailing", "count": 2, "length": 3, "allowed": [ "e", "v", "cb", "fn", "pr", ], }, ],
9-
"@getify/proper-arrows/name": "error",
10-
"@getify/proper-arrows/return": [ "error", { "ternary": 1, }, ],
11-
"@getify/proper-arrows/where": "error",
12-
"@getify/proper-arrows/this": [ "error", "nested", { "no-global": true, }, ],
13-
},
14-
},
17+
rules: presetRulesConfig["getify-says"],
18+
}
1519
},
1620
rules: {
1721
"params": {
@@ -110,7 +114,8 @@ module.exports = {
110114

111115
// handle "unused" mode
112116
if (!noneUnusedMode) {
113-
let scope = context.getScope();
117+
let sourceCode = getSourceCode(context);
118+
let scope = getScope(context,sourceCode,node);
114119
let checkParamNames = checkParamIds.map(function getName(paramId){
115120
return paramId.name;
116121
});
@@ -301,7 +306,9 @@ module.exports = {
301306
return;
302307
}
303308

304-
var globalArrow = currentlyInGlobalScope(context.parserOptions,context.getScope());
309+
var sourceCode = getSourceCode(context);
310+
var scope = getScope(context,sourceCode,node);
311+
var globalArrow = currentlyInGlobalScope(context.parserOptions,scope);
305312
var globalArrowDeclaration = (
306313
globalArrow &&
307314
node.parent.type == "VariableDeclarator"
@@ -414,12 +421,13 @@ module.exports = {
414421
var sequenceMode = defaultsOnly || !("sequence" in extraOptions) || extraOptions.sequence === true;
415422
var ignoreTrivial = !(extraOptions && extraOptions.trivial === true);
416423

417-
var sourceCode = context.getSourceCode();
424+
var sourceCode = getSourceCode(context);
418425
var ternaryBodyStack = new Map();
419426

420427
return {
421428
"ConditionalExpression:exit": function exit(node) {
422-
var parentArrow = getParentArrowFunction(context.getAncestors(),/*onlyFromBody=*/true);
429+
var ancestors = getAncestors(context,sourceCode,node);
430+
var parentArrow = getParentArrowFunction(ancestors,/*onlyFromBody=*/true);
423431
if (parentArrow) {
424432
if (!ternaryBodyStack.has(parentArrow)) {
425433
ternaryBodyStack.set(parentArrow,[]);
@@ -562,7 +570,9 @@ module.exports = {
562570

563571
return {
564572
"ThisExpression": function enter(node) {
565-
var parentArrow = getParentArrowFunction(context.getAncestors());
573+
var sourceCode = getSourceCode(context);
574+
var ancestors = getAncestors(context, sourceCode,node);
575+
var parentArrow = getParentArrowFunction(ancestors);
566576
thisFoundIn.add(parentArrow);
567577
},
568578
"ArrowFunctionExpression:exit": function exit(node) {
@@ -571,7 +581,9 @@ module.exports = {
571581
return;
572582
}
573583

574-
var globalArrow = currentlyInGlobalScope(context.parserOptions,context.getScope());
584+
var sourceCode = getSourceCode(context);
585+
var scope = getScope(context, sourceCode, node);
586+
var globalArrow = currentlyInGlobalScope(context.parserOptions,scope);
575587
var foundThis = thisFoundIn.has(node);
576588

577589
// `this` found in arrow function?
@@ -605,7 +617,8 @@ module.exports = {
605617

606618
// need to track nested `this`?
607619
if (nestedThis || neverGlobalThis) {
608-
let parentArrow = getParentArrowFunction(context.getAncestors());
620+
let ancestors = getAncestors(context, sourceCode,node);
621+
let parentArrow = getParentArrowFunction(ancestors);
609622
if (parentArrow) {
610623
thisFoundIn.add(parentArrow);
611624
}
@@ -627,6 +640,16 @@ module.exports = {
627640
},
628641
};
629642

643+
plugin.flatConfigs = {
644+
"getify-says": {
645+
name: "getify-says",
646+
plugins: {
647+
"@getify/proper-arrows": plugin
648+
},
649+
rules: presetRulesConfig["getify-says"],
650+
}
651+
}
652+
630653

631654
// ***************************
632655

@@ -801,3 +824,32 @@ function currentlyInGlobalScope(parserOptions,scope) {
801824
)
802825
);
803826
}
827+
828+
var getSourceCode = context => {
829+
getSourceCode = (
830+
context.sourceCode ?
831+
context => context.sourceCode :
832+
context => context.getSourceCode()
833+
);
834+
return getSourceCode(context);
835+
};
836+
837+
var getScope = (context, sourceCode, node) => {
838+
getScope = (
839+
sourceCode.getScope ?
840+
(context, sourceCode, node) => sourceCode.getScope(node) :
841+
(context, sourceCode, node) => context.getScope()
842+
);
843+
return getScope(context, sourceCode, node);
844+
};
845+
846+
var getAncestors = (context, sourceCode, node) => {
847+
getAncestors = (
848+
sourceCode.getAncestors ?
849+
(context, sourceCode, node) => sourceCode.getAncestors(node) :
850+
(context, sourceCode, node) => context.getAncestors()
851+
);
852+
return getAncestors(context, sourceCode, node);
853+
};
854+
855+
module.exports = plugin;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getify/eslint-plugin-proper-arrows",
3-
"version": "11.0.3",
3+
"version": "12.0.0",
44
"description": "ESLint rules to ensure proper arrow function definitions",
55
"main": "./lib/index.js",
66
"scripts": {
@@ -18,12 +18,12 @@
1818
},
1919
"devDependencies": {
2020
"coveralls": "~3.1.0",
21-
"eslint": "~7.25.0",
21+
"eslint": "~9.17.0",
2222
"qunit": "~2.15.0",
2323
"terser": "~5.7.0"
2424
},
2525
"peerDependencies": {
26-
"eslint": ">= 7.25.0"
26+
"eslint": ">= 9.17.0"
2727
},
2828
"repository": "getify/eslint-plugin-proper-arrows",
2929
"keywords": [

scripts/node-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var path = require("path");
66

77
var Linter = require("eslint").Linter;
8-
var eslinter = global.eslinter = new Linter();
8+
var eslinter = global.eslinter = new Linter({ configType: "eslintrc"});
99
var properArrows;
1010

1111
/* istanbul ignore next */

0 commit comments

Comments
 (0)