From 8be8a36010145dfcd31cbdd4f781a91989e3b1bd Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Mon, 26 Jul 2021 19:56:48 -0700 Subject: [PATCH] Chore: Adopt `eslint-plugin/require-meta-docs-url` rule internally (#14823) https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/require-meta-docs-url.md --- .eslintrc.js | 2 +- package.json | 2 +- .../internal-rules/consistent-docs-url.js | 105 ---------------- tools/internal-rules/consistent-docs-url.js | 119 ------------------ 4 files changed, 2 insertions(+), 226 deletions(-) delete mode 100644 tests/tools/internal-rules/consistent-docs-url.js delete mode 100644 tools/internal-rules/consistent-docs-url.js diff --git a/.eslintrc.js b/.eslintrc.js index 668bc2747c5..a7e0f1153d8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -88,7 +88,7 @@ module.exports = { files: ["lib/rules/*"], excludedFiles: ["index.js"], rules: { - "internal-rules/consistent-docs-url": "error" + "eslint-plugin/require-meta-docs-url": ["error", { pattern: "https://eslint.org/docs/rules/{{name}}" }] } }, { diff --git a/package.json b/package.json index 0e64b624b96..ea08a90ef65 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,7 @@ "ejs": "^3.0.2", "eslint": "file:.", "eslint-config-eslint": "file:packages/eslint-config-eslint", - "eslint-plugin-eslint-plugin": "^3.2.0", + "eslint-plugin-eslint-plugin": "^3.4.0", "eslint-plugin-internal-rules": "file:tools/internal-rules", "eslint-plugin-jsdoc": "^25.4.3", "eslint-plugin-node": "^11.1.0", diff --git a/tests/tools/internal-rules/consistent-docs-url.js b/tests/tools/internal-rules/consistent-docs-url.js deleted file mode 100644 index 4b3d3a05b67..00000000000 --- a/tests/tools/internal-rules/consistent-docs-url.js +++ /dev/null @@ -1,105 +0,0 @@ -/** - * @fileoverview Tests for internal-consistent-docs-url rule. - * @author Patrick McElhaney - */ - -"use strict"; - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const rule = require("../../../tools/internal-rules/consistent-docs-url"), - { RuleTester } = require("../../../lib/rule-tester"); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const ruleTester = new RuleTester(); - -ruleTester.run("consistent-docs-url", rule, { - valid: [ - - // wrong exports format: "internal-no-invalid-meta" reports this already - [ - "module.exports = function(context) {", - " return {", - " Program: function(node) {}", - " };", - "};" - ].join("\n"), - [ - "module.exports = {", - " meta: {", - " docs: {", - " url: 'https://eslint.org/docs/rules/'", - " }", - " },", - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n") - ], - invalid: [ - { - code: [ - "module.exports = {", - " meta: {", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - messageId: "missingMetaDocs", - line: 2, - column: 5 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {}", - " },", - - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - messageId: "missingMetaDocsUrl", - line: 3, - column: 9 - }] - }, - { - code: [ - "module.exports = {", - " meta: {", - " docs: {", - " url: 'http://example.com/wrong-url'", - " }", - " },", - " create: function(context) {", - " return {};", - " }", - "};" - ].join("\n"), - errors: [{ - messageId: "incorrectUrl", - data: { - expected: "https://eslint.org/docs/rules/", - url: "http://example.com/wrong-url" - }, - line: 4, - column: 18 - }] - } - ] -}); diff --git a/tools/internal-rules/consistent-docs-url.js b/tools/internal-rules/consistent-docs-url.js deleted file mode 100644 index 052fe55f28a..00000000000 --- a/tools/internal-rules/consistent-docs-url.js +++ /dev/null @@ -1,119 +0,0 @@ -/** - * @fileoverview Internal rule to enforce meta.docs.url conventions. - * @author Patrick McElhaney - */ - -"use strict"; - -const path = require("path"); - -//------------------------------------------------------------------------------ -// 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.url property is present and has the correct value. - * @param {RuleContext} context The ESLint rule context. - * @param {ASTNode} exportsNode ObjectExpression node that the rule exports. - * @returns {void} - */ -function checkMetaDocsUrl(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 metaDocsUrl = metaDocs && getPropertyFromObject("url", metaDocs.value); - - if (!metaDocs) { - context.report({ - node: metaProperty, - messageId: "missingMetaDocs" - }); - return; - } - - if (!metaDocsUrl) { - context.report({ - node: metaDocs, - messageId: "missingMetaDocsUrl" - }); - return; - } - - const ruleId = path.basename(context.getFilename().replace(/.js$/u, "")); - const expected = `https://eslint.org/docs/rules/${ruleId}`; - const url = metaDocsUrl.value.value; - - if (url !== expected) { - context.report({ - node: metaDocsUrl.value, - messageId: "incorrectUrl", - data: { expected, url } - }); - } - -} - -//------------------------------------------------------------------------------ -// Rule Definition -//------------------------------------------------------------------------------ - -module.exports = { - meta: { - docs: { - description: "enforce correct conventions of `meta.docs.url` property in core rules", - category: "Internal", - recommended: false - }, - type: "suggestion", - schema: [], - messages: { - missingMetaDocs: "Rule is missing a meta.docs property.", - missingMetaDocsUrl: "Rule is missing a meta.docs.url property.", - incorrectUrl: 'Incorrect url. Expected "{{ expected }}" but got "{{ url }}".' - } - }, - - create(context) { - return { - AssignmentExpression(node) { - if (node.left && - node.right && - node.left.type === "MemberExpression" && - node.left.object.name === "module" && - node.left.property.name === "exports") { - - checkMetaDocsUrl(context, node.right); - } - } - }; - } -};