mirror of https://github.com/sveltejs/svelte
Approach: For each property name, construct a string that defines a function and see if parsing that string with Acorn throws an exception. If it does, assemble an informative error message that states which property is invalid, the first invalid character, and the location of that character within the name. Changes to codebase: - Added new validator test "properties-computed-must-be-valid-function-names" - Added new check into src/validate/js/propValidators/computed.ts, "checkForValidIdentifiers" - this check was added to src/validate/js/utils/checkForValidIdentifiers.ts like the other checks in "computed.ts"pull/1099/head
parent
2537db90be
commit
02afdb03fa
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
@ -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
|
||||||
|
}]
|
@ -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>
|
Loading…
Reference in new issue