Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add declaration files #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/node_modules
/index.*
/test.*
/types
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions src/find-variable.js
Original file line number Diff line number Diff line change
@@ -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 = ""
Expand Down
7 changes: 4 additions & 3 deletions src/get-function-head-location.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/get-function-name-with-kind.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/get-innermost-scope.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/get-property-name.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions src/get-static-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* globals BigInt, globalThis, global, self, window */

import { Scope, Rule } from "eslint"

import { findVariable } from "./find-variable"

const globalObject =
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/get-string-if-constant.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/has-side-effect.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Rule, SourceCode } from "eslint"
import evk from "eslint-visitor-keys"

const typeConversionBinaryOps = Object.freeze(
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/is-parenthesized.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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.
*/
Expand Down
13 changes: 7 additions & 6 deletions src/reference-tracker.js
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 16 additions & 14 deletions src/token-predicate.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -9,16 +11,16 @@ 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)
}

/**
* 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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down