-
-
Notifications
You must be signed in to change notification settings - Fork 679
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
⭐️New: Add vue/component-name-in-template-casing
#397
Merged
michalsnik
merged 21 commits into
vuejs:master
from
ota-meshi:add-component-name-in-template-casing
Jul 30, 2018
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
98dbf8a
Add Vue.extend support, add missing info about Vue.mixin check in readme
michalsnik 7c4a1d2
Docs: fixes wording in docs (#372)
samturrell cd22a28
Fix: fix script-indent to prevent removing <script> tag (fixes #367) …
mysticatea ee5cc0a
[Update] Make `vue/max-attributes-per-line` fixable (#380)
ota-meshi 6861c81
Update: make `vue/order-in-components` fixable (#381)
ota-meshi 2b4798b
[New] Add `vue/component-name-in-template-casing`
ota-meshi 3f86365
[update] documents
ota-meshi 3dd2038
[fix] require-meta-docs-url
ota-meshi e35ca6c
[fix] failed tests
ota-meshi 243e61a
[fix] review contents
ota-meshi de29eb8
[fix] No deletes space and attributes of endTag
ota-meshi fea5dfc
[fix] Remove test unnecessary option
ota-meshi 38e4849
Merge branch 'master' into add-component-name-in-template-casing
ota-meshi 2ce184b
[fix] lint error caused by merging the master for conflict resolution
ota-meshi 25c7e4d
Merge branch 'upstream/master' into add-component-name-in-template-ca…
ota-meshi 54fb3fb
Add ignores option.
ota-meshi e8e0db0
Fixed that extra differences.
ota-meshi 802a9a8
Merge branch 'master' into add-component-name-in-template-casing
ota-meshi 4b300dc
update docs link
ota-meshi 33b47d5
Merge branch 'master' into pr/397
michalsnik fb3d433
Update formatting
michalsnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,74 @@ | ||
# enforce specific casing for the component naming style in template (vue/component-name-in-template-casing) | ||
|
||
- :gear: This rule is included in `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`. | ||
- :wrench: The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule. | ||
|
||
Define a style for the component name in template casing for consistency purposes. | ||
|
||
## :book: Rule Details | ||
|
||
:+1: Examples of **correct** code for `PascalCase`: | ||
|
||
```html | ||
<template> | ||
<TheComponent /> | ||
</template> | ||
``` | ||
|
||
:-1: Examples of **incorrect** code for `PascalCase`: | ||
|
||
```html | ||
<template> | ||
<the-component /> | ||
<theComponent /> | ||
<The-component /> | ||
</template> | ||
``` | ||
|
||
:+1: Examples of **correct** code for `kebab-case`: | ||
|
||
```html | ||
<template> | ||
<the-component /> | ||
</template> | ||
``` | ||
|
||
:-1: Examples of **incorrect** code for `kebab-case`: | ||
|
||
```html | ||
<template> | ||
<TheComponent /> | ||
<theComponent /> | ||
<Thecomponent /> | ||
<The-component /> | ||
</template> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Default casing is set to `PascalCase`. | ||
|
||
```json | ||
"vue/component-name-in-template-casing": ["error", | ||
"PascalCase|kebab-case", | ||
{ | ||
"ignores": [] | ||
} | ||
] | ||
``` | ||
|
||
- `ignores` (`string[]`) ... The element name to ignore. Sets the element name to allow. For example, a custom element or a non-Vue component. | ||
|
||
|
||
:+1: Examples of **correct** code for `{ignores: ["custom-element"]}`: | ||
|
||
```html | ||
<template> | ||
<custom-element></custom-element> | ||
<TheComponent/> | ||
</template> | ||
``` | ||
|
||
## Related links | ||
|
||
- [Style guide - Component name casing in templates](https://vuejs.org/v2/style-guide/#Component-name-casing-in-templates-strongly-recommended) |
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
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,108 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* issue https://github.com/vuejs/eslint-plugin-vue/issues/250 | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
const allowedCaseOptions = ['PascalCase', 'kebab-case'] | ||
const defaultCase = 'PascalCase' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'enforce specific casing for the component naming style in template', | ||
category: undefined, // strongly-recommended | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.1/docs/rules/component-name-in-template-casing.md' | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
enum: allowedCaseOptions | ||
}, | ||
{ | ||
type: 'object', | ||
properties: { | ||
ignores: { | ||
type: 'array', | ||
items: { type: 'string' }, | ||
uniqueItems: true, | ||
additionalItems: false | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
|
||
create (context) { | ||
const caseOption = context.options[0] | ||
const options = context.options[1] || {} | ||
const caseType = allowedCaseOptions.indexOf(caseOption) !== -1 ? caseOption : defaultCase | ||
const ignores = options.ignores || [] | ||
const tokens = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() | ||
const sourceCode = context.getSourceCode() | ||
|
||
let hasInvalidEOF = false | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
'VElement' (node) { | ||
if (hasInvalidEOF) { | ||
return | ||
} | ||
|
||
if (!utils.isCustomComponent(node)) { | ||
return | ||
} | ||
|
||
const name = node.rawName | ||
if (ignores.indexOf(name) >= 0) { | ||
return | ||
} | ||
const casingName = casing.getConverter(caseType)(name) | ||
if (casingName !== name) { | ||
const startTag = node.startTag | ||
const open = tokens.getFirstToken(startTag) | ||
|
||
context.report({ | ||
node: open, | ||
loc: open.loc, | ||
message: 'Component name "{{name}}" is not {{caseType}}.', | ||
data: { | ||
name, | ||
caseType | ||
}, | ||
fix: fixer => { | ||
const endTag = node.endTag | ||
if (!endTag) { | ||
return fixer.replaceText(open, `<${casingName}`) | ||
} | ||
const endTagOpen = tokens.getFirstToken(endTag) | ||
// If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by: | ||
// return [ | ||
// fixer.replaceText(open, `<${casingName}`), | ||
// fixer.replaceText(endTagOpen, `</${casingName}`) | ||
// ] | ||
const code = `<${casingName}${sourceCode.text.slice(open.range[1], endTagOpen.range[0])}</${casingName}` | ||
return fixer.replaceTextRange([open.range[0], endTagOpen.range[1]], code) | ||
} | ||
}) | ||
} | ||
} | ||
}, { | ||
Program (node) { | ||
hasInvalidEOF = utils.hasInvalidEOF(node) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't be the default for the reasons Iv listed on #250 (comment)
Please don't merge this until that's fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding, we can only lint Single File Components and according to the Vue Style Guide PascalCase is strongly recommended in SFC's, so this seems like a sensible default. If you want to use kebab-case it's configurable so it can be either. +1 for this PR, really looking forward to it being merged 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this plugin uses the Style Guide as the source of truth, so If you believe kebab-case should be the default (a lot of people I work with agree) it might be more appropriate to raise a new issue over on the Style Guide repo itself, see https://github.com/vuejs/vuejs.org/blob/e93b8371d73e4467dd8152703ddf1db423f489a2/src/v2/style-guide/index.md#single-file-component-filename-casing-strongly-recommended