From 1b867c52b2a54cff51125bf915b5c8790334a376 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:27:23 -0700 Subject: [PATCH] Switch to JSDoc imports in scripts (#59068) --- scripts/build/tests.mjs | 5 ++++- scripts/build/utils.mjs | 5 ++++- scripts/eslint/rules/argument-trivia.cjs | 10 ++++++---- scripts/eslint/rules/debug-assert.cjs | 11 +++++++---- scripts/eslint/rules/js-extensions.cjs | 13 ++++++++----- scripts/eslint/rules/jsdoc-format.cjs | 15 +++++++++------ scripts/eslint/rules/no-in-operator.cjs | 5 ++++- scripts/eslint/rules/no-keywords.cjs | 11 +++++++---- scripts/eslint/rules/only-arrow-functions.cjs | 8 ++++++-- 9 files changed, 55 insertions(+), 28 deletions(-) diff --git a/scripts/build/tests.mjs b/scripts/build/tests.mjs index d5a273aa41d..d921d0e5704 100644 --- a/scripts/build/tests.mjs +++ b/scripts/build/tests.mjs @@ -15,6 +15,9 @@ import { rimraf, } from "./utils.mjs"; +/** @import { CancelToken } from "@esfx/canceltoken" */ +void 0; + const mochaJs = path.resolve(findUpRoot(), "node_modules", "mocha", "bin", "_mocha"); export const localBaseline = "tests/baselines/local/"; export const refBaseline = "tests/baselines/reference/"; @@ -25,7 +28,7 @@ export const coverageDir = "coverage"; * @param {string} defaultReporter * @param {boolean} runInParallel * @param {object} options - * @param {import("@esfx/canceltoken").CancelToken} [options.token] + * @param {CancelToken} [options.token] * @param {boolean} [options.watching] */ export async function runConsoleTests(runJs, defaultReporter, runInParallel, options = {}) { diff --git a/scripts/build/utils.mjs b/scripts/build/utils.mjs index aec939861bf..4ffa94aaded 100644 --- a/scripts/build/utils.mjs +++ b/scripts/build/utils.mjs @@ -6,6 +6,9 @@ import fs from "fs"; import JSONC from "jsonc-parser"; import which from "which"; +/** @import { CancelToken } from "@esfx/canceltoken" */ +void 0; + /** * Executes the provided command once with the supplied arguments. * @param {string} cmd @@ -17,7 +20,7 @@ import which from "which"; * @property {boolean} [hidePrompt] * @property {boolean} [waitForExit=true] * @property {boolean} [ignoreStdout] - * @property {import("@esfx/canceltoken").CancelToken} [token] + * @property {CancelToken} [token] */ export async function exec(cmd, args, options = {}) { return /**@type {Promise<{exitCode?: number}>}*/ (new Promise((resolve, reject) => { diff --git a/scripts/eslint/rules/argument-trivia.cjs b/scripts/eslint/rules/argument-trivia.cjs index eaef1368830..1efc77795ba 100644 --- a/scripts/eslint/rules/argument-trivia.cjs +++ b/scripts/eslint/rules/argument-trivia.cjs @@ -3,8 +3,10 @@ const { createRule } = require("./utils.cjs"); const ts = require("typescript"); /** - * @typedef {import("@typescript-eslint/utils").TSESTree.CallExpression | import("@typescript-eslint/utils").TSESTree.NewExpression} CallOrNewExpression + * @import { TSESTree } from "@typescript-eslint/utils" + * @typedef {TSESTree.CallExpression | TSESTree.NewExpression} CallOrNewExpression */ +void 0; const unset = Symbol(); /** @@ -46,7 +48,7 @@ module.exports = createRule({ /** @type {(name: string) => boolean} */ const isSetOrAssert = name => name.startsWith("set") || name.startsWith("assert"); - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Node) => boolean} */ + /** @type {(node: TSESTree.Node) => boolean} */ const isTrivia = node => { if (node.type === AST_NODE_TYPES.Identifier) { return node.name === "undefined"; @@ -101,7 +103,7 @@ module.exports = createRule({ return false; }; - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Node, i: number, getSignature: () => ts.Signature | undefined) => void} */ + /** @type {(node: TSESTree.Node, i: number, getSignature: () => ts.Signature | undefined) => void} */ const checkArg = (node, i, getSignature) => { if (!isTrivia(node)) { return; @@ -123,7 +125,7 @@ module.exports = createRule({ }); const comments = sourceCode.getCommentsBefore(node); - /** @type {import("@typescript-eslint/utils").TSESTree.Comment | undefined} */ + /** @type {TSESTree.Comment | undefined} */ const comment = comments[comments.length - 1]; if (!comment || comment.type !== "Block") { diff --git a/scripts/eslint/rules/debug-assert.cjs b/scripts/eslint/rules/debug-assert.cjs index ca3a4263d7d..5feb9de81cd 100644 --- a/scripts/eslint/rules/debug-assert.cjs +++ b/scripts/eslint/rules/debug-assert.cjs @@ -1,6 +1,9 @@ const { AST_NODE_TYPES } = require("@typescript-eslint/utils"); const { createRule } = require("./utils.cjs"); +/** @import { TSESTree } from "@typescript-eslint/utils" */ +void 0; + module.exports = createRule({ name: "debug-assert", meta: { @@ -17,14 +20,14 @@ module.exports = createRule({ defaultOptions: [], create(context) { - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Node) => boolean} */ + /** @type {(node: TSESTree.Node) => boolean} */ const isArrowFunction = node => node.type === AST_NODE_TYPES.ArrowFunctionExpression; - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Node) => boolean} */ + /** @type {(node: TSESTree.Node) => boolean} */ const isStringLiteral = node => ( (node.type === AST_NODE_TYPES.Literal && typeof node.value === "string") || node.type === AST_NODE_TYPES.TemplateLiteral ); - /** @type {(node: import("@typescript-eslint/utils").TSESTree.MemberExpression) => boolean} */ + /** @type {(node: TSESTree.MemberExpression) => boolean} */ const isDebugAssert = node => ( node.object.type === AST_NODE_TYPES.Identifier && node.object.name === "Debug" @@ -32,7 +35,7 @@ module.exports = createRule({ && node.property.name === "assert" ); - /** @type {(node: import("@typescript-eslint/utils").TSESTree.CallExpression) => void} */ + /** @type {(node: TSESTree.CallExpression) => void} */ const checkDebugAssert = node => { const args = node.arguments; const argsLen = args.length; diff --git a/scripts/eslint/rules/js-extensions.cjs b/scripts/eslint/rules/js-extensions.cjs index 7e435d1ada8..2d73360571b 100644 --- a/scripts/eslint/rules/js-extensions.cjs +++ b/scripts/eslint/rules/js-extensions.cjs @@ -1,5 +1,8 @@ const { createRule } = require("./utils.cjs"); +/** @import { TSESTree } from "@typescript-eslint/utils" */ +void 0; + module.exports = createRule({ name: "js-extensions", meta: { @@ -18,11 +21,11 @@ module.exports = createRule({ create(context) { /** @type {( * node: - * | import("@typescript-eslint/utils").TSESTree.ImportDeclaration - * | import("@typescript-eslint/utils").TSESTree.ExportAllDeclaration - * | import("@typescript-eslint/utils").TSESTree.ExportNamedDeclaration - * | import("@typescript-eslint/utils").TSESTree.TSImportEqualsDeclaration - * | import("@typescript-eslint/utils").TSESTree.TSModuleDeclaration + * | TSESTree.ImportDeclaration + * | TSESTree.ExportAllDeclaration + * | TSESTree.ExportNamedDeclaration + * | TSESTree.TSImportEqualsDeclaration + * | TSESTree.TSModuleDeclaration * ) => void} */ const check = node => { diff --git a/scripts/eslint/rules/jsdoc-format.cjs b/scripts/eslint/rules/jsdoc-format.cjs index 85825845f3d..3d74d64e0ba 100644 --- a/scripts/eslint/rules/jsdoc-format.cjs +++ b/scripts/eslint/rules/jsdoc-format.cjs @@ -1,5 +1,8 @@ const { createRule } = require("./utils.cjs"); +/** @import { TSESTree } from "@typescript-eslint/utils" */ +void 0; + module.exports = createRule({ name: "jsdoc-format", meta: { @@ -25,17 +28,17 @@ module.exports = createRule({ const atInternal = "@internal"; const jsdocStart = "/**"; - /** @type {Map} */ + /** @type {Map} */ const isExportedCache = new Map(); - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Node) => boolean} */ + /** @type {(node: TSESTree.Node) => boolean} */ function isExported(node) { const exported = isExportedCache.get(node); if (exported !== undefined) { return exported; } - /** @type {import("@typescript-eslint/utils").TSESTree.Node | undefined} */ + /** @type {TSESTree.Node | undefined} */ let current = node; while (current) { // https://github.com/typescript-eslint/typescript-eslint/blob/e44a1a280f08f9fd0d29f74e5c3e73b7b64a9606/packages/eslint-plugin/src/util/collectUnusedVariables.ts#L440 @@ -55,7 +58,7 @@ module.exports = createRule({ return text.startsWith(jsdocStart); } - /** @type {(c: import("@typescript-eslint/utils").TSESTree.Comment, indexInComment: number) => import("@typescript-eslint/utils").TSESTree.SourceLocation} */ + /** @type {(c: TSESTree.Comment, indexInComment: number) => TSESTree.SourceLocation} */ const getAtInternalLoc = (c, indexInComment) => { return { start: context.sourceCode.getLocFromIndex(c.range[0] + indexInComment), @@ -63,7 +66,7 @@ module.exports = createRule({ }; }; - /** @type {(c: import("@typescript-eslint/utils").TSESTree.Comment) => import("@typescript-eslint/utils").TSESTree.SourceLocation} */ + /** @type {(c: TSESTree.Comment) => TSESTree.SourceLocation} */ const getJSDocStartLoc = c => { return { start: c.loc.start, @@ -74,7 +77,7 @@ module.exports = createRule({ }; }; - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Node) => void} */ + /** @type {(node: TSESTree.Node) => void} */ const checkDeclaration = node => { const blockComments = sourceCode.getCommentsBefore(node).filter(c => c.type === "Block"); if (blockComments.length === 0) { diff --git a/scripts/eslint/rules/no-in-operator.cjs b/scripts/eslint/rules/no-in-operator.cjs index b81d346eaa8..6a526a51226 100644 --- a/scripts/eslint/rules/no-in-operator.cjs +++ b/scripts/eslint/rules/no-in-operator.cjs @@ -1,5 +1,8 @@ const { createRule } = require("./utils.cjs"); +/** @import { TSESTree } from "@typescript-eslint/utils" */ +void 0; + module.exports = createRule({ name: "no-in-operator", meta: { @@ -16,7 +19,7 @@ module.exports = createRule({ create(context) { const IN_OPERATOR = "in"; - /** @type {(node: import("@typescript-eslint/utils").TSESTree.BinaryExpression) => void} */ + /** @type {(node: TSESTree.BinaryExpression) => void} */ const checkInOperator = node => { if (node.operator === IN_OPERATOR) { context.report({ messageId: "noInOperatorError", node }); diff --git a/scripts/eslint/rules/no-keywords.cjs b/scripts/eslint/rules/no-keywords.cjs index 7aee37ed35d..653f22a5814 100644 --- a/scripts/eslint/rules/no-keywords.cjs +++ b/scripts/eslint/rules/no-keywords.cjs @@ -1,6 +1,9 @@ const { AST_NODE_TYPES } = require("@typescript-eslint/utils"); const { createRule } = require("./utils.cjs"); +/** @import { TSESTree } from "@typescript-eslint/utils" */ +void 0; + module.exports = createRule({ name: "no-keywords", meta: { @@ -37,12 +40,12 @@ module.exports = createRule({ /** @type {(name: string) => boolean} */ const isKeyword = name => keywords.includes(name); - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Identifier) => void} */ + /** @type {(node: TSESTree.Identifier) => void} */ const report = node => { context.report({ messageId: "noKeywordsError", data: { name: node.name }, node }); }; - /** @type {(node: import("@typescript-eslint/utils").TSESTree.ObjectPattern) => void} */ + /** @type {(node: TSESTree.ObjectPattern) => void} */ const checkProperties = node => { node.properties.forEach(property => { if ( @@ -56,7 +59,7 @@ module.exports = createRule({ }); }; - /** @type {(node: import("@typescript-eslint/utils").TSESTree.ArrayPattern) => void} */ + /** @type {(node: TSESTree.ArrayPattern) => void} */ const checkElements = node => { node.elements.forEach(element => { if ( @@ -69,7 +72,7 @@ module.exports = createRule({ }); }; - /** @type {(node: import("@typescript-eslint/utils").TSESTree.ArrowFunctionExpression | import("@typescript-eslint/utils").TSESTree.FunctionDeclaration | import("@typescript-eslint/utils").TSESTree.FunctionExpression | import("@typescript-eslint/utils").TSESTree.TSMethodSignature | import("@typescript-eslint/utils").TSESTree.TSFunctionType) => void} */ + /** @type {(node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.TSMethodSignature | TSESTree.TSFunctionType) => void} */ const checkParams = node => { if (!node || !node.params || !node.params.length) { return; diff --git a/scripts/eslint/rules/only-arrow-functions.cjs b/scripts/eslint/rules/only-arrow-functions.cjs index 206a178bc73..c3f814b21d0 100644 --- a/scripts/eslint/rules/only-arrow-functions.cjs +++ b/scripts/eslint/rules/only-arrow-functions.cjs @@ -1,7 +1,11 @@ const { AST_NODE_TYPES } = require("@typescript-eslint/utils"); const { createRule } = require("./utils.cjs"); -/** @typedef {import("@typescript-eslint/utils").TSESTree.FunctionDeclaration | import("@typescript-eslint/utils").TSESTree.FunctionExpression} FunctionDeclarationOrExpression */ +/** + * @import { TSESTree } from "@typescript-eslint/utils" + * @typedef {TSESTree.FunctionDeclaration | TSESTree.FunctionExpression} FunctionDeclarationOrExpression + */ +void 0; module.exports = createRule({ name: "only-arrow-functions", @@ -32,7 +36,7 @@ module.exports = createRule({ /** @type {(node: FunctionDeclarationOrExpression) => boolean} */ const isThisParameter = node => !!node.params.length && !!node.params.find(param => param.type === AST_NODE_TYPES.Identifier && param.name === "this"); - /** @type {(node: import("@typescript-eslint/utils").TSESTree.Node) => boolean} */ + /** @type {(node: TSESTree.Node) => boolean} */ const isMethodType = node => { const types = [ AST_NODE_TYPES.MethodDefinition,