-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8437f12
commit 97acbc6
Showing
6 changed files
with
269 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,68 @@ | ||
/** | ||
* @fileoverview expored all rules in the plugin. | ||
* @author 唯然<weiran.zsd@outlook.com> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
const eslintVersion = require("eslint/package.json").version; | ||
|
||
|
||
/** | ||
* Loads a given rule from the filesystem and generates its documentation URL | ||
* @param {string} ruleName The name of the rule | ||
* @returns {Rule} The ESLint rule to export | ||
*/ | ||
function loadRule(ruleName) { | ||
const rule = require(path.join(__dirname, "rules", ruleName)); | ||
|
||
rule.meta.docs.url = | ||
`https://eslint.org/docs/rules/${ruleName}`; | ||
|
||
return rule; | ||
} | ||
|
||
const allRules = {}; | ||
|
||
// eslint v6 restructed its codebase | ||
// TODO: this might be unreliable | ||
if (eslintVersion >= "6.0.0") { | ||
const builtin = require("eslint/lib/rules"); | ||
|
||
for (const [ruleId, rule] of builtin) { | ||
if (rule.meta.fixable) { | ||
allRules[ruleId] = rule; | ||
} | ||
} | ||
} else { | ||
const builtin = require("eslint/lib/built-in-rules-index"); // eslint-disable-line node/no-missing-require | ||
|
||
Object.keys(builtin) | ||
.filter(rule => builtin[rule].meta.fixable) | ||
.reduce((acc, cur) => { | ||
acc[cur] = builtin[cur]; | ||
return acc; | ||
}, allRules); | ||
} | ||
|
||
// import all rules in lib/rules | ||
fs.readdirSync(`${__dirname}/rules`) | ||
.filter(fileName => fileName.endsWith(".js") && /^[^._]/u.test(fileName)) | ||
.map(fileName => fileName.replace(/\.js$/u, "")) | ||
.reduce((rules, ruleName) => Object.assign(rules, { [ruleName]: loadRule(ruleName) }), allRules); | ||
|
||
module.exports = allRules; | ||
/** | ||
* @fileoverview expored all rules in the plugin. | ||
* @author 唯然<weiran.zsd@outlook.com> | ||
*/ | ||
|
||
"use strict"; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
const eslintVersion = Number.parseInt(require("eslint/package.json").version, 10); | ||
|
||
|
||
/** | ||
* Loads a given rule from the filesystem and generates its documentation URL | ||
* @param {string} ruleName The name of the rule | ||
* @returns {Rule} The ESLint rule to export | ||
*/ | ||
function loadRule(ruleName) { | ||
const rule = require(path.join(__dirname, "rules", ruleName)); | ||
|
||
rule.meta.docs.url = | ||
`https://eslint.org/docs/rules/${ruleName}`; | ||
|
||
return rule; | ||
} | ||
|
||
const allRules = {}; | ||
|
||
// eslint v6 restructed its codebase | ||
// TODO: this might be unreliable | ||
if (eslintVersion >= 8) { | ||
const { builtinRules } = require("eslint/use-at-your-own-risk"); // eslint-disable-line node/no-missing-require | ||
|
||
for (const [ruleId, rule] of builtinRules) { | ||
if (rule.meta.fixable) { | ||
allRules[ruleId] = rule; | ||
} | ||
} | ||
} else if (eslintVersion >= 6) { | ||
const builtin = require("eslint/lib/rules"); | ||
|
||
for (const [ruleId, rule] of builtin) { | ||
if (rule.meta.fixable) { | ||
allRules[ruleId] = rule; | ||
} | ||
} | ||
} else { | ||
const builtin = require("eslint/lib/built-in-rules-index"); // eslint-disable-line node/no-missing-require | ||
|
||
Object.keys(builtin) | ||
.filter(rule => builtin[rule].meta.fixable) | ||
.reduce((acc, cur) => { | ||
acc[cur] = builtin[cur]; | ||
return acc; | ||
}, allRules); | ||
} | ||
|
||
// import all rules in lib/rules | ||
fs.readdirSync(path.join(__dirname, "rules")) | ||
.filter(fileName => fileName.endsWith(".js") && /^[^._]/u.test(fileName)) | ||
.map(fileName => fileName.replace(/\.js$/u, "")) | ||
.reduce((rules, ruleName) => Object.assign(rules, { [ruleName]: loadRule(ruleName) }), allRules); | ||
|
||
module.exports = allRules; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,83 @@ | ||
/** | ||
* @fileoverview Rule definition of "rule-def-format" | ||
* @author Pig Fang <g-plane@hotmail.com> | ||
*/ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
|
||
module.exports = { | ||
meta: { | ||
fixable: "code" | ||
}, | ||
create: context => ({ | ||
CallExpression: node => { | ||
if (node.callee.type !== "MemberExpression") { | ||
return; | ||
} | ||
|
||
const { callee: { object: { name: objectName }, property: { name: propertyName } }, arguments: args } = node; | ||
const sourceCode = context.getSourceCode(); | ||
|
||
if (objectName === "utils" && propertyName === "getFixableRule") { | ||
const arg0 = args[0]; | ||
const ruleName = path.basename(context.getFilename(), ".js"); | ||
|
||
if (arg0.value !== ruleName) { | ||
context.report({ | ||
node: arg0, | ||
message: `Rule name should be '${ruleName}'`, | ||
fix: fixer => fixer.replaceText(arg0, `"${ruleName}"`) | ||
}); | ||
} | ||
return; | ||
} | ||
|
||
if (objectName === "ruleComposer") { | ||
if (propertyName === "mapReports") { | ||
const [, predicate] = args; | ||
|
||
if (predicate.type === "ArrowFunctionExpression" || predicate.type === "FunctionExpression") { | ||
if (predicate.body.type !== "BlockStatement") { | ||
context.report({ | ||
node: predicate.body, | ||
message: "Body of predicate should be a block statement." | ||
}); | ||
return; | ||
} | ||
const param0 = predicate.params[0].name; | ||
|
||
const statements = predicate.body.body; | ||
|
||
const returnStatement = statements.find(stmt => stmt.type === "ReturnStatement"); | ||
|
||
if (!returnStatement) { | ||
context.report({ | ||
node: predicate, | ||
message: "Should returning the problem object.", | ||
fix: fixer => { | ||
const lastToken = sourceCode.getLastToken(predicate); | ||
|
||
return fixer.insertTextBefore(lastToken, `\nreturn ${param0};\n`); | ||
} | ||
}); | ||
} else if (returnStatement.argument.name !== param0) { | ||
context.report({ | ||
node: predicate, | ||
message: "Should returning the problem object.", | ||
fix: fixer => fixer.replaceText(returnStatement, `return ${param0};`) | ||
}); | ||
} | ||
} | ||
} else if (propertyName === "joinReports" && args[0].type !== "ArrayExpression") { | ||
context.report({ | ||
node: args[0], | ||
message: "The first argument of 'joinReports' should be an array." | ||
}); | ||
} | ||
} | ||
} | ||
}) | ||
}; | ||
/** | ||
* @fileoverview Rule definition of "rule-def-format" | ||
* @author Pig Fang <g-plane@hotmail.com> | ||
*/ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
|
||
module.exports = { | ||
meta: { | ||
fixable: "code", | ||
type: "problem", | ||
schema: [] | ||
}, | ||
create: context => ({ | ||
CallExpression: node => { | ||
if (node.callee.type !== "MemberExpression") { | ||
return; | ||
} | ||
|
||
const { callee: { object: { name: objectName }, property: { name: propertyName } }, arguments: args } = node; | ||
const sourceCode = context.getSourceCode(); | ||
|
||
if (objectName === "utils" && propertyName === "getFixableRule") { | ||
const arg0 = args[0]; | ||
const ruleName = path.basename(context.getFilename(), ".js"); | ||
|
||
if (arg0.value !== ruleName) { | ||
context.report({ | ||
node: arg0, | ||
message: `Rule name should be '${ruleName}'`, | ||
fix: fixer => fixer.replaceText(arg0, `"${ruleName}"`) | ||
}); | ||
} | ||
return; | ||
} | ||
|
||
if (objectName === "ruleComposer") { | ||
if (propertyName === "mapReports") { | ||
const [, predicate] = args; | ||
|
||
if (predicate.type === "ArrowFunctionExpression" || predicate.type === "FunctionExpression") { | ||
if (predicate.body.type !== "BlockStatement") { | ||
context.report({ | ||
node: predicate.body, | ||
message: "Body of predicate should be a block statement." | ||
}); | ||
return; | ||
} | ||
const param0 = predicate.params[0].name; | ||
|
||
const statements = predicate.body.body; | ||
|
||
const returnStatement = statements.find(stmt => stmt.type === "ReturnStatement"); | ||
|
||
if (!returnStatement) { | ||
context.report({ | ||
node: predicate, | ||
message: "Should returning the problem object.", | ||
fix: fixer => { | ||
const lastToken = sourceCode.getLastToken(predicate); | ||
|
||
return fixer.insertTextBefore(lastToken, `\nreturn ${param0};\n`); | ||
} | ||
}); | ||
} else if (returnStatement.argument.name !== param0) { | ||
context.report({ | ||
node: predicate, | ||
message: "Should returning the problem object.", | ||
fix: fixer => fixer.replaceText(returnStatement, `return ${param0};`) | ||
}); | ||
} | ||
} | ||
} else if (propertyName === "joinReports" && args[0].type !== "ArrayExpression") { | ||
context.report({ | ||
node: args[0], | ||
message: "The first argument of 'joinReports' should be an array." | ||
}); | ||
} | ||
} | ||
} | ||
}) | ||
}; |
Oops, something went wrong.