Skip to content

Improved allowTestedProperty option to allow more tested properties #257

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

Open
wants to merge 2 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
87 changes: 59 additions & 28 deletions lib/util/type-checker/property-guards.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const TS_NODE_TYPES = [
* @typedef {import("estree").ReturnStatement} ReturnStatement
* @typedef {import("estree").ContinueStatement} ContinueStatement
* @typedef {import("estree").BreakStatement} BreakStatement
* @typedef {import("estree").IfStatement} IfStatement
* @typedef {import("estree").Node} Node
* @typedef {import("eslint").SourceCode} SourceCode
* @typedef {import("eslint").Rule.RuleContext} RuleContext
Expand All @@ -50,7 +51,7 @@ module.exports = { createPropertyGuardsContext }
/**
* @typedef {object} GuardChecker
* @property {(node: MemberExpression|Property)=>boolean} test
* @property {"instanceof"|"definedValue"|"definedType"|"hasValue"|"unknown"} kind
* @property {"instanceof"|"definedValue"|"definedType"|"hasValue"|"optional"|"unknown"} kind
*/
/**
* @typedef {object} MaybeGuard
Expand Down Expand Up @@ -251,6 +252,15 @@ function createPropertyGuardsContext(options) {
}
return null
}
if (
((parent.type === "CallExpression" && parent.callee === node) ||
(parent.type === "MemberExpression" &&
parent.object === node)) &&
parent.optional
) {
// e.g. x.property?.()
return { test: (n) => n === node, kind: "optional" }
}

if (
propertyTypes.every(
Expand Down Expand Up @@ -289,36 +299,54 @@ function createPropertyGuardsContext(options) {
return getGuardCheckerForExpression(parent, { not: !not })
}
if (parent.type === "IfStatement" && parent.test === node) {
if (!not) {
const block = parent.consequent
return (n) =>
block.range[0] <= n.range[0] && n.range[1] <= block.range[1]
}
// e.g. if (typeof x.property === 'undefined')
if (parent.alternate) {
const block = parent.alternate
return (n) =>
block.range[0] <= n.range[0] && n.range[1] <= block.range[1]
}
if (!hasJumpStatementInAllPath(parent.consequent)) {
return null
}
/** @type {Node|null} */
const pp = getParent(parent)
if (
!pp ||
(pp.type !== "BlockStatement" && pp.type !== "Program")
) {
return null
}
const start = parent.range[1]
const end = pp.range[1]

return (n) => start <= n.range[0] && n.range[1] <= end
return getGuardCheckerForIfStatement(parent, { not })
}
if (
!not &&
parent.type === "LogicalExpression" &&
parent.operator === "&&" &&
parent.left === node
) {
// e.g. typeof x.property !== 'undefined' && x.property
const block = parent.right
return (n) =>
block.range[0] <= n.range[0] && n.range[1] <= block.range[1]
}
return null
}

/**
* @param {IfStatement} node
* @returns {((node: MemberExpression|Property)=>boolean)|null} The guard tester.
*/
function getGuardCheckerForIfStatement(node, { not = false } = {}) {
if (!not) {
const block = node.consequent
return (n) =>
block.range[0] <= n.range[0] && n.range[1] <= block.range[1]
}
if (node.alternate) {
const block = node.alternate
return (n) =>
block.range[0] <= n.range[0] && n.range[1] <= block.range[1]
}
if (!hasJumpStatementInAllPath(node.consequent)) {
return null
}
/** @type {Node|null} */
const parent = getParent(node)
if (
!parent ||
(parent.type !== "BlockStatement" && parent.type !== "Program")
) {
return null
}
const start = node.range[1]
const end = parent.range[1]

return (n) => start <= n.range[0] && n.range[1] <= end
}

/**
* @param {MemberExpression|Property} node
* @returns {GuardChecker|null} The guard checker.
Expand Down Expand Up @@ -421,7 +449,10 @@ function createPropertyGuardsContext(options) {
const guard = {
...params,
prototypeGuard: isPrototypePropertyAccess(params.node),
used: false,
used:
// A optional chain allows the property access expression of its own.
// but we mark expressions that are candidates for guarding as used here because we won't check them later.
checker.kind === "optional",
isAvailableLocation: checker.test,
}
let classGuards = maybeGuards.get(params.className)
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-array-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ new RuleTester().run("no-array-from", rule, {
code: "if (Array.from) { const {from} = Array }",
options: [{ allowTestedProperty: true }],
},
{
code: "typeof Array.from !== 'undefined' && Array.from",
options: [{ allowTestedProperty: true }],
},
{
code: "Array.from && Array.from(it)",
options: [{ allowTestedProperty: true }],
},
{
code: "Array.from?.(it)",
options: [{ allowTestedProperty: true }],
},
],
invalid: [
{
Expand All @@ -47,5 +59,10 @@ new RuleTester().run("no-array-from", rule, {
options: [{ allowTestedProperty: true }],
errors: ["ES2015 'Array.from' method is forbidden."],
},
{
code: "Array?.from(it)",
options: [{ allowTestedProperty: true }],
errors: ["ES2015 'Array.from' method is forbidden."],
},
],
})
15 changes: 14 additions & 1 deletion tests/lib/rules/no-number-epsilon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-number-epsilon.js")

new RuleTester().run("no-number-epsilon", rule, {
valid: ["Number", "Number.xyz", "let Number = 0; Number.EPSILON"],
valid: [
"Number",
"Number.xyz",
"let Number = 0; Number.EPSILON",
{
code: "Number.EPSILON?.toString()",
options: [{ allowTestedProperty: true }],
},
],
invalid: [
{
code: "Number.EPSILON",
errors: ["ES2015 'Number.EPSILON' property is forbidden."],
},
{
code: "Number.EPSILON.toString?.()",
options: [{ allowTestedProperty: true }],
errors: ["ES2015 'Number.EPSILON' property is forbidden."],
},
],
})