Skip to content

Commit 7dffe48

Browse files
yeonjuanplatinumazure
authored andcommitted
Update: Enable function string option in comma-dangle (fixes #12058) (#12462)
1 parent e15e1f9 commit 7dffe48

File tree

2 files changed

+293
-17
lines changed

2 files changed

+293
-17
lines changed

lib/rules/comma-dangle.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ function isTrailingCommaAllowed(lastItem) {
4141
/**
4242
* Normalize option value.
4343
* @param {string|Object|undefined} optionValue The 1st option value to normalize.
44+
* @param {number} ecmaVersion The normalized ECMAScript version.
4445
* @returns {Object} The normalized option value.
4546
*/
46-
function normalizeOptions(optionValue) {
47+
function normalizeOptions(optionValue, ecmaVersion) {
4748
if (typeof optionValue === "string") {
4849
return {
4950
arrays: optionValue,
5051
objects: optionValue,
5152
imports: optionValue,
5253
exports: optionValue,
53-
54-
// For backward compatibility, always ignore functions.
55-
functions: "ignore"
54+
functions: (!ecmaVersion || ecmaVersion < 8) ? "ignore" : optionValue
5655
};
5756
}
5857
if (typeof optionValue === "object" && optionValue !== null) {
@@ -135,7 +134,8 @@ module.exports = {
135134
},
136135

137136
create(context) {
138-
const options = normalizeOptions(context.options[0]);
137+
const options = normalizeOptions(context.options[0], context.parserOptions.ecmaVersion);
138+
139139
const sourceCode = context.getSourceCode();
140140

141141
/**

tests/lib/rules/comma-dangle.js

Lines changed: 288 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,31 +235,100 @@ ruleTester.run("comma-dangle", rule, {
235235
options: ["only-multiline"],
236236
parserOptions: { ecmaVersion: 6, sourceType: "module" }
237237
},
238-
239-
240-
// trailing comma in functions -- ignore by default
241238
{
242-
code: "function foo(a,) {}",
239+
code: "function foo(a) {}",
240+
options: ["always"]
241+
},
242+
{
243+
code: "foo(a)",
244+
options: ["always"]
245+
},
246+
{
247+
code: "function foo(a) {}",
248+
options: ["never"]
249+
},
250+
{
251+
code: "foo(a)",
252+
options: ["never"]
253+
},
254+
{
255+
code: "function foo(a,\nb) {}",
256+
options: ["always-multiline"]
257+
},
258+
{
259+
code: "foo(a,\nb)",
260+
options: ["always-multiline"]
261+
},
262+
{
263+
code: "function foo(a,\nb) {}",
264+
options: ["only-multiline"]
265+
},
266+
{
267+
code: "foo(a,\nb)",
268+
options: ["only-multiline"]
269+
},
270+
{
271+
code: "function foo(a) {}",
272+
options: ["always"],
273+
parserOptions: { ecmaVersion: 7 }
274+
},
275+
{
276+
code: "foo(a)",
277+
options: ["always"],
278+
parserOptions: { ecmaVersion: 7 }
279+
},
280+
{
281+
code: "function foo(a) {}",
282+
options: ["never"],
283+
parserOptions: { ecmaVersion: 7 }
284+
},
285+
{
286+
code: "foo(a)",
287+
options: ["never"],
288+
parserOptions: { ecmaVersion: 7 }
289+
},
290+
{
291+
code: "function foo(a,\nb) {}",
292+
options: ["always-multiline"],
293+
parserOptions: { ecmaVersion: 7 }
294+
},
295+
{
296+
code: "foo(a,\nb)",
297+
options: ["always-multiline"],
298+
parserOptions: { ecmaVersion: 7 }
299+
},
300+
{
301+
code: "function foo(a,\nb) {}",
302+
options: ["only-multiline"],
303+
parserOptions: { ecmaVersion: 7 }
304+
},
305+
{
306+
code: "foo(a,\nb)",
307+
options: ["only-multiline"],
308+
parserOptions: { ecmaVersion: 7 }
309+
},
310+
{
311+
code: "function foo(a) {}",
243312
options: ["never"],
244313
parserOptions: { ecmaVersion: 8 }
245314
},
246315
{
247-
code: "foo(a,)",
316+
code: "foo(a)",
248317
options: ["never"],
249318
parserOptions: { ecmaVersion: 8 }
250319
},
251320
{
252-
code: "function foo(a) {}",
321+
code: "function foo(a,) {}",
253322
options: ["always"],
254323
parserOptions: { ecmaVersion: 8 }
255324
},
256325
{
257-
code: "foo(a)",
326+
code: "foo(a,)",
258327
options: ["always"],
259328
parserOptions: { ecmaVersion: 8 }
260329
},
261330
{
262-
code: "function foo(\na,\nb\n) {}",
331+
code: "function foo(\na,\nb,\n) {}",
263332
options: ["always-multiline"],
264333
parserOptions: { ecmaVersion: 8 }
265334
},
@@ -269,22 +338,22 @@ ruleTester.run("comma-dangle", rule, {
269338
parserOptions: { ecmaVersion: 8 }
270339
},
271340
{
272-
code: "function foo(a,b,) {}",
341+
code: "function foo(a,b) {}",
273342
options: ["always-multiline"],
274343
parserOptions: { ecmaVersion: 8 }
275344
},
276345
{
277-
code: "foo(a,b,)",
346+
code: "foo(a,b)",
278347
options: ["always-multiline"],
279348
parserOptions: { ecmaVersion: 8 }
280349
},
281350
{
282-
code: "function foo(a,b,) {}",
351+
code: "function foo(a,b) {}",
283352
options: ["only-multiline"],
284353
parserOptions: { ecmaVersion: 8 }
285354
},
286355
{
287-
code: "foo(a,b,)",
356+
code: "foo(a,b)",
288357
options: ["only-multiline"],
289358
parserOptions: { ecmaVersion: 8 }
290359
},
@@ -315,6 +384,11 @@ ruleTester.run("comma-dangle", rule, {
315384
options: [{ functions: "always" }],
316385
parserOptions: { ecmaVersion: 8 }
317386
},
387+
{
388+
code: "foo(a,)",
389+
options: [{ functions: "always" }],
390+
parserOptions: { ecmaVersion: 9 }
391+
},
318392
{
319393
code: "bar(...a,)",
320394
options: [{ functions: "always" }],
@@ -1283,6 +1357,208 @@ ruleTester.run("comma-dangle", rule, {
12831357
parserOptions: { ecmaVersion: 8 },
12841358
errors: [{ messageId: "unexpected", type: "SpreadElement" }]
12851359
},
1360+
{
1361+
code: "function foo(a,) {}",
1362+
output: "function foo(a) {}",
1363+
options: ["never"],
1364+
parserOptions: { ecmaVersion: 8 },
1365+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1366+
},
1367+
{
1368+
code: "(function foo(a,) {})",
1369+
output: "(function foo(a) {})",
1370+
options: ["never"],
1371+
parserOptions: { ecmaVersion: 8 },
1372+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1373+
},
1374+
{
1375+
code: "(a,) => a",
1376+
output: "(a) => a",
1377+
options: ["never"],
1378+
parserOptions: { ecmaVersion: 8 },
1379+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1380+
},
1381+
{
1382+
code: "(a,) => (a)",
1383+
output: "(a) => (a)",
1384+
options: ["never"],
1385+
parserOptions: { ecmaVersion: 8 },
1386+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1387+
},
1388+
{
1389+
code: "({foo(a,) {}})",
1390+
output: "({foo(a) {}})",
1391+
options: ["never"],
1392+
parserOptions: { ecmaVersion: 8 },
1393+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1394+
},
1395+
{
1396+
code: "class A {foo(a,) {}}",
1397+
output: "class A {foo(a) {}}",
1398+
options: ["never"],
1399+
parserOptions: { ecmaVersion: 8 },
1400+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1401+
},
1402+
{
1403+
code: "foo(a,)",
1404+
output: "foo(a)",
1405+
options: ["never"],
1406+
parserOptions: { ecmaVersion: 8 },
1407+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1408+
},
1409+
{
1410+
code: "foo(...a,)",
1411+
output: "foo(...a)",
1412+
options: ["never"],
1413+
parserOptions: { ecmaVersion: 8 },
1414+
errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1415+
},
1416+
1417+
{
1418+
code: "function foo(a) {}",
1419+
output: "function foo(a,) {}",
1420+
options: ["always"],
1421+
parserOptions: { ecmaVersion: 8 },
1422+
errors: [{ messageId: "missing", type: "Identifier" }]
1423+
},
1424+
{
1425+
code: "(function foo(a) {})",
1426+
output: "(function foo(a,) {})",
1427+
options: ["always"],
1428+
parserOptions: { ecmaVersion: 8 },
1429+
errors: [{ messageId: "missing", type: "Identifier" }]
1430+
},
1431+
{
1432+
code: "(a) => a",
1433+
output: "(a,) => a",
1434+
options: ["always"],
1435+
parserOptions: { ecmaVersion: 8 },
1436+
errors: [{ messageId: "missing", type: "Identifier" }]
1437+
},
1438+
{
1439+
code: "(a) => (a)",
1440+
output: "(a,) => (a)",
1441+
options: ["always"],
1442+
parserOptions: { ecmaVersion: 8 },
1443+
errors: [{ messageId: "missing", type: "Identifier" }]
1444+
},
1445+
{
1446+
code: "({foo(a) {}})",
1447+
output: "({foo(a,) {},})",
1448+
options: ["always"],
1449+
parserOptions: { ecmaVersion: 8 },
1450+
errors: [
1451+
{ messageId: "missing", type: "Identifier" },
1452+
{ messageId: "missing", type: "Property" }
1453+
]
1454+
},
1455+
{
1456+
code: "class A {foo(a) {}}",
1457+
output: "class A {foo(a,) {}}",
1458+
options: ["always"],
1459+
parserOptions: { ecmaVersion: 8 },
1460+
errors: [{ messageId: "missing", type: "Identifier" }]
1461+
},
1462+
{
1463+
code: "foo(a)",
1464+
output: "foo(a,)",
1465+
options: ["always"],
1466+
parserOptions: { ecmaVersion: 8 },
1467+
errors: [{ messageId: "missing", type: "Identifier" }]
1468+
},
1469+
{
1470+
code: "foo(...a)",
1471+
output: "foo(...a,)",
1472+
options: ["always"],
1473+
parserOptions: { ecmaVersion: 8 },
1474+
errors: [{ messageId: "missing", type: "SpreadElement" }]
1475+
},
1476+
1477+
{
1478+
code: "function foo(a,) {}",
1479+
output: "function foo(a) {}",
1480+
options: ["always-multiline"],
1481+
parserOptions: { ecmaVersion: 8 },
1482+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1483+
},
1484+
{
1485+
code: "(function foo(a,) {})",
1486+
output: "(function foo(a) {})",
1487+
options: ["always-multiline"],
1488+
parserOptions: { ecmaVersion: 8 },
1489+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1490+
},
1491+
{
1492+
code: "foo(a,)",
1493+
output: "foo(a)",
1494+
options: ["always-multiline"],
1495+
parserOptions: { ecmaVersion: 8 },
1496+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1497+
},
1498+
{
1499+
code: "foo(...a,)",
1500+
output: "foo(...a)",
1501+
options: ["always-multiline"],
1502+
parserOptions: { ecmaVersion: 8 },
1503+
errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1504+
},
1505+
{
1506+
code: "function foo(\na,\nb\n) {}",
1507+
output: "function foo(\na,\nb,\n) {}",
1508+
options: ["always-multiline"],
1509+
parserOptions: { ecmaVersion: 8 },
1510+
errors: [{ messageId: "missing", type: "Identifier" }]
1511+
},
1512+
{
1513+
code: "foo(\na,\nb\n)",
1514+
output: "foo(\na,\nb,\n)",
1515+
options: ["always-multiline"],
1516+
parserOptions: { ecmaVersion: 8 },
1517+
errors: [{ messageId: "missing", type: "Identifier" }]
1518+
},
1519+
{
1520+
code: "foo(\n...a,\n...b\n)",
1521+
output: "foo(\n...a,\n...b,\n)",
1522+
options: ["always-multiline"],
1523+
parserOptions: { ecmaVersion: 8 },
1524+
errors: [{ messageId: "missing", type: "SpreadElement" }]
1525+
},
1526+
1527+
{
1528+
code: "function foo(a,) {}",
1529+
output: "function foo(a) {}",
1530+
options: ["only-multiline"],
1531+
parserOptions: { ecmaVersion: 8 },
1532+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1533+
},
1534+
{
1535+
code: "(function foo(a,) {})",
1536+
output: "(function foo(a) {})",
1537+
options: ["only-multiline"],
1538+
parserOptions: { ecmaVersion: 8 },
1539+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1540+
},
1541+
{
1542+
code: "foo(a,)",
1543+
output: "foo(a)",
1544+
options: ["only-multiline"],
1545+
parserOptions: { ecmaVersion: 8 },
1546+
errors: [{ messageId: "unexpected", type: "Identifier" }]
1547+
},
1548+
{
1549+
code: "foo(...a,)",
1550+
output: "foo(...a)",
1551+
options: ["only-multiline"],
1552+
parserOptions: { ecmaVersion: 8 },
1553+
errors: [{ messageId: "unexpected", type: "SpreadElement" }]
1554+
},
1555+
{
1556+
code: "function foo(a) {}",
1557+
output: "function foo(a,) {}",
1558+
options: ["always"],
1559+
parserOptions: { ecmaVersion: 9 },
1560+
errors: [{ messageId: "missing", type: "Identifier" }]
1561+
},
12861562

12871563
// separated options
12881564
{

0 commit comments

Comments
 (0)