Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support eslint 9 #38

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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`:
Expand Down
84 changes: 68 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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", ],
Smrtnyk marked this conversation as resolved.
Show resolved Hide resolved
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": {
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,[]);
Expand Down Expand Up @@ -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) {
Expand All @@ -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?
Expand Down Expand Up @@ -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);
}
Expand All @@ -627,6 +640,16 @@ module.exports = {
},
};

plugin.flatConfigs = {
"getify-says": {
name: "getify-says",
plugins: {
"@getify/proper-arrows": plugin
},
rules: presetRulesConfig["getify-says"],
}
}


// ***************************

Expand Down Expand Up @@ -801,3 +824,32 @@ function currentlyInGlobalScope(parserOptions,scope) {
)
);
}

var getSourceCode = context => {
getSourceCode = (
context.sourceCode ?
context => context.sourceCode :
context => context.getSourceCode()
);
return getSourceCode(context);
};
Smrtnyk marked this conversation as resolved.
Show resolved Hide resolved

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;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion scripts/node-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading