Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Improve location for no-mixed-spaces-and-tabs (refs #12334) (#…
  • Loading branch information
mdjermanovic committed May 31, 2020
1 parent f858f2a commit cb50b69
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 21 deletions.
20 changes: 14 additions & 6 deletions lib/rules/no-mixed-spaces-and-tabs.js
Expand Up @@ -67,27 +67,35 @@ module.exports = {
* or the reverse before non-tab/-space
* characters begin.
*/
let regex = /^(?=[\t ]*(\t | \t))/u;
let regex = /^(?=( +|\t+))\1(?:\t| )/u;

if (smartTabs) {

/*
* At least one space followed by a tab
* before non-tab/-space characters begin.
*/
regex = /^(?=[\t ]* \t)/u;
regex = /^(?=(\t*))\1(?=( +))\2\t/u;
}

lines.forEach((line, i) => {
const match = regex.exec(line);

if (match) {
const lineNumber = i + 1,
column = match.index + 1,
loc = { line: lineNumber, column };
const lineNumber = i + 1;
const loc = {
start: {
line: lineNumber,
column: match[0].length - 2
},
end: {
line: lineNumber,
column: match[0].length
}
};

if (!ignoredCommentLines.has(lineNumber)) {
const containingNode = sourceCode.getNodeByRangeIndex(sourceCode.getIndexFromLoc(loc));
const containingNode = sourceCode.getNodeByRangeIndex(sourceCode.getIndexFromLoc(loc.start));

if (!(containingNode && ["Literal", "TemplateElement"].includes(containingNode.type))) {
context.report({
Expand Down
178 changes: 163 additions & 15 deletions tests/lib/rules/no-mixed-spaces-and-tabs.js
Expand Up @@ -20,8 +20,15 @@ const ruleTester = new RuleTester();
ruleTester.run("no-mixed-spaces-and-tabs", rule, {

valid: [
"foo",
"foo \t",
"foo\t ",
"\tvar x = 5;",
"\t\tvar x = 5;",
" var x = 5;",
" var x = 5;",
" foo\t",
"\tfoo ",
"\t/*\n\t * Hello\n\t */",
"// foo\n\t/**\n\t * Hello\n\t */",
"/*\n\n \t \n*/",
Expand Down Expand Up @@ -80,6 +87,50 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
code: "\tvar x = 5,\n\t y = 2;",
options: ["smart-tabs"]
},
{
code: "\t\t\t foo",
options: ["smart-tabs"]
},
{
code: "foo",
options: ["smart-tabs"]
},
{
code: "foo \t",
options: ["smart-tabs"]
},
{
code: "foo\t ",
options: ["smart-tabs"]
},
{
code: "\tfoo \t",
options: ["smart-tabs"]
},
{
code: "\tvar x = 5;",
options: ["smart-tabs"]
},
{
code: "\t\tvar x = 5;",
options: ["smart-tabs"]
},
{
code: " var x = 5;",
options: ["smart-tabs"]
},
{
code: " var x = 5;",
options: ["smart-tabs"]
},
{
code: " foo\t",
options: ["smart-tabs"]
},
{
code: "\tfoo ",
options: ["smart-tabs"]
}
],

Expand All @@ -90,7 +141,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 2
line: 2,
column: 1,
endLine: 2,
endColumn: 3
}
]
},
Expand All @@ -100,7 +154,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1
line: 1,
column: 1,
endLine: 1,
endColumn: 3
}
]
},
Expand All @@ -110,7 +167,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1
line: 1,
column: 1,
endLine: 1,
endColumn: 3
}
]
},
Expand All @@ -120,7 +180,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1
line: 1,
column: 1,
endLine: 1,
endColumn: 3
}
]
},
Expand All @@ -130,7 +193,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1
line: 1,
column: 1,
endLine: 1,
endColumn: 3
}
]
},
Expand All @@ -140,7 +206,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1
line: 1,
column: 1,
endLine: 1,
endColumn: 3
}
]
},
Expand All @@ -150,7 +219,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 2
line: 2,
column: 1,
endLine: 2,
endColumn: 3
}
]
},
Expand All @@ -160,12 +232,18 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1
line: 1,
column: 1,
endLine: 1,
endColumn: 3
},
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 3
line: 3,
column: 1,
endLine: 3,
endColumn: 3
}
]
},
Expand All @@ -176,7 +254,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 2
line: 2,
column: 2,
endLine: 2,
endColumn: 4
}
]
},
Expand All @@ -187,7 +268,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 2
line: 2,
column: 2,
endLine: 2,
endColumn: 4
}
]
},
Expand All @@ -200,7 +284,9 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 2,
column: 2
column: 1,
endLine: 2,
endColumn: 3
}
]
},
Expand All @@ -212,7 +298,9 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 2,
column: 2
column: 1,
endLine: 2,
endColumn: 3
}
]
},
Expand All @@ -222,7 +310,10 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1
line: 1,
column: 2,
endLine: 1,
endColumn: 4
}
]
},
Expand All @@ -232,7 +323,64 @@ ruleTester.run("no-mixed-spaces-and-tabs", rule, {
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 2
line: 2,
column: 1,
endLine: 2,
endColumn: 3
}
]
},
{
code: " \tfoo",
errors: [
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1,
column: 3,
endLine: 1,
endColumn: 5
}
]
},
{
code: "\t\t\t foo",
errors: [
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1,
column: 3,
endLine: 1,
endColumn: 5
}
]
},
{
code: "\t \tfoo",
options: ["smart-tabs"],
errors: [
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1,
column: 2,
endLine: 1,
endColumn: 4
}
]
},
{
code: "\t\t\t \tfoo",
options: ["smart-tabs"],
errors: [
{
messageId: "mixedSpacesAndTabs",
type: "Program",
line: 1,
column: 6,
endLine: 1,
endColumn: 8
}
]
}
Expand Down

0 comments on commit cb50b69

Please sign in to comment.