From c287e49eacb348537edb05c38a1c82e546f00507 Mon Sep 17 00:00:00 2001 From: David Anson Date: Wed, 6 Mar 2024 21:18:51 -0800 Subject: [PATCH] Add typings for Rule object to all Rule samples and examples. --- .eslintrc.json | 8 ++++++++ doc/CustomRules.md | 1 + example/typescript/type-check.ts | 2 +- helpers/README.md | 1 + test/rules/any-blockquote.js | 25 +++++++++++++++++-------- test/rules/every-n-lines.js | 1 + test/rules/first-line.js | 1 + test/rules/letters-E-X.js | 1 + test/rules/lint-javascript.js | 1 + test/rules/npm/sample-rule.js | 1 + test/rules/validate-json.js | 1 + 11 files changed, 34 insertions(+), 9 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 60153c78..89043c52 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -54,6 +54,14 @@ "object-property-newline": "off" } }, + { + "files": [ + "test/rules/**/*.js" + ], + "rules": { + "jsdoc/valid-types": "off" + } + }, { "files": [ "**/*.mjs" diff --git a/doc/CustomRules.md b/doc/CustomRules.md index 7a16b774..a4368b5e 100644 --- a/doc/CustomRules.md +++ b/doc/CustomRules.md @@ -28,6 +28,7 @@ the parsed input and a function to log any violations. A simple rule implementation looks like: ```javascript +/** @type import("markdownlint").Rule */ module.exports = { "names": [ "any-blockquote" ], "description": "Rule that reports an error for any blockquote", diff --git a/example/typescript/type-check.ts b/example/typescript/type-check.ts index 3c53f652..758fbba0 100644 --- a/example/typescript/type-check.ts +++ b/example/typescript/type-check.ts @@ -109,7 +109,7 @@ markdownlint(options, assertLintResultsCallback); assertLintResultsCallback(null, await markdownlint.promises.markdownlint(options)); })(); -const testRule = { +const testRule: markdownlint.Rule = { "names": [ "test-rule" ], "description": "Test rule", "information": new URL("https://example.com/rule-information"), diff --git a/helpers/README.md b/helpers/README.md index 4f5d66cd..dd0abd25 100644 --- a/helpers/README.md +++ b/helpers/README.md @@ -24,6 +24,7 @@ be useful to custom rule authors and may avoid duplicating code. ```javascript const { forEachLine, getLineMetadata } = require("markdownlint-rule-helpers"); +/** @type import("markdownlint").Rule */ module.exports = { "names": [ "every-n-lines" ], "description": "Rule that reports an error every N lines", diff --git a/test/rules/any-blockquote.js b/test/rules/any-blockquote.js index a9e8dd82..67af8e31 100644 --- a/test/rules/any-blockquote.js +++ b/test/rules/any-blockquote.js @@ -4,6 +4,10 @@ const { filterTokens } = require("markdownlint-rule-helpers"); +/** @typedef {import("../../lib/markdownlint").MarkdownItToken} MarkdownItToken */ +/** @typedef {(MarkdownItToken) => void} FilterTokensCallback */ + +/** @type import("../../lib/markdownlint").Rule */ module.exports = { "names": [ "any-blockquote" ], "description": "Rule that reports an error for any blockquote", @@ -13,13 +17,18 @@ module.exports = { ), "tags": [ "test" ], "function": (params, onError) => { - filterTokens(params, "blockquote_open", (blockquote) => { - const lines = blockquote.map[1] - blockquote.map[0]; - onError({ - "lineNumber": blockquote.lineNumber, - "detail": "Blockquote spans " + lines + " line(s).", - "context": blockquote.line.substr(0, 7) - }); - }); + filterTokens( + params, + "blockquote_open", + /** @type FilterTokensCallback */ + (blockquote) => { + const lines = blockquote.map[1] - blockquote.map[0]; + onError({ + "lineNumber": blockquote.lineNumber, + "detail": "Blockquote spans " + lines + " line(s).", + "context": blockquote.line.substr(0, 7) + }); + } + ); } }; diff --git a/test/rules/every-n-lines.js b/test/rules/every-n-lines.js index 04f3aaca..abbc8f1c 100644 --- a/test/rules/every-n-lines.js +++ b/test/rules/every-n-lines.js @@ -4,6 +4,7 @@ const { forEachLine, getLineMetadata } = require("markdownlint-rule-helpers"); +/** @type import("../../lib/markdownlint").Rule */ module.exports = { "names": [ "every-n-lines" ], "description": "Rule that reports an error every N lines", diff --git a/test/rules/first-line.js b/test/rules/first-line.js index 6974d2fb..4d7580c9 100644 --- a/test/rules/first-line.js +++ b/test/rules/first-line.js @@ -2,6 +2,7 @@ "use strict"; +/** @type import("../../lib/markdownlint").Rule */ module.exports = { "names": [ "first-line" ], "description": "Rule that reports an error for the first line", diff --git a/test/rules/letters-E-X.js b/test/rules/letters-E-X.js index 26e364b9..f6de35a1 100644 --- a/test/rules/letters-E-X.js +++ b/test/rules/letters-E-X.js @@ -2,6 +2,7 @@ "use strict"; +/** @type import("../../lib/markdownlint").Rule */ module.exports = { "names": [ "letters-E-X", "letter-E-letter-X", "contains-ex" ], "description": "Rule that reports an error for lines with the letters 'EX'", diff --git a/test/rules/lint-javascript.js b/test/rules/lint-javascript.js index 7564963c..5d96a05b 100644 --- a/test/rules/lint-javascript.js +++ b/test/rules/lint-javascript.js @@ -24,6 +24,7 @@ function cleanJsdocRulesFromEslintConfig(config) { return cleanedConfig; } +/** @type import("../../lib/markdownlint").Rule */ module.exports = { "names": [ "lint-javascript" ], "description": "Rule that lints JavaScript code", diff --git a/test/rules/npm/sample-rule.js b/test/rules/npm/sample-rule.js index eca4f376..691f6d49 100644 --- a/test/rules/npm/sample-rule.js +++ b/test/rules/npm/sample-rule.js @@ -2,6 +2,7 @@ "use strict"; +/** @type import("../../../lib/markdownlint").Rule */ module.exports = { "names": [ "sample-rule" ], "description": "Sample rule", diff --git a/test/rules/validate-json.js b/test/rules/validate-json.js index 16a582b9..6db0575e 100644 --- a/test/rules/validate-json.js +++ b/test/rules/validate-json.js @@ -5,6 +5,7 @@ const { filterTokens } = require("markdownlint-rule-helpers"); const { parse, printParseErrorCode } = require("jsonc-parser"); +/** @type import("../../lib/markdownlint").Rule */ module.exports = { "names": [ "validate-json" ], "description": "Rule that validates JSON code",