Skip to content

Commit

Permalink
Fix block-indentation rule for embedded templates
Browse files Browse the repository at this point in the history
  • Loading branch information
robinborst95 committed Mar 8, 2023
1 parent 50c1bad commit 23e6fa3
Show file tree
Hide file tree
Showing 2 changed files with 390 additions and 13 deletions.
51 changes: 38 additions & 13 deletions lib/rules/block-indentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ function get(node, path) {
return value;
}

function removeWhitespaceEnd(str) {
return str.replace(/ +$/, '');
function addWhitespaceEnd(str, count) {
return `${str}${' '.repeat(count)}`;
}

function removeWhitespaceEnd(str, count) {
return str.replace(new RegExp(` {${count}}$`), '');
}

export default class BlockIndentation extends Rule {
Expand Down Expand Up @@ -306,13 +310,20 @@ export default class BlockIndentation extends Rule {
const hasLeadingContent =
!isDirectChildOfTemplate || this.hasLeadingContent(node, this.template.body);
const startColumn = node.loc.start.column;
const expectedStartColumn =
this.columnOffset === 0 || node.loc.start.line === 1
? 0
: this.columnOffset + this.config.indentation;

// If it's not a direct child of the template the block start is already fixed by the validateBlockChildren function
if (isDirectChildOfTemplate && startColumn !== 0 && !hasLeadingContent) {
if (isDirectChildOfTemplate && startColumn !== expectedStartColumn && !hasLeadingContent) {
if (this.mode === 'fix') {
const previousNode = this.template.body[indexOfNodeInTemplate - 1];
if (previousNode.type === 'TextNode') {
previousNode.chars = removeWhitespaceEnd(previousNode.chars);
previousNode.chars =
startColumn > expectedStartColumn
? removeWhitespaceEnd(previousNode.chars, startColumn - expectedStartColumn)
: addWhitespaceEnd(previousNode.chars, expectedStartColumn - startColumn);
}
} else {
let isElementNode = node && node.type === 'ElementNode';
Expand All @@ -322,7 +333,7 @@ export default class BlockIndentation extends Rule {

let warning =
`Incorrect indentation for \`${display}\` beginning at ${startLocation}. ` +
`Expected \`${display}\` to be at an indentation of 0, but was found at ${startColumn}.`;
`Expected \`${display}\` to be at an indentation of ${expectedStartColumn}, but was found at ${startColumn}.`;

this.log({
message: warning,
Expand All @@ -347,8 +358,12 @@ export default class BlockIndentation extends Rule {

let controlCharCount = this.endingControlCharCount(node);
let correctedEndColumn = endColumn - displayName.length - controlCharCount;
let expectedEndColumn =
this.columnOffset === 0 || node.loc.end.line !== this.template.loc.end.line
? startColumn
: this.columnOffset;

if (correctedEndColumn !== startColumn) {
if (correctedEndColumn !== expectedEndColumn) {
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
Expand All @@ -362,7 +377,7 @@ export default class BlockIndentation extends Rule {
lines,
node.loc.end.line - (path ? 1 : node.loc.start.line),
correctedEndColumn,
startColumn,
expectedEndColumn,
path
);
return fixedNode;
Expand All @@ -372,7 +387,7 @@ export default class BlockIndentation extends Rule {

let warning =
`Incorrect indentation for \`${displayName}\` beginning at ${startLocation}` +
`. Expected \`${display}\` ending at ${endLocation} to be at an indentation of ${startColumn} but ` +
`. Expected \`${display}\` ending at ${endLocation} to be at an indentation of ${expectedEndColumn} but ` +
`was found at ${correctedEndColumn}.`;

this.log({
Expand Down Expand Up @@ -405,7 +420,9 @@ export default class BlockIndentation extends Rule {
}

let startColumn = node.loc.start.column;
let expectedStartColumn = startColumn + this.config.indentation;
let correctedStartColumn =
this.columnOffset > 0 && node.loc.start.line === 1 ? this.columnOffset : startColumn;
let expectedStartColumn = correctedStartColumn + this.config.indentation;
let fixedNode = node;

let i = 0;
Expand Down Expand Up @@ -477,7 +494,7 @@ export default class BlockIndentation extends Rule {

fixedNode = this.fixLine(
lines,
childStartLine - 1,
childStartLine - (path ? 1 : fixedNode.loc.start.line),
childStartColumn,
expectedStartColumn,
path
Expand Down Expand Up @@ -520,9 +537,11 @@ export default class BlockIndentation extends Rule {

let inverse = node.inverse;
let startColumn = node.loc.start.column;
let expectedStartColumn =
this.columnOffset > 0 && node.loc.start.line === 1 ? this.columnOffset : startColumn;
let elseStartColumn = node.program.loc.end.column;

if (elseStartColumn !== startColumn) {
if (elseStartColumn !== expectedStartColumn) {
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
Expand All @@ -533,7 +552,13 @@ export default 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, path);
const fixedNode = this.fixLine(
lines,
elseLine - 1,
elseStartColumn,
expectedStartColumn,
path
);
return fixedNode;
} else {
let displayName = node.path.original;
Expand All @@ -542,7 +567,7 @@ export default class BlockIndentation extends Rule {

let warning =
`Incorrect indentation for inverse block of \`{{#${displayName}}}\` beginning at ${startLocation}` +
`. Expected \`{{else}}\` starting at ${elseLocation} to be at an indentation of ${startColumn} but ` +
`. Expected \`{{else}}\` starting at ${elseLocation} to be at an indentation of ${expectedStartColumn} but ` +
`was found at ${elseStartColumn}.`;

this.log({
Expand Down

0 comments on commit 23e6fa3

Please sign in to comment.