eslint-utils
Advanced tools
Comparing version 2.0.0 to 2.1.0
103
index.js
@@ -494,15 +494,23 @@ /*! @author Toru Nagashima <https://github.com/mysticatea> */ | ||
const object = getStaticValueR(calleeNode.object, initialScope); | ||
const property = calleeNode.computed | ||
? getStaticValueR(calleeNode.property, initialScope) | ||
: { value: calleeNode.property.name }; | ||
if (object != null) { | ||
if ( | ||
object.value == null && | ||
(object.optional || node.optional) | ||
) { | ||
return { value: undefined, optional: true } | ||
} | ||
const property = calleeNode.computed | ||
? getStaticValueR(calleeNode.property, initialScope) | ||
: { value: calleeNode.property.name }; | ||
if (object != null && property != null) { | ||
const receiver = object.value; | ||
const methodName = property.value; | ||
if (callAllowed.has(receiver[methodName])) { | ||
return { value: receiver[methodName](...args) } | ||
if (property != null) { | ||
const receiver = object.value; | ||
const methodName = property.value; | ||
if (callAllowed.has(receiver[methodName])) { | ||
return { value: receiver[methodName](...args) } | ||
} | ||
if (callPassThrough.has(receiver[methodName])) { | ||
return { value: args[0] } | ||
} | ||
} | ||
if (callPassThrough.has(receiver[methodName])) { | ||
return { value: args[0] } | ||
} | ||
} | ||
@@ -512,2 +520,5 @@ } else { | ||
if (callee != null) { | ||
if (callee.value == null && node.optional) { | ||
return { value: undefined, optional: true } | ||
} | ||
const func = callee.value; | ||
@@ -585,3 +596,4 @@ if (callAllowed.has(func)) { | ||
(node.operator === "||" && Boolean(left.value) === true) || | ||
(node.operator === "&&" && Boolean(left.value) === false) | ||
(node.operator === "&&" && Boolean(left.value) === false) || | ||
(node.operator === "??" && left.value != null) | ||
) { | ||
@@ -602,12 +614,13 @@ return left | ||
const object = getStaticValueR(node.object, initialScope); | ||
const property = node.computed | ||
? getStaticValueR(node.property, initialScope) | ||
: { value: node.property.name }; | ||
if (object != null) { | ||
if (object.value == null && (object.optional || node.optional)) { | ||
return { value: undefined, optional: true } | ||
} | ||
const property = node.computed | ||
? getStaticValueR(node.property, initialScope) | ||
: { value: node.property.name }; | ||
if ( | ||
object != null && | ||
property != null && | ||
!isGetter(object.value, property.value) | ||
) { | ||
return { value: object.value[property.value] } | ||
if (property != null && !isGetter(object.value, property.value)) { | ||
return { value: object.value[property.value] } | ||
} | ||
} | ||
@@ -617,2 +630,10 @@ return null | ||
ChainExpression(node, initialScope) { | ||
const expression = getStaticValueR(node.expression, initialScope); | ||
if (expression != null) { | ||
return { value: expression.value } | ||
} | ||
return null | ||
}, | ||
NewExpression(node, initialScope) { | ||
@@ -741,3 +762,3 @@ const callee = getStaticValueR(node.callee, initialScope); | ||
* @param {Scope|undefined} initialScope The scope to start finding variable. | ||
* @returns {{value:any}|null} The static value of the node, or `null`. | ||
* @returns {{value:any}|{value:undefined,optional?:true}|null} The static value of the node, or `null`. | ||
*/ | ||
@@ -755,3 +776,3 @@ function getStaticValueR(node, initialScope) { | ||
* @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. | ||
* @returns {{value:any}|null} The static value of the node, or `null`. | ||
* @returns {{value:any}|{value:undefined,optional?:true}|null} The static value of the node, or `null`. | ||
*/ | ||
@@ -866,2 +887,19 @@ function getStaticValue(node, initialScope = null) { | ||
if (node.type === "ArrowFunctionExpression") { | ||
if ( | ||
parent.type === "VariableDeclarator" && | ||
parent.id && | ||
parent.id.type === "Identifier" | ||
) { | ||
tokens.push(`'${parent.id.name}'`); | ||
} | ||
if ( | ||
parent.type === "AssignmentExpression" && | ||
parent.left && | ||
parent.left.type === "Identifier" | ||
) { | ||
tokens.push(`'${parent.left.name}'`); | ||
} | ||
} | ||
return tokens.join(" ") | ||
@@ -893,2 +931,12 @@ } | ||
const typeConversionUnaryOps = Object.freeze(new Set(["-", "+", "!", "~"])); | ||
/** | ||
* Check whether the given value is an ASTNode or not. | ||
* @param {any} x The value to check. | ||
* @returns {boolean} `true` if the value is an ASTNode. | ||
*/ | ||
function isNode(x) { | ||
return x !== null && typeof x === "object" && typeof x.type === "string" | ||
} | ||
const visitor = Object.freeze( | ||
@@ -915,3 +963,3 @@ Object.assign(Object.create(null), { | ||
if ( | ||
element && | ||
isNode(element) && | ||
this.$visit(element, options, visitorKeys) | ||
@@ -922,3 +970,6 @@ ) { | ||
} | ||
} else if (value && this.$visit(value, options, visitorKeys)) { | ||
} else if ( | ||
isNode(value) && | ||
this.$visit(value, options, visitorKeys) | ||
) { | ||
return true | ||
@@ -1351,2 +1402,4 @@ } | ||
return parent.expressions[parent.expressions.length - 1] === node | ||
case "ChainExpression": | ||
return true | ||
@@ -1353,0 +1406,0 @@ default: |
{ | ||
"name": "eslint-utils", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Utilities for ESLint plugins.", | ||
@@ -31,2 +31,3 @@ "engines": { | ||
"rollup-plugin-sourcemaps": "^0.4.2", | ||
"semver": "^7.3.2", | ||
"vuepress": "^1.2.0", | ||
@@ -63,3 +64,4 @@ "warun": "^1.0.0" | ||
}, | ||
"homepage": "https://github.com/mysticatea/eslint-utils#readme" | ||
"homepage": "https://github.com/mysticatea/eslint-utils#readme", | ||
"funding": "https://github.com/sponsors/mysticatea" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
341971
3373
16