Skip to content

Commit 98e893b

Browse files
authored
Chore: refactor utils.getRuleInfo (#112)
1 parent 57a711d commit 98e893b

10 files changed

+16
-16
lines changed

lib/rules/meta-property-ordering.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727

2828
create (context) {
2929
const sourceCode = context.getSourceCode();
30-
const info = getRuleInfo(sourceCode.ast);
30+
const info = getRuleInfo(sourceCode);
3131

3232
const message = 'The meta properties should be placed in a consistent order: [{{order}}].';
3333
const order = context.options[0] || ['type', 'docs', 'fixable', 'schema', 'messages'];

lib/rules/prefer-object-rule.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
// ----------------------------------------------------------------------
3232

3333
const sourceCode = context.getSourceCode();
34-
const ruleInfo = utils.getRuleInfo(sourceCode.ast);
34+
const ruleInfo = utils.getRuleInfo(sourceCode);
3535

3636
return {
3737
Program () {

lib/rules/report-message-format.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = {
5454
return {
5555
Program (node) {
5656
contextIdentifiers = utils.getContextIdentifiers(context, node);
57-
const ruleInfo = utils.getRuleInfo(context.getSourceCode().ast);
57+
const ruleInfo = utils.getRuleInfo(context.getSourceCode());
5858
const messagesObject = ruleInfo &&
5959
ruleInfo.meta &&
6060
ruleInfo.meta.type === 'ObjectExpression' &&

lib/rules/require-meta-docs-description.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = {
4747

4848
create (context) {
4949
const sourceCode = context.getSourceCode();
50-
const info = utils.getRuleInfo(sourceCode.ast, sourceCode.scopeManager);
50+
const info = utils.getRuleInfo(sourceCode);
5151

5252
return {
5353
Program () {

lib/rules/require-meta-docs-url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = {
6666

6767
return {
6868
Program (node) {
69-
const info = util.getRuleInfo(node);
69+
const info = util.getRuleInfo(sourceCode);
7070
if (info === null) {
7171
return;
7272
}

lib/rules/require-meta-fixable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424

2525
create (context) {
2626
const sourceCode = context.getSourceCode();
27-
const ruleInfo = utils.getRuleInfo(sourceCode.ast);
27+
const ruleInfo = utils.getRuleInfo(sourceCode);
2828
let contextIdentifiers;
2929
let usesFixFunctions;
3030

lib/rules/require-meta-schema.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ module.exports = {
3535

3636
create (context) {
3737
const sourceCode = context.getSourceCode();
38-
const { ast, scopeManager } = sourceCode;
39-
const info = utils.getRuleInfo(ast, scopeManager);
38+
const { scopeManager } = sourceCode;
39+
const info = utils.getRuleInfo(sourceCode);
4040

4141
return {
4242
Program () {

lib/rules/require-meta-type.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030

3131
create (context) {
3232
const sourceCode = context.getSourceCode();
33-
const info = utils.getRuleInfo(sourceCode.ast, sourceCode.scopeManager);
33+
const info = utils.getRuleInfo(sourceCode);
3434

3535
// ----------------------------------------------------------------------
3636
// Helpers

lib/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ module.exports = {
8585

8686
/**
8787
* Performs static analysis on an AST to try to determine the final value of `module.exports`.
88-
* @param {ASTNode} ast The `Program` AST node
88+
* @param {{ast: ASTNode, scopeManager?: ScopeManager}} sourceCode The object contains `Program` AST node, and optional `scopeManager`
8989
* @returns {Object} An object with keys `meta`, `create`, and `isNewStyle`. `meta` and `create` correspond to the AST nodes
9090
for the final values of `module.exports.meta` and `module.exports.create`. `isNewStyle` will be `true` if `module.exports`
9191
is an object, and `false` if module.exports is just the `create` function. If no valid ESLint rule info can be extracted
9292
from the file, the return value will be `null`.
9393
*/
94-
getRuleInfo (ast, scopeManager) {
94+
getRuleInfo ({ ast, scopeManager }) {
9595
const INTERESTING_KEYS = new Set(['create', 'meta']);
9696
let exportsVarOverridden = false;
9797
let exportsIsFunction = false;
@@ -171,7 +171,7 @@ module.exports = {
171171
* @returns {Set<ASTNode>} A Set of all `Identifier` nodes that are references to the `context` value for the file
172172
*/
173173
getContextIdentifiers (context, ast) {
174-
const ruleInfo = module.exports.getRuleInfo(ast);
174+
const ruleInfo = module.exports.getRuleInfo({ ast });
175175

176176
if (!ruleInfo || !ruleInfo.create.params.length || ruleInfo.create.params[0].type !== 'Identifier') {
177177
return new Set();

tests/lib/utils.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('utils', () => {
2828
].forEach(noRuleCase => {
2929
it(`returns null for ${noRuleCase}`, () => {
3030
const ast = espree.parse(noRuleCase, { ecmaVersion: 8, range: true });
31-
assert.isNull(utils.getRuleInfo(ast), 'Expected no rule to be found');
31+
assert.isNull(utils.getRuleInfo({ ast }), 'Expected no rule to be found');
3232
});
3333
});
3434
});
@@ -115,7 +115,7 @@ describe('utils', () => {
115115
Object.keys(CASES).forEach(ruleSource => {
116116
it(ruleSource, () => {
117117
const ast = espree.parse(ruleSource, { ecmaVersion: 6, range: true });
118-
const ruleInfo = utils.getRuleInfo(ast);
118+
const ruleInfo = utils.getRuleInfo({ ast });
119119
assert(
120120
lodash.isMatch(ruleInfo, CASES[ruleSource]),
121121
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(CASES[ruleSource])}`
@@ -139,8 +139,8 @@ describe('utils', () => {
139139
isNewStyle: true,
140140
};
141141
it(`ScopeOptions: ${JSON.stringify(scopeOptions)}`, () => {
142-
const scope = eslintScope.analyze(ast, scopeOptions);
143-
const ruleInfo = utils.getRuleInfo(ast, scope);
142+
const scopeManager = eslintScope.analyze(ast, scopeOptions);
143+
const ruleInfo = utils.getRuleInfo({ ast, scopeManager });
144144
assert(
145145
lodash.isMatch(ruleInfo, expected),
146146
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(expected)}`

0 commit comments

Comments
 (0)