-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule "indent" to enforce consistent indentation (#39)
It always requires 2-space indentation. Request: #35
- Loading branch information
Showing
6 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Enforce consistent indentation (react-pug/indent) | ||
|
||
This helps to be consistent with using indentation across the codebase. | ||
|
||
Basically it requires 2 spaces always. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
pug` | ||
div | ||
` | ||
``` | ||
|
||
```jsx | ||
pug` | ||
div | ||
div | ||
` | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
pug`div= 'Hello'` | ||
``` | ||
|
||
```jsx | ||
pug` | ||
div | ||
div | ||
` | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you are not using Pug then you can disable this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @fileoverview Enforce consistent indentation | ||
* @author Eugene Zhlobo | ||
*/ | ||
|
||
const { isReactPugReference, buildLocation, docsUrl } = require('../util/eslint') | ||
const getTemplate = require('../util/getTemplate') | ||
const getTokens = require('../util/getTokens') | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
const buildMessage = (expected, actual) => ( | ||
`Expected indentation of ${expected} spaces but found ${actual}` | ||
) | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Enforce consistent indentation', | ||
category: 'Stylistic Issues', | ||
recommended: true, | ||
url: docsUrl('indent'), | ||
}, | ||
schema: [], | ||
}, | ||
|
||
create: function (context) { | ||
const STEP = 2 | ||
|
||
return { | ||
TaggedTemplateExpression: function (node) { | ||
if (isReactPugReference(node)) { | ||
if (node.loc.end.line === node.loc.start.line) { | ||
return false | ||
} | ||
|
||
const wrongIndent = [] | ||
|
||
const template = getTemplate(node) | ||
const tokens = getTokens(template) | ||
|
||
const minimalIndent = node.loc.start.column | ||
|
||
let currentIndent = 0 | ||
tokens.forEach((token, index) => { | ||
if (token.type === 'indent' || index === 0) { | ||
currentIndent += 1 | ||
|
||
const actual = token.loc.end.column - 1 | ||
const expected = minimalIndent + currentIndent * STEP | ||
|
||
if (actual !== expected) { | ||
wrongIndent.push({ | ||
line: token.loc.start.line, | ||
actual, | ||
expected, | ||
}) | ||
} | ||
} else if (token.type === 'outdent') { | ||
currentIndent -= 1 | ||
} | ||
}) | ||
|
||
wrongIndent.forEach((item) => { | ||
context.report({ | ||
node, | ||
loc: buildLocation( | ||
[item.line + node.loc.start.line - 1, 0], | ||
[item.line + node.loc.start.line - 1, item.actual], | ||
), | ||
message: buildMessage(item.expected, item.actual), | ||
}) | ||
}) | ||
} | ||
|
||
return false | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/** | ||
* @fileoverview Tests for indent | ||
* @author Eugene Zhlobo | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const eslint = require('eslint') | ||
|
||
const { RuleTester } = eslint | ||
|
||
const rule = require('../../../lib/rules/indent') | ||
|
||
const parserOptions = { | ||
ecmaVersion: 8, | ||
sourceType: 'module', | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ parserOptions }) | ||
|
||
const buildMessage = (expected, actual) => ( | ||
`Expected indentation of ${expected} spaces but found ${actual}` | ||
) | ||
|
||
ruleTester.run('rule "indent"', rule, { | ||
valid: [ | ||
{ code: 'pug`div`' }, | ||
{ | ||
code: ` | ||
pug\` | ||
div | ||
div: div | ||
div | ||
div | ||
\` | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
pug\` | ||
test | ||
\` | ||
`, | ||
errors: [{ | ||
message: buildMessage(10, 0), | ||
line: 3, | ||
column: 1, | ||
endLine: 3, | ||
endColumn: 1, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
pug\` | ||
test | ||
\` | ||
`, | ||
errors: [{ | ||
message: buildMessage(10, 9), | ||
line: 3, | ||
column: 1, | ||
endLine: 3, | ||
endColumn: 10, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
pug\` | ||
div | ||
\` | ||
`, | ||
errors: [{ | ||
message: buildMessage(10, 11), | ||
line: 3, | ||
column: 1, | ||
endLine: 3, | ||
endColumn: 12, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
pug\` | ||
div | ||
\` | ||
`, | ||
errors: [{ | ||
message: buildMessage(12, 13), | ||
line: 3, | ||
column: 1, | ||
endLine: 3, | ||
endColumn: 14, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
pug\` | ||
div | ||
div | ||
div | ||
div | ||
div | ||
\` | ||
`, | ||
errors: [{ | ||
message: buildMessage(12, 11), | ||
line: 7, | ||
column: 1, | ||
endLine: 7, | ||
endColumn: 12, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
pug\` | ||
div | ||
div | ||
div(incorrect) | ||
p | ||
div(incorrect) | ||
\` | ||
`, | ||
errors: [{ | ||
message: buildMessage(14, 13), | ||
line: 5, | ||
column: 1, | ||
endLine: 5, | ||
endColumn: 14, | ||
}, { | ||
message: buildMessage(16, 14), | ||
line: 6, | ||
column: 1, | ||
endLine: 6, | ||
endColumn: 15, | ||
}], | ||
}, | ||
{ | ||
code: ` | ||
pug\` | ||
div | ||
div | ||
\` | ||
`, | ||
errors: [{ | ||
message: buildMessage(10, 12), | ||
line: 3, | ||
column: 1, | ||
endLine: 3, | ||
endColumn: 13, | ||
}, { | ||
message: buildMessage(12, 16), | ||
line: 4, | ||
column: 1, | ||
endLine: 4, | ||
endColumn: 17, | ||
}], | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters