-
-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add
vue/name-property-required
rule (#945)
* New: Add `vue/name-property-required` rule * Rename to `require-name-property`, remove literal value assertion, add docs
- Loading branch information
1 parent
a2865e4
commit 06dc4a2
Showing
3 changed files
with
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/require-name-property | ||
description: require a component name property | ||
--- | ||
# vue/require-name-property | ||
> require a component name property | ||
## :book: Rule Details | ||
|
||
This rule requires a `name` property to be set on components. | ||
|
||
<eslint-code-block :rules="{'vue/require-name-property': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
/* ✓ GOOD */ | ||
export default { | ||
name: 'OurButton' | ||
} | ||
/* ✗ BAD */ | ||
export default { | ||
} | ||
/* ✗ BAD */ | ||
export default { | ||
notName: 'OurButton' | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/require-name-property.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/require-name-property.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @fileoverview Require a name property in Vue components | ||
* @author LukeeeeBennett | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
function isNameProperty (node) { | ||
return node.type === 'Property' && | ||
node.key.name === 'name' && | ||
!node.computed | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'require a name property in Vue components', | ||
category: undefined, | ||
url: 'https://eslint.vuejs.org/rules/require-name-property.html' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
|
||
create (context) { | ||
return utils.executeOnVue(context, component => { | ||
if (component.properties.some(isNameProperty)) return | ||
|
||
context.report({ | ||
node: component, | ||
message: 'Required name property is not set.' | ||
}) | ||
}) | ||
} | ||
} |
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,102 @@ | ||
/** | ||
* @fileoverview Require a name property in Vue components | ||
* @author LukeeeeBennett | ||
*/ | ||
'use strict' | ||
|
||
const rule = require('../../../lib/rules/require-name-property') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
const ruleTester = new RuleTester() | ||
ruleTester.run('require-name-property', rule, { | ||
valid: [ | ||
{ | ||
filename: 'ValidComponent.vue', | ||
code: ` | ||
export default { | ||
name: 'IssaName' | ||
} | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'ValidComponent.vue', | ||
code: ` | ||
export default { | ||
name: undefined | ||
} | ||
`, | ||
parserOptions | ||
}, | ||
{ | ||
filename: 'ValidComponent.vue', | ||
code: ` | ||
export default { | ||
name: '' | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is not set.', | ||
type: 'ObjectExpression' | ||
}] | ||
}, | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
nameNot: 'IssaNameNot' | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is not set.', | ||
type: 'ObjectExpression' | ||
}] | ||
}, | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
computed: { | ||
name() { return 'name' } | ||
} | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is not set.', | ||
type: 'ObjectExpression' | ||
}] | ||
}, | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
[name]: 'IssaName' | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is not set.', | ||
type: 'ObjectExpression' | ||
}] | ||
} | ||
] | ||
}) |