From 10a79a672b42d51539bcd6ace482be7afa5f34f8 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Mon, 27 Jan 2020 13:53:13 -0800 Subject: [PATCH] Chore: Adopt `eslint-plugin/require-meta-docs-description` internally (#12762) * Upgrade: eslint-plugin-eslint-plugin to 2.2.1 https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/CHANGELOG.md#v221-2020-01-17 * Chore: enable `eslint-plugin/require-meta-docs-description` rule internally https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-docs-description.md * Chore: delete `internal-rules/consistent-docs-description` Replaced by `eslint-plugin/require-meta-docs-description`: https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-docs-description.md --- .eslintrc.js | 4 +- package.json | 2 +- .../consistent-docs-description.js | 264 ------------------ .../consistent-docs-description.js | 134 --------- 4 files changed, 3 insertions(+), 401 deletions(-) delete mode 100644 tests/tools/internal-rules/consistent-docs-description.js delete mode 100644 tools/internal-rules/consistent-docs-description.js diff --git a/.eslintrc.js b/.eslintrc.js index c347bf858f9..6ab4510cb37 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -28,6 +28,7 @@ module.exports = { "eslint-plugin/prefer-output-null": "error", "eslint-plugin/prefer-placeholders": "error", "eslint-plugin/report-message-format": ["error", "[^a-z].*\\.$"], + "eslint-plugin/require-meta-docs-description": "error", "eslint-plugin/require-meta-type": "error", "eslint-plugin/test-case-property-ordering": [ "error", @@ -53,8 +54,7 @@ module.exports = { files: ["lib/rules/*", "tools/internal-rules/*"], excludedFiles: ["index.js"], rules: { - "internal-rules/no-invalid-meta": "error", - "internal-rules/consistent-docs-description": "error" + "internal-rules/no-invalid-meta": "error" /* * TODO: enable it when all the rules using meta.messages diff --git a/package.json b/package.json index 6954953b25e..89f72eb520b 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,7 @@ "ejs": "^2.6.1", "eslint": "file:.", "eslint-config-eslint": "file:packages/eslint-config-eslint", - "eslint-plugin-eslint-plugin": "^2.0.1", + "eslint-plugin-eslint-plugin": "^2.2.1", "eslint-plugin-internal-rules": "file:tools/internal-rules", "eslint-plugin-jsdoc": "^15.9.5", "eslint-plugin-node": "^9.0.0", diff --git a/tests/tools/internal-rules/consistent-docs-description.js b/tests/tools/internal-rules/consistent-docs-description.js deleted file mode 100644 index 80fa7784a49..00000000000 --- a/tests/tools/internal-rules/consistent-docs-description.js +++ /dev/null @@ -1,264 +0,0 @@ -/** - * @fileoverview Tests for internal-consistent-docs-description rule. - * @author Vitor Balocco - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const rule = require("../../../tools/internal-rules/consistent-docs-description"), - { RuleTester } = require("../../../lib/rule-tester"); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const ruleTester = new RuleTester(); - -ruleTester.run("consistent-docs-description", rule, { - valid: [ - - // wrong exports format: "internal-no-invalid-meta" reports this already - [ - "module.exports = function(context) {", - " return {", - " Program: function(node) {}", - " };", - "};" - ].join("\n"), - - // missing `meta.docs.description` property: "internal-no-invalid-meta" reports this already - [ - "module.exports = {", - " meta: {},", - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - [ - "module.exports = {", - " meta: {", - " docs: {", - " description: 'enforce some stuff'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - [ - "module.exports = {", - " meta: {", - " docs: {", - " description: 'require some things'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - [ - "module.exports = {", - " meta: {", - " docs: {", - " description: 'disallow bad things'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n") - ], - invalid: [ - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: 'do stuff'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should start with one of the following words: enforce, require, disallow. Started with \"do\" instead.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: 'Require stuff'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should start with one of the following words: enforce, require, disallow. Started with \"Require\" instead.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: 'Enforce stuff'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should start with one of the following words: enforce, require, disallow. Started with \"Enforce\" instead.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: 'Disallow stuff'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should start with one of the following words: enforce, require, disallow. Started with \"Disallow\" instead.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: ' disallow (whitespace in the beginning)'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should not start with whitespace.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: ' disallow (whitespaces in the beginning)'", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should not start with whitespace.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: ' '", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should not start with whitespace.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: ''", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should not be empty.", - line: 4, - column: 26 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " description: true", - " }", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - message: "`meta.docs.description` should be a string.", - line: 4, - column: 26 - }] - } - ] -}); diff --git a/tools/internal-rules/consistent-docs-description.js b/tools/internal-rules/consistent-docs-description.js deleted file mode 100644 index 1519c0247ff..00000000000 --- a/tools/internal-rules/consistent-docs-description.js +++ /dev/null @@ -1,134 +0,0 @@ -/** - * @fileoverview Internal rule to enforce meta.docs.description conventions. - * @author Vitor Balocco - */ - -"use strict"; - -const ALLOWED_FIRST_WORDS = [ - "enforce", - "require", - "disallow" -]; - -//------------------------------------------------------------------------------ -// Helpers -//------------------------------------------------------------------------------ - -/** - * Gets the property of the Object node passed in that has the name specified. - * @param {string} property Name of the property to return. - * @param {ASTNode} node The ObjectExpression node. - * @returns {ASTNode} The Property node or null if not found. - */ -function getPropertyFromObject(property, node) { - const properties = node.properties; - - if (!Array.isArray(properties)) { - - // if properties is not an array, "internal-no-invalid-meta" will already report this. - return null; - } - - for (let i = 0; i < properties.length; i++) { - if (properties[i].key.name === property) { - return properties[i]; - } - } - - return null; -} - -/** - * Verifies that the meta.docs.description property follows our internal conventions. - * @param {RuleContext} context The ESLint rule context. - * @param {ASTNode} exportsNode ObjectExpression node that the rule exports. - * @returns {void} - */ -function checkMetaDocsDescription(context, exportsNode) { - if (exportsNode.type !== "ObjectExpression") { - - // if the exported node is not the correct format, "internal-no-invalid-meta" will already report this. - return; - } - - const metaProperty = getPropertyFromObject("meta", exportsNode); - const metaDocs = metaProperty && getPropertyFromObject("docs", metaProperty.value); - const metaDocsDescription = metaDocs && getPropertyFromObject("description", metaDocs.value); - - if (!metaDocsDescription) { - - // if there is no `meta.docs.description` property, "internal-no-invalid-meta" will already report this. - return; - } - - const description = metaDocsDescription.value.value; - - if (typeof description !== "string") { - context.report({ - node: metaDocsDescription.value, - message: "`meta.docs.description` should be a string." - }); - return; - } - - if (description === "") { - context.report({ - node: metaDocsDescription.value, - message: "`meta.docs.description` should not be empty." - }); - return; - } - - if (description.indexOf(" ") === 0) { - context.report({ - node: metaDocsDescription.value, - message: "`meta.docs.description` should not start with whitespace." - }); - return; - } - - const firstWord = description.split(" ")[0]; - - if (ALLOWED_FIRST_WORDS.indexOf(firstWord) === -1) { - context.report({ - node: metaDocsDescription.value, - message: "`meta.docs.description` should start with one of the following words: {{ allowedWords }}. Started with \"{{ firstWord }}\" instead.", - data: { - allowedWords: ALLOWED_FIRST_WORDS.join(", "), - firstWord - } - }); - } -} - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -module.exports = { - meta: { - docs: { - description: "enforce correct conventions of `meta.docs.description` property in core rules", - category: "Internal", - recommended: false - }, - type: "suggestion", - schema: [] - }, - - create(context) { - return { - AssignmentExpression(node) { - if (node.left && - node.right && - node.left.type === "MemberExpression" && - node.left.object.name === "module" && - node.left.property.name === "exports") { - - checkMetaDocsDescription(context, node.right); - } - } - }; - } -};