-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Enforce valid names for computed properties
- Loading branch information
Showing
10 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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> |