Skip to content

[1083] Svelte should throw a compile time error when illegal characte… #1099

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/validate/js/propValidators/computed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import checkForDupes from '../utils/checkForDupes';
import checkForComputedKeys from '../utils/checkForComputedKeys';
import checkForValidIdentifiers from '../utils/checkForValidIdentifiers';
import { Validator } from '../../';
import { Node } from '../../../interfaces';
import walkThroughTopFunctionScope from '../../../utils/walkThroughTopFunctionScope';
Expand All @@ -20,6 +21,7 @@ export default function computed(validator: Validator, prop: Node) {

checkForDupes(validator, prop.value.properties);
checkForComputedKeys(validator, prop.value.properties);
checkForValidIdentifiers(validator, prop.value.properties);

prop.value.properties.forEach((computation: Node) => {
if (!isFunctionExpression.has(computation.value.type)) {
Expand Down
21 changes: 21 additions & 0 deletions src/validate/js/utils/checkForValidIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Validator } from '../../';
import { Node } from '../../../interfaces';
import getName from '../../../utils/getName';
import { parse } from 'acorn';

export default function checkForValidIdentifiers(
validator: Validator,
properties: Node[]
) {
properties.forEach(prop => {
const name = getName(prop.key);
const functionDefinitionString = `function ${name}() {}`;
try {
parse(functionDefinitionString);
} catch(exception) {
const invalidCharacter = functionDefinitionString[exception.pos]
validator.error(`Computed property name "${name}" is invalid. Character '${invalidCharacter}' at position ${exception.pos} is illegal in function identifiers`, prop.start);
}

});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "Computed property name \"hours-hyphen\" is invalid. Character '-' at position 14 is illegal in function identifiers",
"loc": {
"line": 14,
"column": 6
},
"pos": 172
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<p>
The hour is
<strong>{{hours}}</strong>
</p>

<script>
export default {
data() {
return {
time: new Date()
};
},
computed: {
"hours-hyphen": time => time.getHours()
}
};
</script>