Skip to content
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

Merged
merged 1 commit into from
Aug 1, 2017

Conversation

armano2
Copy link
Contributor

@armano2 armano2 commented Jul 16, 2017

This PR implements rules proposed in #19

DONE:

  • Create documtation
  • Add tests
  • Implement rule

@armano2 armano2 changed the title WIP: Add prop-specificity rule. Add prop-specificity rule. Jul 17, 2017
@armano2 armano2 force-pushed the patch-10-prop-specificity branch from b53a8e0 to 71e583e Compare July 19, 2017 17:39
Copy link
Member

@mysticatea mysticatea left a 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?

@armano2 armano2 changed the title Add prop-specificity rule. Add require-prop-types rule. Jul 19, 2017
@armano2
Copy link
Contributor Author

armano2 commented Jul 19, 2017

@mysticatea i like more require-prop-types -> renamed

Copy link
Member

@michalsnik michalsnik left a 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 👍

// Helpers
// ----------------------------------------------------------------------

function getPropTypes (componentProperties) {
Copy link
Member

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.

return { key, node: cp, hasType }
})
} else if (type === 'ArrayExpression') { // props: [
// nodes = node.elements
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this?

if (!cp.hasType) {
context.report({
node: cp.node || data.node,
message: 'Prop "{{name}}" definitions should always be as detailed with at least type(s).',
Copy link
Member

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.

} 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).'
Copy link
Member

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

@armano2
Copy link
Contributor Author

armano2 commented Jul 21, 2017

@michalsnik suggestions applied 🐶

Copy link
Member

@michalsnik michalsnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there 🚀

// ----------------------------------------------------------------------

function objectHasType (nodes) {
return !!(nodes
Copy link
Member

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)

const key = cp.key.name
let hasType = true
if (cp.value.type === 'ObjectExpression') { // foo: {
hasType = objectHasType(cp.value.properties)
Copy link
Member

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.

@armano2
Copy link
Contributor Author

armano2 commented Jul 22, 2017

@michalsnik i found out that we are not supporting

['type']: ....

in rules 🗡 and i added getStaticPropertyName what we should always use when we are trying to get name of object property name

@armano2 armano2 force-pushed the patch-10-prop-specificity branch from f6f4872 to c53fa49 Compare July 22, 2017 16:45
* @param {ASTNode} node - The node to get.
* @return {string|null} The property name if static. Otherwise, null.
*/
getStaticPropertyName (node) {
Copy link
Member

@michalsnik michalsnik Jul 23, 2017

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalsnik added

Copy link
Member

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).

Copy link
Contributor Author

@armano2 armano2 Jul 23, 2017

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.

@armano2 armano2 force-pushed the patch-10-prop-specificity branch 2 times, most recently from 7e19002 to 2dc3d98 Compare July 23, 2017 13:52
Copy link
Member

@mysticatea mysticatea left a 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.


return utils.executeOnVue(context, (obj) => {
const node = obj.properties
.filter(p =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

function checkProperties (items) {
items.map(cp => {
Copy link
Member

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 😃

Copy link
Contributor Author

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 🍡


function objectHasType (node) {
const typeProperty = node.properties
.filter(p =>
Copy link
Member

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.

Copy link
Contributor Author

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 🍏

@armano2
Copy link
Contributor Author

armano2 commented Jul 23, 2017

@mysticatea suggestions applied, thank you, and thank you for sharing "http://node.green/"

@armano2 armano2 force-pushed the patch-10-prop-specificity branch from cd86f33 to d7d5182 Compare July 31, 2017 19:50
Copy link
Member

@mysticatea mysticatea left a 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!

@mysticatea mysticatea merged commit b8d11de into vuejs:master Aug 1, 2017
@armano2 armano2 deleted the patch-10-prop-specificity branch August 1, 2017 13:33
filipalacerda pushed a commit to filipalacerda/eslint-plugin-vue that referenced this pull request Aug 5, 2017
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants