\` beginning at L1:C2. Expected \`
\` to be at an indentation of 0, but was found at 2.",
"rule": "block-indentation",
@@ -956,6 +1063,12 @@ generateRuleTests({
' {{foobar.baz}}',
'{{/foo}}',
].join('\n'),
+ fixedTemplate: [
+ '{{#foo bar as |foobar|}}',
+ ' {{#foobar.baz}}{{/foobar.baz}}',
+ ' {{foobar.baz}}',
+ '{{/foo}}',
+ ].join('\n'),
verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
@@ -965,6 +1078,7 @@ generateRuleTests({
"endColumn": 8,
"endLine": 4,
"filePath": "layout.hbs",
+ "isFixable": true,
"line": 2,
"message": "Incorrect indentation for \`{{#foobar.baz}}\` beginning at L2:C3. Expected \`{{#foobar.baz}}\` to be at an indentation of 2 but was found at 3.",
"rule": "block-indentation",
@@ -979,6 +1093,7 @@ generateRuleTests({
"endColumn": 8,
"endLine": 4,
"filePath": "layout.hbs",
+ "isFixable": true,
"line": 3,
"message": "Incorrect indentation for \`{{foobar.baz}}\` beginning at L3:C3. Expected \`{{foobar.baz}}\` to be at an indentation of 2 but was found at 3.",
"rule": "block-indentation",
@@ -994,6 +1109,7 @@ generateRuleTests({
},
{
template: ['
', '', '
'].join('\n'),
+ fixedTemplate: ['
', ' ', '
'].join('\n'),
verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
Array [
@@ -1002,6 +1118,7 @@ generateRuleTests({
"endColumn": 6,
"endLine": 3,
"filePath": "layout.hbs",
+ "isFixable": true,
"line": 2,
"message": "Incorrect indentation for \`\` beginning at L2:C0. Expected \`\` to be at an indentation of 2 but was found at 0.",
"rule": "block-indentation",
@@ -1016,6 +1133,7 @@ generateRuleTests({
},
{
template: ['
', '{{! Comment }}', '
'].join('\n'),
+ fixedTemplate: ['
', ' {{! Comment }}', '
'].join('\n'),
verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
Array [
@@ -1024,6 +1142,7 @@ generateRuleTests({
"endColumn": 6,
"endLine": 3,
"filePath": "layout.hbs",
+ "isFixable": true,
"line": 2,
"message": "Incorrect indentation for \`{{! Comment }}\` beginning at L2:C0. Expected \`{{! Comment }}\` to be at an indentation of 2 but was found at 0.",
"rule": "block-indentation",
@@ -1038,6 +1157,7 @@ generateRuleTests({
},
{
template: ['
', 'test{{! Comment }}', '
'].join('\n'),
+ fixedTemplate: ['
', ' test{{! Comment }}', '
'].join('\n'),
config: {
ignoreComments: true,
},
@@ -1049,8 +1169,9 @@ generateRuleTests({
"endColumn": 6,
"endLine": 3,
"filePath": "layout.hbs",
- "line": 1,
- "message": "Incorrect indentation for \`test\` beginning at L1:C0. Expected \`test\` to be at an indentation of 2 but was found at 0.",
+ "isFixable": true,
+ "line": 2,
+ "message": "Incorrect indentation for \`test\` beginning at L2:C0. Expected \`test\` to be at an indentation of 2 but was found at 0.",
"rule": "block-indentation",
"severity": 2,
"source": "
From 0537cae6cbce4d869d8eb3022a666577206ff7b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vincent=20Molini=C3=A9?=
Date: Sat, 18 Dec 2021 13:28:01 +0100
Subject: [PATCH 2/8] chore: update readme
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index e7ee376af..6931375f9 100644
--- a/README.md
+++ b/README.md
@@ -87,8 +87,8 @@ Each rule has emojis denoting:
| Name | ✅ | 💅 | ⌨️ | 🔧 |
| :-------------------------------------------------------------------------------------------------------- | :-- | :-- | :-- | --- |
-| [attribute-indentation](./docs/rule/attribute-indentation.md) | | | | 🔧 |
-| [block-indentation](./docs/rule/block-indentation.md) | | 💅 | | |
+| [attribute-indentation](./docs/rule/attribute-indentation.md) | | | | |
+| [block-indentation](./docs/rule/block-indentation.md) | | 💅 | | 🔧 |
| [builtin-component-arguments](./docs/rule/builtin-component-arguments.md) | ✅ | | | |
| [deprecated-each-syntax](./docs/rule/deprecated-each-syntax.md) | | | | |
| [deprecated-inline-view-helper](./docs/rule/deprecated-inline-view-helper.md) | ✅ | | | |
From a327c23f1f002b15df2c44def75cb012353d05be Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vincent=20Molini=C3=A9?=
Date: Sat, 18 Dec 2021 13:50:47 +0100
Subject: [PATCH 3/8] chore: get rid of useless nodeToSource
---
lib/rules/block-indentation.js | 91 +++++++++++++++-------------------
1 file changed, 41 insertions(+), 50 deletions(-)
diff --git a/lib/rules/block-indentation.js b/lib/rules/block-indentation.js
index e0a342b5c..35f6eea97 100644
--- a/lib/rules/block-indentation.js
+++ b/lib/rules/block-indentation.js
@@ -176,7 +176,6 @@ module.exports = class BlockIndentation extends Rule {
visitor() {
this._elementStack = [];
this.seen = new Set();
- this.nodeToSource = new WeakMap();
return {
Template(node) {
@@ -215,7 +214,7 @@ module.exports = class BlockIndentation extends Rule {
// issues (since the column of the start block was already checked in the
// parent's validateBlockChildren())
if (node.loc.start.line === node.loc.end.line || this.seen.has(node)) {
- return nestedOptions ? nestedOptions.sourceToEdit : node;
+ return nestedOptions ? this.sourceEdited : node;
}
let fixedNode = this.validateBlockStart(node, nestedOptions);
@@ -224,10 +223,8 @@ module.exports = class BlockIndentation extends Rule {
fixedNode = this.validateBlockChildren(fixedNode, nestedOptions);
if (this.mode === 'fix') {
- const { sourceToEdit, path } = nestedOptions || { path: [] };
- const sourceToEditBeforeFix =
- sourceToEdit || this.nodeToSource.get(fixedNode) || this.source.join('');
- let sourceToEditFixed = sourceToEditBeforeFix;
+ const { path } = nestedOptions || { path: [] };
+ const sourceBeforeFix = this.sourceEdited || this.source.join('');
const childrenWithPath = [];
@@ -254,19 +251,15 @@ module.exports = class BlockIndentation extends Rule {
for (const { path, child } of childrenWithPath) {
if (['BlockStatement', 'ElementNode'].includes(child.type)) {
- sourceToEditFixed = this.process(child, {
- sourceToEdit: sourceToEditFixed,
+ this.process(child, {
path,
});
- if (nestedOptions) {
- nestedOptions.sourceToEdit = sourceToEditFixed;
- }
}
}
- if (sourceToEditBeforeFix !== sourceToEditFixed) {
+ if (sourceBeforeFix !== this.sourceEdited) {
fixedNode = this.fixLine(
- sourceToEditFixed.split('\n'),
+ this.sourceEdited.split('\n'),
0,
fixedNode.loc.start.column,
fixedNode.loc.start.column,
@@ -277,7 +270,7 @@ module.exports = class BlockIndentation extends Rule {
this.seen.add(fixedNode);
- return nestedOptions ? nestedOptions.sourceToEdit : fixedNode;
+ return nestedOptions ? this.sourceEdited : fixedNode;
}
validateBlockStart(node, nestedOptions = {}) {
@@ -294,12 +287,12 @@ module.exports = class BlockIndentation extends Rule {
if (startLine === 1 && startColumn !== 0) {
if (this.mode === 'fix') {
- const elementSource =
- nestedOptions.sourceToEdit ||
- sourceForLoc(this.nodeToSource.get(node) || this.source, {
- end: node.loc.end,
- start: { line: node.loc.start.line, column: 0 },
- });
+ const elementSource = nestedOptions.path
+ ? this.sourceEdited || this.source.join('')
+ : sourceForLoc(this.sourceEdited || this.source, {
+ end: node.loc.end,
+ start: { line: node.loc.start.line, column: 0 },
+ });
const fixedNode = this.fixLine(
elementSource.split('\n'),
0,
@@ -307,7 +300,6 @@ module.exports = class BlockIndentation extends Rule {
0,
nestedOptions.path
);
- nestedOptions.sourceToEdit = this.nodeToSource.get(fixedNode);
return fixedNode;
} else {
let isElementNode = node && node.type === 'ElementNode';
@@ -349,24 +341,20 @@ module.exports = class BlockIndentation extends Rule {
if (correctedEndColumn !== startColumn) {
if (this.mode === 'fix') {
- const elementSource =
- nestedOptions.sourceToEdit ||
- `${' '.repeat(node.loc.start.column)}${sourceForLoc(
- this.nodeToSource.get(node) || this.source,
- {
+ const elementSource = nestedOptions.path
+ ? this.sourceEdited || this.source.join('')
+ : `${' '.repeat(node.loc.start.column)}${sourceForLoc(this.sourceEdited || this.source, {
end: node.loc.end,
start: { line: node.loc.start.line, column: node.loc.start.column },
- }
- )}`;
+ })}`;
const lines = elementSource.split('\n');
const fixedNode = this.fixLine(
lines,
- node.loc.end.line - (nestedOptions.sourceToEdit ? 1 : node.loc.start.line),
+ node.loc.end.line - (nestedOptions.path ? 1 : node.loc.start.line),
correctedEndColumn,
startColumn,
nestedOptions.path
);
- nestedOptions.sourceToEdit = this.nodeToSource.get(fixedNode);
return fixedNode;
} else {
let startLocation = `L${node.loc.start.line}:C${node.loc.start.column}`;
@@ -497,12 +485,12 @@ module.exports = class BlockIndentation extends Rule {
if (expectedStartColumn !== childStartColumn) {
if (this.mode === 'fix') {
- const sourceToFix =
- nestedOptions.sourceToEdit ||
- sourceForLoc(this.nodeToSource.get(fixedNode) || this.source, {
- end: fixedNode.loc.end,
- start: { line: fixedNode.loc.start.line, column: 0 },
- });
+ const sourceToFix = nestedOptions.path
+ ? this.sourceEdited || this.source.join('')
+ : sourceForLoc(this.sourceEdited || this.source, {
+ end: fixedNode.loc.end,
+ start: { line: fixedNode.loc.start.line, column: 0 },
+ });
const lines = sourceToFix.split('\n');
fixedNode = this.fixLine(
@@ -512,7 +500,6 @@ module.exports = class BlockIndentation extends Rule {
expectedStartColumn,
nestedOptions.path
);
- nestedOptions.sourceToEdit = this.nodeToSource.get(fixedNode);
children = AstNodeInfo.childrenFor(fixedNode).filter((x) => !x._isElseIfBlock);
} else {
@@ -555,12 +542,12 @@ module.exports = class BlockIndentation extends Rule {
if (elseStartColumn !== startColumn) {
if (this.mode === 'fix') {
- const sourceToFix =
- nestedOptions.sourceToEdit ||
- sourceForLoc(this.nodeToSource.get(node) || this.source, {
- end: node.loc.end,
- start: { line: node.loc.start.line, column: 0 },
- });
+ const sourceToFix = nestedOptions.path
+ ? this.sourceEdited || this.source.join('')
+ : sourceForLoc(this.sourceEdited || this.source, {
+ end: node.loc.end,
+ start: { line: node.loc.start.line, column: 0 },
+ });
const lines = sourceToFix.split('\n');
const elseLine = node.program.loc.end.line;
const fixedNode = this.fixLine(
@@ -570,7 +557,6 @@ module.exports = class BlockIndentation extends Rule {
startColumn,
nestedOptions.path
);
- nestedOptions.sourceToEdit = this.nodeToSource.get(fixedNode);
return fixedNode;
} else {
let displayName = node.path.original;
@@ -622,26 +608,31 @@ module.exports = class BlockIndentation extends Rule {
fixLine(lines, lineNumber, actualColumn, expectedColumn, path) {
const line = lines[lineNumber];
- if (!line.startsWith(' '.repeat(actualColumn))) {
- lines[lineNumber] = `${line.slice(0, Math.max(0, actualColumn)).trimEnd()}\n${' '.repeat(
- expectedColumn
- )}${line.slice(Math.max(0, actualColumn))}`;
- } else {
+
+ // If line starts with only white spaces...
+ if (line.startsWith(' '.repeat(actualColumn))) {
+ // ... then it means we just need to fix the number of whitespace
lines[lineNumber] = actualColumn
? line.replace(/^ +/, ' '.repeat(expectedColumn))
: `${' '.repeat(expectedColumn)}${line}`;
+ } else {
+ // ... otherwise this mean the node is on the same line as another one so we need to add new line between the 2 nodes
+ lines[lineNumber] = `${line.slice(0, Math.max(0, actualColumn)).trimEnd()}\n${' '.repeat(
+ expectedColumn
+ )}${line.slice(Math.max(0, actualColumn))}`;
}
let node = parse(lines.join('\n')).body.find((node) => node.type !== 'TextNode');
node = get(node, path);
+ // As we recreate a new node the _isElseIfBlock is not set anymore so we need to recompute it
if (this.detectNestedElseIfBlock(node)) {
let elseBlockStatement = node.inverse.body[0];
elseBlockStatement._isElseIfBlock = true;
}
- this.nodeToSource.set(node, lines.join('\n'));
+ this.sourceEdited = lines.join('\n');
return node;
}
From 268688c53a55558db4a544cce4b54cc2dc2ca6dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vincent=20Molini=C3=A9?=
Date: Sat, 18 Dec 2021 13:54:13 +0100
Subject: [PATCH 4/8] chore: get rid of nestedOptions
---
lib/rules/block-indentation.js | 68 ++++++++++++++--------------------
1 file changed, 28 insertions(+), 40 deletions(-)
diff --git a/lib/rules/block-indentation.js b/lib/rules/block-indentation.js
index 35f6eea97..ba8c7f084 100644
--- a/lib/rules/block-indentation.js
+++ b/lib/rules/block-indentation.js
@@ -209,27 +209,27 @@ module.exports = class BlockIndentation extends Rule {
};
}
- process(node, nestedOptions) {
+ process(node, path) {
// Nodes that start and end on the same line cannot have any indentation
// issues (since the column of the start block was already checked in the
// parent's validateBlockChildren())
if (node.loc.start.line === node.loc.end.line || this.seen.has(node)) {
- return nestedOptions ? this.sourceEdited : node;
+ return path ? this.sourceEdited : node;
}
- let fixedNode = this.validateBlockStart(node, nestedOptions);
- fixedNode = this.validateBlockElse(fixedNode, nestedOptions);
- fixedNode = this.validateBlockEnd(fixedNode, nestedOptions);
- fixedNode = this.validateBlockChildren(fixedNode, nestedOptions);
+ let fixedNode = this.validateBlockStart(node, path);
+ fixedNode = this.validateBlockElse(fixedNode, path);
+ fixedNode = this.validateBlockEnd(fixedNode, path);
+ fixedNode = this.validateBlockChildren(fixedNode, path);
if (this.mode === 'fix') {
- const { path } = nestedOptions || { path: [] };
+ const currentPath = path || [];
const sourceBeforeFix = this.sourceEdited || this.source.join('');
const childrenWithPath = [];
if (fixedNode.inverse) {
- const inverseChildPath = [...path, 'inverse', 'body'];
+ const inverseChildPath = [...currentPath, 'inverse', 'body'];
for (const [index, inverseNestedNode] of fixedNode.inverse.body.entries()) {
childrenWithPath.push({
path: [...inverseChildPath, index],
@@ -239,7 +239,9 @@ module.exports = class BlockIndentation extends Rule {
}
const childPath =
- fixedNode.type === 'BlockStatement' ? [...path, 'program', 'body'] : [...path, 'children'];
+ fixedNode.type === 'BlockStatement'
+ ? [...currentPath, 'program', 'body']
+ : [...currentPath, 'children'];
const children =
fixedNode.type === 'BlockStatement' ? fixedNode.program.body : fixedNode.children;
for (const [index, nestedNode] of children.entries()) {
@@ -249,11 +251,9 @@ module.exports = class BlockIndentation extends Rule {
});
}
- for (const { path, child } of childrenWithPath) {
+ for (const { path: childPath, child } of childrenWithPath) {
if (['BlockStatement', 'ElementNode'].includes(child.type)) {
- this.process(child, {
- path,
- });
+ this.process(child, childPath);
}
}
@@ -263,17 +263,17 @@ module.exports = class BlockIndentation extends Rule {
0,
fixedNode.loc.start.column,
fixedNode.loc.start.column,
- nestedOptions && nestedOptions.path
+ path
);
}
}
this.seen.add(fixedNode);
- return nestedOptions ? this.sourceEdited : fixedNode;
+ return path ? this.sourceEdited : fixedNode;
}
- validateBlockStart(node, nestedOptions = {}) {
+ validateBlockStart(node, path) {
if (!this.shouldValidateBlockEnd(node)) {
return node;
}
@@ -287,19 +287,13 @@ module.exports = class BlockIndentation extends Rule {
if (startLine === 1 && startColumn !== 0) {
if (this.mode === 'fix') {
- const elementSource = nestedOptions.path
+ const elementSource = path
? this.sourceEdited || this.source.join('')
: sourceForLoc(this.sourceEdited || this.source, {
end: node.loc.end,
start: { line: node.loc.start.line, column: 0 },
});
- const fixedNode = this.fixLine(
- elementSource.split('\n'),
- 0,
- startColumn,
- 0,
- nestedOptions.path
- );
+ const fixedNode = this.fixLine(elementSource.split('\n'), 0, startColumn, 0, path);
return fixedNode;
} else {
let isElementNode = node && node.type === 'ElementNode';
@@ -321,7 +315,7 @@ module.exports = class BlockIndentation extends Rule {
return node;
}
- validateBlockEnd(node, nestedOptions = {}) {
+ validateBlockEnd(node, path) {
if (!this.shouldValidateBlockEnd(node)) {
return node;
}
@@ -341,7 +335,7 @@ module.exports = class BlockIndentation extends Rule {
if (correctedEndColumn !== startColumn) {
if (this.mode === 'fix') {
- const elementSource = nestedOptions.path
+ const elementSource = path
? this.sourceEdited || this.source.join('')
: `${' '.repeat(node.loc.start.column)}${sourceForLoc(this.sourceEdited || this.source, {
end: node.loc.end,
@@ -350,10 +344,10 @@ module.exports = class BlockIndentation extends Rule {
const lines = elementSource.split('\n');
const fixedNode = this.fixLine(
lines,
- node.loc.end.line - (nestedOptions.path ? 1 : node.loc.start.line),
+ node.loc.end.line - (path ? 1 : node.loc.start.line),
correctedEndColumn,
startColumn,
- nestedOptions.path
+ path
);
return fixedNode;
} else {
@@ -378,7 +372,7 @@ module.exports = class BlockIndentation extends Rule {
return node;
}
- validateBlockChildren(node, nestedOptions = {}) {
+ validateBlockChildren(node, path) {
if (this.isWithinIgnoredElement()) {
return node;
}
@@ -485,7 +479,7 @@ module.exports = class BlockIndentation extends Rule {
if (expectedStartColumn !== childStartColumn) {
if (this.mode === 'fix') {
- const sourceToFix = nestedOptions.path
+ const sourceToFix = path
? this.sourceEdited || this.source.join('')
: sourceForLoc(this.sourceEdited || this.source, {
end: fixedNode.loc.end,
@@ -498,7 +492,7 @@ module.exports = class BlockIndentation extends Rule {
childStartLine - 1,
childStartColumn,
expectedStartColumn,
- nestedOptions.path
+ path
);
children = AstNodeInfo.childrenFor(fixedNode).filter((x) => !x._isElseIfBlock);
@@ -525,7 +519,7 @@ module.exports = class BlockIndentation extends Rule {
return fixedNode;
}
- validateBlockElse(node, nestedOptions = {}) {
+ validateBlockElse(node, path) {
if (node.type !== 'BlockStatement' || !node.inverse) {
return node;
}
@@ -542,7 +536,7 @@ module.exports = class BlockIndentation extends Rule {
if (elseStartColumn !== startColumn) {
if (this.mode === 'fix') {
- const sourceToFix = nestedOptions.path
+ const sourceToFix = path
? this.sourceEdited || this.source.join('')
: sourceForLoc(this.sourceEdited || this.source, {
end: node.loc.end,
@@ -550,13 +544,7 @@ module.exports = class BlockIndentation extends Rule {
});
const lines = sourceToFix.split('\n');
const elseLine = node.program.loc.end.line;
- const fixedNode = this.fixLine(
- lines,
- elseLine - 1,
- elseStartColumn,
- startColumn,
- nestedOptions.path
- );
+ const fixedNode = this.fixLine(lines, elseLine - 1, elseStartColumn, startColumn, path);
return fixedNode;
} else {
let displayName = node.path.original;
From ffda9d003e0b1f1d5e7bd1ba4062409ec06afcf2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vincent=20Molini=C3=A9?=
Date: Sat, 18 Dec 2021 14:07:10 +0100
Subject: [PATCH 5/8] chore: add comments
---
lib/rules/block-indentation.js | 29 ++++++++++++++++++++++++++---
1 file changed, 26 insertions(+), 3 deletions(-)
diff --git a/lib/rules/block-indentation.js b/lib/rules/block-indentation.js
index ba8c7f084..0f751ced3 100644
--- a/lib/rules/block-indentation.js
+++ b/lib/rules/block-indentation.js
@@ -83,6 +83,11 @@ function isControlChar(char) {
return char === '~' || char === '{' || char === '}';
}
+/**
+ * @param {Node} node
+ * @param {String[]} path - A list of property to search for in the node
+ * @returns {Node}
+ */
function get(node, path) {
let value = node;
@@ -95,6 +100,10 @@ function get(node, path) {
return value;
}
+function removeWhitespaceEnd(str) {
+ return str.replace(/ +$/, '');
+}
+
module.exports = class BlockIndentation extends Rule {
parseConfig(config) {
let configType = typeof config;
@@ -183,7 +192,7 @@ module.exports = class BlockIndentation extends Rule {
if (
node.body[0] &&
node.body[0].type === 'TextNode' &&
- !node.body[0].chars.replace(/ +/, '')
+ !removeWhitespaceEnd(node.body[0].chars)
) {
// We need to remove the text node in this case
node.body.shift();
@@ -209,12 +218,18 @@ module.exports = class BlockIndentation extends Rule {
};
}
+ /**
+ *
+ * @param {Node} node - Current node or parent node if path is set
+ * @param {String[]} path - path of the node being currently edited
+ * @returns {Node}
+ */
process(node, path) {
// Nodes that start and end on the same line cannot have any indentation
// issues (since the column of the start block was already checked in the
// parent's validateBlockChildren())
if (node.loc.start.line === node.loc.end.line || this.seen.has(node)) {
- return path ? this.sourceEdited : node;
+ return node;
}
let fixedNode = this.validateBlockStart(node, path);
@@ -222,6 +237,8 @@ module.exports = class BlockIndentation extends Rule {
fixedNode = this.validateBlockEnd(fixedNode, path);
fixedNode = this.validateBlockChildren(fixedNode, path);
+ // If we are in fix mode we need to also fix all the children right now as fixing a child node
+ // might introduce new line and so change the position ending of its parent
if (this.mode === 'fix') {
const currentPath = path || [];
const sourceBeforeFix = this.sourceEdited || this.source.join('');
@@ -251,12 +268,14 @@ module.exports = class BlockIndentation extends Rule {
});
}
+ // Process all the child with the path to access the child from the current node
for (const { path: childPath, child } of childrenWithPath) {
if (['BlockStatement', 'ElementNode'].includes(child.type)) {
this.process(child, childPath);
}
}
+ // Process all the child with the path to access the child from the current node
if (sourceBeforeFix !== this.sourceEdited) {
fixedNode = this.fixLine(
this.sourceEdited.split('\n'),
@@ -270,7 +289,7 @@ module.exports = class BlockIndentation extends Rule {
this.seen.add(fixedNode);
- return path ? this.sourceEdited : fixedNode;
+ return fixedNode;
}
validateBlockStart(node, path) {
@@ -287,6 +306,7 @@ module.exports = class BlockIndentation extends Rule {
if (startLine === 1 && startColumn !== 0) {
if (this.mode === 'fix') {
+ // If we are in a child (path set) we want to edit directly the source and not the source of the node
const elementSource = path
? this.sourceEdited || this.source.join('')
: sourceForLoc(this.sourceEdited || this.source, {
@@ -335,6 +355,7 @@ module.exports = class BlockIndentation extends Rule {
if (correctedEndColumn !== startColumn) {
if (this.mode === 'fix') {
+ // If we are in a child (path set) we want to edit directly the source and not the source of the node
const elementSource = path
? this.sourceEdited || this.source.join('')
: `${' '.repeat(node.loc.start.column)}${sourceForLoc(this.sourceEdited || this.source, {
@@ -479,6 +500,7 @@ module.exports = class BlockIndentation extends Rule {
if (expectedStartColumn !== childStartColumn) {
if (this.mode === 'fix') {
+ // If we are in a child (path set) we want to edit directly the source and not the source of the node
const sourceToFix = path
? this.sourceEdited || this.source.join('')
: sourceForLoc(this.sourceEdited || this.source, {
@@ -536,6 +558,7 @@ module.exports = class BlockIndentation extends Rule {
if (elseStartColumn !== startColumn) {
if (this.mode === 'fix') {
+ // If we are in a child (path set) we want to edit directly the source and not the source of the node
const sourceToFix = path
? this.sourceEdited || this.source.join('')
: sourceForLoc(this.sourceEdited || this.source, {
From af67a6d7690e67c465f538f64867b2f33c704c40 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vincent=20Molini=C3=A9?=
Date: Sat, 18 Dec 2021 14:09:25 +0100
Subject: [PATCH 6/8] chore: remove useless condition
---
lib/rules/block-indentation.js | 10 +---------
1 file changed, 1 insertion(+), 9 deletions(-)
diff --git a/lib/rules/block-indentation.js b/lib/rules/block-indentation.js
index 0f751ced3..9fc8a045b 100644
--- a/lib/rules/block-indentation.js
+++ b/lib/rules/block-indentation.js
@@ -297,10 +297,6 @@ module.exports = class BlockIndentation extends Rule {
return node;
}
- if (this.isWithinIgnoredElement()) {
- return node;
- }
-
let startColumn = node.loc.start.column;
let startLine = node.loc.start.line;
@@ -340,10 +336,6 @@ module.exports = class BlockIndentation extends Rule {
return node;
}
- if (this.isWithinIgnoredElement()) {
- return node;
- }
-
let isElementNode = node && node.type === 'ElementNode';
let displayName = isElementNode ? node.tag : node.path.original;
let display = isElementNode ? `${displayName}>` : `{{/${displayName}}}`;
@@ -676,7 +668,7 @@ module.exports = class BlockIndentation extends Rule {
}
if (this.isWithinIgnoredElement()) {
- return;
+ return false;
}
// do not validate nodes without children (whitespace will count as TextNodes)
From 5ec705fe11dd5d9a8b03103a486bbe557e650308 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vincent=20Molini=C3=A9?=
Date: Sun, 19 Dec 2021 09:36:27 +0100
Subject: [PATCH 7/8] chore: add example
---
lib/rules/block-indentation.js | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/lib/rules/block-indentation.js b/lib/rules/block-indentation.js
index 9fc8a045b..da6def14c 100644
--- a/lib/rules/block-indentation.js
+++ b/lib/rules/block-indentation.js
@@ -84,9 +84,14 @@ function isControlChar(char) {
}
/**
+ * This function allows to get a node inside a node given a path
+ *
* @param {Node} node
* @param {String[]} path - A list of property to search for in the node
* @returns {Node}
+ *
+ * @example
+ * get(node, ['program', 'body', 1]) === node.program.body[1]
*/
function get(node, path) {
let value = node;
From 179bc67ed3ce50034a2ba424c2455fffc815fa26 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vincent=20Molini=C3=A9?=
Date: Fri, 24 Dec 2021 14:23:51 +0100
Subject: [PATCH 8/8] chore: review fixes
---
lib/rules/block-indentation.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/rules/block-indentation.js b/lib/rules/block-indentation.js
index da6def14c..59ac80e00 100644
--- a/lib/rules/block-indentation.js
+++ b/lib/rules/block-indentation.js
@@ -280,7 +280,7 @@ module.exports = class BlockIndentation extends Rule {
}
}
- // Process all the child with the path to access the child from the current node
+ // Recreate the node only if the source has changed, otherwise this would mean useless computation and possible mistake
if (sourceBeforeFix !== this.sourceEdited) {
fixedNode = this.fixLine(
this.sourceEdited.split('\n'),