Skip to content

Commit

Permalink
Require single quotes inside javascript in pug & prohibit empty lines…
Browse files Browse the repository at this point in the history
… for nested items (#47)

* Add better check that some parts of template are javascript

* Prohibit new lines for nested items

* Add missed comma
  • Loading branch information
ezhlobo authored Sep 3, 2018
1 parent 9975eb2 commit 78074da
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 7 deletions.
16 changes: 16 additions & 0 deletions docs/rules/empty-lines.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ It creates rules for managing empty lines:
* Require empty line before we go out from nesting
* Require empty line between siblings (if there are more than 2 siblings)
* Prohibit more than 1 empty line
* Prohibit empty line for nested items

## Rule Details

Expand All @@ -33,6 +34,14 @@ pug`
`
```

```jsx
pug`
div
div Nested
`
```

The following patterns are **not** considered warnings:

```jsx
Expand Down Expand Up @@ -64,6 +73,13 @@ pug`
`
```

```jsx
pug`
div
div Nested
`
```

## When Not To Use It

If you don't want to have consistency in empty lines.
20 changes: 19 additions & 1 deletion lib/rules/empty-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const MESSAGE = {
newline_start: 'Expected new line in the beginning',
newline_end: 'Expected new line in the end',
single_empty_lines: 'Use 1 empty line',
no_lines_indent: 'Expected no empty lines for nested items',
need_empty_siblings: 'Need empty line for more than two siblings',
need_empty_outdent: 'Need empty line when you are off from the scope',
}

const isLineEmpty = line => typeof line === 'string' && line.trim() === ''

const normalizeToken = token => ({
type: '_nothing',
type: null,
loc: buildLocation([-1, -1], [-1, -1]),
...token,
})
Expand Down Expand Up @@ -118,6 +119,23 @@ module.exports = {
}
}

if (token.type === 'indent') {
// Prohibit empty lines for nested items
if (prevToken.type && token.loc.start.line - prevToken.loc.start.line > 1) {
const startLine = node.loc.start.line + prevToken.loc.end.line - 1
const endLine = node.loc.start.line + token.loc.end.line - 1

context.report({
node,
loc: buildLocation(
[startLine, prevToken.loc.end.column - 1],
[endLine, token.loc.end.column - 1],
),
message: MESSAGE.no_lines_indent,
})
}
}

if (
token.type === 'newline'

Expand Down
17 changes: 11 additions & 6 deletions lib/rules/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const codeWalk = require('@babel/traverse').default
const { isReactPugReference, buildLocation, docsUrl } = require('../util/eslint')
const getTemplate = require('../util/getTemplate')
const getTokens = require('../util/getTokens')
const ToggleBoolean = require('../util/ToggleBoolean')

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -48,30 +49,34 @@ module.exports = {
const tokens = getTokens(template)

tokens.forEach((token) => {
const normalizedValue = normalizeValue(token)

if (
token.type === 'attribute'
|| token.type === 'code'
|| token.type === 'interpolated-code'
|| token.type === 'each'
) {
const normalizedValue = normalizeValue(token)
const code = parse(normalizedValue)

const isCode = new ToggleBoolean()

codeWalk(code, {
StringLiteral(path) {
const rawValue = path.node.extra.raw
ObjectExpression: isCode.on,
ArrayExpression: isCode.on,
CallExpression: isCode.on,

StringLiteral(path) {
const doesNeedSingleQuote = (
// Object- or array-like
/^({.*}|\[.*\])$/.test(token.code || token.val)
isCode.check()

// Code in template
|| token.type === 'code'
|| token.type === 'interpolated-code'
|| token.type === 'each'
)

const rawValue = path.node.extra.raw

if (isStringValid(rawValue, doesNeedSingleQuote)) {
return false
}
Expand Down
17 changes: 17 additions & 0 deletions lib/util/ToggleBoolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ToggleBoolean {
constructor(initialStatus) {
this.on = this.on.bind(this)

this.status = initialStatus || false
}

on() {
this.status = true
}

check() {
return this.status
}
}

module.exports = ToggleBoolean
36 changes: 36 additions & 0 deletions tests/lib/rules/empty-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const MESSAGE = {
newline_start: 'Expected new line in the beginning',
newline_end: 'Expected new line in the end',
single_empty_lines: 'Use 1 empty line',
no_lines_indent: 'Expected no empty lines for nested items',
need_empty_siblings: 'Need empty line for more than two siblings',
need_empty_outdent: 'Need empty line when you are off from the scope',
}
Expand Down Expand Up @@ -132,6 +133,14 @@ ruleTester.run('rule "empty-lines"', rule, {
\`
`,
},
{
code: `
pug\`
div
div text
\`
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -396,5 +405,32 @@ ruleTester.run('rule "empty-lines"', rule, {
buildError([8, 1], [8, 11], MESSAGE.need_empty_outdent),
],
},
{
code: `
pug\`
div
div text
\`
`,
errors: [
buildError([3, 14], [5, 13], MESSAGE.no_lines_indent),
],
},
{
code: `
pug\`
div
div
div
div text
\`
`,
errors: [
buildError([6, 16], [8, 15], MESSAGE.no_lines_indent),
],
},
],
})
20 changes: 20 additions & 0 deletions tests/lib/rules/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ ruleTester.run('rule "quotes"', rule, {
\`
`,
},
{
code: `
pug\`
div(class=['one', 'two', test['three']].join(' '))
\`
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -170,5 +177,18 @@ ruleTester.run('rule "quotes"', rule, {
buildError([3, 39], [3, 44], MESSAGE_CODE),
],
},
{
code: `
pug\`
div(class=["one", "two", test["three"]].join(" "))
\`
`,
errors: [
buildError([3, 22], [3, 27], MESSAGE_CODE),
buildError([3, 29], [3, 34], MESSAGE_CODE),
buildError([3, 41], [3, 48], MESSAGE_CODE),
buildError([3, 56], [3, 59], MESSAGE_CODE),
],
},
],
})

0 comments on commit 78074da

Please sign in to comment.