-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module.exports = { | ||
meta: { | ||
type: "layout", | ||
docs: { | ||
description: "enforce consistent indentation in `<template>`", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
}, | ||
fixable: "whitespace" | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode() | ||
const template = ( | ||
sourceCode.parserServices.getTemplateBodyTokenStore && | ||
sourceCode.parserServices.getTemplateBodyTokenStore() | ||
); | ||
|
||
function report(element, type, expected, actual) { | ||
const reportObj = { | ||
node: element, | ||
message: `Expected ${type} indentation of ${expected} spaces but found ${actual}` | ||
}; | ||
|
||
if (type === "attribute") { | ||
reportObj.fix = function(fixer) { | ||
const startToken = template.getTokenBefore(element); | ||
const endToken = template.getTokenAfter(element); | ||
const indent = " ".repeat(expected); | ||
|
||
return fixer.replaceTextRange( | ||
[startToken.range[1], element.range[0]], | ||
`\n${indent}` | ||
); | ||
}; | ||
} | ||
|
||
context.report(reportObj); | ||
} | ||
|
||
return context.parserServices.defineTemplateBodyVisitor({ | ||
VStartTag(node) { | ||
// Skip "template" tags | ||
if (node.parent.name === "template") { | ||
return; | ||
} | ||
|
||
// Skip singleline element (no indentation to check) | ||
if (node.loc.start.line === node.loc.end.line) { | ||
return; | ||
} | ||
|
||
// There is a shift of 1 space on the closing tag (likely caused by Pug) | ||
const actual = node.loc.end.column - 1; | ||
const expected = node.loc.start.column; | ||
|
||
// Check closing tag indentation | ||
if (actual !== expected) { | ||
report( | ||
node, | ||
"closing tag", | ||
|
||
expected, | ||
actual | ||
); | ||
} | ||
|
||
node.attributes.forEach((attribute) => { | ||
const actual = attribute.loc.start.column; | ||
const expected = node.loc.start.column + 2; | ||
|
||
// Check attribute indentation | ||
if (actual !== expected) { | ||
report( | ||
attribute, | ||
"attribute", | ||
|
||
expected, | ||
actual | ||
); | ||
} | ||
}); | ||
} | ||
}) | ||
} | ||
}; |