-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Enforce valid names for computed properties #1106
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02afdb0
[1083] Svelte should throw a compile time error when illegal characte…
eh-dub 25a18ab
Merge branch 'master' of https://github.com/asweingarten/svelte into …
Rich-Harris cfdc890
run prettier (spaces -> tabs)
Rich-Harris 146327e
fix expected error position, tweak expected message to include sugges…
Rich-Harris b193036
simplify test slightly, add test for reserved words
Rich-Harris 1833bc1
use acorn.isIdentifierStart and isIdentifierChar to determine validity
Rich-Harris 66b64e2
types
Rich-Harris a85b09e
handle wacky identifier names in templates
Rich-Harris 80c55b1
oops
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Adapted from https://github.com/acornjs/acorn/blob/6584815dca7440e00de841d1dad152302fdd7ca5/src/tokenize.js | ||
// Reproduced under MIT License https://github.com/acornjs/acorn/blob/master/LICENSE | ||
|
||
export default function fullCharCodeAt(str: string, i: number): number { | ||
let code = str.charCodeAt(i) | ||
if (code <= 0xd7ff || code >= 0xe000) return code; | ||
|
||
let next = str.charCodeAt(i + 1); | ||
return (code << 10) + next - 0x35fdc00; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isIdentifierStart, isIdentifierChar } from 'acorn'; | ||
import fullCharCodeAt from './fullCharCodeAt'; | ||
|
||
export default function isValidIdentifier(str: string): boolean { | ||
let i = 0; | ||
|
||
while (i < str.length) { | ||
const code = fullCharCodeAt(str, i); | ||
if (!(i === 0 ? isIdentifierStart : isIdentifierChar)(code, true)) return false; | ||
|
||
i += code <= 0xffff ? 1 : 2; | ||
} | ||
|
||
return true; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{#each things as 𐊧}} | ||
<p>{{𐊧}}</p> | ||
{{/each}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"hash": 795130236, | ||
"html": { | ||
"start": 0, | ||
"end": 47, | ||
"type": "Fragment", | ||
"children": [ | ||
{ | ||
"start": 0, | ||
"end": 47, | ||
"type": "EachBlock", | ||
"expression": { | ||
"type": "Identifier", | ||
"start": 8, | ||
"end": 14, | ||
"name": "things" | ||
}, | ||
"children": [ | ||
{ | ||
"start": 24, | ||
"end": 37, | ||
"type": "Element", | ||
"name": "p", | ||
"attributes": [], | ||
"children": [ | ||
{ | ||
"start": 27, | ||
"end": 33, | ||
"type": "MustacheTag", | ||
"expression": { | ||
"type": "Identifier", | ||
"start": 29, | ||
"end": 31, | ||
"name": "𐊧" | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"context": "𐊧" | ||
} | ||
] | ||
}, | ||
"css": null, | ||
"js": null | ||
} |
9 changes: 9 additions & 0 deletions
9
test/validator/samples/properties-computed-cannot-be-reserved/errors.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[{ | ||
"message": | ||
"Computed property name 'new' is invalid — cannot be a JavaScript reserved word", | ||
"loc": { | ||
"line": 9, | ||
"column": 3 | ||
}, | ||
"pos": 87 | ||
}] |
12 changes: 12 additions & 0 deletions
12
test/validator/samples/properties-computed-cannot-be-reserved/input.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
a: 1 | ||
}; | ||
}, | ||
computed: { | ||
new: a => a * 2 | ||
} | ||
}; | ||
</script> |
8 changes: 8 additions & 0 deletions
8
test/validator/samples/properties-computed-must-be-valid-function-names/errors.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[{ | ||
"message": "Computed property name 'with-hyphen' is invalid — must be a valid identifier such as with_hyphen", | ||
"loc": { | ||
"line": 9, | ||
"column": 3 | ||
}, | ||
"pos": 87 | ||
}] |
12 changes: 12 additions & 0 deletions
12
test/validator/samples/properties-computed-must-be-valid-function-names/input.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
a: 1 | ||
}; | ||
}, | ||
computed: { | ||
"with-hyphen": a => a * 2 | ||
} | ||
}; | ||
</script> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
should this be moved to utilities?
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 tend to favour leaving things like this inline until you need them in more than one place, otherwise I find you can end up with a lot of premature abstractions