From e40118b4cf9c42653b5bf3be937cf52e7d847e32 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Wed, 5 May 2021 14:44:10 +0900 Subject: [PATCH 1/3] feat: add type test --- .gitignore | 1 + index.d.ts | 12 ++++++++++++ package.json | 4 ++++ test/index.types.ts | 7 +++++++ 4 files changed, 24 insertions(+) create mode 100644 index.d.ts create mode 100644 test/index.types.ts diff --git a/.gitignore b/.gitignore index 0d8768b..ebb9536 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /docs/.vuepress/dist /node_modules /index.* +!/index.d.ts /test.* diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..b58c654 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,12 @@ +import * as ESLint from "eslint"; + +/** + * Find the variable of a given name. + * @param initialScope The scope to start finding. + * @param nameOrNode The variable name to find. If this is a Node object then it should be an Identifier node. + * @returns The found variable or null. + */ +export declare function findVariable( + initialScope: ESLint.Scope.Scope, + nameOrNode: string | ESLint.Rule.Node +): ESLint.Scope.Variable | null; diff --git a/package.json b/package.json index 661a97f..4a5c673 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,14 @@ "files": [ "index.*" ], + "types": "index.d.ts", "dependencies": { + "@types/eslint": "^7.2.10", "eslint-visitor-keys": "^1.1.0" }, "devDependencies": { "@mysticatea/eslint-plugin": "^12.0.0", + "check-dts": "^0.4.4", "codecov": "^3.6.1", "dot-prop": "^4.2.0", "eslint": "^6.5.1", @@ -43,6 +46,7 @@ "lint": "eslint src test", "test": "run-s lint build test:mocha", "test:mocha": "nyc mocha --reporter dot \"test/*.js\"", + "test:type": "check-dts", "preversion": "npm test && npm run -s build", "postversion": "git push && git push --tags", "prewatch": "npm run -s clean", diff --git a/test/index.types.ts b/test/index.types.ts new file mode 100644 index 0000000..703f4f8 --- /dev/null +++ b/test/index.types.ts @@ -0,0 +1,7 @@ +import * as ESLintUtils from "../"; +import * as ESLint from "eslint"; + +ESLintUtils.findVariable( + ("initialScope" as unknown) as ESLint.Scope.Scope, + "nameOrNode" as string +); From f547f704e3bfd5f11100277743d4c0ca0be22583 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Wed, 5 May 2021 18:02:06 +0900 Subject: [PATCH 2/3] refactor: use eslint types --- index.d.ts | 12 --- package.json | 2 - src/find-variable.js | 7 +- src/get-function-head-location.js | 7 +- src/get-function-name-with-kind.js | 3 +- src/get-innermost-scope.js | 6 +- src/get-property-name.js | 5 +- src/get-static-value.js | 14 +-- src/get-string-if-constant.js | 5 +- src/has-side-effect.js | 3 +- src/is-parenthesized.js | 7 +- src/reference-tracker.js | 13 +-- src/token-predicate.js | 30 ++++--- test/index.types.ts | 7 -- types/find-variable.d.ts | 9 ++ types/get-function-head-location.d.ts | 9 ++ types/get-function-name-with-kind.d.ts | 7 ++ types/get-innermost-scope.d.ts | 9 ++ types/get-property-name.d.ts | 9 ++ types/get-static-value.d.ts | 14 +++ types/get-string-if-constant.d.ts | 9 ++ types/has-side-effect.d.ts | 17 ++++ types/index.d.ts | 78 ++++++++++++++++ types/is-parenthesized.d.ts | 14 +++ types/pattern-matcher.d.ts | 32 +++++++ types/reference-tracker.d.ts | 118 +++++++++++++++++++++++++ types/token-predicate.d.ts | 78 ++++++++++++++++ 27 files changed, 460 insertions(+), 64 deletions(-) delete mode 100644 index.d.ts delete mode 100644 test/index.types.ts create mode 100644 types/find-variable.d.ts create mode 100644 types/get-function-head-location.d.ts create mode 100644 types/get-function-name-with-kind.d.ts create mode 100644 types/get-innermost-scope.d.ts create mode 100644 types/get-property-name.d.ts create mode 100644 types/get-static-value.d.ts create mode 100644 types/get-string-if-constant.d.ts create mode 100644 types/has-side-effect.d.ts create mode 100644 types/index.d.ts create mode 100644 types/is-parenthesized.d.ts create mode 100644 types/pattern-matcher.d.ts create mode 100644 types/reference-tracker.d.ts create mode 100644 types/token-predicate.d.ts diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b58c654..0000000 --- a/index.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import * as ESLint from "eslint"; - -/** - * Find the variable of a given name. - * @param initialScope The scope to start finding. - * @param nameOrNode The variable name to find. If this is a Node object then it should be an Identifier node. - * @returns The found variable or null. - */ -export declare function findVariable( - initialScope: ESLint.Scope.Scope, - nameOrNode: string | ESLint.Rule.Node -): ESLint.Scope.Variable | null; diff --git a/package.json b/package.json index 4a5c673..2e00ff6 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ }, "devDependencies": { "@mysticatea/eslint-plugin": "^12.0.0", - "check-dts": "^0.4.4", "codecov": "^3.6.1", "dot-prop": "^4.2.0", "eslint": "^6.5.1", @@ -46,7 +45,6 @@ "lint": "eslint src test", "test": "run-s lint build test:mocha", "test:mocha": "nyc mocha --reporter dot \"test/*.js\"", - "test:type": "check-dts", "preversion": "npm test && npm run -s build", "postversion": "git push && git push --tags", "prewatch": "npm run -s clean", 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) { diff --git a/test/index.types.ts b/test/index.types.ts deleted file mode 100644 index 703f4f8..0000000 --- a/test/index.types.ts +++ /dev/null @@ -1,7 +0,0 @@ -import * as ESLintUtils from "../"; -import * as ESLint from "eslint"; - -ESLintUtils.findVariable( - ("initialScope" as unknown) as ESLint.Scope.Scope, - "nameOrNode" as string -); diff --git a/types/find-variable.d.ts b/types/find-variable.d.ts new file mode 100644 index 0000000..ce07fe8 --- /dev/null +++ b/types/find-variable.d.ts @@ -0,0 +1,9 @@ +/** + * Find the variable of a given name. + * @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: Scope.Scope, nameOrNode: string | Rule.Node): Scope.Variable | null; +import { Scope } from "eslint"; +import { Rule } from "eslint"; diff --git a/types/get-function-head-location.d.ts b/types/get-function-head-location.d.ts new file mode 100644 index 0000000..79486ed --- /dev/null +++ b/types/get-function-head-location.d.ts @@ -0,0 +1,9 @@ +/** + * Get the location of the given function node for reporting. + * @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. + */ +export function getFunctionHeadLocation(node: Rule.Node, sourceCode: SourceCode): string; +import { Rule } from "eslint"; +import { SourceCode } from "eslint"; diff --git a/types/get-function-name-with-kind.d.ts b/types/get-function-name-with-kind.d.ts new file mode 100644 index 0000000..fa515e2 --- /dev/null +++ b/types/get-function-name-with-kind.d.ts @@ -0,0 +1,7 @@ +/** + * Get the name and kind of the given function node. + * @param {Rule.Node} node - The function node to get. + * @returns {string} The name and kind of the function node. + */ +export function getFunctionNameWithKind(node: Rule.Node): string; +import { Rule } from "eslint"; diff --git a/types/get-innermost-scope.d.ts b/types/get-innermost-scope.d.ts new file mode 100644 index 0000000..1384379 --- /dev/null +++ b/types/get-innermost-scope.d.ts @@ -0,0 +1,9 @@ +/** + * Get the innermost scope which contains a given location. + * @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: Scope.Scope, node: Rule.Node): any; +import { Scope } from "eslint"; +import { Rule } from "eslint"; diff --git a/types/get-property-name.d.ts b/types/get-property-name.d.ts new file mode 100644 index 0000000..73ed7dc --- /dev/null +++ b/types/get-property-name.d.ts @@ -0,0 +1,9 @@ +/** + * Get the property name from a MemberExpression node or a Property node. + * @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: Rule.Node, initialScope?: Scope.Scope): string | null; +import { Rule } from "eslint"; +import { Scope } from "eslint"; diff --git a/types/get-static-value.d.ts b/types/get-static-value.d.ts new file mode 100644 index 0000000..96fd061 --- /dev/null +++ b/types/get-static-value.d.ts @@ -0,0 +1,14 @@ +/** + * Get the value of a given node if it's a static value. + * @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: Rule.Node, initialScope?: Scope.Scope): { + value: any; +} | { + value: undefined; + optional?: true; +} | null; +import { Rule } from "eslint"; +import { Scope } from "eslint"; diff --git a/types/get-string-if-constant.d.ts b/types/get-string-if-constant.d.ts new file mode 100644 index 0000000..bc7490b --- /dev/null +++ b/types/get-string-if-constant.d.ts @@ -0,0 +1,9 @@ +/** + * Get the value of a given node if it's a literal or a template literal. + * @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: Rule.Node, initialScope?: Scope.Scope): string | null; +import { Rule } from "eslint"; +import { Scope } from "eslint"; diff --git a/types/has-side-effect.d.ts b/types/has-side-effect.d.ts new file mode 100644 index 0000000..0e1872a --- /dev/null +++ b/types/has-side-effect.d.ts @@ -0,0 +1,17 @@ +/** + * Check whether a given node has any side effect or not. + * @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. + * @param {boolean} [options.considerImplicitTypeConversion=false] If `true` then it considers implicit type conversion as the node which has side effects. + * @param {object} [options.visitorKeys=evk.KEYS] The keys to traverse nodes. Use `context.getSourceCode().visitorKeys`. + * @returns {boolean} `true` if the node has a certain side effect. + */ +export function hasSideEffect(node: Rule.Node, sourceCode: SourceCode, { considerGetters, considerImplicitTypeConversion }?: { + considerGetters?: boolean; + considerImplicitTypeConversion?: boolean; + visitorKeys?: object; +}): boolean; +import { Rule } from "eslint"; +import { SourceCode } from "eslint"; diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..d5c4246 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,78 @@ +declare namespace _default { + export { CALL }; + export { CONSTRUCT }; + export { ESM }; + export { findVariable }; + export { getFunctionHeadLocation }; + export { getFunctionNameWithKind }; + export { getInnermostScope }; + export { getPropertyName }; + export { getStaticValue }; + export { getStringIfConstant }; + export { hasSideEffect }; + export { isArrowToken }; + export { isClosingBraceToken }; + export { isClosingBracketToken }; + export { isClosingParenToken }; + export { isColonToken }; + export { isCommaToken }; + export { isCommentToken }; + export { isNotArrowToken }; + export { isNotClosingBraceToken }; + export { isNotClosingBracketToken }; + export { isNotClosingParenToken }; + export { isNotColonToken }; + export { isNotCommaToken }; + export { isNotCommentToken }; + export { isNotOpeningBraceToken }; + export { isNotOpeningBracketToken }; + export { isNotOpeningParenToken }; + export { isNotSemicolonToken }; + export { isOpeningBraceToken }; + export { isOpeningBracketToken }; + export { isOpeningParenToken }; + export { isParenthesized }; + export { isSemicolonToken }; + export { PatternMatcher }; + export { READ }; + export { ReferenceTracker }; +} +export default _default; +import { CALL } from "./reference-tracker"; +import { CONSTRUCT } from "./reference-tracker"; +import { ESM } from "./reference-tracker"; +import { findVariable } from "./find-variable"; +import { getFunctionHeadLocation } from "./get-function-head-location"; +import { getFunctionNameWithKind } from "./get-function-name-with-kind"; +import { getInnermostScope } from "./get-innermost-scope"; +import { getPropertyName } from "./get-property-name"; +import { getStaticValue } from "./get-static-value"; +import { getStringIfConstant } from "./get-string-if-constant"; +import { hasSideEffect } from "./has-side-effect"; +import { isArrowToken } from "./token-predicate"; +import { isClosingBraceToken } from "./token-predicate"; +import { isClosingBracketToken } from "./token-predicate"; +import { isClosingParenToken } from "./token-predicate"; +import { isColonToken } from "./token-predicate"; +import { isCommaToken } from "./token-predicate"; +import { isCommentToken } from "./token-predicate"; +import { isNotArrowToken } from "./token-predicate"; +import { isNotClosingBraceToken } from "./token-predicate"; +import { isNotClosingBracketToken } from "./token-predicate"; +import { isNotClosingParenToken } from "./token-predicate"; +import { isNotColonToken } from "./token-predicate"; +import { isNotCommaToken } from "./token-predicate"; +import { isNotCommentToken } from "./token-predicate"; +import { isNotOpeningBraceToken } from "./token-predicate"; +import { isNotOpeningBracketToken } from "./token-predicate"; +import { isNotOpeningParenToken } from "./token-predicate"; +import { isNotSemicolonToken } from "./token-predicate"; +import { isOpeningBraceToken } from "./token-predicate"; +import { isOpeningBracketToken } from "./token-predicate"; +import { isOpeningParenToken } from "./token-predicate"; +import { isParenthesized } from "./is-parenthesized"; +import { isSemicolonToken } from "./token-predicate"; +import { PatternMatcher } from "./pattern-matcher"; +import { READ } from "./reference-tracker"; +import { ReferenceTracker } from "./reference-tracker"; +export { CALL, CONSTRUCT, ESM, findVariable, getFunctionHeadLocation, getFunctionNameWithKind, getInnermostScope, getPropertyName, getStaticValue, getStringIfConstant, hasSideEffect, isArrowToken, isClosingBraceToken, isClosingBracketToken, isClosingParenToken, isColonToken, isCommaToken, isCommentToken, isNotArrowToken, isNotClosingBraceToken, isNotClosingBracketToken, isNotClosingParenToken, isNotColonToken, isNotCommaToken, isNotCommentToken, isNotOpeningBraceToken, isNotOpeningBracketToken, isNotOpeningParenToken, isNotSemicolonToken, isOpeningBraceToken, isOpeningBracketToken, isOpeningParenToken, isParenthesized, isSemicolonToken, PatternMatcher, READ, ReferenceTracker }; diff --git a/types/is-parenthesized.d.ts b/types/is-parenthesized.d.ts new file mode 100644 index 0000000..6ee70eb --- /dev/null +++ b/types/is-parenthesized.d.ts @@ -0,0 +1,14 @@ +/** + * Check whether a given node is parenthesized or not. + * @param {number} times The number of parantheses. + * @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 {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. + */ +export function isParenthesized(timesOrNode: any, nodeOrSourceCode: any, optionalSourceCode: any): boolean; diff --git a/types/pattern-matcher.d.ts b/types/pattern-matcher.d.ts new file mode 100644 index 0000000..0a6a1cd --- /dev/null +++ b/types/pattern-matcher.d.ts @@ -0,0 +1,32 @@ +/** + * The class to find patterns as considering escape sequences. + */ +export class PatternMatcher { + /** + * Initialize this matcher. + * @param {RegExp} pattern The pattern to match. + * @param {{escaped:boolean}} options The options. + */ + constructor(pattern: RegExp, { escaped }?: { + escaped: boolean; + }); + /** + * Find the pattern in a given string. + * @param {string} str The string to find. + * @returns {IterableIterator} The iterator which iterate the matched information. + */ + execAll(str: string): IterableIterator; + /** + * Check whether the pattern is found in a given string. + * @param {string} str The string to check. + * @returns {boolean} `true` if the pattern was found in the string. + */ + test(str: string): boolean; + /** + * Replace a given string. + * @param {string} str The string to be replaced. + * @param {(string|((...strs:string[])=>string))} replacer The string or function to replace. This is the same as the 2nd argument of `String.prototype.replace`. + * @returns {string} The replaced string. + */ + [Symbol.replace](str: string, replacer: string | ((...strs: string[]) => string)): string; +} diff --git a/types/reference-tracker.d.ts b/types/reference-tracker.d.ts new file mode 100644 index 0000000..992ea09 --- /dev/null +++ b/types/reference-tracker.d.ts @@ -0,0 +1,118 @@ +export const READ: unique symbol; +export const CALL: unique symbol; +export const CONSTRUCT: unique symbol; +export const ESM: unique symbol; +/** + * The reference tracker. + */ +export class ReferenceTracker { + /** + * Initialize this tracker. + * @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. + */ + constructor(globalScope: Scope.Scope, { mode, globalObjectNames, }?: { + mode?: "legacy" | "strict"; + globalObjectNames?: string[]; + }); + variableStack: any[]; + globalScope: Scope.Scope; + mode: "strict" | "legacy"; + globalObjectNames: string[]; + /** + * Iterate the references of global variables. + * @param {object} traceMap The trace map. + * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. + */ + iterateGlobalReferences(traceMap: object): IterableIterator<{ + node: Node; + path: string[]; + type: symbol; + info: any; + }>; + /** + * Iterate the references of CommonJS modules. + * @param {object} traceMap The trace map. + * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. + */ + iterateCjsReferences(traceMap: object): IterableIterator<{ + node: Node; + path: string[]; + type: symbol; + info: any; + }>; + /** + * Iterate the references of ES modules. + * @param {object} traceMap The trace map. + * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. + */ + iterateEsmReferences(traceMap: object): IterableIterator<{ + node: Node; + path: string[]; + type: symbol; + info: any; + }>; + /** + * Iterate the references for a given variable. + * @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. + * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. + */ + _iterateVariableReferences(variable: Scope.Variable, path: string[], traceMap: object, shouldReport: boolean): IterableIterator<{ + node: Node; + path: string[]; + type: symbol; + info: any; + }>; + /** + * Iterate the references for a given AST node. + * @param rootNode The AST 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. + */ + _iteratePropertyReferences(rootNode: any, path: string[], traceMap: object): IterableIterator<{ + node: Node; + path: string[]; + type: symbol; + info: any; + }>; + /** + * Iterate the references for a given Pattern node. + * @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. + */ + _iterateLhsReferences(patternNode: Rule.Node, path: string[], traceMap: object): IterableIterator<{ + node: Node; + path: string[]; + type: symbol; + info: any; + }>; + /** + * Iterate the references for a given ModuleSpecifier node. + * @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. + */ + _iterateImportReferences(specifierNode: Rule.Node, path: string[], traceMap: object): IterableIterator<{ + node: Node; + path: string[]; + type: symbol; + info: any; + }>; +} +export namespace ReferenceTracker { + export { READ }; + export { CALL }; + export { CONSTRUCT }; + export { ESM }; +} +import { Scope } from "eslint"; +import { Rule } from "eslint"; diff --git a/types/token-predicate.d.ts b/types/token-predicate.d.ts new file mode 100644 index 0000000..2ae134b --- /dev/null +++ b/types/token-predicate.d.ts @@ -0,0 +1,78 @@ +/** + * Checks if the given token is an arrow token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is an arrow token. + */ +export function isArrowToken(token: AST.Token): boolean; +/** + * Checks if the given token is a comma token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is a comma token. + */ +export function isCommaToken(token: AST.Token): boolean; +/** + * Checks if the given token is a semicolon token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is a semicolon token. + */ +export function isSemicolonToken(token: AST.Token): boolean; +/** + * Checks if the given token is a colon token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is a colon token. + */ +export function isColonToken(token: AST.Token): boolean; +/** + * Checks if the given token is an opening parenthesis token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is an opening parenthesis token. + */ +export function isOpeningParenToken(token: AST.Token): boolean; +/** + * Checks if the given token is a closing parenthesis token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is a closing parenthesis token. + */ +export function isClosingParenToken(token: AST.Token): boolean; +/** + * Checks if the given token is an opening square bracket token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is an opening square bracket token. + */ +export function isOpeningBracketToken(token: AST.Token): boolean; +/** + * Checks if the given token is a closing square bracket token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is a closing square bracket token. + */ +export function isClosingBracketToken(token: AST.Token): boolean; +/** + * Checks if the given token is an opening brace token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is an opening brace token. + */ +export function isOpeningBraceToken(token: AST.Token): boolean; +/** + * Checks if the given token is a closing brace token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is a closing brace token. + */ +export function isClosingBraceToken(token: AST.Token): boolean; +/** + * Checks if the given token is a comment token or not. + * @param {AST.Token} token - The token to check. + * @returns {boolean} `true` if the token is a comment token. + */ +export function isCommentToken(token: AST.Token): boolean; +export function isNotArrowToken(arg0: AST.Token): boolean; +export function isNotCommaToken(arg0: AST.Token): boolean; +export function isNotSemicolonToken(arg0: AST.Token): boolean; +export function isNotColonToken(arg0: AST.Token): boolean; +export function isNotOpeningParenToken(arg0: AST.Token): boolean; +export function isNotClosingParenToken(arg0: AST.Token): boolean; +export function isNotOpeningBracketToken(arg0: AST.Token): boolean; +export function isNotClosingBracketToken(arg0: AST.Token): boolean; +export function isNotOpeningBraceToken(arg0: AST.Token): boolean; +export function isNotClosingBraceToken(arg0: AST.Token): boolean; +export function isNotCommentToken(arg0: AST.Token): boolean; +import { AST } from "eslint"; From 01acd01c00d0ec974e0dd04b64ef396e1d4e3f59 Mon Sep 17 00:00:00 2001 From: tyankatsu Date: Wed, 5 May 2021 18:56:00 +0900 Subject: [PATCH 3/3] feat: build declaration --- .github/workflows/CI.yml | 2 + .gitignore | 2 +- package.json | 7 +- types/find-variable.d.ts | 9 -- types/get-function-head-location.d.ts | 9 -- types/get-function-name-with-kind.d.ts | 7 -- types/get-innermost-scope.d.ts | 9 -- types/get-property-name.d.ts | 9 -- types/get-static-value.d.ts | 14 --- types/get-string-if-constant.d.ts | 9 -- types/has-side-effect.d.ts | 17 ---- types/index.d.ts | 78 ---------------- types/is-parenthesized.d.ts | 14 --- types/pattern-matcher.d.ts | 32 ------- types/reference-tracker.d.ts | 118 ------------------------- types/token-predicate.d.ts | 78 ---------------- 16 files changed, 8 insertions(+), 406 deletions(-) delete mode 100644 types/find-variable.d.ts delete mode 100644 types/get-function-head-location.d.ts delete mode 100644 types/get-function-name-with-kind.d.ts delete mode 100644 types/get-innermost-scope.d.ts delete mode 100644 types/get-property-name.d.ts delete mode 100644 types/get-static-value.d.ts delete mode 100644 types/get-string-if-constant.d.ts delete mode 100644 types/has-side-effect.d.ts delete mode 100644 types/index.d.ts delete mode 100644 types/is-parenthesized.d.ts delete mode 100644 types/pattern-matcher.d.ts delete mode 100644 types/reference-tracker.d.ts delete mode 100644 types/token-predicate.d.ts 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 ebb9536..b7a0a56 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ /docs/.vuepress/dist /node_modules /index.* -!/index.d.ts /test.* +/types \ No newline at end of file diff --git a/package.json b/package.json index 2e00ff6..bb431d6 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,10 @@ "main": "index", "module": "index.mjs", "files": [ - "index.*" + "index.*", + "types" ], - "types": "index.d.ts", + "types": "types/index.d.ts", "dependencies": { "@types/eslint": "^7.2.10", "eslint-visitor-keys": "^1.1.0" @@ -31,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/types/find-variable.d.ts b/types/find-variable.d.ts deleted file mode 100644 index ce07fe8..0000000 --- a/types/find-variable.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Find the variable of a given name. - * @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: Scope.Scope, nameOrNode: string | Rule.Node): Scope.Variable | null; -import { Scope } from "eslint"; -import { Rule } from "eslint"; diff --git a/types/get-function-head-location.d.ts b/types/get-function-head-location.d.ts deleted file mode 100644 index 79486ed..0000000 --- a/types/get-function-head-location.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Get the location of the given function node for reporting. - * @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. - */ -export function getFunctionHeadLocation(node: Rule.Node, sourceCode: SourceCode): string; -import { Rule } from "eslint"; -import { SourceCode } from "eslint"; diff --git a/types/get-function-name-with-kind.d.ts b/types/get-function-name-with-kind.d.ts deleted file mode 100644 index fa515e2..0000000 --- a/types/get-function-name-with-kind.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Get the name and kind of the given function node. - * @param {Rule.Node} node - The function node to get. - * @returns {string} The name and kind of the function node. - */ -export function getFunctionNameWithKind(node: Rule.Node): string; -import { Rule } from "eslint"; diff --git a/types/get-innermost-scope.d.ts b/types/get-innermost-scope.d.ts deleted file mode 100644 index 1384379..0000000 --- a/types/get-innermost-scope.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Get the innermost scope which contains a given location. - * @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: Scope.Scope, node: Rule.Node): any; -import { Scope } from "eslint"; -import { Rule } from "eslint"; diff --git a/types/get-property-name.d.ts b/types/get-property-name.d.ts deleted file mode 100644 index 73ed7dc..0000000 --- a/types/get-property-name.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Get the property name from a MemberExpression node or a Property node. - * @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: Rule.Node, initialScope?: Scope.Scope): string | null; -import { Rule } from "eslint"; -import { Scope } from "eslint"; diff --git a/types/get-static-value.d.ts b/types/get-static-value.d.ts deleted file mode 100644 index 96fd061..0000000 --- a/types/get-static-value.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Get the value of a given node if it's a static value. - * @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: Rule.Node, initialScope?: Scope.Scope): { - value: any; -} | { - value: undefined; - optional?: true; -} | null; -import { Rule } from "eslint"; -import { Scope } from "eslint"; diff --git a/types/get-string-if-constant.d.ts b/types/get-string-if-constant.d.ts deleted file mode 100644 index bc7490b..0000000 --- a/types/get-string-if-constant.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Get the value of a given node if it's a literal or a template literal. - * @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: Rule.Node, initialScope?: Scope.Scope): string | null; -import { Rule } from "eslint"; -import { Scope } from "eslint"; diff --git a/types/has-side-effect.d.ts b/types/has-side-effect.d.ts deleted file mode 100644 index 0e1872a..0000000 --- a/types/has-side-effect.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Check whether a given node has any side effect or not. - * @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. - * @param {boolean} [options.considerImplicitTypeConversion=false] If `true` then it considers implicit type conversion as the node which has side effects. - * @param {object} [options.visitorKeys=evk.KEYS] The keys to traverse nodes. Use `context.getSourceCode().visitorKeys`. - * @returns {boolean} `true` if the node has a certain side effect. - */ -export function hasSideEffect(node: Rule.Node, sourceCode: SourceCode, { considerGetters, considerImplicitTypeConversion }?: { - considerGetters?: boolean; - considerImplicitTypeConversion?: boolean; - visitorKeys?: object; -}): boolean; -import { Rule } from "eslint"; -import { SourceCode } from "eslint"; diff --git a/types/index.d.ts b/types/index.d.ts deleted file mode 100644 index d5c4246..0000000 --- a/types/index.d.ts +++ /dev/null @@ -1,78 +0,0 @@ -declare namespace _default { - export { CALL }; - export { CONSTRUCT }; - export { ESM }; - export { findVariable }; - export { getFunctionHeadLocation }; - export { getFunctionNameWithKind }; - export { getInnermostScope }; - export { getPropertyName }; - export { getStaticValue }; - export { getStringIfConstant }; - export { hasSideEffect }; - export { isArrowToken }; - export { isClosingBraceToken }; - export { isClosingBracketToken }; - export { isClosingParenToken }; - export { isColonToken }; - export { isCommaToken }; - export { isCommentToken }; - export { isNotArrowToken }; - export { isNotClosingBraceToken }; - export { isNotClosingBracketToken }; - export { isNotClosingParenToken }; - export { isNotColonToken }; - export { isNotCommaToken }; - export { isNotCommentToken }; - export { isNotOpeningBraceToken }; - export { isNotOpeningBracketToken }; - export { isNotOpeningParenToken }; - export { isNotSemicolonToken }; - export { isOpeningBraceToken }; - export { isOpeningBracketToken }; - export { isOpeningParenToken }; - export { isParenthesized }; - export { isSemicolonToken }; - export { PatternMatcher }; - export { READ }; - export { ReferenceTracker }; -} -export default _default; -import { CALL } from "./reference-tracker"; -import { CONSTRUCT } from "./reference-tracker"; -import { ESM } from "./reference-tracker"; -import { findVariable } from "./find-variable"; -import { getFunctionHeadLocation } from "./get-function-head-location"; -import { getFunctionNameWithKind } from "./get-function-name-with-kind"; -import { getInnermostScope } from "./get-innermost-scope"; -import { getPropertyName } from "./get-property-name"; -import { getStaticValue } from "./get-static-value"; -import { getStringIfConstant } from "./get-string-if-constant"; -import { hasSideEffect } from "./has-side-effect"; -import { isArrowToken } from "./token-predicate"; -import { isClosingBraceToken } from "./token-predicate"; -import { isClosingBracketToken } from "./token-predicate"; -import { isClosingParenToken } from "./token-predicate"; -import { isColonToken } from "./token-predicate"; -import { isCommaToken } from "./token-predicate"; -import { isCommentToken } from "./token-predicate"; -import { isNotArrowToken } from "./token-predicate"; -import { isNotClosingBraceToken } from "./token-predicate"; -import { isNotClosingBracketToken } from "./token-predicate"; -import { isNotClosingParenToken } from "./token-predicate"; -import { isNotColonToken } from "./token-predicate"; -import { isNotCommaToken } from "./token-predicate"; -import { isNotCommentToken } from "./token-predicate"; -import { isNotOpeningBraceToken } from "./token-predicate"; -import { isNotOpeningBracketToken } from "./token-predicate"; -import { isNotOpeningParenToken } from "./token-predicate"; -import { isNotSemicolonToken } from "./token-predicate"; -import { isOpeningBraceToken } from "./token-predicate"; -import { isOpeningBracketToken } from "./token-predicate"; -import { isOpeningParenToken } from "./token-predicate"; -import { isParenthesized } from "./is-parenthesized"; -import { isSemicolonToken } from "./token-predicate"; -import { PatternMatcher } from "./pattern-matcher"; -import { READ } from "./reference-tracker"; -import { ReferenceTracker } from "./reference-tracker"; -export { CALL, CONSTRUCT, ESM, findVariable, getFunctionHeadLocation, getFunctionNameWithKind, getInnermostScope, getPropertyName, getStaticValue, getStringIfConstant, hasSideEffect, isArrowToken, isClosingBraceToken, isClosingBracketToken, isClosingParenToken, isColonToken, isCommaToken, isCommentToken, isNotArrowToken, isNotClosingBraceToken, isNotClosingBracketToken, isNotClosingParenToken, isNotColonToken, isNotCommaToken, isNotCommentToken, isNotOpeningBraceToken, isNotOpeningBracketToken, isNotOpeningParenToken, isNotSemicolonToken, isOpeningBraceToken, isOpeningBracketToken, isOpeningParenToken, isParenthesized, isSemicolonToken, PatternMatcher, READ, ReferenceTracker }; diff --git a/types/is-parenthesized.d.ts b/types/is-parenthesized.d.ts deleted file mode 100644 index 6ee70eb..0000000 --- a/types/is-parenthesized.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Check whether a given node is parenthesized or not. - * @param {number} times The number of parantheses. - * @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 {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. - */ -export function isParenthesized(timesOrNode: any, nodeOrSourceCode: any, optionalSourceCode: any): boolean; diff --git a/types/pattern-matcher.d.ts b/types/pattern-matcher.d.ts deleted file mode 100644 index 0a6a1cd..0000000 --- a/types/pattern-matcher.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * The class to find patterns as considering escape sequences. - */ -export class PatternMatcher { - /** - * Initialize this matcher. - * @param {RegExp} pattern The pattern to match. - * @param {{escaped:boolean}} options The options. - */ - constructor(pattern: RegExp, { escaped }?: { - escaped: boolean; - }); - /** - * Find the pattern in a given string. - * @param {string} str The string to find. - * @returns {IterableIterator} The iterator which iterate the matched information. - */ - execAll(str: string): IterableIterator; - /** - * Check whether the pattern is found in a given string. - * @param {string} str The string to check. - * @returns {boolean} `true` if the pattern was found in the string. - */ - test(str: string): boolean; - /** - * Replace a given string. - * @param {string} str The string to be replaced. - * @param {(string|((...strs:string[])=>string))} replacer The string or function to replace. This is the same as the 2nd argument of `String.prototype.replace`. - * @returns {string} The replaced string. - */ - [Symbol.replace](str: string, replacer: string | ((...strs: string[]) => string)): string; -} diff --git a/types/reference-tracker.d.ts b/types/reference-tracker.d.ts deleted file mode 100644 index 992ea09..0000000 --- a/types/reference-tracker.d.ts +++ /dev/null @@ -1,118 +0,0 @@ -export const READ: unique symbol; -export const CALL: unique symbol; -export const CONSTRUCT: unique symbol; -export const ESM: unique symbol; -/** - * The reference tracker. - */ -export class ReferenceTracker { - /** - * Initialize this tracker. - * @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. - */ - constructor(globalScope: Scope.Scope, { mode, globalObjectNames, }?: { - mode?: "legacy" | "strict"; - globalObjectNames?: string[]; - }); - variableStack: any[]; - globalScope: Scope.Scope; - mode: "strict" | "legacy"; - globalObjectNames: string[]; - /** - * Iterate the references of global variables. - * @param {object} traceMap The trace map. - * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. - */ - iterateGlobalReferences(traceMap: object): IterableIterator<{ - node: Node; - path: string[]; - type: symbol; - info: any; - }>; - /** - * Iterate the references of CommonJS modules. - * @param {object} traceMap The trace map. - * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. - */ - iterateCjsReferences(traceMap: object): IterableIterator<{ - node: Node; - path: string[]; - type: symbol; - info: any; - }>; - /** - * Iterate the references of ES modules. - * @param {object} traceMap The trace map. - * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. - */ - iterateEsmReferences(traceMap: object): IterableIterator<{ - node: Node; - path: string[]; - type: symbol; - info: any; - }>; - /** - * Iterate the references for a given variable. - * @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. - * @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}>} The iterator to iterate references. - */ - _iterateVariableReferences(variable: Scope.Variable, path: string[], traceMap: object, shouldReport: boolean): IterableIterator<{ - node: Node; - path: string[]; - type: symbol; - info: any; - }>; - /** - * Iterate the references for a given AST node. - * @param rootNode The AST 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. - */ - _iteratePropertyReferences(rootNode: any, path: string[], traceMap: object): IterableIterator<{ - node: Node; - path: string[]; - type: symbol; - info: any; - }>; - /** - * Iterate the references for a given Pattern node. - * @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. - */ - _iterateLhsReferences(patternNode: Rule.Node, path: string[], traceMap: object): IterableIterator<{ - node: Node; - path: string[]; - type: symbol; - info: any; - }>; - /** - * Iterate the references for a given ModuleSpecifier node. - * @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. - */ - _iterateImportReferences(specifierNode: Rule.Node, path: string[], traceMap: object): IterableIterator<{ - node: Node; - path: string[]; - type: symbol; - info: any; - }>; -} -export namespace ReferenceTracker { - export { READ }; - export { CALL }; - export { CONSTRUCT }; - export { ESM }; -} -import { Scope } from "eslint"; -import { Rule } from "eslint"; diff --git a/types/token-predicate.d.ts b/types/token-predicate.d.ts deleted file mode 100644 index 2ae134b..0000000 --- a/types/token-predicate.d.ts +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Checks if the given token is an arrow token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is an arrow token. - */ -export function isArrowToken(token: AST.Token): boolean; -/** - * Checks if the given token is a comma token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is a comma token. - */ -export function isCommaToken(token: AST.Token): boolean; -/** - * Checks if the given token is a semicolon token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is a semicolon token. - */ -export function isSemicolonToken(token: AST.Token): boolean; -/** - * Checks if the given token is a colon token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is a colon token. - */ -export function isColonToken(token: AST.Token): boolean; -/** - * Checks if the given token is an opening parenthesis token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is an opening parenthesis token. - */ -export function isOpeningParenToken(token: AST.Token): boolean; -/** - * Checks if the given token is a closing parenthesis token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is a closing parenthesis token. - */ -export function isClosingParenToken(token: AST.Token): boolean; -/** - * Checks if the given token is an opening square bracket token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is an opening square bracket token. - */ -export function isOpeningBracketToken(token: AST.Token): boolean; -/** - * Checks if the given token is a closing square bracket token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is a closing square bracket token. - */ -export function isClosingBracketToken(token: AST.Token): boolean; -/** - * Checks if the given token is an opening brace token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is an opening brace token. - */ -export function isOpeningBraceToken(token: AST.Token): boolean; -/** - * Checks if the given token is a closing brace token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is a closing brace token. - */ -export function isClosingBraceToken(token: AST.Token): boolean; -/** - * Checks if the given token is a comment token or not. - * @param {AST.Token} token - The token to check. - * @returns {boolean} `true` if the token is a comment token. - */ -export function isCommentToken(token: AST.Token): boolean; -export function isNotArrowToken(arg0: AST.Token): boolean; -export function isNotCommaToken(arg0: AST.Token): boolean; -export function isNotSemicolonToken(arg0: AST.Token): boolean; -export function isNotColonToken(arg0: AST.Token): boolean; -export function isNotOpeningParenToken(arg0: AST.Token): boolean; -export function isNotClosingParenToken(arg0: AST.Token): boolean; -export function isNotOpeningBracketToken(arg0: AST.Token): boolean; -export function isNotClosingBracketToken(arg0: AST.Token): boolean; -export function isNotOpeningBraceToken(arg0: AST.Token): boolean; -export function isNotClosingBraceToken(arg0: AST.Token): boolean; -export function isNotCommentToken(arg0: AST.Token): boolean; -import { AST } from "eslint";