Skip to content

Commit

Permalink
Add support for relative or infer indent level in CSS blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Aug 17, 2018
1 parent 2301a48 commit e53f6af
Show file tree
Hide file tree
Showing 4 changed files with 552 additions and 77 deletions.
19 changes: 19 additions & 0 deletions lib/rules/indentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ a {

## Optional secondary options

### `baseIndentLevel: int|"auto"`

By default, the indent level of the CSS code block in non-CSS-like files is determined by the shortest non-empty line indented. The setting `baseIndentLevel` allows you to define a relative indent level based on CSS code block opening or closing line.

For example, with `[ 2, { baseIndentLevel: 1 } ]`, CSS should be indented 1 levels higher than `<style>` tag:

```html
<!DOCTYPE html>
<html lang="en">
<head>
<style>
a {
display: block;
}
</style>
</head>
</html>
```

### `indentInsideParens: "twice"|"once-at-root-twice-in-block"`

By default, *one extra* indentation (of your specified type) is expected after newlines inside parentheses, and the closing parenthesis is expected to have no extra indentation.
Expand Down
225 changes: 216 additions & 9 deletions lib/rules/indentation/__tests__/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ a {
\tdisplay:block;
}
</style>`
},
{
code: `
<style>a {
\tdisplay:block;
}
</style>`
},
{
code: '<a style="display:block; color:red;"></a>'
}
],

Expand All @@ -48,6 +58,23 @@ a {
},
{
code: `
<style>
a {
display:block;
}
</style>`,
fixed: `
<style>
\ta {
\t\tdisplay:block;
\t}
</style>`,
message: messages.expected("1 tab"),
line: 3,
column: 3
},
{
code: `
<style>
a {
display:block;
Expand Down Expand Up @@ -83,23 +110,203 @@ a {
{
code: `
\t<style>
\t a {
\t display:block;
\t }
\t</style>
a {
display:block;
}
b {
display:block;
}
\t</style>`,
fixed: `
\t<style>
\ta {
\t\tdisplay:block;
\t}
\tb {
\t\tdisplay:block;
\t}
\t</style>`,
message: messages.expected("1 tab"),
line: 3,
column: 5
}
]
});

testRule(rule, {
ruleName,
config: [2],
syntax: "html",
fix: true,

accept: [
{
code: `
<style>
a {
display:block;
}
</style>`
},
{
code: `
<style>
a {
display:block;
}
</style>`
},
{
code: `
<style>a {
display:block;
}
</style>`
}
],
reject: [
{
code: `
<style>a {
display:block;
}
</style>`,
fixed: `
<style>a {
display:block;
}
</style>`,
message: messages.expected("2 spaces"),
line: 3,
column: 2
}
]
});

testRule(rule, {
ruleName,
config: [
"tab",
{
baseIndentLevel: 1
}
],
syntax: "html",
fix: true,

accept: [
{
code: `
<style>
\ta {
\t\tdisplay:block;
\t}
</style>`
},
{
code: `
\t<style>
\t\ta {
\t\t\tdisplay:block;
\t\t}
\t</style>`
}
],
reject: [
{
code: `
<style>
a {
\tdisplay:block;
}
</style>`,
fixed: `
<style>
\ta {
\t\tdisplay:block;
\t}
</style>`,
message: messages.expected("1 tab"),
line: 3,
column: 1
},
{
code: `
\t<style>
\t b {
\t display:block;
\t }
\ta {
\t\tdisplay:block;
\t}
\t</style>`,
fixed: `
\t<style>
\t\ta {
\t\t\tdisplay:block;
\t\t}
\t</style>`,
message: messages.expected("2 tabs"),
line: 3,
column: 2
}
]
});

testRule(rule, {
ruleName,
config: [
"tab",
{
baseIndentLevel: 0
}
],
syntax: "html",
fix: true,

accept: [
{
code: `
<style>
a {
\tdisplay:block;
}
</style>`
},
{
code: `
\t<style>
\ta {
\t\tdisplay:block;
\t}
\t</style>`
}
],
reject: [
{
code: `
<style>
\ta {
\t\tdisplay:block;
\t}
\t</style>
</style>`,
fixed: `
<style>
a {
\tdisplay:block;
}
</style>`,
message: messages.expected("0 tabs"),
line: 3,
column: 2
},
{
code: `
\t<style>
\tb {
\t\ta {
\t\t\tdisplay:block;
\t\t}
\t</style>`,
fixed: `
\t<style>
\ta {
\t\tdisplay:block;
\t}
\t</style>`,
Expand Down

0 comments on commit e53f6af

Please sign in to comment.