-
-
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
(implements #323) Add require-prop-type-constructor
rule
#546
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,60 @@ | ||
# require prop type to be a constructor (vue/require-prop-type-constructor) | ||
|
||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
This rule reports prop types that can't be presumed as constructors. | ||
|
||
It's impossible to catch every possible case and know whether the prop type is a constructor or not, hence this rule black list few types of nodes, instead of white-listing correct ones. | ||
|
||
The following types are forbidden and will be reported: | ||
|
||
- Literal | ||
- TemplateLiteral | ||
- BinaryExpression | ||
- UpdateExpression | ||
|
||
It will catch most commonly made mistakes which are using strings instead of constructors. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
export default { | ||
props: { | ||
myProp: "Number", | ||
anotherProp: ["Number", "String"], | ||
myFieldWithBadType: { | ||
type: "Object", | ||
default: function() { | ||
return {} | ||
}, | ||
}, | ||
myOtherFieldWithBadType: { | ||
type: "Number", | ||
default: 1, | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
export default { | ||
props: { | ||
myProp: Number, | ||
anotherProp: [Number, String], | ||
myFieldWithBadType: { | ||
type: Object, | ||
default: function() { | ||
return {} | ||
}, | ||
}, | ||
myOtherFieldWithBadType: { | ||
type: Number, | ||
default: 1, | ||
}, | ||
} | ||
} | ||
``` |
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,98 @@ | ||
/** | ||
* @fileoverview require prop type to be a constructor | ||
* @author Michał Sajnóg | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const message = 'The "{{name}}" property should be a constructor.' | ||
|
||
const forbiddenTypes = [ | ||
'Literal', | ||
'TemplateLiteral', | ||
'BinaryExpression', | ||
'UpdateExpression' | ||
] | ||
|
||
const isForbiddenType = nodeType => forbiddenTypes.indexOf(nodeType) > -1 | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'require prop type to be a constructor', | ||
category: undefined, // essential | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.1/docs/rules/require-prop-type-constructor.md' | ||
}, | ||
fixable: 'code', // or "code" or "whitespace" | ||
schema: [] | ||
}, | ||
|
||
create (context) { | ||
const fix = node => fixer => { | ||
if (node.type === 'Literal') { | ||
return fixer.replaceText(node, node.value) | ||
} else if ( | ||
node.type === 'TemplateLiteral' && | ||
node.expressions.length === 0 && | ||
node.quasis.length === 1 | ||
) { | ||
return fixer.replaceText(node, node.quasis[0].value.cooked) | ||
} | ||
} | ||
|
||
const checkPropertyNode = (p) => { | ||
if (isForbiddenType(p.value.type)) { | ||
context.report({ | ||
node: p.value, | ||
message, | ||
data: { | ||
name: utils.getStaticPropertyName(p.key) | ||
}, | ||
fix: fix(p.value) | ||
}) | ||
} else if (p.value.type === 'ArrayExpression') { | ||
p.value.elements | ||
.filter(prop => isForbiddenType(prop.type)) | ||
.forEach(prop => context.report({ | ||
node: prop, | ||
message, | ||
data: { | ||
name: utils.getStaticPropertyName(p.key) | ||
}, | ||
fix: fix(prop) | ||
})) | ||
} | ||
} | ||
|
||
return utils.executeOnVueComponent(context, (obj) => { | ||
const node = obj.properties.find(p => | ||
p.type === 'Property' && | ||
p.key.type === 'Identifier' && | ||
p.key.name === 'props' && | ||
p.value.type === 'ObjectExpression' | ||
) | ||
|
||
if (!node) return | ||
|
||
node.value.properties.forEach(p => { | ||
if (isForbiddenType(p.value.type) || p.value.type === 'ArrayExpression') { | ||
checkPropertyNode(p) | ||
} else if (p.value.type === 'ObjectExpression') { | ||
const typeProperty = p.value.properties.find(prop => | ||
prop.type === 'Property' && | ||
prop.key.name === 'type' | ||
) | ||
|
||
if (!typeProperty) return | ||
|
||
checkPropertyNode(typeProperty) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
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,134 @@ | ||
/** | ||
* @fileoverview require prop type to be a constructor | ||
* @author Michał Sajnóg | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/require-prop-type-constructor') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 7, | ||
sourceType: 'module' | ||
} | ||
}) | ||
ruleTester.run('require-prop-type-constructor', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'SomeComponent.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
myProp: Number, | ||
anotherType: [Number, String], | ||
extraProp: { | ||
type: Number, | ||
default: 10 | ||
}, | ||
lastProp: { | ||
type: [Number, Boolean] | ||
} | ||
} | ||
} | ||
` | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'SomeComponent.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
myProp: 'Number', | ||
anotherType: ['Number', 'String'], | ||
extraProp: { | ||
type: 'Number', | ||
default: 10 | ||
}, | ||
lastProp: { | ||
type: ['Boolean'] | ||
} | ||
} | ||
} | ||
`, | ||
output: ` | ||
export default { | ||
props: { | ||
myProp: Number, | ||
anotherType: [Number, String], | ||
extraProp: { | ||
type: Number, | ||
default: 10 | ||
}, | ||
lastProp: { | ||
type: [Boolean] | ||
} | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'The "myProp" property should be a constructor.', | ||
line: 4 | ||
}, { | ||
message: 'The "anotherType" property should be a constructor.', | ||
line: 5 | ||
}, { | ||
message: 'The "anotherType" property should be a constructor.', | ||
line: 5 | ||
}, { | ||
message: 'The "type" property should be a constructor.', | ||
line: 7 | ||
}, { | ||
message: 'The "type" property should be a constructor.', | ||
line: 11 | ||
}] | ||
}, | ||
{ | ||
filename: 'SomeComponent.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
a: \`String\`, | ||
b: Foo + '', | ||
c: 1, | ||
d: true, | ||
} | ||
} | ||
`, | ||
output: ` | ||
export default { | ||
props: { | ||
a: String, | ||
b: Foo + '', | ||
c: 1, | ||
d: true, | ||
} | ||
} | ||
`, | ||
errors: [{ | ||
message: 'The "a" property should be a constructor.', | ||
line: 4 | ||
}, { | ||
message: 'The "b" property should be a constructor.', | ||
line: 5 | ||
}, { | ||
message: 'The "c" property should be a constructor.', | ||
line: 6 | ||
}, { | ||
message: 'The "d" property should be a constructor.', | ||
line: 7 | ||
}] | ||
} | ||
] | ||
}) |
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 is need to check if the value type is string.
Hexadecimal literals such as
0xF
are converted to numeric literals like15
.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.
I didn't consider this case, as it seems extremely rare and nearly impossible to get unless you really want to break it.
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.
I think that you are right. I do not think it is necessary to take time and fix it.