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 8cc077d829..72e1534526 100644 Binary files a/test/snapshots/prefer-string-starts-ends-with.js.snap and b/test/snapshots/prefer-string-starts-ends-with.js.snap differ