Skip to content

Commit

Permalink
fix: ensure rules bail out early when no rule present
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Aug 21, 2022
1 parent d06d1f2 commit 2723b66
Show file tree
Hide file tree
Showing 37 changed files with 147 additions and 45 deletions.
5 changes: 4 additions & 1 deletion lib/rules/meta-property-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const info = getRuleInfo(sourceCode);
if (!info) {
return {};
}

const order = context.options[0] || [
'type',
Expand All @@ -50,7 +53,7 @@ module.exports = {

return {
Program() {
if (!info || !info.meta || info.meta.properties.length < 2) {
if (!info.meta || info.meta.properties.length < 2) {
return;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/rules/no-missing-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}

const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);

Expand Down
4 changes: 4 additions & 0 deletions lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module.exports = {
let contextIdentifiers;

const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}

const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);

return {
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/no-unused-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module.exports = {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const info = utils.getRuleInfo(sourceCode);
if (!info) {
return {};
}

const messageIdsUsed = new Set();
let contextIdentifiers;
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ module.exports = {
let contextIdentifiers;

const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);

return {
Expand Down
7 changes: 3 additions & 4 deletions lib/rules/prefer-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const info = utils.getRuleInfo(sourceCode);
if (!info) {
return {};
}

let contextIdentifiers;

Expand All @@ -44,10 +47,6 @@ module.exports = {
ast
);

if (info === null) {
return;
}

const metaNode = info.meta;
const messagesNode =
metaNode &&
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/prefer-object-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ module.exports = {

const sourceCode = context.getSourceCode();
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}

return {
Program() {
if (!ruleInfo || ruleInfo.isNewStyle) {
if (ruleInfo.isNewStyle) {
return;
}

Expand Down
9 changes: 7 additions & 2 deletions lib/rules/report-message-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,23 @@ module.exports = {
}
}

const sourceCode = context.getSourceCode();
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return {
Program(ast) {
const sourceCode = context.getSourceCode();
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast
);
const ruleInfo = utils.getRuleInfo(sourceCode);

const messagesObject =
ruleInfo &&
ruleInfo.meta &&
Expand Down
12 changes: 6 additions & 6 deletions lib/rules/require-meta-docs-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode();
const info = utils.getRuleInfo(sourceCode);
if (!info) {
return {};
}

return {
Program() {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const info = utils.getRuleInfo(sourceCode);

if (info === null) {
return;
}

const pattern =
context.options[0] && context.options[0].pattern
Expand Down
12 changes: 6 additions & 6 deletions lib/rules/require-meta-docs-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ module.exports = {
);
}

const sourceCode = context.getSourceCode();
const info = util.getRuleInfo(sourceCode);
if (!info) {
return {};
}

return {
Program() {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;

const info = util.getRuleInfo(sourceCode);
if (info === null) {
return;
}

const metaNode = info.meta;
const docsPropNode = util
.evaluateObjectProperties(metaNode, scopeManager)
Expand Down
7 changes: 3 additions & 4 deletions lib/rules/require-meta-has-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module.exports = {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const ruleInfo = utils.getRuleInfo(sourceCode);
if (!ruleInfo) {
return {};
}
let contextIdentifiers;
let ruleReportsSuggestions;

Expand All @@ -58,10 +61,6 @@ module.exports = {
return false;
}

if (!ruleInfo) {
return {};
}

return {
Program(ast) {
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const info = utils.getRuleInfo(sourceCode);
if (info === null) {
if (!info) {
return {};
}

Expand Down
12 changes: 6 additions & 6 deletions lib/rules/require-meta-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ module.exports = {
// Public
// ----------------------------------------------------------------------

const sourceCode = context.getSourceCode();
const info = utils.getRuleInfo(sourceCode);
if (!info) {
return {};
}

return {
Program() {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;
const info = utils.getRuleInfo(sourceCode);

if (info === null) {
return;
}

const metaNode = info.meta;
const typeNode = utils
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/consistent-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ ruleTester.run('consistent-output', rule, {
`,
options: ['always'],
},
`
new NotRuleTester().run('foo', bar, {
valid: [],
invalid: [{code: 'foo', output: 'baz', errors: ['bar']},{code: 'foo', errors: ['bar']}]
});`, // Not RuleTester.
`
new RuleTester().notRun('foo', bar, {
valid: [],
invalid: [{code: 'foo', output: 'baz', errors: ['bar']},{code: 'foo', errors: ['bar']}]
});`, // Not run() from RuleTester.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ ruleTester.run('fixer-return', rule, {
}
};
`,
`module.exports = {};`, // Not a rule.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/meta-property-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ruleTester.run('test-case-property-ordering', rule, {
create() {},
};`,
'module.exports = { create() {} };', // No `meta`.
'module.exports = {};', // No rule;
],

invalid: [
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/no-deprecated-context-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ ruleTester.run('no-deprecated-context-methods', rule, {
sourceCode.getFirstToken();
return {};
}
`,
`,
`module.exports = {};`, // Not a rule.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-deprecated-report-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ruleTester.run('no-deprecated-report-api', rule, {
}
};
`,
`module.exports = {};`, // Not a rule.
],

invalid: [
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ ruleTester.run('no-identical-tests', rule, {
invalid: []
});
`,
`
new NotRuleTester().run('foo', bar, {
valid: [],
invalid: [{code: 'foo', output: 'baz', errors: ['bar']},{code: 'foo', output: 'baz', errors: ['bar']}]
});`, // Not RuleTester.
`
new RuleTester().notRun('foo', bar, {
valid: [],
invalid: [{code: 'foo', output: 'baz', errors: ['bar']},{code: 'foo', output: 'baz', errors: ['bar']}]
});`, // Not run() from RuleTester.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-missing-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ ruleTester.run('no-missing-message-ids', rule, {
}
};
`,
'module.exports = {};', // No rule.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
`module.exports = {};`, // No rule.
],

invalid: [
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-only-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ ruleTester.run('no-only-tests', rule, {
]
});
`,
`
new NotRuleTester().run('foo', bar, {
valid: [{ code: 'foo', only: true },],
invalid: []
});`, // Not RuleTester.
`
new RuleTester().notRun('foo', bar, {
valid: [{ code: 'foo', only: true },],
invalid: []
});`, // Not run() from RuleTester.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-unused-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ ruleTester.run('no-unused-message-ids', rule, {
}
};
`,
'module.exports = {};', // No rule.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ ruleTester.run('no-unused-placeholders', rule, {
}
};
`,
'module.exports = {};', // No rule.
],

invalid: [
Expand Down
25 changes: 14 additions & 11 deletions tests/lib/rules/no-useless-token-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,20 @@ const INVALID_CASES = [
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run('no-useless-token-range', rule, {
valid: [
'sourceCode.getLastToken(foo).range[0]',
'sourceCode.getFirstToken(foo).range[1]',
'sourceCode.getLastToken(foo).start',
'sourceCode.getFirstToken(foo).end',
'sourceCode.getSomethingElse(foo).range[0]',
'notSourceCode.getFirstToken(foo).range[0]',
'sourceCode.getFirstToken(foo, bar).range[0]',
'sourceCode.getFirstToken(foo, { skip: 1 }).start',
'sourceCode.getLastToken(foo, bar).range[1]',
'sourceCode.getLastToken(foo, { skip: 1 }).end',
].map(wrapRule),
...[
'sourceCode.getLastToken(foo).range[0]',
'sourceCode.getFirstToken(foo).range[1]',
'sourceCode.getLastToken(foo).start',
'sourceCode.getFirstToken(foo).end',
'sourceCode.getSomethingElse(foo).range[0]',
'notSourceCode.getFirstToken(foo).range[0]',
'sourceCode.getFirstToken(foo, bar).range[0]',
'sourceCode.getFirstToken(foo, { skip: 1 }).start',
'sourceCode.getLastToken(foo, bar).range[1]',
'sourceCode.getLastToken(foo, { skip: 1 }).end',
].map(wrapRule),
'module.exports = {};', // Not a rule.
],

invalid: [
...INVALID_CASES,
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/prefer-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ ruleTester.run('prefer-message-ids', rule, {
}
};
`,
'module.exports = {};', // No rule.
],

invalid: [
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/prefer-object-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ruleTester.run('prefer-object-rule', rule, {
`,
parserOptions: { sourceType: 'module' },
},
'module.exports = {};', // No rule.
],

invalid: [
Expand Down
Loading

0 comments on commit 2723b66

Please sign in to comment.