From d3a706552854016932aa36d5153bc367da6ef957 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 31 Dec 2020 18:09:08 +0800 Subject: [PATCH] Improve NewExpression arguments detection --- ...parentheses-to-member-expression-object.js | 12 +++- test/prefer-string-starts-ends-with.js | 3 + .../prefer-string-starts-ends-with.js.md | 62 ++++++++++++++++-- .../prefer-string-starts-ends-with.js.snap | Bin 838 -> 981 bytes 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/rules/utils/need-add-parentheses-to-member-expression-object.js b/rules/utils/need-add-parentheses-to-member-expression-object.js index edf3f5b735..45f6f15f93 100644 --- a/rules/utils/need-add-parentheses-to-member-expression-object.js +++ b/rules/utils/need-add-parentheses-to-member-expression-object.js @@ -1,5 +1,7 @@ 'use strict'; +const {isNotOpeningParenToken, isNotClosingParenToken} = require('eslint-utils'); + /** Check if need add parentheses to node, when it's used as `object` of `MemberExpression`. @@ -20,8 +22,14 @@ function needAddParenthesesToMemberExpressionObject(node, sourceCode) { return false; case 'NewExpression': { - // `new Foo.bar` is different with `new Foo().bar` - return !sourceCode.getText(node).endsWith(')'); + // `new Foo` and `new (Foo)` need add `()` + if (node.arguments.length === 0) { + const [maybeOpeningParenthesisToken, maybeClosingParenthesisToken] = sourceCode.getLastTokens(node, 2); + if (isNotOpeningParenToken(maybeOpeningParenthesisToken) || isNotClosingParenToken(maybeClosingParenthesisToken)) { + return true; + } + } + return false; } case 'Literal': { diff --git a/test/prefer-string-starts-ends-with.js b/test/prefer-string-starts-ends-with.js index fe515afcde..beb3fef58b 100644 --- a/test/prefer-string-starts-ends-with.js +++ b/test/prefer-string-starts-ends-with.js @@ -141,7 +141,10 @@ test.visualize([ '/^a/.test(foo + bar)', '/^a/.test(foo || bar)', '/^a/.test(new SomeString)', + '/^a/.test(new (SomeString))', '/^a/.test(new SomeString())', + '/^a/.test(new SomeString(/* comment */))', + '/^a/.test(new SomeString("string"))', '/^a/.test(foo.bar)', '/^a/.test(foo.bar())', '/^a/.test(foo?.bar)', diff --git a/test/snapshots/prefer-string-starts-ends-with.js.md b/test/snapshots/prefer-string-starts-ends-with.js.md index f2262e174a..bfb7cd09ae 100644 --- a/test/snapshots/prefer-string-starts-ends-with.js.md +++ b/test/snapshots/prefer-string-starts-ends-with.js.md @@ -102,6 +102,22 @@ Generated by [AVA](https://avajs.dev). ## prefer-string-starts-ends-with - #7 +> Snapshot 1 + + `␊ + Input:␊ + 1 | /^a/.test(new (SomeString))␊ + ␊ + Output:␊ + 1 | (new (SomeString)).startsWith('a')␊ + ␊ + Error 1/1:␊ + > 1 | /^a/.test(new (SomeString))␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ + ` + +## prefer-string-starts-ends-with - #8 + > Snapshot 1 `␊ @@ -116,7 +132,39 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ ` -## prefer-string-starts-ends-with - #8 +## prefer-string-starts-ends-with - #9 + +> Snapshot 1 + + `␊ + Input:␊ + 1 | /^a/.test(new SomeString(/* comment */))␊ + ␊ + Output:␊ + 1 | new SomeString(/* comment */).startsWith('a')␊ + ␊ + Error 1/1:␊ + > 1 | /^a/.test(new SomeString(/* comment */))␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ + ` + +## prefer-string-starts-ends-with - #10 + +> Snapshot 1 + + `␊ + Input:␊ + 1 | /^a/.test(new SomeString("string"))␊ + ␊ + Output:␊ + 1 | new SomeString("string").startsWith('a')␊ + ␊ + Error 1/1:␊ + > 1 | /^a/.test(new SomeString("string"))␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ + ` + +## prefer-string-starts-ends-with - #11 > Snapshot 1 @@ -132,7 +180,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ ` -## prefer-string-starts-ends-with - #9 +## prefer-string-starts-ends-with - #12 > Snapshot 1 @@ -148,7 +196,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ ` -## prefer-string-starts-ends-with - #10 +## prefer-string-starts-ends-with - #13 > Snapshot 1 @@ -164,7 +212,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ ` -## prefer-string-starts-ends-with - #11 +## prefer-string-starts-ends-with - #14 > Snapshot 1 @@ -180,7 +228,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ ` -## prefer-string-starts-ends-with - #12 +## prefer-string-starts-ends-with - #15 > Snapshot 1 @@ -196,7 +244,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ ` -## prefer-string-starts-ends-with - #13 +## prefer-string-starts-ends-with - #16 > Snapshot 1 @@ -212,7 +260,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `String#startsWith()` over a regex with `^`.␊ ` -## prefer-string-starts-ends-with - #14 +## prefer-string-starts-ends-with - #17 > Snapshot 1 diff --git a/test/snapshots/prefer-string-starts-ends-with.js.snap b/test/snapshots/prefer-string-starts-ends-with.js.snap index 8cc077d8296958a12c2dad2717a5a5f49ea61a3b..72e153452640c7dfe2266326652e616cf2c884eb 100644 GIT binary patch literal 981 zcmV;`11kJMRzVMzH8ob_RxQlc>6Vjt^xIcFxXS z`81G`5iDB7!NAb`Nz^_}K)(IBcgv;Rl8x?+V9`iU28OO6dAY~Jha^*H< zT%wU!o|svpkd~jXp{ZHR#pPdGLYxUG8uf}x5{pWT!!t`VG}IH-K?b=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