-
-
Notifications
You must be signed in to change notification settings - Fork 669
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
Add require-prop-types
rule.
#85
Conversation
b53a8e0
to
71e583e
Compare
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.
Thank you for contributing!
The implementation looks good to me.
But I'm not that this rule name is proper. This rule reports the property definitions which don't have their type definition, so I think the rule name should include "type" word. Maybe require-prop-types
?
@mysticatea i like more |
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.
Few suggestions @armano2, otherwise LGTM 👍
lib/rules/require-prop-types.js
Outdated
// Helpers | ||
// ---------------------------------------------------------------------- | ||
|
||
function getPropTypes (componentProperties) { |
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.
In my opinion this function contains too much logic. Can you please try to split it into smaller chunks - easier to understand and test? We should make the code as simple as possible, so that it's easier for anyone to contribute later on.
Such functions should preferable contain a small comment above on what they do, what parameters are required and what's the supposed output.
lib/rules/require-prop-types.js
Outdated
return { key, node: cp, hasType } | ||
}) | ||
} else if (type === 'ArrayExpression') { // props: [ | ||
// nodes = node.elements |
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.
What about this?
lib/rules/require-prop-types.js
Outdated
if (!cp.hasType) { | ||
context.report({ | ||
node: cp.node || data.node, | ||
message: 'Prop "{{name}}" definitions should always be as detailed with at least type(s).', |
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.
What about this message instead?
Prop "{{name}}" should define at least it's type.
lib/rules/require-prop-types.js
Outdated
} else if (data.type === 'ArrayExpression') { | ||
context.report({ | ||
node: data.node, | ||
message: 'Props definitions should always be an object and detailed with at least type(s).' |
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.
Maybe this instead?
Props' should at least define their types.
or
Prop types are required
@michalsnik suggestions applied 🐶 |
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.
Almost there 🚀
lib/rules/require-prop-types.js
Outdated
// ---------------------------------------------------------------------- | ||
|
||
function objectHasType (nodes) { | ||
return !!(nodes |
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.
KISS:
const typeProperty = nodes.filter(p =>
// ...
return Boolean(typeProperty)
lib/rules/require-prop-types.js
Outdated
const key = cp.key.name | ||
let hasType = true | ||
if (cp.value.type === 'ObjectExpression') { // foo: { | ||
hasType = objectHasType(cp.value.properties) |
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.
If this function name is to be accurate you should pass cp.value
as it's an ObjectExpression
.
@michalsnik i found out that we are not supporting
in rules 🗡 and i added |
f6f4872
to
c53fa49
Compare
* @param {ASTNode} node - The node to get. | ||
* @return {string|null} The property name if static. Otherwise, null. | ||
*/ | ||
getStaticPropertyName (node) { |
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.
Please add unit tests for this method, I just realised they're not there.
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.
@michalsnik added
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's sweet memories, I wrote it at eslint/lib/ast-utils.
I hope eslint/lib/ast-utils
to separate to another package (though it's an item of ESLint 5 wish list).
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.
this method is not same but almost, and ast-utils is not exported than i will have to import file directly what can cause some errors between versions of eslint.
7e19002
to
2dc3d98
Compare
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.
Thank you! Mostly LGTM. There are some nit-picking.
lib/rules/require-prop-types.js
Outdated
|
||
return utils.executeOnVue(context, (obj) => { | ||
const node = obj.properties | ||
.filter(p => |
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.
We can use Array#find
instead.
http://node.green/#ES2015-built-ins-typed-arrays--TypedArray--prototype-find
lib/rules/require-prop-types.js
Outdated
} | ||
|
||
function checkProperties (items) { | ||
items.map(cp => { |
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'm wondering that it uses Array#map
to iterate the array. I like for-of
😃
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.
this map is incorrect 👍 we are even not returning any values 🍡
lib/rules/require-prop-types.js
Outdated
|
||
function objectHasType (node) { | ||
const typeProperty = node.properties | ||
.filter(p => |
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.
We can use Array#find
instead.
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 have to start using http://node.green/ to be sure what i can/can't use 🍏
@mysticatea suggestions applied, thank you, and thank you for sharing "http://node.green/" |
cd86f33
to
d7d5182
Compare
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.
LGTM, thank you for amazing work!
* master: Add rule `vue/require-valid-default-prop`. (vuejs#119) 3.10.0 Update readme to 3.10.0 Chore: remove package-lock.json (vuejs#128) Fix: parserService must exist always (fixes vuejs#125) (vuejs#127) Add rule `require-render-return`. (vuejs#114) 3.9.0 Update package-lock Update: options for `no-duplicate-attributes` (fixes vuejs#112)(vuejs#113) New: add rule `attribute-hyphenation`. (fixes vuejs#92)(vuejs#95) Add namespace check of svg & mathML instead of tag names (vuejs#120)⚠️ Add support for deprecated state in update-rules⚠️ (vuejs#121) Add rules: `no-dupe-keys` and `no-reserved-keys`. (vuejs#88) Chore: Improve tests for name-property-casing & improve documentation (vuejs#115) New: add `require-prop-types` rule (fixes vuejs#19)(vuejs#85) Update: upgrade vue-eslint-parser (fixes vuejs#36, fixes vuejs#56, fixes vuejs#96) (vuejs#116)
This PR implements rules proposed in #19
DONE: