Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/rules/indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ Or, for tabbed indentation:
}
```

Object option for template literals:

```json
{
"@html-eslint/indent": ["error", 2, {
"skipTemplateStartIndent": true // Skip starting indent from JS code
}]
}
```

### skipTemplateStartIndent

When enabled, this option ignores the leading indentation from the JS code that appears before the template literal start (e.g. code on the same line as the opening backtick). This prevents the JS line's indentation from increasing the base indent level of the template content.

Example with `skipTemplateStartIndent: false` (default):
```js
const html = /*html*/ `
<div>yeah</div> // indented based on JS code's indentation
`;
```

Example with `skipTemplateStartIndent: true`:
```js
const html = /*html*/ `
<div>yeah</div> // no extra indent from JS code
`;
```

Examples of **incorrect** code for this rule with the default option:

```html,incorrect
Expand Down
24 changes: 22 additions & 2 deletions packages/eslint-plugin/lib/rules/indent/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @typedef {"tab" | number} Option1
* @typedef {Object} Option2
* @property {number} [Option2.Attribute]
* @property {boolean} [Option2.skipTemplateStartIndent]
* @property {Record<string, number>} [Option2.tagChildrenIndent]
*/

Expand Down Expand Up @@ -86,6 +87,15 @@ module.exports = {
minimum: 1,
default: 1,
},
// When true, ignore the leading indentation from the JS code
// that appears before the template literal start (e.g. code on
// the same line as the opening backtick). This prevents the
// JS line's indentation from increasing the base indent level
// of the template content.
skipTemplateStartIndent: {
type: "boolean",
default: false,
},
tagChildrenIndent: {
default: {},
type: "object",
Expand Down Expand Up @@ -164,8 +174,18 @@ module.exports = {
// @ts-ignore
const lineIndex = node.loc.start.line - 1;
const line = lines[lineIndex];

const spaceCount = countLeftPadding(line);
// If configured to skip the starting indent of the JS code that
// appears before the template literal start (e.g. `let x = /*html*/ \``),
// then count left padding from the template literal's start column
// rather than the whole line. This effectively ignores the leading
// JS indentation for base calculation.
const startColumn = node.loc.start.column;
const relevant =
indentLevelOptions && indentLevelOptions.skipTemplateStartIndent
? line.slice(startColumn)
: line;

const spaceCount = countLeftPadding(relevant);
if (indentType === "space") {
return Math.floor(spaceCount / indentSize) + 1;
} else {
Expand Down
49 changes: 49 additions & 0 deletions packages/eslint-plugin/tests/rules/indent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,55 @@ class Component extends LitElement {
}
`,
output: `

class Component extends LitElement {
render() {
return html\`
<p>content</p>
\`;
}
}
`,
errors: wrongIndentErrors(1),
},
// Tests for skipTemplateStartIndent option
{
code: `
let html = /*html*/ \`
<div>yeah</div>
\`
`,
output: `
let html = /*html*/ \`
<div>yeah</div>
\`
`,
options: [2],
errors: wrongIndentErrors(1),
},
{
code: `
let html = /*html*/ \`
<div>yeah</div>
\`
`,
options: [2, { skipTemplateStartIndent: true }],
errors: [],
},
{
code: `
const obj = {
nested: {
html: /*html*/ \`
<div>yeah</div>
\`
}
}`,
options: [2, { skipTemplateStartIndent: true }],
errors: [],
},
{
code: `
class Component extends LitElement {
render() {
return html\`
Expand Down
Loading
Loading