This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): add require-func-head in recommended (#62)
- Loading branch information
Showing
7 changed files
with
182 additions
and
3 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,40 @@ | ||
# nuxt/require-func-head | ||
|
||
> enforce `head` property in component to be a function. | ||
- :gear: This rule is included in `"plugin:nuxt/recommended"`. | ||
|
||
## Rule Details | ||
|
||
This rule is enforcing `head` property in component to be a function. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
head: { | ||
title: "My page" | ||
} | ||
} | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
|
||
export default { | ||
head() { | ||
return { | ||
title: "My page" | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](../../lib/rules/require-func-head.js) | ||
- [Test source](../../lib/rules/__tests__/require-func-head.test.js) |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports = { | ||
extends: require.resolve('./base.js'), | ||
rules: { | ||
'nuxt/no-timing-in-fetch-data': 'error' | ||
'nuxt/no-timing-in-fetch-data': 'error', | ||
'nuxt/require-func-head': 'error' | ||
} | ||
} |
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,60 @@ | ||
/** | ||
* @fileoverview disallow `setTimeout/setInterval` in `asyncData/fetch` | ||
* @author Xin Du <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../require-func-head') | ||
|
||
var RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('require-func-head', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
head() { | ||
return { | ||
title: "My page" | ||
} | ||
} | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
head: { | ||
title: "My page" | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: '`head` property in component must be a function.', | ||
type: 'Property' | ||
}], | ||
parserOptions | ||
} | ||
] | ||
}) |
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,54 @@ | ||
/** | ||
* @fileoverview enforce component's head property to be a function. | ||
* @author Xin Du <[email protected]> | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "enforce component's head property to be a function", | ||
category: 'recommended' | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
head: '`head` property in component must be a function.' | ||
} | ||
}, | ||
|
||
create (context) { | ||
const sourceCode = context.getSourceCode() | ||
|
||
return utils.executeOnVueComponent(context, (obj) => { | ||
obj.properties | ||
.filter(p => | ||
p.type === 'Property' && | ||
p.key.type === 'Identifier' && | ||
p.key.name === 'head' && | ||
p.value.type !== 'FunctionExpression' && | ||
p.value.type !== 'ArrowFunctionExpression' && | ||
p.value.type !== 'Identifier' | ||
) | ||
.forEach(p => { | ||
context.report({ | ||
node: p, | ||
messageId: 'head', | ||
fix (fixer) { | ||
const tokens = utils.getFirstAndLastTokens(p.value, sourceCode) | ||
|
||
return [ | ||
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '), | ||
fixer.insertTextAfter(tokens.last, ';\n}') | ||
] | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
} |
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