-
Notifications
You must be signed in to change notification settings - Fork 15
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
fix parser for computed properties with an explicit getter function #12
Conversation
Thanks for your PR. :-) |
if (property.key.name === 'computed') { | ||
if (entry.value instanceof utils.NodeFunction) { | ||
entry.dependencies = utils.getDependencies(entry.value, this.source.script) | ||
} else if (entry.value instanceof Object && entry.value.get instanceof utils.NodeFunction) { |
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 branch need to be cover by unit tests
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 added the unit tests, coverage is back up :-)
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.
Good. Now you have to add a unit test to cover the case where we have a missing get()
function. Some think like:
it('should parse computed prop with missing get() method', (done) => {
const filename = './fixtures/checkbox.vue'
const script = `
export default {
computed: {
idGetter: {
foo () {
const value = this.value
return this.name + value
}
}
}
}
`
const options = {
source: { script },
filename
}
const parser = new Parser(options)
parser.walk().on('computed', (prop) => {
if (prop.dependencies.length) {
return done(new Error('Should parse computed prop without dependencies'))
}
done()
})
})
…nor an object with a getter
* @private | ||
*/ | ||
idGetter: { | ||
get () { |
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 if it's set()
?
I think dependencies prop is about getter... is not it?
If right, the parser should only parse the getter and ignore the setter function.
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 mistaken... your changes already cover this purpose.
lib/parser.js
Outdated
@@ -60,6 +60,7 @@ class Parser extends EventEmitter { | |||
entry.value = entry.value[entry.name] | |||
|
|||
if (property.key.name === 'computed') { | |||
/* istanbul ignore else */ |
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.
Hum... I don't like istanbul ignore ...
statement. It may a problem if we add the else
statement and forget to remove the istanbul
statement. I prefer to add a unit test to cover the else
branch
lib/parser.js
Outdated
if (entry.value instanceof utils.NodeFunction) { | ||
entry.dependencies = utils.getDependencies(entry.value, this.source.script) | ||
} else if (entry.value instanceof Object && entry.value.get instanceof utils.NodeFunction) { | ||
entry.dependencies = utils.getDependencies(entry.value.get, this.source.script) | ||
} else { | ||
const error = new Error('Computed property must be a function or an object with a get() function') |
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 the parser should not emit Vue syntax error here. Syntax error must be throwed by #13
The parser should only parse the get
function and ignore the others one.
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 added this case because otherwise the coverage will go down (slightly) as there is no "else" case. It is fine by me to remove the else block here, should I do that?
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.
Yes, the else
branch is not required in this case.
* @private | ||
*/ | ||
idGetter: { | ||
get () { |
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 mistaken... your changes already cover this purpose.
if (property.key.name === 'computed') { | ||
if (entry.value instanceof utils.NodeFunction) { | ||
entry.dependencies = utils.getDependencies(entry.value, this.source.script) | ||
} else if (entry.value instanceof Object && entry.value.get instanceof utils.NodeFunction) { |
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.
Finally I think the else
block is require to set entry.dependencie = []
.
What do you think?
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.
Hi, ok I will change it to return an empty array and change the test.
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.
Done :-)
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.
Great!!!
Thank you @pschmiedel for your PR.
I'll release it soon.
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.
Thanks a lot for the quick response!
I added a test for an invalid computed property, coverage is back up :-) |
…pty array as dependencies
Hi, I added a small fix for parsing computed properties that use the getter/setter syntax. Currently this breaks with the following error:
I'd appreciate it if you could merge this PR.