Skip to content

Commit

Permalink
v1.0.86
Browse files Browse the repository at this point in the history
New rule
  • Loading branch information
eliottvincent committed Jun 22, 2024
1 parent 768352f commit f3800a1
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ Each item has emojis denoting:
| [crisp/vue-computed-order](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-computed-order.js) | Ensures computed properties are alphabetically ordered | | 🟢 |
| [crisp/vue-emits-order](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-emits-order.js) | Ensures emits properties are alphabetically ordered | | 🟢 |
| [crisp/vue-header-check](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-header-check.js) | Ensures `script`, `template` and `style` tags start with corresponding comment block | | 🟢 |
| [crisp/vue-html-indent](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-html-indent.js) | Enforces consistent indentation in `template` (supports for Pug) | | 🟢 |
| [crisp/vue-html-quotes](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-html-quotes.js) | Enforces HTML attributes to be enclosed with double quotes | | 🟢 |
| [crisp/vue-no-regex-data](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-no-regex-data.js) | Disallows regular expressions to be declared in Vue data object | | 🟢 |
| [crisp/vue-props-declaration-multiline](https://github.com/crisp-oss/eslint-plugin-crisp/blob/master/rules/vue-props-declaration-multiline.js) | Enforces props declarations to be multiline | | 🟢 |
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
"vue-computed-order": require("./rules/vue-computed-order"),
"vue-emits-order": require("./rules/vue-emits-order"),
"vue-header-check": require("./rules/vue-header-check"),
"vue-html-indent": require("./rules/vue-html-indent"),
"vue-html-quotes": require("./rules/vue-html-quotes"),
"vue-no-regex-data": require("./rules/vue-no-regex-data"),
"vue-props-declaration-order": require("./rules/vue-props-declaration-order"),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-crisp",
"version": "1.0.85",
"version": "1.0.86",
"description": "Custom ESLint Rules for Crisp",
"author": "Crisp IM SAS",
"main": "index.js",
Expand Down
1 change: 1 addition & 0 deletions recommended-vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ module.exports = {
"crisp/vue-computed-order": "error",
"crisp/vue-emits-order": "error",
"crisp/vue-header-check": "error",
"crisp/vue-html-indent": "error",
"crisp/vue-html-quotes": "error",
"crisp/vue-no-regex-data": "error",
"crisp/vue-props-declaration-multiline": "error",
Expand Down
86 changes: 86 additions & 0 deletions rules/vue-html-indent.js
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
);
}
});
}
})
}
};

0 comments on commit f3800a1

Please sign in to comment.