diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 95a6301..ab513bd 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -60,6 +60,8 @@ jobs: run: npm install --no-save eslint@^${{ matrix.eslint }}.0.0 - name: Build run: npm run -s build + - name: Build declaration + run: npm run -s build:declaration - name: Test run: npm run -s test:mocha - name: Send Coverage diff --git a/.gitignore b/.gitignore index 0d8768b..b7a0a56 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /node_modules /index.* /test.* +/types \ No newline at end of file diff --git a/package.json b/package.json index 661a97f..bb431d6 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,12 @@ "main": "index", "module": "index.mjs", "files": [ - "index.*" + "index.*", + "types" ], + "types": "types/index.d.ts", "dependencies": { + "@types/eslint": "^7.2.10", "eslint-visitor-keys": "^1.1.0" }, "devDependencies": { @@ -29,12 +32,14 @@ "rollup": "^1.25.0", "rollup-plugin-sourcemaps": "^0.4.2", "semver": "^7.3.2", + "typescript": "^4.2.4", "vuepress": "^1.2.0", "warun": "^1.0.0" }, "scripts": { "prebuild": "npm run -s clean", "build": "rollup -c", + "build:declaration": "tsc src/*.js --declaration --allowJs --emitDeclarationOnly --outDir types", "clean": "rimraf .nyc_output coverage index.*", "codecov": "nyc report -r lcovonly && codecov", "coverage": "opener ./coverage/lcov-report/index.html", diff --git a/src/find-variable.js b/src/find-variable.js index c67b02f..cad22d3 100644 --- a/src/find-variable.js +++ b/src/find-variable.js @@ -1,10 +1,11 @@ +import { Scope, Rule } from "eslint" import { getInnermostScope } from "./get-innermost-scope" /** * Find the variable of a given name. - * @param {Scope} initialScope The scope to start finding. - * @param {string|Node} nameOrNode The variable name to find. If this is a Node object then it should be an Identifier node. - * @returns {Variable|null} The found variable or null. + * @param {Scope.Scope} initialScope The scope to start finding. + * @param {string|Rule.Node} nameOrNode The variable name to find. If this is a Node object then it should be an Identifier node. + * @returns {Scope.Variable|null} The found variable or null. */ export function findVariable(initialScope, nameOrNode) { let name = "" diff --git a/src/get-function-head-location.js b/src/get-function-head-location.js index a2a5163..9ada027 100644 --- a/src/get-function-head-location.js +++ b/src/get-function-head-location.js @@ -1,10 +1,11 @@ +import { Scope, Rule, SourceCode, AST } from "eslint" import { isArrowToken, isOpeningParenToken } from "./token-predicate" /** * Get the `(` token of the given function node. - * @param {Node} node - The function node to get. + * @param {Rule.Node} node - The function node to get. * @param {SourceCode} sourceCode - The source code object to get tokens. - * @returns {Token} `(` token. + * @returns {AST.Token} `(` token. */ function getOpeningParenOfParams(node, sourceCode) { return node.id @@ -14,7 +15,7 @@ function getOpeningParenOfParams(node, sourceCode) { /** * Get the location of the given function node for reporting. - * @param {Node} node - The function node to get. + * @param {Rule.Node} node - The function node to get. * @param {SourceCode} sourceCode - The source code object to get tokens. * @returns {string} The location of the function node for reporting. */ diff --git a/src/get-function-name-with-kind.js b/src/get-function-name-with-kind.js index 779dce3..36b0d8a 100644 --- a/src/get-function-name-with-kind.js +++ b/src/get-function-name-with-kind.js @@ -1,8 +1,9 @@ +import { Rule } from "eslint" import { getPropertyName } from "./get-property-name" /** * Get the name and kind of the given function node. - * @param {ASTNode} node - The function node to get. + * @param {Rule.Node} node - The function node to get. * @returns {string} The name and kind of the function node. */ export function getFunctionNameWithKind(node) { diff --git a/src/get-innermost-scope.js b/src/get-innermost-scope.js index d62ec69..1f96326 100644 --- a/src/get-innermost-scope.js +++ b/src/get-innermost-scope.js @@ -1,7 +1,9 @@ +import { Scope, Rule } from "eslint" + /** * Get the innermost scope which contains a given location. - * @param {Scope} initialScope The initial scope to search. - * @param {Node} node The location to search. + * @param {Scope.Scope} initialScope The initial scope to search. + * @param {Rule.Node} node The location to search. * @returns {Scope} The innermost scope. */ export function getInnermostScope(initialScope, node) { diff --git a/src/get-property-name.js b/src/get-property-name.js index 817d7e5..8fd3300 100644 --- a/src/get-property-name.js +++ b/src/get-property-name.js @@ -1,9 +1,10 @@ +import { Scope, Rule } from "eslint" import { getStringIfConstant } from "./get-string-if-constant" /** * Get the property name from a MemberExpression node or a Property node. - * @param {Node} node The node to get. - * @param {Scope} [initialScope] The scope to start finding variable. Optional. If the node is a computed property node and this scope was given, this checks the computed property name by the `getStringIfConstant` function with the scope, and returns the value of it. + * @param {Rule.Node} node The node to get. + * @param {Scope.Scope} [initialScope] The scope to start finding variable. Optional. If the node is a computed property node and this scope was given, this checks the computed property name by the `getStringIfConstant` function with the scope, and returns the value of it. * @returns {string|null} The property name of the node. */ export function getPropertyName(node, initialScope) { diff --git a/src/get-static-value.js b/src/get-static-value.js index 2511a98..1025305 100644 --- a/src/get-static-value.js +++ b/src/get-static-value.js @@ -1,5 +1,7 @@ /* globals BigInt, globalThis, global, self, window */ +import { Scope, Rule } from "eslint" + import { findVariable } from "./find-variable" const globalObject = @@ -142,8 +144,8 @@ function isGetter(object, name) { /** * Get the element values of a given node list. - * @param {Node[]} nodeList The node list to get values. - * @param {Scope|undefined} initialScope The initial scope to find variables. + * @param {Rule.Node[]} nodeList The node list to get values. + * @param {Scope.Scope|undefined} initialScope The initial scope to find variables. * @returns {any[]|null} The value list if all nodes are constant. Otherwise, null. */ function getElementValues(nodeList, initialScope) { @@ -512,8 +514,8 @@ const operations = Object.freeze({ /** * Get the value of a given node if it's a static value. - * @param {Node} node The node to get. - * @param {Scope|undefined} initialScope The scope to start finding variable. + * @param {Rule.Node} node The node to get. + * @param {Scope.Scope|undefined} initialScope The scope to start finding variable. * @returns {{value:any}|{value:undefined,optional?:true}|null} The static value of the node, or `null`. */ function getStaticValueR(node, initialScope) { @@ -525,8 +527,8 @@ function getStaticValueR(node, initialScope) { /** * Get the value of a given node if it's a static value. - * @param {Node} node The node to get. - * @param {Scope} [initialScope] The scope to start finding variable. Optional. If this scope was given, this tries to resolve identifier references which are in the given node as much as possible. + * @param {Rule.Node} node The node to get. + * @param {Scope.Scope} [initialScope] The scope to start finding variable. Optional. If this scope was given, this tries to resolve identifier references which are in the given node as much as possible. * @returns {{value:any}|{value:undefined,optional?:true}|null} The static value of the node, or `null`. */ export function getStaticValue(node, initialScope = null) { diff --git a/src/get-string-if-constant.js b/src/get-string-if-constant.js index 751018a..fe8ee6c 100644 --- a/src/get-string-if-constant.js +++ b/src/get-string-if-constant.js @@ -1,9 +1,10 @@ +import { Scope, Rule } from "eslint" import { getStaticValue } from "./get-static-value" /** * Get the value of a given node if it's a literal or a template literal. - * @param {Node} node The node to get. - * @param {Scope} [initialScope] The scope to start finding variable. Optional. If the node is an Identifier node and this scope was given, this checks the variable of the identifier, and returns the value of it if the variable is a constant. + * @param {Rule.Node} node The node to get. + * @param {Scope.Scope} [initialScope] The scope to start finding variable. Optional. If the node is an Identifier node and this scope was given, this checks the variable of the identifier, and returns the value of it if the variable is a constant. * @returns {string|null} The value of the node, or `null`. */ export function getStringIfConstant(node, initialScope = null) { diff --git a/src/has-side-effect.js b/src/has-side-effect.js index d5a248c..58b5bd1 100644 --- a/src/has-side-effect.js +++ b/src/has-side-effect.js @@ -1,3 +1,4 @@ +import { Rule, SourceCode } from "eslint" import evk from "eslint-visitor-keys" const typeConversionBinaryOps = Object.freeze( @@ -159,7 +160,7 @@ const visitor = Object.freeze( /** * Check whether a given node has any side effect or not. - * @param {Node} node The node to get. + * @param {Rule.Node} node The node to get. * @param {SourceCode} sourceCode The source code object. * @param {object} [options] The option object. * @param {boolean} [options.considerGetters=false] If `true` then it considers member accesses as the node which has side effects. diff --git a/src/is-parenthesized.js b/src/is-parenthesized.js index 1ec81a4..d5042f8 100644 --- a/src/is-parenthesized.js +++ b/src/is-parenthesized.js @@ -1,9 +1,10 @@ +import { Scope, Rule, SourceCode, AST } from "eslint" import { isClosingParenToken, isOpeningParenToken } from "./token-predicate" /** * Get the left parenthesis of the parent node syntax if it exists. * E.g., `if (a) {}` then the `(`. - * @param {Node} node The AST node to check. + * @param {Rule.Node} node The AST node to check. * @param {SourceCode} sourceCode The source code object to get tokens. * @returns {Token|null} The left parenthesis of the parent node syntax */ @@ -63,13 +64,13 @@ function getParentSyntaxParen(node, sourceCode) { /** * Check whether a given node is parenthesized or not. * @param {number} times The number of parantheses. - * @param {Node} node The AST node to check. + * @param {Rule.Node} node The AST node to check. * @param {SourceCode} sourceCode The source code object to get tokens. * @returns {boolean} `true` if the node is parenthesized the given times. */ /** * Check whether a given node is parenthesized or not. - * @param {Node} node The AST node to check. + * @param {Rule.Node} node The AST node to check. * @param {SourceCode} sourceCode The source code object to get tokens. * @returns {boolean} `true` if the node is parenthesized. */ diff --git a/src/reference-tracker.js b/src/reference-tracker.js index f88e0e3..e223331 100644 --- a/src/reference-tracker.js +++ b/src/reference-tracker.js @@ -1,3 +1,4 @@ +import { Scope, Rule } from "eslint" import { findVariable } from "./find-variable" import { getPropertyName } from "./get-property-name" import { getStringIfConstant } from "./get-string-if-constant" @@ -14,7 +15,7 @@ const requireCall = { require: { [CALL]: true } } /** * Check whether a given variable is modified or not. - * @param {Variable} variable The variable to check. + * @param {Scope.Variable} variable The variable to check. * @returns {boolean} `true` if the variable is modified. */ function isModifiedGlobal(variable) { @@ -28,7 +29,7 @@ function isModifiedGlobal(variable) { /** * Check if the value of a given node is passed through to the parent syntax as-is. * For example, `a` and `b` in (`a || b` and `c ? a : b`) are passed through. - * @param {Node} node A node to check. + * @param {Rule.Node} node A node to check. * @returns {boolean} `true` if the node is passed through. */ function isPassThrough(node) { @@ -55,7 +56,7 @@ function isPassThrough(node) { export class ReferenceTracker { /** * Initialize this tracker. - * @param {Scope} globalScope The global scope. + * @param {Scope.Scope} globalScope The global scope. * @param {object} [options] The options. * @param {"legacy"|"strict"} [options.mode="strict"] The mode to determine the ImportDeclaration's behavior for CJS modules. * @param {string[]} [options.globalObjectNames=["global","globalThis","self","window"]] The variable names for Global Object. @@ -212,7 +213,7 @@ export class ReferenceTracker { /** * Iterate the references for a given variable. - * @param {Variable} variable The variable to iterate that references. + * @param {Scope.Variable} variable The variable to iterate that references. * @param {string[]} path The current path. * @param {object} traceMap The trace map. * @param {boolean} shouldReport = The flag to report those references. @@ -319,7 +320,7 @@ export class ReferenceTracker { /** * Iterate the references for a given Pattern node. - * @param {Node} patternNode The Pattern node to iterate references. + * @param {Rule.Node} patternNode The Pattern node to iterate references. * @param {string[]} path The current path. * @param {object} traceMap The trace map. * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. @@ -370,7 +371,7 @@ export class ReferenceTracker { /** * Iterate the references for a given ModuleSpecifier node. - * @param {Node} specifierNode The ModuleSpecifier node to iterate references. + * @param {Rule.Node} specifierNode The ModuleSpecifier node to iterate references. * @param {string[]} path The current path. * @param {object} traceMap The trace map. * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. diff --git a/src/token-predicate.js b/src/token-predicate.js index 3d155da..2d9a602 100644 --- a/src/token-predicate.js +++ b/src/token-predicate.js @@ -1,6 +1,8 @@ +import { Scope, Rule, SourceCode, AST } from "eslint" + /** * Negate the result of `this` calling. - * @param {Token} token The token to check. + * @param {AST.Token} token The token to check. * @returns {boolean} `true` if the result of `this(token)` is `false`. */ function negate0(token) { @@ -9,8 +11,8 @@ function negate0(token) { /** * Creates the negate function of the given function. - * @param {function(Token):boolean} f - The function to negate. - * @returns {function(Token):boolean} Negated function. + * @param {function(AST.Token):boolean} f - The function to negate. + * @returns {function(AST.Token):boolean} Negated function. */ function negate(f) { return negate0.bind(f) @@ -18,7 +20,7 @@ function negate(f) { /** * Checks if the given token is an arrow token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is an arrow token. */ export function isArrowToken(token) { @@ -27,7 +29,7 @@ export function isArrowToken(token) { /** * Checks if the given token is a comma token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is a comma token. */ export function isCommaToken(token) { @@ -36,7 +38,7 @@ export function isCommaToken(token) { /** * Checks if the given token is a semicolon token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is a semicolon token. */ export function isSemicolonToken(token) { @@ -45,7 +47,7 @@ export function isSemicolonToken(token) { /** * Checks if the given token is a colon token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is a colon token. */ export function isColonToken(token) { @@ -54,7 +56,7 @@ export function isColonToken(token) { /** * Checks if the given token is an opening parenthesis token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is an opening parenthesis token. */ export function isOpeningParenToken(token) { @@ -63,7 +65,7 @@ export function isOpeningParenToken(token) { /** * Checks if the given token is a closing parenthesis token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is a closing parenthesis token. */ export function isClosingParenToken(token) { @@ -72,7 +74,7 @@ export function isClosingParenToken(token) { /** * Checks if the given token is an opening square bracket token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is an opening square bracket token. */ export function isOpeningBracketToken(token) { @@ -81,7 +83,7 @@ export function isOpeningBracketToken(token) { /** * Checks if the given token is a closing square bracket token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is a closing square bracket token. */ export function isClosingBracketToken(token) { @@ -90,7 +92,7 @@ export function isClosingBracketToken(token) { /** * Checks if the given token is an opening brace token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is an opening brace token. */ export function isOpeningBraceToken(token) { @@ -99,7 +101,7 @@ export function isOpeningBraceToken(token) { /** * Checks if the given token is a closing brace token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is a closing brace token. */ export function isClosingBraceToken(token) { @@ -108,7 +110,7 @@ export function isClosingBraceToken(token) { /** * Checks if the given token is a comment token or not. - * @param {Token} token - The token to check. + * @param {AST.Token} token - The token to check. * @returns {boolean} `true` if the token is a comment token. */ export function isCommentToken(token) {