Skip to content

Commit ed0c0f1

Browse files
authored
fix: handle all CSS newlines in rules (eslint#280)
1 parent 33ea905 commit ed0c0f1

File tree

4 files changed

+109
-3
lines changed

4 files changed

+109
-3
lines changed

src/rules/no-duplicate-imports.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,19 @@ export default {
5252
data: { url },
5353
fix(fixer) {
5454
const [start, end] = sourceCode.getRange(node);
55+
const text = sourceCode.text;
5556
// Remove the node, and also remove a following newline if present
56-
const removeEnd =
57-
sourceCode.text[end] === "\n" ? end + 1 : end;
57+
let removeEnd = end;
58+
if (text[removeEnd] === "\r") {
59+
removeEnd +=
60+
text[removeEnd + 1] === "\n" ? 2 : 1;
61+
} else if (
62+
text[removeEnd] === "\n" ||
63+
text[removeEnd] === "\f"
64+
) {
65+
removeEnd += 1;
66+
}
67+
5868
return fixer.removeRange([start, removeEnd]);
5969
},
6070
});

src/rules/no-important.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default {
5555
const textWithoutComments = declarationText.replace(
5656
commentPattern,
5757
/* eslint-disable-next-line require-unicode-regexp -- we want to replace each code unit with a space */
58-
match => match.replace(/[^\n]/g, " "),
58+
match => match.replace(/[^\r\n\f]/g, " "),
5959
);
6060
const importantMatch =
6161
importantPattern.exec(textWithoutComments);

tests/rules/no-duplicate-imports.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,47 @@ ruleTester.run("no-duplicate-imports", rule, {
238238
},
239239
],
240240
},
241+
{
242+
code: "@import url('a.css');\r\n@import url('a.css');\r\n@import url('b.css');",
243+
output: "@import url('a.css');\r\n@import url('b.css');",
244+
errors: [
245+
{
246+
messageId: "duplicateImport",
247+
data: { url: "a.css" },
248+
line: 2,
249+
column: 1,
250+
endLine: 2,
251+
endColumn: 22,
252+
},
253+
],
254+
},
255+
{
256+
code: "@import url('a.css');\r@import url('a.css');\r@import url('b.css');",
257+
output: "@import url('a.css');\r@import url('b.css');",
258+
errors: [
259+
{
260+
messageId: "duplicateImport",
261+
data: { url: "a.css" },
262+
line: 2,
263+
column: 1,
264+
endLine: 2,
265+
endColumn: 22,
266+
},
267+
],
268+
},
269+
{
270+
code: "@import url('a.css');\f@import url('a.css');\f@import url('b.css');",
271+
output: "@import url('a.css');\f@import url('b.css');",
272+
errors: [
273+
{
274+
messageId: "duplicateImport",
275+
data: { url: "a.css" },
276+
line: 2,
277+
column: 1,
278+
endLine: 2,
279+
endColumn: 22,
280+
},
281+
],
282+
},
241283
],
242284
});

tests/rules/no-important.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,60 @@ ruleTester.run("no-important", rule, {
200200
},
201201
],
202202
},
203+
{
204+
code: "a { color: red\r\n!important; }",
205+
errors: [
206+
{
207+
messageId: "unexpectedImportant",
208+
line: 2,
209+
column: 1,
210+
endLine: 2,
211+
endColumn: 11,
212+
suggestions: [
213+
{
214+
messageId: "removeImportant",
215+
output: "a { color: red; }",
216+
},
217+
],
218+
},
219+
],
220+
},
221+
{
222+
code: "a { color: red\r!important; }",
223+
errors: [
224+
{
225+
messageId: "unexpectedImportant",
226+
line: 2,
227+
column: 1,
228+
endLine: 2,
229+
endColumn: 11,
230+
suggestions: [
231+
{
232+
messageId: "removeImportant",
233+
output: "a { color: red; }",
234+
},
235+
],
236+
},
237+
],
238+
},
239+
{
240+
code: "a { color: red\f!important; }",
241+
errors: [
242+
{
243+
messageId: "unexpectedImportant",
244+
line: 2,
245+
column: 1,
246+
endLine: 2,
247+
endColumn: 11,
248+
suggestions: [
249+
{
250+
messageId: "removeImportant",
251+
output: "a { color: red; }",
252+
},
253+
],
254+
},
255+
],
256+
},
203257
{
204258
code: dedent`
205259
a {

0 commit comments

Comments
 (0)