Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix no-eol-whitespace #4224

Merged
merged 3 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions lib/rules/no-eol-whitespace/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ testRule(rule, {
{
code:
"@media print {\n a {\n color: pink;\n }\n}\n\n@media screen {\n b { color: orange; }\n}"
},
{
code: "/* comment\n*/"
},
{
code: "/* comment\r\n*/\r\n"
}
],

Expand Down Expand Up @@ -383,6 +389,150 @@ testRule(rule, {
message: messages.rejected,
line: 1,
column: 16
},
{
code: "/* comment \n*/ \n",
fixed: "/* comment\n*/\n",
description: "comments fix properly (1)",
warnings: [
{
message: messages.rejected,
line: 1,
column: 16
},
{
message: messages.rejected,
line: 2,
column: 5
}
]
},
{
code: "/* comment \n*/ ",
fixed: "/* comment\n*/",
description: "comments fix properly (2)",
warnings: [
{
message: messages.rejected,
line: 1,
column: 16
},
{
message: messages.rejected,
line: 2,
column: 5
}
]
},
{
code: "/* comment \r\n*/ \r\n",
fixed: "/* comment\r\n*/\r\n",
description: "comments fix properly (3)",
warnings: [
{
message: messages.rejected,
line: 1,
column: 16
},
{
message: messages.rejected,
line: 2,
column: 5
}
]
},
{
code: "/* comment \r\n*/ ",
fixed: "/* comment\r\n*/",
description: "comments fix properly (4)",
warnings: [
{
message: messages.rejected,
line: 1,
column: 16
},
{
message: messages.rejected,
line: 2,
column: 5
}
]
},
{
code: "a\n{ color: red \n} ",
fixed: "a\n{ color: red\n}",
description: "fix properly without trailing EOL",
warnings: [
{
message: messages.rejected,
line: 2,
column: 13
},
{
message: messages.rejected,
line: 3,
column: 3
}
]
}
]
});

testRule(rule, {
ruleName,
config: [true],
syntax: "scss",
fix: true,

accept: [
{
code: "// comment 1\n",
description: "scss comment (1)"
},
{
code: "//\n//comment\n a\n{ color: red\n}",
description: "scss comment (2)"
}
],

reject: [
{
code: "// comment 2 ",
fixed: "// comment 2",
warnings: [
{
message: messages.rejected,
line: 1,
column: 14
}
]
},
{
code: "// comment 3 \r\n",
fixed: "// comment 3\r\n",
warnings: [
{
message: messages.rejected,
line: 1,
column: 14
}
]
},
{
code: "// comment 3 \ra {color: red} ",
fixed: "// comment 3\ra {color: red}",
warnings: [
{
message: messages.rejected,
line: 1,
column: 14
},
{
message: messages.rejected,
line: 1,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi looks like we don't support CR =(

column: 31
}
]
}
]
});
Expand Down
125 changes: 84 additions & 41 deletions lib/rules/no-eol-whitespace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ const messages = ruleMessages(ruleName, {

const whitespacesToReject = new Set([" ", "\t"]);

function fixString(str) {
return str.replace(/[ \t]+$/, "");
}

function findErrorStartIndex(
lastEOLIndex,
string,
{ ignoreEmptyLines, isRootFirst } = {
ignoreEmptyLines: false,
isRootFirst: false
}
) {
const eolWhitespaceIndex = lastEOLIndex - 1;

// If the character before newline is not whitespace, ignore
if (!whitespacesToReject.has(string[eolWhitespaceIndex])) {
return -1;
}

if (ignoreEmptyLines) {
// If there is only whitespace between the previous newline and
// this newline, ignore
const beforeNewlineIndex = string.lastIndexOf("\n", eolWhitespaceIndex);

if (beforeNewlineIndex >= 0 || isRootFirst) {
const line = string.substring(beforeNewlineIndex, eolWhitespaceIndex);

if (isOnlyWhitespace(line)) {
return -1;
}
}
}

return eolWhitespaceIndex;
}

const rule = function(on, options, context) {
return (root, result) => {
const validOptions = validateOptions(
Expand All @@ -36,25 +72,33 @@ const rule = function(on, options, context) {
return;
}

const ignoreEmptyLines = optionsMatches(options, "ignore", "empty-lines");

if (context.fix) {
fix(root);
}

const rootString = context.fix ? root.toString() : root.source.input.css;
const reportFromIndex = index => {
report({
message: messages.rejected,
node: root,
index,
result,
ruleName
});
};

eachEolWhitespace(
rootString,
index => {
report({
message: messages.rejected,
node: root,
index,
result,
ruleName
});
},
true
);
eachEolWhitespace(rootString, reportFromIndex, true);

const errorIndex = findErrorStartIndex(rootString.length, rootString, {
ignoreEmptyLines,
isRootFirst: true
});

if (errorIndex > -1) {
reportFromIndex(errorIndex);
}

/**
* Iterate each whitespace at the end of each line of the given string.
Expand All @@ -71,34 +115,14 @@ const rule = function(on, options, context) {
comments: "check"
},
match => {
const eolWhitespaceIndex = match.startIndex - 1;

// If the character before newline is not whitespace, ignore
if (!whitespacesToReject.has(string[eolWhitespaceIndex])) {
return;
}
const errorIndex = findErrorStartIndex(match.startIndex, string, {
ignoreEmptyLines,
isRootFirst
});

if (optionsMatches(options, "ignore", "empty-lines")) {
// If there is only whitespace between the previous newline and
// this newline, ignore
const beforeNewlineIndex = string.lastIndexOf(
"\n",
eolWhitespaceIndex
);

if (beforeNewlineIndex >= 0 || isRootFirst) {
const line = string.substring(
beforeNewlineIndex,
eolWhitespaceIndex
);

if (isOnlyWhitespace(line)) {
return;
}
}
if (errorIndex > -1) {
callback(errorIndex);
}

callback(eolWhitespaceIndex);
}
);
}
Expand Down Expand Up @@ -162,11 +186,19 @@ const rule = function(on, options, context) {
fixText(node.raws.left, fixed => {
node.raws.left = fixed;
});

if (node.raws.inline) {
node.raws.right = fixString(node.raws.right);
} else {
fixText(node.raws.right, fixed => {
node.raws.right = fixed;
});
}

fixText(node.text, fixed => {
node.text = fixed;
});

//
fixText(node.raws.after, fixed => {
node.raws.after = fixed;
});
Expand All @@ -179,6 +211,17 @@ const rule = function(on, options, context) {
},
isRootFirst
);

const lastEOL = Math.max(
root.raws.after.lastIndexOf("\n"),
root.raws.after.lastIndexOf("\r")
);

if (lastEOL !== root.raws.after.length - 1) {
root.raws.after =
root.raws.after.slice(0, lastEOL + 1) +
fixString(root.raws.after.slice(lastEOL + 1));
}
}

function fixText(value, fix, isRootFirst) {
Expand All @@ -194,7 +237,7 @@ const rule = function(on, options, context) {
index => {
const newlineIndex = index + 1;

fixed += value.slice(lastIndex, newlineIndex).replace(/[ \t]+$/, "");
fixed += fixString(value.slice(lastIndex, newlineIndex));
lastIndex = newlineIndex;
},
isRootFirst
Expand Down