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

Supports for Optional chaining & Nullish coalescing #8

Merged
merged 5 commits into from
Jun 15, 2020
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"rimraf": "^3.0.0",
"rollup": "^1.25.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"semver": "^7.3.2",
"vuepress": "^1.2.0",
"warun": "^1.0.0"
},
Expand Down
69 changes: 45 additions & 24 deletions src/get-static-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,23 +251,34 @@ const operations = Object.freeze({
if (args != null) {
if (calleeNode.type === "MemberExpression") {
const object = getStaticValueR(calleeNode.object, initialScope)
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 (object != null) {
if (
object.value == null &&
(object.optional || node.optional)
) {
return { value: undefined, optional: true }
}
if (callPassThrough.has(receiver[methodName])) {
return { value: args[0] }
const property = calleeNode.computed
? getStaticValueR(calleeNode.property, initialScope)
: { value: calleeNode.property.name }

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] }
}
}
}
} else {
const callee = getStaticValueR(calleeNode, initialScope)
if (callee != null) {
if (callee.value == null && node.optional) {
return { value: undefined, optional: true }
}
const func = callee.value
if (callAllowed.has(func)) {
return { value: func(...args) }
Expand Down Expand Up @@ -340,7 +351,8 @@ const operations = Object.freeze({
if (left != null) {
if (
(node.operator === "||" && Boolean(left.value) === true) ||
(node.operator === "&&" && Boolean(left.value) === false)
(node.operator === "&&" && Boolean(left.value) === false) ||
(node.operator === "??" && left.value != null)
) {
return left
}
Expand All @@ -356,16 +368,25 @@ const operations = Object.freeze({

MemberExpression(node, initialScope) {
const object = getStaticValueR(node.object, initialScope)
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 (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 (property != null && !isGetter(object.value, property.value)) {
return { value: object.value[property.value] }
}
}
return null
},

ChainExpression(node, initialScope) {
const expression = getStaticValueR(node.expression, initialScope)
if (expression != null) {
return { value: expression.value }
}
return null
},
Expand Down Expand Up @@ -493,7 +514,7 @@ 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.
* @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`.
*/
function getStaticValueR(node, initialScope) {
if (node != null && Object.hasOwnProperty.call(operations, node.type)) {
Expand All @@ -506,7 +527,7 @@ 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.
* @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`.
*/
export function getStaticValue(node, initialScope = null) {
try {
Expand Down
17 changes: 15 additions & 2 deletions src/has-side-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const typeConversionBinaryOps = Object.freeze(
])
)
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(
Object.assign(Object.create(null), {
$visit(node, options, visitorKeys) {
Expand All @@ -44,13 +54,16 @@ const visitor = Object.freeze(
if (Array.isArray(value)) {
for (const element of value) {
if (
element &&
isNode(element) &&
this.$visit(element, options, visitorKeys)
) {
return true
}
}
} else if (value && this.$visit(value, options, visitorKeys)) {
} else if (
isNode(value) &&
this.$visit(value, options, visitorKeys)
) {
return true
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/reference-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function isPassThrough(node) {
return true
case "SequenceExpression":
return parent.expressions[parent.expressions.length - 1] === node
case "ChainExpression":
return true

default:
return false
Expand Down
103 changes: 102 additions & 1 deletion test/get-static-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "assert"
import eslint from "eslint"
import semver from "semver"
import { getStaticValue } from "../src/"

describe("The 'getStaticValue' function", () => {
Expand Down Expand Up @@ -143,6 +144,102 @@ const aMap = Object.freeze({
code: "RegExp.$1",
expected: null,
},
...(semver.gte(eslint.CLIEngine.version, "6.0.0")
? [
{
code: "const a = null, b = 42; a ?? b",
expected: { value: 42 },
},
{
code: "const a = undefined, b = 42; a ?? b",
expected: { value: 42 },
},
{
code: "const a = false, b = 42; a ?? b",
expected: { value: false },
},
{
code: "const a = 42, b = null; a ?? b",
expected: { value: 42 },
},
{
code: "const a = 42, b = undefined; a ?? b",
expected: { value: 42 },
},
{
code: "const a = { b: { c: 42 } }; a?.b?.c",
expected: { value: 42 },
},
{
code: "const a = { b: { c: 42 } }; a?.b?.['c']",
expected: { value: 42 },
},
{
code: "const a = { b: null }; a?.b?.c",
expected: { value: undefined },
},
{
code: "const a = { b: undefined }; a?.b?.c",
expected: { value: undefined },
},
{
code: "const a = { b: null }; a?.b?.['c']",
expected: { value: undefined },
},
{
code: "const a = null; a?.b?.c",
expected: { value: undefined },
},
{
code: "const a = null; a?.b.c",
expected: { value: undefined },
},
{
code: "const a = void 0; a?.b.c",
expected: { value: undefined },
},
{
code: "const a = { b: { c: 42 } }; (a?.b).c",
expected: { value: 42 },
},
{
code: "const a = null; (a?.b).c",
expected: null,
},
{
code: "const a = { b: null }; (a?.b).c",
expected: null,
},
{
code: "const a = { b: { c: String } }; a?.b?.c?.(42)",
expected: { value: "42" },
},
{
code: "const a = null; a?.b?.c?.(42)",
expected: { value: undefined },
},
{
code: "const a = { b: { c: String } }; a?.b.c(42)",
expected: { value: "42" },
},
{
code: "const a = null; a?.b.c(42)",
expected: { value: undefined },
},
{
code: "null?.()",
expected: { value: undefined },
},
{
code: "const a = null; a?.()",
expected: { value: undefined },
},
{
code: "a?.()",
expected: null,
},
]
: []),
]) {
it(`should return ${JSON.stringify(expected)} from ${code}`, () => {
const linter = new eslint.Linter()
Expand All @@ -158,7 +255,11 @@ const aMap = Object.freeze({
}))
linter.verify(code, {
env: { es6: true },
parserOptions: { ecmaVersion: 2018 },
parserOptions: {
ecmaVersion: semver.gte(eslint.CLIEngine.version, "6.0.0")
? 2020
: 2018,
},
rules: { test: "error" },
})

Expand Down
58 changes: 57 additions & 1 deletion test/has-side-effect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "assert"
import eslint from "eslint"
import semver from "semver"
import dp from "dot-prop"
import { hasSideEffect } from "../src/"

Expand Down Expand Up @@ -46,11 +47,29 @@ describe("The 'hasSideEffect' function", () => {
options: undefined,
expected: true,
},
...(semver.gte(eslint.CLIEngine.version, "6.0.0")
? [
{
code: "f?.()",
options: undefined,
expected: true,
},
]
: []),
{
code: "a + f()",
options: undefined,
expected: true,
},
...(semver.gte(eslint.CLIEngine.version, "6.0.0")
? [
{
code: "a + f?.()",
options: undefined,
expected: true,
},
]
: []),
{
code: "obj.a",
options: undefined,
Expand All @@ -61,6 +80,20 @@ describe("The 'hasSideEffect' function", () => {
options: { considerGetters: true },
expected: true,
},
...(semver.gte(eslint.CLIEngine.version, "6.0.0")
? [
{
code: "obj?.a",
options: undefined,
expected: false,
},
{
code: "obj?.a",
options: { considerGetters: true },
expected: true,
},
]
: []),
{
code: "obj[a]",
options: undefined,
Expand All @@ -76,6 +109,25 @@ describe("The 'hasSideEffect' function", () => {
options: { considerImplicitTypeConversion: true },
expected: true,
},
...(semver.gte(eslint.CLIEngine.version, "6.0.0")
? [
{
code: "obj?.[a]",
options: undefined,
expected: false,
},
{
code: "obj?.[a]",
options: { considerGetters: true },
expected: true,
},
{
code: "obj?.[a]",
options: { considerImplicitTypeConversion: true },
expected: true,
},
]
: []),
{
code: "obj[0]",
options: { considerImplicitTypeConversion: true },
Expand Down Expand Up @@ -242,7 +294,11 @@ describe("The 'hasSideEffect' function", () => {
}))
const messages = linter.verify(code, {
env: { es6: true },
parserOptions: { ecmaVersion: 2018 },
parserOptions: {
ecmaVersion: semver.gte(eslint.CLIEngine.version, "6.0.0")
? 2020
: 2018,
},
rules: { test: "error" },
})

Expand Down
Loading