Skip to content

Commit 33ea905

Browse files
authored
fix: correct location reporting for !important in no-important rule (eslint#286)
* fix: correct location reporting for `!important` in `no-important` rule * update tests to avoid problems with dedent in Bun
1 parent a1c0ee1 commit 33ea905

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/rules/no-important.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export default {
5454
const declarationText = sourceCode.getText(node);
5555
const textWithoutComments = declarationText.replace(
5656
commentPattern,
57-
match => match.replace(/[^\n]/gu, " "),
57+
/* eslint-disable-next-line require-unicode-regexp -- we want to replace each code unit with a space */
58+
match => match.replace(/[^\n]/g, " "),
5859
);
5960
const importantMatch =
6061
importantPattern.exec(textWithoutComments);

tests/rules/no-important.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ ruleTester.run("no-important", rule, {
245245
},
246246
],
247247
},
248+
{
249+
code: "a { color: red /* comment with surrogate pairs: 👍🚀 */ !important; }",
250+
errors: [
251+
{
252+
messageId: "unexpectedImportant",
253+
line: 1,
254+
column: 57,
255+
endLine: 1,
256+
endColumn: 67,
257+
suggestions: [
258+
{
259+
messageId: "removeImportant",
260+
output: "a { color: red /* comment with surrogate pairs: 👍🚀 */; }",
261+
},
262+
],
263+
},
264+
],
265+
},
248266
{
249267
code: dedent`
250268
a {
@@ -272,6 +290,25 @@ ruleTester.run("no-important", rule, {
272290
},
273291
],
274292
},
293+
{
294+
// NOTE: dedent`` converts 👍🚀 to \u{1f44d}\u{1f680} in Bun, causing unexpected report locations
295+
code: "a {\ncolor: red /* comment with surrogate pairs: 👍🚀 */\n!important;\n}",
296+
errors: [
297+
{
298+
messageId: "unexpectedImportant",
299+
line: 3,
300+
column: 1,
301+
endLine: 3,
302+
endColumn: 11,
303+
suggestions: [
304+
{
305+
messageId: "removeImportant",
306+
output: "a {\ncolor: red /* comment with surrogate pairs: 👍🚀 */;\n}",
307+
},
308+
],
309+
},
310+
],
311+
},
275312
{
276313
code: "a { color: red !/* comment */important; }",
277314
errors: [
@@ -290,6 +327,24 @@ ruleTester.run("no-important", rule, {
290327
},
291328
],
292329
},
330+
{
331+
code: "a { color: red !/* comment with surrogate pairs: 👍🚀 */important; }",
332+
errors: [
333+
{
334+
messageId: "unexpectedImportant",
335+
line: 1,
336+
column: 16,
337+
endLine: 1,
338+
endColumn: 66,
339+
suggestions: [
340+
{
341+
messageId: "removeImportant",
342+
output: "a { color: red; }",
343+
},
344+
],
345+
},
346+
],
347+
},
293348
{
294349
code: "a { color: red ! /* comment */ important; }",
295350
errors: [
@@ -335,6 +390,25 @@ ruleTester.run("no-important", rule, {
335390
},
336391
],
337392
},
393+
{
394+
// NOTE: dedent`` converts 👍🚀 to \u{1f44d}\u{1f680} in Bun, causing unexpected report locations
395+
code: "a {\ncolor: red\n!/* comment with surrogate pairs: 👍🚀 */important;\n}",
396+
errors: [
397+
{
398+
messageId: "unexpectedImportant",
399+
line: 3,
400+
column: 1,
401+
endLine: 3,
402+
endColumn: 51,
403+
suggestions: [
404+
{
405+
messageId: "removeImportant",
406+
output: "a {\ncolor: red;\n}",
407+
},
408+
],
409+
},
410+
],
411+
},
338412
{
339413
code: dedent`
340414
a {

0 commit comments

Comments
 (0)