Skip to content

Commit

Permalink
Improve NewExpression arguments detection
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 31, 2020
1 parent bc58ccd commit d3a7065
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
12 changes: 10 additions & 2 deletions 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`.
Expand All @@ -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': {
Expand Down
3 changes: 3 additions & 0 deletions test/prefer-string-starts-ends-with.js
Expand Up @@ -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)',
Expand Down
62 changes: 55 additions & 7 deletions test/snapshots/prefer-string-starts-ends-with.js.md
Expand Up @@ -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
`␊
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Binary file modified test/snapshots/prefer-string-starts-ends-with.js.snap
Binary file not shown.

0 comments on commit d3a7065

Please sign in to comment.