type: "layout",
docs: {
- description: "require or disallow trailing commas",
+ description: "Require or disallow trailing commas",
recommended: false,
url: "https://eslint.org/docs/rules/comma-dangle"
},
node: lastItem,
loc: trailingToken.loc,
messageId: "unexpected",
- fix(fixer) {
- return fixer.remove(trailingToken);
+ *fix(fixer) {
+ yield fixer.remove(trailingToken);
+
+ /*
+ * Extend the range of the fix to include surrounding tokens to ensure
+ * that the element after which the comma is removed stays _last_.
+ * This intentionally makes conflicts in fix ranges with rules that may be
+ * adding or removing elements in the same autofix pass.
+ * https://github.com/eslint/eslint/issues/15660
+ */
+ yield fixer.insertTextBefore(sourceCode.getTokenBefore(trailingToken), "");
+ yield fixer.insertTextAfter(sourceCode.getTokenAfter(trailingToken), "");
}
});
}
end: astUtils.getNextLocation(sourceCode, trailingToken.loc.end)
},
messageId: "missing",
- fix(fixer) {
- return fixer.insertTextAfter(trailingToken, ",");
+ *fix(fixer) {
+ yield fixer.insertTextAfter(trailingToken, ",");
+
+ /*
+ * Extend the range of the fix to include surrounding tokens to ensure
+ * that the element after which the comma is inserted stays _last_.
+ * This intentionally makes conflicts in fix ranges with rules that may be
+ * adding or removing elements in the same autofix pass.
+ * https://github.com/eslint/eslint/issues/15660
+ */
+ yield fixer.insertTextBefore(trailingToken, "");
+ yield fixer.insertTextAfter(sourceCode.getTokenAfter(trailingToken), "");
}
});
}