Skip to content

Allow to use @vue/component to set enable parsing vue objects #123

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 3 commits into from
Aug 8, 2017

Conversation

armano2
Copy link
Contributor

@armano2 armano2 commented Aug 2, 2017

fixes #109

@@ -77,8 +77,8 @@ ruleTester.run('no-dupe-keys', rule, {

invalid: [
{
filename: 'test.vue',
code: `
// filename: 'test.vue',
Copy link
Member

Choose a reason for hiding this comment

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

Please create another tests for that, don't comment actual ones. Also try case with block comment too :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@armano2 armano2 force-pushed the patch-28-comment-vue-component branch from 5ff441f to 56a0306 Compare August 4, 2017 17:02
@armano2
Copy link
Contributor Author

armano2 commented Aug 4, 2017

@michalsnik new tests added

@armano2 armano2 force-pushed the patch-28-comment-vue-component branch from c3484a4 to eacc0d0 Compare August 4, 2017 17:22
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.

LGTM 👍 Nice one. Only one more thing - we need to add information about this somewhere in readme, so that people actually know it's there and that it's ready to be used.

@armano2
Copy link
Contributor Author

armano2 commented Aug 4, 2017

@michalsnik do you have any suggestions? I'm terrible at describing/naming stuff xd

@michalsnik
Copy link
Member

michalsnik commented Aug 4, 2017

I was thinking about adding information in Usage section, that we currently detect components based on certain requirements (list them) and if someone wants custom .js files to be checked in terms of the available rules he/she needs to add special comment, so that we can assume it's also to be considered as a component.

Btw. Now I realised that there might be other cases, when you for example want to create few small components inside one .js files and compose them, you could get:

// utils/example.js
const ChildComponent = {
  template: `<div>{{msg}}</div>`,
  data () {
    return { msg: 'Hello child' }
  }
}

export default {
 template: `<child-component />`,
  components: {
    ChildComponent
  }
}

such a situation might also have place in .vue files. My proposition would be to detect if @vue/component is exactly one line above the ObjectExpression - if yes -> treat the ObjectExpression as a component. This should solve all cases and shouldn't be hard to implement :) What do you think @armano2 ?

The above file could look like this:

// @vue/component
const ChildComponent = {
  template: `<div>{{msg}}</div>`,
  data () {
    return { msg: 'Hello child' }
  }
}

// @vue/component
export default {
 template: `<child-component />`,
  components: {
    ChildComponent
  }
}

@armano2
Copy link
Contributor Author

armano2 commented Aug 4, 2017

@michalsnik hmm i like it, and i was thinking about it before.

component definition should work only if next block is
export default {
export const/var/let [name] = {
const/var/let [name] = {

what do you think?

@michalsnik
Copy link
Member

There are also async components, I wonder if simple check for an object right below comment wouldn't be enough. Then it would be also possible to do:

Vue.component('async-example', function (resolve, reject) {
  setTimeout(function () {
    // @vue/component
    resolve({
      template: '<div>I am async!</div>'
    })
  }, 1000)
})

@armano2
Copy link
Contributor Author

armano2 commented Aug 4, 2017

@michalsnik i'm going to create separate test file for all cases to have them all in one place and i'm going to include async component to

@armano2
Copy link
Contributor Author

armano2 commented Aug 4, 2017

},
{
filename: 'test.js',
code: `Vue.component('async-example', function (resolve, reject) { })`,
Copy link
Member

Choose a reason for hiding this comment

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

resolve({}) missing

parserOptions
},
{
filename: 'test.js',
Copy link
Member

Choose a reason for hiding this comment

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

Add some cases with .jsx and .vue files too that are not to be treated as components by default.

errors: [makeError(1)]
},
{
filename: 'test.vue',
Copy link
Member

Choose a reason for hiding this comment

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

Add the same test for test.jsx file

{
filename: 'test.js',
code: `Vue.component('async-example', function (resolve, reject) {
resolve({})
Copy link
Member

Choose a reason for hiding this comment

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

This should actually be not treated as component, we're not supporting this case by default, only with optional comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@armano2 armano2 force-pushed the patch-28-comment-vue-component branch from dad5cd6 to 753d86d Compare August 5, 2017 01:25
@armano2
Copy link
Contributor Author

armano2 commented Aug 5, 2017

@michalsnik i applied all requested changes, and all tests are more generic now.

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.

Added few suggestions and questions. Plus we still don't have an information in readme about those comments.

code: `export default { }`,
parserOptions
}
].concat(notFoundTests('js')).concat(notFoundTests('jsx')).concat(notFoundTests('vue')),
Copy link
Member

Choose a reason for hiding this comment

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

I'd call it validTests and invalidTests for consistency.

return [
{
filename: `test.${ext}`,
code: `// ${ext}
Copy link
Member

Choose a reason for hiding this comment

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

I guess you added those comments for easier debugging?
If yes please keep them in once place, as for now I can see them on top and bottom of the code example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that was the point, and i moved them to down (i forgot to this will all of them)

return utils.executeOnVueComponent(context, obj => {
context.report({
node: obj,
message: 'Internal test: component.'
Copy link
Member

Choose a reason for hiding this comment

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

I think Component detected message would be a little bit more descriptive.

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 changed

'ExportDefaultDeclaration:exit' (node) {
// export default {} in .vue || .jsx
if (!_this.isVueComponentFile(node, filePath)) return
if (!_this.isVueComponentFile(node, filePath, context) || hasNode(node)) return
Copy link
Member

Choose a reason for hiding this comment

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

Why did you passed context to isVueComponentFile? And why did you added hasNode here? I thought current check is ok and we only needed to add comment detection before objects?

Copy link
Contributor Author

@armano2 armano2 Aug 5, 2017

Choose a reason for hiding this comment

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

context removed and to prevent duplicates from callback, i renamed this function to isDuplicateNode

cb(node.declaration)
},
'CallExpression:exit' (node) {
// Vue.component('xxx', {}) || component('xxx', {})
if (!_this.isVueComponent(node)) return
if (!_this.isVueComponent(node) || foundNodes.some(el => el.loc.start.line === node.loc.start.line)) return
Copy link
Member

Choose a reason for hiding this comment

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

Why did you added here additional check too?

Copy link
Contributor Author

@armano2 armano2 Aug 5, 2017

Choose a reason for hiding this comment

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

to prevent duplicates when someone do callExpression and declare // @vue/component

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see, good thinking :)

},
{
filename: `test.${ext}`,
code: `// ${ext}
Copy link
Member

Choose a reason for hiding this comment

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

Please consider formatting those code example as follows:

code: `
  /* @vue/component */
  export default {}
`

I think they would be much easier to read :)

@armano2
Copy link
Contributor Author

armano2 commented Aug 5, 2017

@michalsnik thank you for suggestions, changes applied

@michalsnik
Copy link
Member

Cool @armano2 Now please add info in readme and we're good to go :) Good work!

@armano2
Copy link
Contributor Author

armano2 commented Aug 6, 2017

@michalsnik now the hardest part 👎

i wrote something, but i don't like it :(

@michalsnik
Copy link
Member

@armano2 And what do you think about:

Attention

All component-related rules are being applied to code that passes any of the following checks:

  • Vue.component() expression
  • export default {} in .vue or .jsx file

If you however want to take advantage of our rules in any of your custom objects that are Vue components, you might need to use special comment // @vue/component that marks object in the next line as a Vue component in any file, e.g.:

// @vue/component
const CustomComponent = {
  name: 'custom-component',
  template: '<div></div>'
}

Vue.component('AsyncComponent', (resolve, reject) => {
  setTimeout(() => {
    // @vue/component
    resolve({
      name: 'async-component',
      template: '<div></div>'
    })
  }, 500)
})

@armano2
Copy link
Contributor Author

armano2 commented Aug 6, 2017

@michalsnik i like it way more

i'm just terrible at writing documentations

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.

LGTM 👍 But can you please also take a look @mysticatea ?

@armano2 armano2 force-pushed the patch-28-comment-vue-component branch from db720cf to 93ca30f Compare August 6, 2017 20:40
@armano2 armano2 changed the title Allow to use @vue/component to set type of file to vue Allow to use @vue/component to set enable parsing vue objects Aug 6, 2017
@armano2 armano2 force-pushed the patch-28-comment-vue-component branch from 93ca30f to b7b4991 Compare August 6, 2017 20:43
armano2 added 2 commits August 6, 2017 23:49
LineComment and BlockComment was removed in 4.0.0
@armano2 armano2 force-pushed the patch-28-comment-vue-component branch from 1646194 to ba38e5a Compare August 6, 2017 22:14
@michalsnik michalsnik merged commit 4d0c1d3 into vuejs:master Aug 8, 2017
@armano2 armano2 deleted the patch-28-comment-vue-component branch August 8, 2017 15:11
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.

How to make parser work on plain js files.
2 participants