Skip to content

Commit

Permalink
Chore: add eslint-plugin-eslint-plugin (#8198)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Mar 4, 2017
1 parent 580da36 commit 5e3bca7
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 85 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.yml
@@ -1,7 +1,13 @@
root: true

plugins:
- eslint-plugin
- node
extends:
- "./packages/eslint-config-eslint/default.yml"
- "plugin:node/recommended"
- "plugin:eslint-plugin/recommended"
rules:
eslint-plugin/consistent-output: "error"
eslint-plugin/prefer-placeholders: "error"
eslint-plugin/report-message-format: ["error", '[^a-z].*\.$']
42 changes: 2 additions & 40 deletions lib/internal-rules/internal-no-invalid-meta.js
Expand Up @@ -94,16 +94,6 @@ function hasMetaSchema(metaPropertyNode) {
return getPropertyFromObject("schema", metaPropertyNode.value);
}

/**
* Whether this `meta` ObjectExpression has a `fixable` property defined or not.
*
* @param {ASTNode} metaPropertyNode The `meta` ObjectExpression for this rule.
* @returns {boolean} `true` if a `fixable` property exists.
*/
function hasMetaFixable(metaPropertyNode) {
return getPropertyFromObject("fixable", metaPropertyNode.value);
}

/**
* Checks the validity of the meta definition of this rule and reports any errors found.
*
Expand All @@ -112,7 +102,7 @@ function hasMetaFixable(metaPropertyNode) {
* @param {boolean} ruleIsFixable whether the rule is fixable or not.
* @returns {void}
*/
function checkMetaValidity(context, exportsNode, ruleIsFixable) {
function checkMetaValidity(context, exportsNode) {
const metaProperty = getMetaPropertyFromExportsNode(exportsNode);

if (!metaProperty) {
Expand Down Expand Up @@ -142,11 +132,6 @@ function checkMetaValidity(context, exportsNode, ruleIsFixable) {

if (!hasMetaSchema(metaProperty)) {
context.report(metaProperty, "Rule is missing a meta.schema property.");
return;
}

if (ruleIsFixable && !hasMetaFixable(metaProperty)) {
context.report(metaProperty, "Rule is fixable, but is missing a meta.fixable property.");
}
}

Expand Down Expand Up @@ -177,7 +162,6 @@ module.exports = {

create(context) {
let exportsNode;
let ruleIsFixable = false;

return {
AssignmentExpression(node) {
Expand All @@ -191,35 +175,13 @@ module.exports = {
}
},

CallExpression(node) {

// If the rule has a call for `context.report` and a property `fix`
// is being passed in, then we consider that the rule is fixable.
//
// Note that we only look for context.report() calls in the new
// style (with single MessageDescriptor argument), because only
// calls in the new style can specify a fix.
if (node.callee.type === "MemberExpression" &&
node.callee.object.type === "Identifier" &&
node.callee.object.name === "context" &&
node.callee.property.type === "Identifier" &&
node.callee.property.name === "report" &&
node.arguments.length === 1 &&
node.arguments[0].type === "ObjectExpression") {

if (getPropertyFromObject("fix", node.arguments[0])) {
ruleIsFixable = true;
}
}
},

"Program:exit"() {
if (!isCorrectExportsFormat(exportsNode)) {
context.report({ node: exportsNode, message: "Rule does not export an Object. Make sure the rule follows the new rule format." });
return;
}

checkMetaValidity(context, exportsNode, ruleIsFixable);
checkMetaValidity(context, exportsNode);
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-invalid-regexp.js
Expand Up @@ -74,7 +74,8 @@ module.exports = {
} catch (e) {
context.report({
node,
message: `${e.message}.`
message: "{{message}}.",
data: e
});
}

Expand Down
2 changes: 2 additions & 0 deletions lib/rules/no-restricted-properties.js
Expand Up @@ -109,6 +109,7 @@ module.exports = {
if (matchedObjectProperty) {
const message = matchedObjectProperty.message ? ` ${matchedObjectProperty.message}` : "";

// eslint-disable-next-line eslint-plugin/report-message-format
context.report({ node, message: "'{{objectName}}.{{propertyName}}' is restricted from being used.{{message}}", data: {
objectName,
propertyName,
Expand All @@ -117,6 +118,7 @@ module.exports = {
} else if (globalMatchedProperty) {
const message = globalMatchedProperty.message ? ` ${globalMatchedProperty.message}` : "";

// eslint-disable-next-line eslint-plugin/report-message-format
context.report({ node, message: "'{{propertyName}}' is restricted from being used.{{message}}", data: {
propertyName,
message
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-return-await.js
Expand Up @@ -19,7 +19,7 @@ module.exports = {
category: "Best Practices",
recommended: false // TODO: set to true
},
fixable: false,
fixable: null,
schema: [
]
},
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/object-shorthand.js
Expand Up @@ -368,11 +368,12 @@ module.exports = {
// Checks for property/method shorthand.
if (isConciseProperty) {
if (node.method && (APPLY_NEVER || AVOID_QUOTES && isStringLiteral(node.key))) {
const message = APPLY_NEVER ? "Expected longform method syntax." : "Expected longform method syntax for string literal keys.";

// { x() {} } should be written as { x: function() {} }
context.report({
node,
message: `Expected longform method syntax${APPLY_NEVER ? "" : " for string literal keys"}.`,
message,
fix: fixer => makeFunctionLongform(fixer, node)
});
} else if (APPLY_NEVER) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-destructuring.js
Expand Up @@ -89,7 +89,7 @@ module.exports = {
* @returns {void}
*/
function report(reportNode, type) {
context.report({ node: reportNode, message: `Use ${type} destructuring` });
context.report({ node: reportNode, message: "Use {{type}} destructuring.", data: { type } });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/yoda.js
Expand Up @@ -267,8 +267,8 @@ module.exports = {
const operatorToken = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
const textBeforeOperator = sourceCode.getText().slice(sourceCode.getTokenBefore(operatorToken).range[1], operatorToken.range[0]);
const textAfterOperator = sourceCode.getText().slice(operatorToken.range[1], sourceCode.getTokenAfter(operatorToken).range[0]);
const leftText = sourceCode.getText().slice(sourceCode.getFirstToken(node).range[0], sourceCode.getTokenBefore(operatorToken).range[1]);
const rightText = sourceCode.getText().slice(sourceCode.getTokenAfter(operatorToken).range[0], sourceCode.getLastToken(node).range[1]);
const leftText = sourceCode.getText().slice(node.range[0], sourceCode.getTokenBefore(operatorToken).range[1]);
const rightText = sourceCode.getText().slice(sourceCode.getTokenAfter(operatorToken).range[0], node.range[1]);

return rightText + textBeforeOperator + OPERATOR_FLIP_MAP[operatorToken.value] + textAfterOperator + leftText;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -81,6 +81,7 @@
"coveralls": "^2.11.16",
"dateformat": "^1.0.8",
"ejs": "^2.3.3",
"eslint-plugin-eslint-plugin": "^0.7.1",
"eslint-plugin-node": "^4.1.0",
"eslint-release": "^0.10.0",
"esprima": "^2.4.1",
Expand Down
31 changes: 0 additions & 31 deletions tests/lib/internal-rules/internal-no-invalid-meta.js
Expand Up @@ -241,37 +241,6 @@ ruleTester.run("internal-no-invalid-meta", rule, {
line: 2,
column: 5
}]
},
{
code: [
"module.exports = {",
" meta: {",
" docs: {",
" description: 'some rule',",
" category: 'Internal',",
" recommended: false",
" },",
" schema: []",
" },",
" create: function(context) {",
" return {",
" Program: function(node) {",
" context.report({",
" node: node,",
" fix: function(fixer) {",
" return fixer.insertTextAfter(node, ' ');",
" }",
" });",
" }",
" };",
" }",
"};"
].join("\n"),
errors: [{
message: "Rule is fixable, but is missing a meta.fixable property.",
line: 2,
column: 5
}]
}
]
});
16 changes: 8 additions & 8 deletions tests/lib/rules/prefer-destructuring.js
Expand Up @@ -81,58 +81,58 @@ ruleTester.run("prefer-destructuring", rule, {
{
code: "var foo = array[0];",
errors: [{
message: "Use array destructuring",
message: "Use array destructuring.",
type: "VariableDeclarator"
}]
},
{
code: "foo = array[0];",
errors: [{
message: "Use array destructuring",
message: "Use array destructuring.",
type: "AssignmentExpression"
}]
},
{
code: "var foo = object.foo;",
errors: [{
message: "Use object destructuring",
message: "Use object destructuring.",
type: "VariableDeclarator"
}]
},
{
code: "var foobar = object.bar;",
options: [{ object: true }, { enforceForRenamedProperties: true }],
errors: [{
message: "Use object destructuring",
message: "Use object destructuring.",
type: "VariableDeclarator"
}]
},
{
code: "var foo = object[bar];",
options: [{ object: true }, { enforceForRenamedProperties: true }],
errors: [{
message: "Use object destructuring",
message: "Use object destructuring.",
type: "VariableDeclarator"
}]
},
{
code: "var foo = array['foo'];",
errors: [{
message: "Use object destructuring",
message: "Use object destructuring.",
type: "VariableDeclarator"
}]
},
{
code: "foo = array.foo;",
errors: [{
message: "Use object destructuring",
message: "Use object destructuring.",
type: "AssignmentExpression"
}]
},
{
code: "foo = array['foo'];",
errors: [{
message: "Use object destructuring",
message: "Use object destructuring.",
type: "AssignmentExpression"
}]
}
Expand Down

0 comments on commit 5e3bca7

Please sign in to comment.