-
-
Notifications
You must be signed in to change notification settings - Fork 680
Fix 1786 #1831
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
Fix 1786 #1831
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c675a61
Fix #1786: Add rule match-component-import-name
doug-wade 9c7084e
Write the documentation for match-component-import-name
doug-wade eb435ae
fix(1786): Respond to PR feedback
doug-wade 3f58b7f
Update lib/rules/match-component-import-name.js
doug-wade 51350ba
Don't change utils if you can avoid it
doug-wade a2b1021
Merge branch 'fix-1786' of https://github.com/doug-wade/eslint-plugin…
doug-wade 73095eb
update docs
doug-wade 4192a84
Update docs/rules/match-component-import-name.md
doug-wade d488031
Update docs/rules/match-component-import-name.md
doug-wade a202f50
Update docs/rules/match-component-import-name.md
doug-wade 65533a4
Update docs/rules/match-component-import-name.md
doug-wade 924570a
remove case option
doug-wade b7c2750
remove stray newline
doug-wade ccbfd9a
Remove prefix; handle computed properties
doug-wade 5e0f072
but who lints the linters
doug-wade 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 hidden or 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,95 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/match-component-import-name | ||
description: xxx | ||
--- | ||
|
||
# vue/match-component-import-name | ||
|
||
> require the registered component name to match the imported component name | ||
|
||
This rule reports if a the name of a registered component does not match its imported name. | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## :book: Rule Details | ||
|
||
This rule has some options. | ||
|
||
```json | ||
{ | ||
"vue/match-component-import-name": [ | ||
"error", | ||
{ | ||
"prefix": "prefix-", | ||
"case": "kebab" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
By default, this rule will validate that the imported name is the same casing. | ||
|
||
Case can be one of: `"kebab"` (`casing-like-this`) or `"pascal"` (`CasingLikeThis`) | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
An optional prefix can be provided that must be prepended to all imports. | ||
|
||
If you are not registering components, this rule will be ignored. | ||
|
||
<eslint-code-block :rules="{'vue/match-component-file-name': ['error']}"> | ||
|
||
```javascript | ||
/* ✓ GOOD */ | ||
export default { components: { AppButton } } | ||
|
||
/* ✗ BAD */ | ||
export default { components: { SomeOtherName: AppButton } } | ||
export default { components: { 'app-button': AppButton } } | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/match-component-file-name': ['error', { case: 'kebab' }]}"> | ||
|
||
```javascript | ||
/* ✓ GOOD */ | ||
export default { components: { 'app-button': AppButton } } | ||
|
||
/* ✗ BAD */ | ||
export default { components: { SomeOtherName: AppButton } } | ||
export default { components: { AppButton } } | ||
``` | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/match-component-file-name': ['error', { prefix: 'Prefix' }]}"> | ||
|
||
```javascript | ||
/* ✓ GOOD */ | ||
export default { components: { PrefixAppButton: AppButton } } | ||
|
||
/* ✗ BAD */ | ||
export default { components: { SomeOtherName: AppButton } } | ||
export default { components: { 'app-button': AppButton } } | ||
export default { components: { 'prefix-app-button': PrefixAppButton } } | ||
``` | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/match-component-import-name": [ | ||
"error", | ||
{ | ||
"prefix": "prefix-", | ||
"case": "kebab" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
- `"prefix": ""` ... array of file extensions to be verified. Default is set to the empty string. | ||
- `"case": "pascal"` ... one of "kebab" or "pascal", indicating the casing of the registered name. Default is set to `pascal`. |
This file contains hidden or 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 hidden or 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,131 @@ | ||
/** | ||
* @author Doug Wade <douglas.b.wade@gmail.com> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
// ... | ||
|
||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: '', | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
categories: undefined, | ||
url: '' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
prefix: { | ||
type: 'string' | ||
}, | ||
casing: { | ||
type: 'string' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
// ... | ||
} | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const options = context.options[0] || {} | ||
|
||
/** | ||
* @param {ExportDefaultDeclaration} node | ||
* @return {Array<Property>} | ||
*/ | ||
function getComponents(node) { | ||
if (node.declaration.type !== 'ObjectExpression') { | ||
return [] | ||
} | ||
|
||
const componentProperty = node.declaration.properties | ||
.filter(utils.isProperty) | ||
.filter((property) => { | ||
return utils.getStaticPropertyName(property) === 'components' | ||
})[0] | ||
doug-wade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ( | ||
!componentProperty || | ||
componentProperty.value.type !== 'ObjectExpression' | ||
) { | ||
return [] | ||
} | ||
|
||
return componentProperty.value.properties.filter(utils.isProperty) | ||
} | ||
|
||
/** @param {Property} property */ | ||
function propertyStartsWithPrefix(property) { | ||
if (!options.prefix) { | ||
return true | ||
} | ||
|
||
const name = utils.getStaticPropertyName(property) | ||
return name ? name.startsWith(options.prefix) : false | ||
} | ||
|
||
return utils.defineScriptVisitor(context, { | ||
ExportDefaultDeclaration(node) { | ||
const components = getComponents(node) | ||
|
||
if (!components) { | ||
return | ||
} | ||
|
||
components.forEach( | ||
/** @param {Property} property */ | ||
(property) => { | ||
if (!propertyStartsWithPrefix(property)) { | ||
context.report({ | ||
node: property, | ||
message: `component alias ${utils.getStaticPropertyName( | ||
property | ||
)} should have the prefix ${options.prefix}` | ||
}) | ||
} | ||
|
||
if (property.value.type !== 'Identifier') { | ||
return | ||
} | ||
|
||
const prefix = options.prefix || '' | ||
const importedName = utils.getStaticPropertyName(property) || '' | ||
const expectedName = | ||
options.casing === 'kebab' | ||
? prefix + casing.kebabCase(property.value.name) | ||
: prefix + casing.pascalCase(property.value.name) | ||
if (importedName !== expectedName) { | ||
context.report({ | ||
node: property, | ||
message: `component alias ${importedName} should match ${expectedName}` | ||
}) | ||
} | ||
} | ||
) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.