Skip to content

Commit

Permalink
Fix edge cases when add new to calls (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored May 10, 2024
1 parent cf2109a commit d8f8161
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 104 deletions.
2 changes: 2 additions & 0 deletions rules/fix/switch-call-expression-to-new-expression.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';
const {isParenthesized} = require('../utils/parentheses.js');
const shouldAddParenthesesToNewExpressionCallee = require('../utils/should-add-parentheses-to-new-expression-callee.js');
const fixSpaceAroundKeyword = require('./fix-space-around-keywords.js');

function * switchCallExpressionToNewExpression(node, sourceCode, fixer) {
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
yield fixer.insertTextBefore(node, 'new ');

const {callee} = node;
Expand Down
41 changes: 23 additions & 18 deletions test/new-for-builtins.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -231,26 +231,31 @@ test.snapshot({
'const foo = new Number(\'123\')',
'const foo = new String()',
'const foo = new Symbol()',
`
function varCheck() {
{
var WeakMap = function() {};
}
// This should not reported
return WeakMap()
outdent`
function varCheck() {
{
var WeakMap = function() {};
}
function constCheck() {
{
const Array = function() {};
}
return Array()
// This should not reported
return WeakMap()
}
function constCheck() {
{
const Array = function() {};
}
function letCheck() {
{
let Map = function() {};
}
return Map()
return Array()
}
function letCheck() {
{
let Map = function() {};
}
`,
return Map()
}
`,
outdent`
function foo() {
return(globalThis).Map()
}
`,
],
});
191 changes: 105 additions & 86 deletions test/snapshots/new-for-builtins.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1267,105 +1267,124 @@ Generated by [AVA](https://avajs.dev).
> Input
`␊
1 |␊
2 | function varCheck() {␊
3 | {␊
4 | var WeakMap = function() {};␊
5 | }␊
6 | // This should not reported␊
7 | return WeakMap()␊
8 | }␊
9 | function constCheck() {␊
10 | {␊
11 | const Array = function() {};␊
12 | }␊
13 | return Array()␊
14 | }␊
15 | function letCheck() {␊
16 | {␊
17 | let Map = function() {};␊
18 | }␊
19 | return Map()␊
20 | }␊
21 | ␊
1 | function varCheck() {␊
2 | {␊
3 | var WeakMap = function() {};␊
4 | }␊
5 | // This should not reported␊
6 | return WeakMap()␊
7 | }␊
8 | function constCheck() {␊
9 | {␊
10 | const Array = function() {};␊
11 | }␊
12 | return Array()␊
13 | }␊
14 | function letCheck() {␊
15 | {␊
16 | let Map = function() {};␊
17 | }␊
18 | return Map()␊
19 | }␊
`

> Output
`␊
1 |␊
2 | function varCheck() {␊
3 | {␊
4 | var WeakMap = function() {};␊
5 | }␊
6 | // This should not reported␊
7 | return WeakMap()␊
8 | }␊
9 | function constCheck() {␊
10 | {␊
11 | const Array = function() {};␊
12 | }␊
13 | return new Array()␊
14 | }␊
15 | function letCheck() {␊
16 | {␊
17 | let Map = function() {};␊
18 | }␊
19 | return new Map()␊
20 | }␊
21 | ␊
1 | function varCheck() {␊
2 | {␊
3 | var WeakMap = function() {};␊
4 | }␊
5 | // This should not reported␊
6 | return WeakMap()␊
7 | }␊
8 | function constCheck() {␊
9 | {␊
10 | const Array = function() {};␊
11 | }␊
12 | return new Array()␊
13 | }␊
14 | function letCheck() {␊
15 | {␊
16 | let Map = function() {};␊
17 | }␊
18 | return new Map()␊
19 | }␊
`

> Error 1/2
`␊
1 |␊
2 | function varCheck() {␊
3 | {␊
4 | var WeakMap = function() {};␊
5 | }␊
6 | // This should not reported␊
7 | return WeakMap()␊
8 | }␊
9 | function constCheck() {␊
10 | {␊
11 | const Array = function() {};␊
12 | }␊
> 13 | return Array()␊
| ^^^^^^^ Use \`new Array()\` instead of \`Array()\`.␊
14 | }␊
15 | function letCheck() {␊
16 | {␊
17 | let Map = function() {};␊
18 | }␊
19 | return Map()␊
20 | }␊
21 | ␊
1 | function varCheck() {␊
2 | {␊
3 | var WeakMap = function() {};␊
4 | }␊
5 | // This should not reported␊
6 | return WeakMap()␊
7 | }␊
8 | function constCheck() {␊
9 | {␊
10 | const Array = function() {};␊
11 | }␊
> 12 | return Array()␊
| ^^^^^^^ Use \`new Array()\` instead of \`Array()\`.␊
13 | }␊
14 | function letCheck() {␊
15 | {␊
16 | let Map = function() {};␊
17 | }␊
18 | return Map()␊
19 | }␊
`

> Error 2/2
`␊
1 |␊
2 | function varCheck() {␊
3 | {␊
4 | var WeakMap = function() {};␊
5 | }␊
6 | // This should not reported␊
7 | return WeakMap()␊
8 | }␊
9 | function constCheck() {␊
10 | {␊
11 | const Array = function() {};␊
12 | }␊
13 | return Array()␊
14 | }␊
15 | function letCheck() {␊
16 | {␊
17 | let Map = function() {};␊
18 | }␊
> 19 | return Map()␊
| ^^^^^ Use \`new Map()\` instead of \`Map()\`.␊
20 | }␊
21 | ␊
1 | function varCheck() {␊
2 | {␊
3 | var WeakMap = function() {};␊
4 | }␊
5 | // This should not reported␊
6 | return WeakMap()␊
7 | }␊
8 | function constCheck() {␊
9 | {␊
10 | const Array = function() {};␊
11 | }␊
12 | return Array()␊
13 | }␊
14 | function letCheck() {␊
15 | {␊
16 | let Map = function() {};␊
17 | }␊
> 18 | return Map()␊
| ^^^^^ Use \`new Map()\` instead of \`Map()\`.␊
19 | }␊
`

## invalid(60): function foo() { return(globalThis).Map() }

> Input
`␊
1 | function foo() {␊
2 | return(globalThis).Map()␊
3 | }␊
`

> Output
`␊
1 | function foo() {␊
2 | return new (globalThis).Map()␊
3 | }␊
`

> Error 1/1
`␊
1 | function foo() {␊
> 2 | return(globalThis).Map()␊
| ^^^^^^^^^^^^^^^^^^ Use \`new Map()\` instead of \`Map()\`.␊
3 | }␊
`
Binary file modified test/snapshots/new-for-builtins.mjs.snap
Binary file not shown.
27 changes: 27 additions & 0 deletions test/snapshots/throw-new-error.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,30 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
3 | });␊
`

## invalid(26): function foo() { return[globalThis][0].Error('message'); }

> Input
`␊
1 | function foo() {␊
2 | return[globalThis][0].Error('message');␊
3 | }␊
`

> Output
`␊
1 | function foo() {␊
2 | return new [globalThis][0].Error('message');␊
3 | }␊
`

> Error 1/1
`␊
1 | function foo() {␊
> 2 | return[globalThis][0].Error('message');␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
3 | }␊
`
Binary file modified test/snapshots/throw-new-error.mjs.snap
Binary file not shown.
5 changes: 5 additions & 0 deletions test/throw-new-error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@ test.snapshot({
reject(Error('message'));
});
`,
outdent`
function foo() {
return[globalThis][0].Error('message');
}
`,
],
});

0 comments on commit d8f8161

Please sign in to comment.