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: treat unknown nodes as having the lowest precedence #17302

Merged
merged 5 commits into from Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/rules/utils/ast-utils.js
Expand Up @@ -9,6 +9,7 @@
// Requirements
//------------------------------------------------------------------------------

const { KEYS: eslintVisitorKeys } = require("eslint-visitor-keys");
const esutils = require("esutils");
const espree = require("espree");
const escapeRegExp = require("escape-string-regexp");
Expand Down Expand Up @@ -1461,7 +1462,16 @@ module.exports = {
return 19;

default:
return 20;
if (node.type in eslintVisitorKeys) {
return 20;
}

/*
* if the node is not a standard node that we know about, then assume it has the lowest precedence
* this will mean that rules will wrap unknown nodes in parentheses where applicable instead of
* unwrapping them and potentially changing the meaning of the code or introducing a syntax error.
*/
return -1;
}
},

Expand Down
@@ -0,0 +1,333 @@
"use strict";

/**
* Parser: @typescript-eslint/parser 5.59.11 (TS 5.1.3)
* Source code:
* if (!Boolean(a as any)) {}
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
*/

exports.parse = () => ({
type: "Program",
body: [
{
type: "IfStatement",
test: {
type: "UnaryExpression",
operator: "!",
prefix: true,
argument: {
type: "CallExpression",
callee: {
type: "Identifier",
decorators: [],
name: "Boolean",
optional: false,
range: [5, 12],
loc: {
start: {
line: 1,
column: 5,
},
end: {
line: 1,
column: 12,
},
},
},
arguments: [
{
type: "TSAsExpression",
expression: {
type: "Identifier",
decorators: [],
name: "a",
optional: false,
range: [13, 14],
loc: {
start: {
line: 1,
column: 13,
},
end: {
line: 1,
column: 14,
},
},
},
typeAnnotation: {
type: "TSAnyKeyword",
range: [18, 21],
loc: {
start: {
line: 1,
column: 18,
},
end: {
line: 1,
column: 21,
},
},
},
range: [13, 21],
loc: {
start: {
line: 1,
column: 13,
},
end: {
line: 1,
column: 21,
},
},
},
],
optional: false,
range: [5, 22],
loc: {
start: {
line: 1,
column: 5,
},
end: {
line: 1,
column: 22,
},
},
},
range: [4, 22],
loc: {
start: {
line: 1,
column: 4,
},
end: {
line: 1,
column: 22,
},
},
},
consequent: {
type: "BlockStatement",
body: [],
range: [24, 27],
loc: {
start: {
line: 1,
column: 24,
},
end: {
line: 1,
column: 27,
},
},
},
alternate: null,
range: [0, 27],
loc: {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: 27,
},
},
},
],
comments: [],
range: [0, 28],
sourceType: "script",
tokens: [
{
type: "Keyword",
value: "if",
range: [0, 2],
loc: {
start: {
line: 1,
column: 0,
},
end: {
line: 1,
column: 2,
},
},
},
{
type: "Punctuator",
value: "(",
range: [3, 4],
loc: {
start: {
line: 1,
column: 3,
},
end: {
line: 1,
column: 4,
},
},
},
{
type: "Punctuator",
value: "!",
range: [4, 5],
loc: {
start: {
line: 1,
column: 4,
},
end: {
line: 1,
column: 5,
},
},
},
{
type: "Identifier",
value: "Boolean",
range: [5, 12],
loc: {
start: {
line: 1,
column: 5,
},
end: {
line: 1,
column: 12,
},
},
},
{
type: "Punctuator",
value: "(",
range: [12, 13],
loc: {
start: {
line: 1,
column: 12,
},
end: {
line: 1,
column: 13,
},
},
},
{
type: "Identifier",
value: "a",
range: [13, 14],
loc: {
start: {
line: 1,
column: 13,
},
end: {
line: 1,
column: 14,
},
},
},
{
type: "Identifier",
value: "as",
range: [15, 17],
loc: {
start: {
line: 1,
column: 15,
},
end: {
line: 1,
column: 17,
},
},
},
{
type: "Identifier",
value: "any",
range: [18, 21],
loc: {
start: {
line: 1,
column: 18,
},
end: {
line: 1,
column: 21,
},
},
},
{
type: "Punctuator",
value: ")",
range: [21, 22],
loc: {
start: {
line: 1,
column: 21,
},
end: {
line: 1,
column: 22,
},
},
},
{
type: "Punctuator",
value: ")",
range: [22, 23],
loc: {
start: {
line: 1,
column: 22,
},
end: {
line: 1,
column: 23,
},
},
},
{
type: "Punctuator",
value: "{",
range: [24, 25],
loc: {
start: {
line: 1,
column: 24,
},
end: {
line: 1,
column: 25,
},
},
},
{
type: "Punctuator",
value: "}",
range: [26, 27],
loc: {
start: {
line: 1,
column: 26,
},
end: {
line: 1,
column: 27,
},
},
},
],
loc: {
start: {
line: 1,
column: 0,
},
end: {
line: 2,
column: 0,
},
},
});