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

quoted method names are treated as undefined when validating / checking for duplicates #914

Closed
m59peacemaker opened this issue Nov 8, 2017 · 3 comments · Fixed by #933
Closed

Comments

@m59peacemaker
Copy link
Contributor

m59peacemaker commented Nov 8, 2017

This is broken:

methods: {
  'foo': () => {},
  'bar': () => {}
}

REPL

Stack Trace bundle.35d793bc40a79d89436e63d8b274bfd88d0208bd.js:1 ValidationError: Duplicate property 'undefined' at ValidationError.CompileError [as constructor] (https://unpkg.com/svelte@1.41.2/compiler/svelte.js:11347:28) at new ValidationError (https://unpkg.com/svelte@1.41.2/compiler/svelte.js:12555:28) at Validator.error (https://unpkg.com/svelte@1.41.2/compiler/svelte.js:12576:15) at https://unpkg.com/svelte@1.41.2/compiler/svelte.js:11501:23 at Array.forEach () at checkForDupes (https://unpkg.com/svelte@1.41.2/compiler/svelte.js:11499:16) at methods (https://unpkg.com/svelte@1.41.2/compiler/svelte.js:11685:5) at https://unpkg.com/svelte@1.41.2/compiler/svelte.js:12067:21 at Array.forEach () at https://unpkg.com/svelte@1.41.2/compiler/svelte.js:12064:41

Admittedly, I encountered this whilst trying to hack svelte's static analysis of my method names. So, perhaps it is best that method names are not permitted to be quoted. I need to call some functions that are created at runtime, so I did this in oncreate this.someFunctions = makeSomeFunctions(args), in the view on:stuff="someFunctions.foo()", and methods: { 'someFunctions.foo': noop }`. It was just a thought to get svelte to stop yelling at me. But quite hacky. Maybe it is good to prevent me and everyone from doing this.

@m59peacemaker
Copy link
Contributor Author

If we do want quoted method names to work, I can probably do the PR.

@Conduitry
Copy link
Member

I was just doing some poking around, and this seems to be an issue in a number of other places as well. When traversing the AST and looking at the properties of an ObjectExpression, Svelte looks at prop.key.name because it's expecting prop.key to be an Identifier. With quoted properties, prop.key is a Literal and what we want to be looking at is prop.key.value.

We could probably do with some common helper function that extracts the name for Identifier props, and extracts the value for string Literal props, and throws an error for other node types. The more challenging part is probably going to be finding all of the places where we need to watch out for this. Even if we wanted to disallow quoted keys, it'd still be almost as much work, since we'd need to hunt down all of these instances anyway. So I'd vote we might as well support literal string property keys.

As for the original issue you were trying to work around, it seems like we could get at that in a couple different ways - One by making it easier to ignore specific warnings (#824) - Or by providing some way in the component definition export default { ... } to specify additional methods (maybe with wildcards?) that Svelte shouldn't warn about being undefined.

@Rich-Harris
Copy link
Member

This is fixed in 1.42. For the warnings, I think #824 is the way to go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants