Skip to content

⭐️New: Add vue/match-component-file-name rule #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
| | Rule ID | Description |
|:---|:--------|:------------|
| :wrench: | [vue/component-name-in-template-casing](./docs/rules/component-name-in-template-casing.md) | enforce specific casing for the component naming style in template |
| | [vue/match-component-file-name](./docs/rules/match-component-file-name.md) | require component name property to match its file name |
| :wrench: | [vue/multiline-html-element-content-newline](./docs/rules/multiline-html-element-content-newline.md) | require a line break before and after the contents of a multiline element |
| :wrench: | [vue/no-spaces-around-equal-signs-in-attribute](./docs/rules/no-spaces-around-equal-signs-in-attribute.md) | disallow spaces around equal signs in attribute |
| :wrench: | [vue/script-indent](./docs/rules/script-indent.md) | enforce consistent indentation in `<script>` |
Expand Down
153 changes: 153 additions & 0 deletions docs/rules/match-component-file-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# require component name property to match its file name (vue/match-component-file-name)

This rule reports if a component `name` property does not match its file name.

You can define an array of file extensions this rule should verify for
the component's name.

## :book: Rule Details

This rule has some options.

```json
{
"vue/match-component-file-name": ["error", {
"extensions": ["jsx"]
}]
}
```

By default this rule will only verify components in a file with a `.jsx`
extension.

You can use any combination of `".jsx"`, `".vue"` and `".js"` extensions with this option.

If you are defining multiple components within the same file, this rule will be ignored.

:-1: Examples of **incorrect** code for this rule:

```jsx
// file name: src/MyComponent.jsx
export default {
name: 'MComponent', // note the missing y
render: () {
return <h1>Hello world</h1>
}
}
```

```vue
// file name: src/MyComponent.vue
<script>
export default {
name: 'MComponent',
template: '<div />'
}
</script>
```

```js
// file name: src/MyComponent.js
new Vue({
name: 'MComponent',
template: '<div />'
})
```

```js
// file name: src/MyComponent.js
Vue.component('MComponent', {
template: '<div />'
})
```

:+1: Examples of **correct** code for this rule:

```jsx
// file name: src/MyComponent.jsx
export default {
name: 'MyComponent',
render: () {
return <h1>Hello world</h1>
}
}
```

```jsx
// file name: src/MyComponent.jsx
// no name property defined
export default {
render: () {
return <h1>Hello world</h1>
}
}
```

```vue
// file name: src/MyComponent.vue
<script>
export default {
name: 'MyComponent',
template: '<div />'
}
</script>
```

```vue
// file name: src/MyComponent.vue
<script>
export default {
template: '<div />'
}
</script>
```

```js
// file name: src/MyComponent.js
new Vue({
name: 'MyComponent',
template: '<div />'
})
```

```js
// file name: src/MyComponent.js
new Vue({
template: '<div />'
})
```

```js
// file name: src/MyComponent.js
Vue.component('MyComponent', {
template: '<div />'
})
```

```js
// file name: src/components.js
// defines multiple components, so this rule is ignored
Vue.component('MyComponent', {
template: '<div />'
})

Vue.component('OtherComponent', {
template: '<div />'
})

new Vue({
name: 'ThirdComponent',
template: '<div />'
})
```

## :wrench: Options

- `{extensions: ["jsx"]}` (default) ... verify components in files with `.jsx` extension.
- `{extensions: ["vue"]}` (default) ... verify components in files with `.vue` extension.
- `{extensions: ["js"]}` (default) ... verify components in files with `.js` extension.
- `{extensions: ["jsx", "vue"]}` ... verify components in files with `.jsx` or `.vue` extensions.
- `{extensions: ["jsx", "js"]}` ... verify components in files with `.jsx` or `.js` extensions.
- `{extensions: ["vue", "js"]}` ... verify components in files with `.vue` or `.js` extensions.
- `{extensions: ["jsx", "vue", "js"]}` ... verify components in files with any of the
provided extensions.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
'html-quotes': require('./rules/html-quotes'),
'html-self-closing': require('./rules/html-self-closing'),
'jsx-uses-vars': require('./rules/jsx-uses-vars'),
'match-component-file-name': require('./rules/match-component-file-name'),
'max-attributes-per-line': require('./rules/max-attributes-per-line'),
'multiline-html-element-content-newline': require('./rules/multiline-html-element-content-newline'),
'mustache-interpolation-spacing': require('./rules/mustache-interpolation-spacing'),
Expand Down
127 changes: 127 additions & 0 deletions lib/rules/match-component-file-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* @fileoverview Require component name property to match its file name
* @author Rodrigo Pedra Brum <[email protected]>
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils')
const casing = require('../utils/casing')
const path = require('path')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'require component name property to match its file name',
category: undefined,
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.4/docs/rules/match-component-file-name.md'
},
fixable: null,
schema: [
{
type: 'object',
properties: {
extensions: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true,
additionalItems: false
}
},
additionalProperties: false
}
]
},

create (context) {
const options = context.options[0]
const extensionsArray = options && options.extensions
const allowedExtensions = Array.isArray(extensionsArray) ? extensionsArray : ['jsx']
const extension = path.extname(context.getFilename())
const filename = path.basename(context.getFilename(), extension)

const errors = []
let componentCount = 0

if (!allowedExtensions.includes(extension.replace(/^\./, ''))) {
return {}
}

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

function verifyName (node) {
let name
if (node.type === 'TemplateLiteral') {
const quasis = node.quasis[0]
name = quasis.value.cooked
} else {
name = node.value
}

if (casing.pascalCase(name) !== filename && casing.kebabCase(name) !== filename) {
errors.push({
node: node,
message: 'Component name `{{name}}` should match file name `{{filename}}`.',
data: { filename, name }
})
}
}

function canVerify (node) {
return node.type === 'Literal' || (
node.type === 'TemplateLiteral' &&
node.expressions.length === 0 &&
node.quasis.length === 1
)
}

return Object.assign({},
{
"CallExpression > MemberExpression > Identifier[name='component']" (node) {
const parent = node.parent.parent
const calleeObject = utils.unwrapTypes(parent.callee.object)

if (calleeObject.type === 'Identifier' && calleeObject.name === 'Vue') {
if (parent.arguments && parent.arguments.length === 2) {
const argument = parent.arguments[0]
if (canVerify(argument)) {
verifyName(argument)
}
}
}
}
},
utils.executeOnVue(context, (object) => {
const node = object.properties
.find(item => (
item.type === 'Property' &&
item.key.name === 'name' &&
canVerify(item.value)
))

componentCount++

if (!node) return
verifyName(node.value)
}),
{
'Program:exit' () {
if (componentCount > 1) return

errors.forEach((error) => context.report(error))
}
}
)
}
}
Loading