Skip to content
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
7 changes: 6 additions & 1 deletion src/is-parenthesized.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isClosingParenToken, isOpeningParenToken } from "./token-predicate.mjs"
/** @typedef {import("estree").Node} Node */
/** @typedef {import("@typescript-eslint/types").TSESTree.NewExpression} TSNewExpression */
/** @typedef {import("@typescript-eslint/types").TSESTree.CallExpression} TSCallExpression */
/** @typedef {import("eslint").SourceCode} SourceCode */
/** @typedef {import("eslint").AST.Token} Token */
/** @typedef {import("eslint").Rule.Node} RuleNode */
Expand All @@ -11,6 +13,7 @@ import { isClosingParenToken, isOpeningParenToken } from "./token-predicate.mjs"
* @param {SourceCode} sourceCode The source code object to get tokens.
* @returns {Token|null} The left parenthesis of the parent node syntax
*/
// eslint-disable-next-line complexity
function getParentSyntaxParen(node, sourceCode) {
const parent = /** @type {RuleNode} */ (node).parent

Expand All @@ -19,7 +22,9 @@ function getParentSyntaxParen(node, sourceCode) {
case "NewExpression":
if (parent.arguments.length === 1 && parent.arguments[0] === node) {
return sourceCode.getTokenAfter(
parent.callee,
/** @type {RuleNode} */ (/** @type {unknown} */ (
/** @type {TSNewExpression | TSCallExpression} */ (parent).typeParameters
)) ?? parent.callee,
isOpeningParenToken,
)
}
Expand Down
71 changes: 70 additions & 1 deletion test/is-parenthesized.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import tsParser from "@typescript-eslint/parser"
import assert from "assert"
import { getProperty } from "dot-prop"
import { isParenthesized } from "../src/index.mjs"
import { newCompatLinter } from "./test-lib/eslint-compat.mjs"

describe("The 'isParenthesized' function", () => {
for (const { code, expected } of [
for (const { code, expected, parser } of [
{
code: "777",
expected: {
Expand Down Expand Up @@ -215,6 +216,73 @@ describe("The 'isParenthesized' function", () => {
"body[0].parent": false,
},
},
// TypeScript support
{
code: "f<import('foo')>(a)",
expected: {
"body[0]": false,
"body[0].expression": false,
"body[0].expression.arguments[0]": false,
},
parser: tsParser,
},
{
code: "f<import('foo')>((a))",
expected: {
"body[0]": false,
"body[0].expression": false,
"body[0].expression.arguments[0]": true,
},
parser: tsParser,
},
{
code: "f<import('foo')>(a,b)",
expected: {
"body[0]": false,
"body[0].expression": false,
"body[0].expression.arguments[0]": false,
"body[0].expression.arguments[1]": false,
},
parser: tsParser,
},
{
code: "f<import('foo')>((a),b)",
expected: {
"body[0]": false,
"body[0].expression": false,
"body[0].expression.arguments[0]": true,
"body[0].expression.arguments[1]": false,
},
parser: tsParser,
},
{
code: "f<import('foo')>(a,(b))",
expected: {
"body[0]": false,
"body[0].expression": false,
"body[0].expression.arguments[0]": false,
"body[0].expression.arguments[1]": true,
},
parser: tsParser,
},
{
code: "new f<import('foo')>(a)",
expected: {
"body[0]": false,
"body[0].expression": false,
"body[0].expression.arguments[0]": false,
},
parser: tsParser,
},
{
code: "new f<import('foo')>((a))",
expected: {
"body[0]": false,
"body[0].expression": false,
"body[0].expression.arguments[0]": true,
},
parser: tsParser,
},
]) {
describe(`on the code \`${code}\``, () => {
for (const key of Object.keys(expected)) {
Expand All @@ -226,6 +294,7 @@ describe("The 'isParenthesized' function", () => {
languageOptions: {
ecmaVersion: 2020,
sourceType: "script",
parser,
},
rules: { "test/test": "error" },
plugins: {
Expand Down