-
-
Notifications
You must be signed in to change notification settings - Fork 680
New Add vue/no-potential-property-typo
rule
#1072
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
ota-meshi
merged 22 commits into
vuejs:master
from
IWANABETHATGUY:feature/no-potential-property-typo
May 20, 2020
Merged
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
69393bd
feat(utils/index.js): add util lib
IWANABETHATGUY 2773283
feat(tests/lib/utils/index.js): add unit test
IWANABETHATGUY 9d9724e
feat: change test, complete rule
IWANABETHATGUY b39cff9
feat: add test, add preset, custom option
IWANABETHATGUY d16d7cd
feat: add testcase
IWANABETHATGUY ce4898d
test: add test, 100% test cover
IWANABETHATGUY 82f2dee
test: menual indentation
IWANABETHATGUY d0a5738
style: remove todo comment that have been done🚀
IWANABETHATGUY 1db57b1
fix: change rule name -> no-unknown-component-options
IWANABETHATGUY 7dc8a29
feat: resolve conflict
IWANABETHATGUY b693c35
feat: rename `no-unknow-component-options` -> `no-potential-component…
IWANABETHATGUY 705fdfd
feat: remove unnecessary readme
IWANABETHATGUY 0806add
feat: revert lib/utils/index.js
IWANABETHATGUY 3ee86c8
docs: update readme
IWANABETHATGUY e15f41d
feat: set categories to undefined
IWANABETHATGUY a2f0057
test: add test case
IWANABETHATGUY d075640
test: add test case
IWANABETHATGUY 6c95217
test: add vue preset as default preset, abcde and abcd test case
IWANABETHATGUY 126ba01
test: udpate test
IWANABETHATGUY aa38664
test: all valid options
IWANABETHATGUY c5f2ac7
improvement: comment
IWANABETHATGUY 324e3e5
test: inline test
IWANABETHATGUY 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
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,123 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-potential-component-option-typo | ||
description: disallow a potential typo in your component property | ||
--- | ||
# vue/no-potential-component-option-typo | ||
> disallow a potential typo in your component property | ||
|
||
- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This Rule disallow a potential typo in your component options | ||
|
||
**Here is the config** | ||
```js | ||
{'vue/no-potential-component-option-typo': ['error', {presets: ['all'], custom: ['test']}]} | ||
``` | ||
|
||
<eslint-code-block :rules="{'vue/no-potential-component-option-typo': ['error', {presets: ['all'], custom: ['test']}]}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✓ GOOD */ | ||
props: { | ||
|
||
}, | ||
/* × BAD */ | ||
method: { | ||
|
||
}, | ||
/* ✓ GOOD */ | ||
data: { | ||
|
||
}, | ||
/* × BAD */ | ||
beforeRouteEnteR() { | ||
|
||
}, | ||
/* × BAD due to custom option 'test'*/ | ||
testt: { | ||
|
||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
> we use editdistance to compare two string similarity, threshold is an option to control upper bound of editdistance to report | ||
|
||
**Here is the another example about config option `threshold`** | ||
```js | ||
{'vue/no-potential-component-option-typo': ['error', {presets: ['vue', 'nuxt'], threshold: 5}]} | ||
``` | ||
|
||
<eslint-code-block :rules="{'vue/no-potential-component-option-typo': ['error', {presets: ['vue', 'nuxt'], threshold: 5}]}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
/* ✓ BAD, due to threshold is 5 */ | ||
props: { | ||
|
||
}, | ||
/* ✓ BAD, due to threshold is 5 */ | ||
method: { | ||
|
||
}, | ||
/* ✓ BAD, due to threshold is 5 */ | ||
data: { | ||
|
||
}, | ||
/* × GOOD, due to we don't choose vue-router preset or add a custom option */ | ||
beforeRouteEnteR() { | ||
|
||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
```js | ||
{ | ||
"vue/no-unsed-vars": [{ | ||
presets: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
enum: ['all', 'vue', 'vue-router', 'nuxt'] | ||
}, | ||
uniqueItems: true, | ||
minItems: 0 | ||
}, | ||
custom: { | ||
type: 'array', | ||
minItems: 0, | ||
items: { type: 'string' }, | ||
uniqueItems: true | ||
}, | ||
threshold: { | ||
type: 'number', | ||
'minimum': 1 | ||
} | ||
}] | ||
} | ||
``` | ||
- `presets` ... `enum type`, contains several common vue component option set, `['all']` is the same as `['vue', 'vue-router', 'nuxt']`. **default** `[]` | ||
- `custom` ... `array type`, a list store your custom component option want to detect. **default** `[]` | ||
- `threshold` ... `number type`, a number used to control the upper limit of the reported editing distance, we recommend don't change this config option, even if it is required, not bigger than `2`. **default** `1` | ||
## :rocket: Suggestion | ||
- We provide all the possible component option that editdistance between your vue component option and configuration options is greater than 0 and lessEqual than threshold | ||
|
||
## :books: Further reading | ||
- [Edit_distance](https://en.wikipedia.org/wiki/Edit_distance) | ||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-potential-component-option-typo.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-potential-component-option-typo.js) |
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
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,104 @@ | ||
/** | ||
* @fileoverview detect if there is a potential typo in your component property | ||
* @author IWANABETHATGUY | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const vueComponentOptions = require('../utils/vue-component-options.json') | ||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow a potential typo in your component property', | ||
categories: ['essential'], | ||
recommended: false, | ||
url: 'https://eslint.vuejs.org/rules/no-potential-component-option-typo.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
presets: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
enum: ['all', 'vue', 'vue-router', 'nuxt'] | ||
}, | ||
uniqueItems: true, | ||
minItems: 0 | ||
}, | ||
custom: { | ||
type: 'array', | ||
minItems: 0, | ||
items: { type: 'string' }, | ||
uniqueItems: true | ||
}, | ||
threshold: { | ||
type: 'number', | ||
'minimum': 1 | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
|
||
create: function (context) { | ||
const option = context.options[0] || {} | ||
const custom = option['custom'] || [] | ||
const presets = option['presets'] || [] | ||
IWANABETHATGUY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const threshold = option['threshold'] || 1 | ||
let candidateOptions = [] | ||
IWANABETHATGUY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (presets.includes('all')) { | ||
candidateOptions = Object.keys(vueComponentOptions).reduce((pre, cur) => { | ||
return [...pre, ...vueComponentOptions[cur]] | ||
}, []) | ||
} else { | ||
candidateOptions = presets.reduce((pre, cur) => { | ||
return [...pre, ...vueComponentOptions[cur]] | ||
}, []) | ||
} | ||
candidateOptions = [...new Set(candidateOptions.concat(custom))] | ||
IWANABETHATGUY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!candidateOptions.length) { | ||
return {} | ||
} | ||
return utils.executeOnVue(context, obj => { | ||
const componentInstanceOptions = obj.properties.filter( | ||
p => p.type === 'Property' && p.key.type === 'Identifier' | ||
) | ||
if (!componentInstanceOptions.length) { | ||
return {} | ||
} | ||
componentInstanceOptions.forEach(option => { | ||
const id = option.key | ||
const name = id.name | ||
const potentialTypoList = candidateOptions | ||
.map(o => ({ option: o, distance: utils.editDistance(o, name) })) | ||
.filter(({ distance }) => distance <= threshold && distance > 0) | ||
.sort((a, b) => a.distance - b.distance) | ||
if (potentialTypoList.length) { | ||
context.report({ | ||
node: id, | ||
loc: id.loc, | ||
message: `'{{name}}' may be a typo, which is similar to option [{{option}}].`, | ||
data: { | ||
name, | ||
option: potentialTypoList.map(({ option }) => option).join(',') | ||
}, | ||
suggest: potentialTypoList.map(({ option }) => ({ | ||
desc: `Replace property '${name}' to '${option}'`, | ||
fix (fixer) { | ||
return fixer.replaceText(id, option) | ||
} | ||
})) | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
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,47 @@ | ||
{ | ||
"nuxt": ["asyncData", "fetch", "head", "key", "layout", "loading", "middleware", "scrollToTop", "transition", "validate", "watchQuery"], | ||
"vue-router": [ | ||
"beforeRouteEnter", | ||
"beforeRouteUpdate", | ||
"beforeRouteLeave" | ||
], | ||
"vue": [ | ||
"data", | ||
"props", | ||
"propsData", | ||
"computed", | ||
"methods", | ||
"watch", | ||
"el", | ||
"template", | ||
"render", | ||
"renderError", | ||
"staticRenderFns", | ||
"beforeCreate", | ||
"created", | ||
"beforeDestroy", | ||
"destroyed", | ||
"beforeMount", | ||
"mounted", | ||
"beforeUpdate", | ||
"updated", | ||
"activated", | ||
"deactivated", | ||
"errorCaptured", | ||
"serverPrefetch", | ||
"directives", | ||
"components", | ||
"transitions", | ||
"filters", | ||
"provide", | ||
"inject", | ||
"model", | ||
"parent", | ||
"mixins", | ||
"name", | ||
"extends", | ||
"delimiters", | ||
"comments", | ||
"inheritAttrs" | ||
] | ||
} |
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.