Skip to content

Commit b25c563

Browse files
authored
feat!: Drop Node < 12, switch to eslint flat config (#16)
* refactor: switch to eslint flat config * chore: apply eslint autofix * chore: up node version requirement * chore: fix eslint errors manually * revert: fixtures change * chore: ignore package-lock.json
1 parent e93aa9d commit b25c563

File tree

8 files changed

+122
-110
lines changed

8 files changed

+122
-110
lines changed

.eslintrc.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
npm-debug.log
33
.DS_Store
44
.eslint-release-info.json
5+
package-lock.json

bin/eslint-transforms.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@
22

33
"use strict";
44

5-
var execSync = require("child_process").execSync;
6-
var path = require("path");
5+
const execSync = require("child_process").execSync;
6+
const path = require("path");
77

8-
var argv = process.argv.slice(2);
9-
var args = argv.slice(1);
10-
var transform = argv[0];
8+
const argv = process.argv.slice(2);
9+
const args = argv.slice(1);
10+
const transform = argv[0];
1111

1212
/**
1313
* Add possible node_modules/.bin paths to env and run the command passed in.
14-
*
1514
* @param {string} cmd The command to run
1615
* @returns {void}
1716
*/
1817
function execWithNodeModules(cmd) {
19-
var SEPARATOR = process.platform === "win32" ? ";" : ":",
18+
const SEPARATOR = process.platform === "win32" ? ";" : ":",
2019
env = Object.assign({}, process.env);
2120

2221
env.PATH = [
22+
2323
// Covers case when npm flattens dependencies and the jscodeshift bin will be directly under the root
2424
// node_modules folder
2525
path.resolve("./node_modules/.bin"),
26+
2627
// Covers case when dependencies are not flattened and the jscodeshift bin can be found under the
2728
// node_modules folder of our package
2829
path.resolve(__dirname, "../node_modules/.bin"),
2930
env.PATH
3031
].join(SEPARATOR);
3132

3233
execSync(cmd, {
33-
env: env,
34+
env,
3435
cwd: process.cwd(),
3536
stdio: "inherit"
3637
});
@@ -39,6 +40,6 @@ function execWithNodeModules(cmd) {
3940
execWithNodeModules([
4041
"jscodeshift",
4142
"-t",
42-
path.resolve(__dirname, "../lib/" + transform + "/" + transform + ".js"),
43+
path.resolve(__dirname, `../lib/${transform}/${transform}.js`),
4344
args.join(" ")
4445
].join(" "));

eslint.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
const eslintConfig = require("eslint-config-eslint");
4+
const globals = require("globals");
5+
6+
module.exports = [
7+
{
8+
ignores: ["tests/fixtures/"]
9+
},
10+
...eslintConfig,
11+
{
12+
files: ["tests/**/*"],
13+
languageOptions: {
14+
globals: {
15+
...globals.mocha
16+
}
17+
}
18+
}
19+
];

lib/new-rule-format/new-rule-format.js

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,47 @@
2121

2222
"use strict";
2323

24-
var j = require("jscodeshift");
24+
const j = require("jscodeshift");
2525

2626
//------------------------------------------------------------------------------
2727
// Helpers
2828
//------------------------------------------------------------------------------
2929

3030
/**
3131
* Returns `true` if the rule is already in the new format
32-
*
33-
* @param {Object} rootNode - where to look for the rule definition
34-
* @returns {Boolean} `true` if rule is already in the new format
32+
* @param {Object} rootNode where to look for the rule definition
33+
* @returns {boolean} `true` if rule is already in the new format
3534
*/
3635
function isAlreadyInNewFormat(rootNode) {
36+
3737
// If there's already a module.exports.meta property, we assume the rule
3838
// is already in the new format.
3939
return rootNode
4040
.find(j.Property)
41-
.filter(function(node) {
42-
return (
43-
node.value.key.name === "meta" &&
41+
.filter(node => (
42+
node.value.key.name === "meta" &&
4443
node.parent.value.type === "ObjectExpression" &&
4544
node.parent.parent.value.type === "AssignmentExpression" &&
4645
node.parent.parent.value.left.type === "MemberExpression" &&
4746
node.parent.parent.value.left.object.name === "module" &&
4847
node.parent.parent.value.left.property.name === "exports"
49-
);
50-
})
48+
))
5149
.size() > 0;
5250
}
5351

5452
/**
5553
* Checks if the node passed is a function expression or an arrow function expression
56-
*
57-
* @param {Object} node - node to check
58-
* @returns {Boolean} - `true` if node is a function or arrow function expression
54+
* @param {Object} node node to check
55+
* @returns {boolean} - `true` if node is a function or arrow function expression
5956
*/
6057
function isFunctionOrArrowFunctionExpression(node) {
6158
return node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
6259
}
6360

6461
/**
6562
* Checks if the node passed can be an "old format" rule definition
66-
*
67-
* @param {Object} node - node to check
68-
* @returns {Boolean} - `true` if node looks like an "old format" rule definition
63+
* @param {Object} node node to check
64+
* @returns {boolean} - `true` if node looks like an "old format" rule definition
6965
*/
7066
function isOldFormatRuleDefinition(node) {
7167
return isFunctionOrArrowFunctionExpression(node) || node.type === "CallExpression";
@@ -75,55 +71,49 @@ function isOldFormatRuleDefinition(node) {
7571
* Returns the node in `rootNode` that is the rule definition in the old format,
7672
* which will be in the format:
7773
* module.exports = function(context) { ... };
78-
*
79-
* @param {Object} rootNode - where to look for the rule definition node
74+
* @param {Object} rootNode where to look for the rule definition node
8075
* @returns {Object} node - rule definition expression node
8176
*/
8277
function getOldFormatRuleDefinition(rootNode) {
8378
return rootNode
8479
.find(j.AssignmentExpression)
85-
.filter(function(node) {
86-
return (
87-
node.value.left.type === "MemberExpression" &&
80+
.filter(node => (
81+
node.value.left.type === "MemberExpression" &&
8882
node.value.left.property.name === "exports" &&
8983
node.value.left.object.name === "module" &&
9084
isOldFormatRuleDefinition(node.value.right)
91-
);
92-
});
85+
));
9386
}
9487

9588
/**
9689
* Returns the node in `rootNode` that is the schema definition in the old format,
9790
* which will be in the format:
9891
* module.exports.schema = [ ... ];
99-
*
100-
* @param {Object} rootNode - where to look for the rule definition node
92+
* @param {Object} rootNode where to look for the rule definition node
10193
* @returns {Object} node - rule definition expression node
10294
*/
10395
function getOldFormatSchemaDefinition(rootNode) {
10496
return rootNode
10597
.find(j.AssignmentExpression)
106-
.filter(function(node) {
107-
return (
108-
node.value.left.type === "MemberExpression" &&
98+
.filter(node => (
99+
node.value.left.type === "MemberExpression" &&
109100
node.value.left.property.name === "schema" &&
110101
node.value.left.object.type === "MemberExpression" &&
111102
node.value.left.object.object.name === "module" &&
112103
node.value.left.object.property.name === "exports"
113-
);
114-
});
104+
));
115105
}
116106

117107
/**
118-
* Creates the object expression node that will be the `meta` property of this rule
119-
*
120-
* @param {Object} schemaNode - node that was the schema definition in the old rule format
121-
* @param {Object} schemaNodeComments - comments that were above the old schema node
122-
* @param {Boolean} isRuleFixable - `true` if the rule is fixable
123-
* @returns {Object} ObjectExpression node
124-
*/
108+
* Creates the object expression node that will be the `meta` property of this rule
109+
* @param {Object} schemaNode node that was the schema definition in the old rule format
110+
* @param {Object} schemaNodeComments comments that were above the old schema node
111+
* @param {boolean} isRuleFixable `true` if the rule is fixable
112+
* @returns {Object} ObjectExpression node
113+
*/
125114
function createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixable) {
126-
var properties = [
115+
const properties = [
116+
127117
// For docs, create just an empty object
128118
j.property("init", j.identifier("docs"), j.objectExpression([]))
129119
];
@@ -136,7 +126,9 @@ function createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixabl
136126

137127
// The schema definition may not exist in some plugins
138128
if (schemaNode) {
139-
var schemaNodeProperty = j.property("init", j.identifier("schema"), schemaNode);
129+
const schemaNodeProperty = j.property("init", j.identifier("schema"), schemaNode);
130+
131+
140132
// Restore comments that were removed when the old format node was removed
141133
schemaNodeProperty.comments = schemaNodeComments;
142134
properties.push(schemaNodeProperty);
@@ -147,21 +139,20 @@ function createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixabl
147139

148140
/**
149141
* Creates the `exports` expression that wil contain the rule definition in the new format
150-
*
151-
* @param {Object} ruleDefinitionNode - node that was the rule definition in the old rule format
152-
* @param {Object} ruleDefinitionNodeComments - comments that were above the old schema rule definition
153-
* @param {Object} ruleMetaDefinitionNode - node that will be the meta definition expression
142+
* @param {Object} ruleDefinitionNode node that was the rule definition in the old rule format
143+
* @param {Object} ruleDefinitionNodeComments comments that were above the old schema rule definition
144+
* @param {Object} ruleMetaDefinitionNode node that will be the meta definition expression
154145
* @returns {Object} ExpressionStatement
155146
*/
156147
function createExportsExpression(ruleDefinitionNode, ruleDefinitionNodeComments, ruleMetaDefinitionNode) {
157-
var exportsExpression = j.expressionStatement(
148+
const exportsExpression = j.expressionStatement(
158149
j.assignmentExpression(
159-
"=",
160-
j.memberExpression(j.identifier("module"), j.identifier("exports"), false),
161-
j.objectExpression([
162-
j.property("init", j.identifier("meta"), ruleMetaDefinitionNode),
163-
j.property("init", j.identifier("create"), ruleDefinitionNode)
164-
])
150+
"=",
151+
j.memberExpression(j.identifier("module"), j.identifier("exports"), false),
152+
j.objectExpression([
153+
j.property("init", j.identifier("meta"), ruleMetaDefinitionNode),
154+
j.property("init", j.identifier("create"), ruleDefinitionNode)
155+
])
165156
)
166157
);
167158

@@ -176,22 +167,25 @@ function createExportsExpression(ruleDefinitionNode, ruleDefinitionNodeComments,
176167
//------------------------------------------------------------------------------
177168

178169
/**
179-
* @param {Object} fileInfo - holds information about the currently processed file.
180-
* @returns {String} the new source code, after being transformed.
170+
* Transforms an ESLint rule from the old format to the new format.
171+
* @param {Object} fileInfo holds information about the currently processed file.
172+
* @returns {string} the new source code, after being transformed.
181173
*/
182174
module.exports = function(fileInfo) {
183-
var root = j(fileInfo.source);
175+
const root = j(fileInfo.source);
184176

185177
if (isAlreadyInNewFormat(root)) {
178+
186179
// don't do anything and return
187180
return root.toSource();
188181
}
189182

190183
// for most plugins, the old format should be:
191184
// module.exports = function(context) { ... }
192185
// but maybe some plugins are using a diferent variable name instead of `context`
193-
var ruleDefinitionExpression = getOldFormatRuleDefinition(root).get().value.right;
194-
var identifierNameForContextObject = "context";
186+
const ruleDefinitionExpression = getOldFormatRuleDefinition(root).get().value.right;
187+
let identifierNameForContextObject = "context";
188+
195189
if (ruleDefinitionExpression.params && ruleDefinitionExpression.params.length > 0) {
196190
identifierNameForContextObject = ruleDefinitionExpression.params[0].name;
197191
}
@@ -202,37 +196,40 @@ module.exports = function(fileInfo) {
202196
// ...
203197
// fix: function() { ... }
204198
// });
205-
var isRuleFixable = root
199+
const isRuleFixable = root
206200
.find(j.Identifier)
207-
.filter(function(node) {
208-
return (
209-
node.value.name === "fix" &&
201+
.filter(node => (
202+
node.value.name === "fix" &&
210203
node.parent.value.type === "Property" &&
211204
node.parent.parent.parent.value.type === "CallExpression" &&
212205
node.parent.parent.parent.value.callee.type === "MemberExpression" &&
213206
node.parent.parent.parent.value.callee.object.name === identifierNameForContextObject &&
214207
node.parent.parent.parent.value.callee.property.name === "report"
215-
);
216-
})
208+
))
217209
.size() > 0;
218210

219-
var oldFormatSchemaDefinition = getOldFormatSchemaDefinition(root);
220-
var schemaNode, schemaNodeComments;
211+
const oldFormatSchemaDefinition = getOldFormatSchemaDefinition(root);
212+
let schemaNode, schemaNodeComments;
213+
214+
221215
// The schema definition may not exist in some plugins
222216
if (oldFormatSchemaDefinition.size() > 0) {
223217
schemaNode = getOldFormatSchemaDefinition(root).get().value.right;
218+
224219
// Store the comments too so we can attach it again later
225220
schemaNodeComments = getOldFormatSchemaDefinition(root).get().parent.value.leadingComments;
226221
getOldFormatSchemaDefinition(root).remove();
227222
}
228223

229-
var ruleDefinitionNode = getOldFormatRuleDefinition(root).get().value.right;
224+
const ruleDefinitionNode = getOldFormatRuleDefinition(root).get().value.right;
225+
230226
// Store the comments too so we can attach it again later
231-
var ruleDefinitionNodeComments = getOldFormatRuleDefinition(root).get().parent.value.leadingComments;
227+
const ruleDefinitionNodeComments = getOldFormatRuleDefinition(root).get().parent.value.leadingComments;
228+
232229
getOldFormatRuleDefinition(root).remove();
233230

234231
// Insert the rule definition in the new format at the end of the file
235-
var newFormat = createExportsExpression(
232+
const newFormat = createExportsExpression(
236233
ruleDefinitionNode,
237234
ruleDefinitionNodeComments,
238235
createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixable)

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"author": "Vitor Balocco",
77
"repository": "eslint/eslint-transforms",
88
"engines": {
9-
"node": ">=4"
9+
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
1010
},
1111
"bin": {
1212
"eslint-transforms": "./bin/eslint-transforms.js"
@@ -27,9 +27,10 @@
2727
},
2828
"devDependencies": {
2929
"chai": "^3.5.0",
30-
"eslint": "^2.9.0",
31-
"eslint-config-eslint": "^3.0.0",
30+
"eslint": "^8.45.0",
31+
"eslint-config-eslint": "^8.0.0",
3232
"eslint-release": "^1.0.0",
33+
"globals": "^13.20.0",
3334
"mocha": "^2.5.3"
3435
},
3536
"dependencies": {

tests/.eslintrc.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)