Skip to content

Commit d8f8161

Browse files
authored
Fix edge cases when add new to calls (#2352)
1 parent cf2109a commit d8f8161

7 files changed

+162
-104
lines changed

rules/fix/switch-call-expression-to-new-expression.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22
const {isParenthesized} = require('../utils/parentheses.js');
33
const shouldAddParenthesesToNewExpressionCallee = require('../utils/should-add-parentheses-to-new-expression-callee.js');
4+
const fixSpaceAroundKeyword = require('./fix-space-around-keywords.js');
45

56
function * switchCallExpressionToNewExpression(node, sourceCode, fixer) {
7+
yield * fixSpaceAroundKeyword(fixer, node, sourceCode);
68
yield fixer.insertTextBefore(node, 'new ');
79

810
const {callee} = node;

test/new-for-builtins.mjs

+23-18
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,31 @@ test.snapshot({
231231
'const foo = new Number(\'123\')',
232232
'const foo = new String()',
233233
'const foo = new Symbol()',
234-
`
235-
function varCheck() {
236-
{
237-
var WeakMap = function() {};
238-
}
239-
// This should not reported
240-
return WeakMap()
234+
outdent`
235+
function varCheck() {
236+
{
237+
var WeakMap = function() {};
241238
}
242-
function constCheck() {
243-
{
244-
const Array = function() {};
245-
}
246-
return Array()
239+
// This should not reported
240+
return WeakMap()
241+
}
242+
function constCheck() {
243+
{
244+
const Array = function() {};
247245
}
248-
function letCheck() {
249-
{
250-
let Map = function() {};
251-
}
252-
return Map()
246+
return Array()
247+
}
248+
function letCheck() {
249+
{
250+
let Map = function() {};
253251
}
254-
`,
252+
return Map()
253+
}
254+
`,
255+
outdent`
256+
function foo() {
257+
return(globalThis).Map()
258+
}
259+
`,
255260
],
256261
});

test/snapshots/new-for-builtins.mjs.md

+105-86
Original file line numberDiff line numberDiff line change
@@ -1267,105 +1267,124 @@ Generated by [AVA](https://avajs.dev).
12671267
> Input
12681268
12691269
`␊
1270-
1 |␊
1271-
2 | function varCheck() {␊
1272-
3 | {␊
1273-
4 | var WeakMap = function() {};␊
1274-
5 | }␊
1275-
6 | // This should not reported␊
1276-
7 | return WeakMap()␊
1277-
8 | }␊
1278-
9 | function constCheck() {␊
1279-
10 | {␊
1280-
11 | const Array = function() {};␊
1281-
12 | }␊
1282-
13 | return Array()␊
1283-
14 | }␊
1284-
15 | function letCheck() {␊
1285-
16 | {␊
1286-
17 | let Map = function() {};␊
1287-
18 | }␊
1288-
19 | return Map()␊
1289-
20 | }␊
1290-
21 | ␊
1270+
1 | function varCheck() {␊
1271+
2 | {␊
1272+
3 | var WeakMap = function() {};␊
1273+
4 | }␊
1274+
5 | // This should not reported␊
1275+
6 | return WeakMap()␊
1276+
7 | }␊
1277+
8 | function constCheck() {␊
1278+
9 | {␊
1279+
10 | const Array = function() {};␊
1280+
11 | }␊
1281+
12 | return Array()␊
1282+
13 | }␊
1283+
14 | function letCheck() {␊
1284+
15 | {␊
1285+
16 | let Map = function() {};␊
1286+
17 | }␊
1287+
18 | return Map()␊
1288+
19 | }␊
12911289
`
12921290

12931291
> Output
12941292
12951293
`␊
1296-
1 |␊
1297-
2 | function varCheck() {␊
1298-
3 | {␊
1299-
4 | var WeakMap = function() {};␊
1300-
5 | }␊
1301-
6 | // This should not reported␊
1302-
7 | return WeakMap()␊
1303-
8 | }␊
1304-
9 | function constCheck() {␊
1305-
10 | {␊
1306-
11 | const Array = function() {};␊
1307-
12 | }␊
1308-
13 | return new Array()␊
1309-
14 | }␊
1310-
15 | function letCheck() {␊
1311-
16 | {␊
1312-
17 | let Map = function() {};␊
1313-
18 | }␊
1314-
19 | return new Map()␊
1315-
20 | }␊
1316-
21 | ␊
1294+
1 | function varCheck() {␊
1295+
2 | {␊
1296+
3 | var WeakMap = function() {};␊
1297+
4 | }␊
1298+
5 | // This should not reported␊
1299+
6 | return WeakMap()␊
1300+
7 | }␊
1301+
8 | function constCheck() {␊
1302+
9 | {␊
1303+
10 | const Array = function() {};␊
1304+
11 | }␊
1305+
12 | return new Array()␊
1306+
13 | }␊
1307+
14 | function letCheck() {␊
1308+
15 | {␊
1309+
16 | let Map = function() {};␊
1310+
17 | }␊
1311+
18 | return new Map()␊
1312+
19 | }␊
13171313
`
13181314

13191315
> Error 1/2
13201316
13211317
`␊
1322-
1 |␊
1323-
2 | function varCheck() {␊
1324-
3 | {␊
1325-
4 | var WeakMap = function() {};␊
1326-
5 | }␊
1327-
6 | // This should not reported␊
1328-
7 | return WeakMap()␊
1329-
8 | }␊
1330-
9 | function constCheck() {␊
1331-
10 | {␊
1332-
11 | const Array = function() {};␊
1333-
12 | }␊
1334-
> 13 | return Array()␊
1335-
| ^^^^^^^ Use \`new Array()\` instead of \`Array()\`.␊
1336-
14 | }␊
1337-
15 | function letCheck() {␊
1338-
16 | {␊
1339-
17 | let Map = function() {};␊
1340-
18 | }␊
1341-
19 | return Map()␊
1342-
20 | }␊
1343-
21 | ␊
1318+
1 | function varCheck() {␊
1319+
2 | {␊
1320+
3 | var WeakMap = function() {};␊
1321+
4 | }␊
1322+
5 | // This should not reported␊
1323+
6 | return WeakMap()␊
1324+
7 | }␊
1325+
8 | function constCheck() {␊
1326+
9 | {␊
1327+
10 | const Array = function() {};␊
1328+
11 | }␊
1329+
> 12 | return Array()␊
1330+
| ^^^^^^^ Use \`new Array()\` instead of \`Array()\`.␊
1331+
13 | }␊
1332+
14 | function letCheck() {␊
1333+
15 | {␊
1334+
16 | let Map = function() {};␊
1335+
17 | }␊
1336+
18 | return Map()␊
1337+
19 | }␊
13441338
`
13451339

13461340
> Error 2/2
13471341
13481342
`␊
1349-
1 |␊
1350-
2 | function varCheck() {␊
1351-
3 | {␊
1352-
4 | var WeakMap = function() {};␊
1353-
5 | }␊
1354-
6 | // This should not reported␊
1355-
7 | return WeakMap()␊
1356-
8 | }␊
1357-
9 | function constCheck() {␊
1358-
10 | {␊
1359-
11 | const Array = function() {};␊
1360-
12 | }␊
1361-
13 | return Array()␊
1362-
14 | }␊
1363-
15 | function letCheck() {␊
1364-
16 | {␊
1365-
17 | let Map = function() {};␊
1366-
18 | }␊
1367-
> 19 | return Map()␊
1368-
| ^^^^^ Use \`new Map()\` instead of \`Map()\`.␊
1369-
20 | }␊
1370-
21 | ␊
1343+
1 | function varCheck() {␊
1344+
2 | {␊
1345+
3 | var WeakMap = function() {};␊
1346+
4 | }␊
1347+
5 | // This should not reported␊
1348+
6 | return WeakMap()␊
1349+
7 | }␊
1350+
8 | function constCheck() {␊
1351+
9 | {␊
1352+
10 | const Array = function() {};␊
1353+
11 | }␊
1354+
12 | return Array()␊
1355+
13 | }␊
1356+
14 | function letCheck() {␊
1357+
15 | {␊
1358+
16 | let Map = function() {};␊
1359+
17 | }␊
1360+
> 18 | return Map()␊
1361+
| ^^^^^ Use \`new Map()\` instead of \`Map()\`.␊
1362+
19 | }␊
1363+
`
1364+
1365+
## invalid(60): function foo() { return(globalThis).Map() }
1366+
1367+
> Input
1368+
1369+
`␊
1370+
1 | function foo() {␊
1371+
2 | return(globalThis).Map()␊
1372+
3 | }␊
1373+
`
1374+
1375+
> Output
1376+
1377+
`␊
1378+
1 | function foo() {␊
1379+
2 | return new (globalThis).Map()␊
1380+
3 | }␊
1381+
`
1382+
1383+
> Error 1/1
1384+
1385+
`␊
1386+
1 | function foo() {␊
1387+
> 2 | return(globalThis).Map()␊
1388+
| ^^^^^^^^^^^^^^^^^^ Use \`new Map()\` instead of \`Map()\`.␊
1389+
3 | }␊
13711390
`
41 Bytes
Binary file not shown.

test/snapshots/throw-new-error.mjs.md

+27
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,30 @@ Generated by [AVA](https://avajs.dev).
534534
| ^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
535535
3 | });␊
536536
`
537+
538+
## invalid(26): function foo() { return[globalThis][0].Error('message'); }
539+
540+
> Input
541+
542+
`␊
543+
1 | function foo() {␊
544+
2 | return[globalThis][0].Error('message');␊
545+
3 | }␊
546+
`
547+
548+
> Output
549+
550+
`␊
551+
1 | function foo() {␊
552+
2 | return new [globalThis][0].Error('message');␊
553+
3 | }␊
554+
`
555+
556+
> Error 1/1
557+
558+
`␊
559+
1 | function foo() {␊
560+
> 2 | return[globalThis][0].Error('message');␊
561+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`new\` when creating an error.␊
562+
3 | }␊
563+
`
92 Bytes
Binary file not shown.

test/throw-new-error.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ test.snapshot({
6060
reject(Error('message'));
6161
});
6262
`,
63+
outdent`
64+
function foo() {
65+
return[globalThis][0].Error('message');
66+
}
67+
`,
6368
],
6469
});

0 commit comments

Comments
 (0)