mirror of https://github.com/sveltejs/svelte
parent
b19303679d
commit
1833bc194f
@ -0,0 +1,11 @@
|
|||||||
|
import { isIdentifierStart, isIdentifierChar } from 'acorn';
|
||||||
|
|
||||||
|
export default function isValidIdentifier(str) {
|
||||||
|
if (!isIdentifierStart(str.charCodeAt(0), true)) return false;
|
||||||
|
|
||||||
|
for (let i = 0; i < str.length; i += 1) {
|
||||||
|
if (!isIdentifierChar(str.charCodeAt(i), true)) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
@ -1,25 +0,0 @@
|
|||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
Reference in new issue