diff --git a/src/parser/converts/attr.ts b/src/parser/converts/attr.ts index 1eaf8f69..912eb4fc 100644 --- a/src/parser/converts/attr.ts +++ b/src/parser/converts/attr.ts @@ -381,7 +381,14 @@ function convertStyleDirective( directive.key, null, (expression) => { - directive.key.name = expression as ESTree.Identifier + if (expression.type !== "Identifier") { + throw new ParseError( + `Expected JS identifier or attribute value.`, + expression.range![0], + ctx, + ) + } + directive.key.name = expression }, ) return directive @@ -627,12 +634,26 @@ function processDirectiveExpression< getWithLoc(node.expression).end = keyName.range[1] } processors.processExpression(node.expression, shorthand).push((es) => { + if (node.expression && es.type !== node.expression.type) { + throw new ParseError( + `Expected ${node.expression.type}, but ${es.type} found.`, + es.range![0], + ctx, + ) + } directive.expression = es }) } if (!shorthand) { if (processors.processName) { processors.processName(keyName).push((es) => { + if (es.type !== "Identifier") { + throw new ParseError( + `Expected JS identifier.`, + es.range![0], + ctx, + ) + } key.name = es }) } else { diff --git a/tests/fixtures/parser/ast/illegal/dot-in-bind02-no-undef-result.json b/tests/fixtures/parser/ast/illegal/dot-in-bind02-no-undef-result.json deleted file mode 100644 index a582b56c..00000000 --- a/tests/fixtures/parser/ast/illegal/dot-in-bind02-no-undef-result.json +++ /dev/null @@ -1,8 +0,0 @@ -[ - { - "ruleId": "no-undef", - "code": "va", - "line": 3, - "column": 13 - } -] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/illegal/dot-in-bind02-output.json b/tests/fixtures/parser/ast/illegal/dot-in-bind02-output.json deleted file mode 100644 index ad8a0d2e..00000000 --- a/tests/fixtures/parser/ast/illegal/dot-in-bind02-output.json +++ /dev/null @@ -1,587 +0,0 @@ -{ - "type": "Program", - "body": [ - { - "type": "SvelteScriptElement", - "name": { - "type": "SvelteName", - "name": "script", - "range": [ - 1, - 7 - ], - "loc": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 7 - } - } - }, - "startTag": { - "type": "SvelteStartTag", - "attributes": [], - "selfClosing": false, - "range": [ - 0, - 8 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 8 - } - } - }, - "body": [], - "endTag": { - "type": "SvelteEndTag", - "range": [ - 9, - 18 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 9 - } - } - }, - "range": [ - 0, - 18 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 2, - "column": 9 - } - } - }, - { - "type": "SvelteText", - "value": "\n", - "range": [ - 18, - 19 - ], - "loc": { - "start": { - "line": 2, - "column": 9 - }, - "end": { - "line": 3, - "column": 0 - } - } - }, - { - "type": "SvelteElement", - "kind": "html", - "name": { - "type": "SvelteName", - "name": "input", - "range": [ - 20, - 25 - ], - "loc": { - "start": { - "line": 3, - "column": 1 - }, - "end": { - "line": 3, - "column": 6 - } - } - }, - "startTag": { - "type": "SvelteStartTag", - "attributes": [ - { - "type": "SvelteDirective", - "kind": "Binding", - "key": { - "type": "SvelteDirectiveKey", - "name": { - "type": "SvelteName", - "name": "va.lue", - "range": [ - 31, - 37 - ], - "loc": { - "start": { - "line": 3, - "column": 12 - }, - "end": { - "line": 3, - "column": 18 - } - } - }, - "modifiers": [], - "range": [ - 26, - 37 - ], - "loc": { - "start": { - "line": 3, - "column": 7 - }, - "end": { - "line": 3, - "column": 18 - } - } - }, - "expression": { - "type": "MemberExpression", - "computed": false, - "object": { - "type": "Identifier", - "name": "va", - "range": [ - 31, - 33 - ], - "loc": { - "start": { - "line": 3, - "column": 12 - }, - "end": { - "line": 3, - "column": 14 - } - } - }, - "optional": false, - "property": { - "type": "Identifier", - "name": "lue", - "range": [ - 34, - 37 - ], - "loc": { - "start": { - "line": 3, - "column": 15 - }, - "end": { - "line": 3, - "column": 18 - } - } - }, - "range": [ - 31, - 37 - ], - "loc": { - "start": { - "line": 3, - "column": 12 - }, - "end": { - "line": 3, - "column": 18 - } - } - }, - "shorthand": true, - "range": [ - 26, - 37 - ], - "loc": { - "start": { - "line": 3, - "column": 7 - }, - "end": { - "line": 3, - "column": 18 - } - } - } - ], - "selfClosing": true, - "range": [ - 19, - 39 - ], - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 20 - } - } - }, - "children": [], - "endTag": null, - "range": [ - 19, - 39 - ], - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 20 - } - } - } - ], - "sourceType": "module", - "comments": [], - "tokens": [ - { - "type": "Punctuator", - "value": "<", - "range": [ - 0, - 1 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 1, - "column": 1 - } - } - }, - { - "type": "HTMLIdentifier", - "value": "script", - "range": [ - 1, - 7 - ], - "loc": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 7 - } - } - }, - { - "type": "Punctuator", - "value": ">", - "range": [ - 7, - 8 - ], - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - } - } - }, - { - "type": "Punctuator", - "value": "<", - "range": [ - 9, - 10 - ], - "loc": { - "start": { - "line": 2, - "column": 0 - }, - "end": { - "line": 2, - "column": 1 - } - } - }, - { - "type": "Punctuator", - "value": "/", - "range": [ - 10, - 11 - ], - "loc": { - "start": { - "line": 2, - "column": 1 - }, - "end": { - "line": 2, - "column": 2 - } - } - }, - { - "type": "HTMLIdentifier", - "value": "script", - "range": [ - 11, - 17 - ], - "loc": { - "start": { - "line": 2, - "column": 2 - }, - "end": { - "line": 2, - "column": 8 - } - } - }, - { - "type": "Punctuator", - "value": ">", - "range": [ - 17, - 18 - ], - "loc": { - "start": { - "line": 2, - "column": 8 - }, - "end": { - "line": 2, - "column": 9 - } - } - }, - { - "type": "HTMLText", - "value": "\n", - "range": [ - 18, - 19 - ], - "loc": { - "start": { - "line": 2, - "column": 9 - }, - "end": { - "line": 3, - "column": 0 - } - } - }, - { - "type": "Punctuator", - "value": "<", - "range": [ - 19, - 20 - ], - "loc": { - "start": { - "line": 3, - "column": 0 - }, - "end": { - "line": 3, - "column": 1 - } - } - }, - { - "type": "HTMLIdentifier", - "value": "input", - "range": [ - 20, - 25 - ], - "loc": { - "start": { - "line": 3, - "column": 1 - }, - "end": { - "line": 3, - "column": 6 - } - } - }, - { - "type": "HTMLIdentifier", - "value": "bind", - "range": [ - 26, - 30 - ], - "loc": { - "start": { - "line": 3, - "column": 7 - }, - "end": { - "line": 3, - "column": 11 - } - } - }, - { - "type": "Punctuator", - "value": ":", - "range": [ - 30, - 31 - ], - "loc": { - "start": { - "line": 3, - "column": 11 - }, - "end": { - "line": 3, - "column": 12 - } - } - }, - { - "type": "Identifier", - "value": "va", - "range": [ - 31, - 33 - ], - "loc": { - "start": { - "line": 3, - "column": 12 - }, - "end": { - "line": 3, - "column": 14 - } - } - }, - { - "type": "Punctuator", - "value": ".", - "range": [ - 33, - 34 - ], - "loc": { - "start": { - "line": 3, - "column": 14 - }, - "end": { - "line": 3, - "column": 15 - } - } - }, - { - "type": "Identifier", - "value": "lue", - "range": [ - 34, - 37 - ], - "loc": { - "start": { - "line": 3, - "column": 15 - }, - "end": { - "line": 3, - "column": 18 - } - } - }, - { - "type": "Punctuator", - "value": "/", - "range": [ - 37, - 38 - ], - "loc": { - "start": { - "line": 3, - "column": 18 - }, - "end": { - "line": 3, - "column": 19 - } - } - }, - { - "type": "Punctuator", - "value": ">", - "range": [ - 38, - 39 - ], - "loc": { - "start": { - "line": 3, - "column": 19 - }, - "end": { - "line": 3, - "column": 20 - } - } - } - ], - "range": [ - 0, - 40 - ], - "loc": { - "start": { - "line": 1, - "column": 0 - }, - "end": { - "line": 4, - "column": 0 - } - } -} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/illegal/dot-in-bind02-scope-output.json b/tests/fixtures/parser/ast/illegal/dot-in-bind02-scope-output.json deleted file mode 100644 index ac8842cd..00000000 --- a/tests/fixtures/parser/ast/illegal/dot-in-bind02-scope-output.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "type": "global", - "variables": [ - { - "name": "$$slots", - "identifiers": [], - "defs": [], - "references": [] - }, - { - "name": "$$props", - "identifiers": [], - "defs": [], - "references": [] - }, - { - "name": "$$restProps", - "identifiers": [], - "defs": [], - "references": [] - } - ], - "references": [], - "childScopes": [ - { - "type": "module", - "variables": [], - "references": [ - { - "identifier": { - "type": "Identifier", - "name": "va", - "range": [ - 31, - 33 - ], - "loc": { - "start": { - "line": 3, - "column": 12 - }, - "end": { - "line": 3, - "column": 14 - } - } - }, - "from": "module", - "init": null, - "resolved": null - } - ], - "childScopes": [], - "through": [ - { - "identifier": { - "type": "Identifier", - "name": "va", - "range": [ - 31, - 33 - ], - "loc": { - "start": { - "line": 3, - "column": 12 - }, - "end": { - "line": 3, - "column": 14 - } - } - }, - "from": "module", - "init": null, - "resolved": null - } - ] - } - ], - "through": [ - { - "identifier": { - "type": "Identifier", - "name": "va", - "range": [ - 31, - 33 - ], - "loc": { - "start": { - "line": 3, - "column": 12 - }, - "end": { - "line": 3, - "column": 14 - } - } - }, - "from": "module", - "init": null, - "resolved": null - } - ] -} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/illegal/dot-in-bind02-input.svelte b/tests/fixtures/parser/error/dot-in-bind02-input.svelte similarity index 100% rename from tests/fixtures/parser/ast/illegal/dot-in-bind02-input.svelte rename to tests/fixtures/parser/error/dot-in-bind02-input.svelte diff --git a/tests/fixtures/parser/error/dot-in-bind02-output.json b/tests/fixtures/parser/error/dot-in-bind02-output.json new file mode 100644 index 00000000..5e4b6c73 --- /dev/null +++ b/tests/fixtures/parser/error/dot-in-bind02-output.json @@ -0,0 +1,6 @@ +{ + "index": 31, + "lineNumber": 3, + "message": "Expected Identifier, but MemberExpression found.", + "column": 12 +} \ No newline at end of file diff --git a/tests/fixtures/parser/error/name01-input.svelte b/tests/fixtures/parser/error/name01-input.svelte new file mode 100644 index 00000000..296cdd7f --- /dev/null +++ b/tests/fixtures/parser/error/name01-input.svelte @@ -0,0 +1,7 @@ + + +