Skip to content

Commit 8b7870c

Browse files
alexander-akaitclaude
authored andcommitted
feat(javascript): parseProgram and parseError, as CSS and HTML have
parseProgram parses into a SyntaxTree read through A until the next parse, as parseHtml and the CSS parseA* do. Its parseError option reports each parse error with Babel's reasonCode kebab-cased, which generate-js-data.js reads off a probe per message: an error the parse reads past as a warning, one that stops it as an error, the call then returning undefined.
1 parent 6c1a170 commit 8b7870c

7 files changed

Lines changed: 1266 additions & 9 deletions

File tree

‎.changeset/041-syntax-parse-error-option.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Add a `parseError` option to the CSS and HTML syntax parse entry points.
5+
Add `parseError` to the CSS, HTML and JavaScript parsers, and `parseProgram`.

‎docs/syntax.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ One visitor serves several node types through an array, resolved once in `use()`
8484
Conventions every language keeps, and a new member follows:
8585

8686
- **A node is an integer id**, valid until the next parse; `0` is no node, and an HTML attribute's id is negative. The path is one object rebound before each callback; read it during the callback, never keep it.
87-
- **A parse entry point returns ids too**, read through the exported path `A` (`parseHtml`, CSS's `parseA*`), and takes a `parseError(input, code, start, end, severity)` option reporting each parse error of the tokenizer and of the parser or tree construction, `"error"` where end of input cuts a token or rule short and `"warning"` otherwise. WHATWG names only one tree-construction error, so HTML's use html5lib-python's names; CSS Syntax names none, so CSS's are webpack's own.
87+
- **A parse entry point returns ids too**, read through the exported path `A` until the next parse (`parseHtml`, CSS's `parseA*`, JavaScript's `parseProgram`), and takes a `parseError(input, code, start, end, severity)` option reporting each parse error of the tokenizer and of the parser or tree construction: `"error"` where end of input cuts a token or rule short, or where a JavaScript one stops the parse, and `"warning"` otherwise. No spec names them all, so HTML's tree-construction errors take html5lib-python's names, JavaScript's take Babel's `reasonCode` kebab-cased (read off probes by `tooling/generate-js-data.js`), and CSS's are webpack's own.
8888
- **The node is the last argument and optional**, defaulting to the current one: `path.range()`, `path.range(other)`, `path.child(0)`.
8989
- **A node has at most one `name` and one `value`; every other part is a `Field`, a `Flag` or a `Part`** (its position, and its text when it has more than the name and value), so the generic members cover any grammar instead of one method per part.
9090
- **`name` and `value` are what the language's spec defines**, decoded as ESTree's `Identifier.name` and `Literal.value` are: escapes and character references resolved, a number as a number, a comment's text without its delimiters. **`source` is exactly the input**, so the spelling as written is `source(path.startOf(Part.name), path.endOf(Part.name))`. A node without a name or value answers `""`, a missing part `null`.

‎lib/javascript/data.js‎

Lines changed: 245 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
// GENERATED by tooling/generate-js-data.js — do not edit.
7-
// Sources: acorn 8.19.0, terser 5.51.2. The tables read from terser are under its
7+
// Sources: acorn 8.19.0, terser 5.51.2, @babel/parser 8.0.7. The tables read from terser are under its
88
// license, quoted in full in syntax-printer.js.
99

1010
"use strict";
@@ -564,6 +564,249 @@ const NODE_FIELDS = {
564564
MetaProperty: "meta:node:0 property:node:1"
565565
};
566566

567+
// Each parse error message, `*` for its variable parts, with its code: Babel's
568+
// `reasonCode` for the same error kebab-cased, where Babel has one.
569+
/** @type {[string, string][]} */
570+
const PARSE_ERROR_CODES = [
571+
[
572+
"'import defer' can only be used with namespace imports ('import defer * as identifierName from ...').",
573+
"defer-import-requires-namespace"
574+
],
575+
[
576+
"'import source' can only be used with direct identifier specifier imports ('import source identifierName from ...').",
577+
"source-phase-import-requires-default"
578+
],
579+
[
580+
"'import source' can only be used with direct identifier specifier imports.",
581+
"source-phase-import-requires-default"
582+
],
583+
[
584+
"'import' and 'export' may appear only with 'sourceType: module'",
585+
"import-outside-module"
586+
],
587+
[
588+
"'import' and 'export' may only appear at the top level",
589+
"unexpected-import-export"
590+
],
591+
["'import.*' can only be used in a dynamic import.", "unexpected-token"],
592+
[
593+
"'import.*' must not contain escaped characters",
594+
"unsupported-meta-property"
595+
],
596+
[
597+
"'new.target' can only be used in functions and class static block",
598+
"unexpected-new-target"
599+
],
600+
[
601+
"'new.target' must not contain escaped characters",
602+
"unsupported-meta-property"
603+
],
604+
["'return' outside of function", "illegal-return"],
605+
["'super' keyword outside a method", "unexpected-super"],
606+
["'with' in strict mode", "strict-with"],
607+
[
608+
"* loop variable declaration may not have an initializer",
609+
"for-in-of-loop-initializer"
610+
],
611+
[
612+
"A string literal cannot be used as an exported binding without `from`.",
613+
"export-binding-is-string"
614+
],
615+
[
616+
"An export name cannot include a lone surrogate.",
617+
"module-export-name-has-lone-surrogate"
618+
],
619+
["Argument name clash", "param-dupe"],
620+
["Assigning to * in strict mode", "strict-eval-arguments"],
621+
["Binding * in strict mode", "strict-eval-arguments-binding"],
622+
["Assigning to rvalue", "invalid-lhs"],
623+
["Binding rvalue", "invalid-lhs-binding"],
624+
[
625+
"Await expression cannot be a default value",
626+
"await-expression-formal-parameter"
627+
],
628+
[
629+
"Await using cannot appear outside of async function",
630+
"await-using-not-in-async-context"
631+
],
632+
["Bad character escape sequence", "invalid-escape-sequence"],
633+
[
634+
"Bad escape sequence in untagged template literal",
635+
"invalid-escape-sequence-template"
636+
],
637+
["Binding member expression", "invalid-property-binding-pattern"],
638+
["Binding parenthesized expression", "invalid-parenthesized-assignment"],
639+
["Parenthesized pattern", "invalid-parenthesized-assignment"],
640+
["Cannot use 'arguments' in class field initializer", "arguments-in-class"],
641+
[
642+
"Cannot use 'await' as identifier inside an async function",
643+
"await-binding-identifier"
644+
],
645+
["Cannot use 'import.meta' outside a module", "import-meta-outside-module"],
646+
[
647+
"Cannot use 'yield' as identifier inside a generator",
648+
"yield-binding-identifier"
649+
],
650+
["Cannot use * in class static initialization block", "arguments-in-class"],
651+
[
652+
"Cannot use keyword 'await' outside an async function",
653+
"await-not-in-async-context"
654+
],
655+
["Classes can't have a field named 'constructor'", "constructor-class-field"],
656+
["Classes can't have a static field named 'prototype'", "static-prototype"],
657+
[
658+
"Classes can't have an element named '#constructor'",
659+
"constructor-class-private-field"
660+
],
661+
[
662+
"Classes may not have a static property named prototype",
663+
"static-prototype"
664+
],
665+
["Code point out of bounds", "invalid-code-point"],
666+
["Comma is not permitted after the rest element", "rest-trailing-comma"],
667+
[
668+
"Complex binding patterns require an initialization value",
669+
"declaration-missing-initializer"
670+
],
671+
["Constructor can't be a generator", "constructor-is-generator"],
672+
["Constructor can't be an async method", "constructor-is-async"],
673+
["Constructor can't have get/set modifier", "constructor-is-accessor"],
674+
["Deleting local variable in strict mode", "strict-delete"],
675+
["Duplicate attribute key '*'", "module-attributes-with-duplicate-keys"],
676+
["Duplicate constructor in the same class", "duplicate-constructor"],
677+
["Duplicate export 'default'", "duplicate-default-export"],
678+
["Duplicate export '*'", "duplicate-export"],
679+
["Duplicate regular expression flag", "duplicate-reg-exp-flags"],
680+
["Escape sequence in keyword *", "invalid-escaped-reserved-word"],
681+
["Expected number in radix *", "invalid-digit"],
682+
["Expecting Unicode escape sequence \\uXXXX", "missing-unicode-escape"],
683+
["Export '*' is not defined", "module-export-undefined"],
684+
["Identifier '#*' has already been declared", "private-name-redeclaration"],
685+
["Identifier '*' has already been declared", "var-redeclaration"],
686+
["Identifier directly after number", "number-identifier"],
687+
[
688+
"Illegal 'use strict' directive in function with non-simple parameter list",
689+
"illegal-language-mode-directive"
690+
],
691+
["Illegal newline after throw", "newline-after-throw"],
692+
["Invalid Unicode escape", "escaped-char-not-an-identifier"],
693+
["Invalid escape sequence", "strict-numeric-escape"],
694+
[
695+
"Invalid escape sequence in template string",
696+
"invalid-escape-sequence-template"
697+
],
698+
["Octal literal in strict mode", "strict-numeric-escape"],
699+
["Octal literal in template string", "invalid-escape-sequence-template"],
700+
["Invalid number", "invalid-or-missing-exponent"],
701+
["Invalid regular expression flag", "malformed-reg-exp-flags"],
702+
["Invalid regular expression: /*/: *", "invalid-regular-expression"],
703+
["Invalid use of 'super'", "unsupported-super"],
704+
["Label '*' is already declared", "label-redeclaration"],
705+
[
706+
"Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses",
707+
"mixing-coalesce-with-logical"
708+
],
709+
["Missing catch or finally clause", "no-catch-or-finally"],
710+
["Missing initializer in * declaration", "declaration-missing-initializer"],
711+
["Multiple default clauses", "multiple-defaults-in-switch"],
712+
["Not enough stack space to parse input", "not-enough-stack-space"],
713+
[
714+
"Numeric separator is not allowed at the first of digits",
715+
"unexpected-numeric-separator"
716+
],
717+
[
718+
"Numeric separator is not allowed at the last of digits",
719+
"unexpected-numeric-separator"
720+
],
721+
[
722+
"Numeric separator is not allowed in legacy octal numeric literals",
723+
"zero-digit-numeric-separator"
724+
],
725+
[
726+
"Numeric separator must be exactly one underscore",
727+
"unexpected-numeric-separator"
728+
],
729+
["Object pattern can't contain getter or setter", "pattern-has-accessor"],
730+
[
731+
"Only '=' operator can be used for specifying default value.",
732+
"missing-eq-in-assignment"
733+
],
734+
[
735+
"Optional chaining cannot appear in left-hand side",
736+
"invalid-lhs-optional-chaining"
737+
],
738+
[
739+
"Optional chaining cannot appear in the callee of new expressions",
740+
"optional-chaining-no-new"
741+
],
742+
[
743+
"Optional chaining cannot appear in the tag of tagged template expressions",
744+
"optional-chaining-no-template"
745+
],
746+
[
747+
"Private field '#*' must be declared in an enclosing class",
748+
"invalid-private-field-resolution"
749+
],
750+
["Private fields can not be deleted", "delete-private-field"],
751+
[
752+
"Private identifier can only be left side of binary expression",
753+
"private-in-expected-in"
754+
],
755+
["Redefinition of __proto__ property", "duplicate-proto"],
756+
["Redefinition of property", "redefinition-of-property"],
757+
[
758+
"Rest elements cannot have a default value",
759+
"invalid-rest-assignment-pattern"
760+
],
761+
["Setter cannot use rest params", "bad-setter-rest-parameter"],
762+
[
763+
"Shorthand property assignments are valid only in destructuring patterns",
764+
"invalid-cover-initialized-name"
765+
],
766+
["The keyword '*' is reserved", "unexpected-reserved-word"],
767+
[
768+
"The left-hand side of a for-of loop may not start with 'let'.",
769+
"for-of-let"
770+
],
771+
[
772+
"The only valid meta property for import is 'import.meta'",
773+
"unsupported-meta-property"
774+
],
775+
[
776+
"The only valid meta property for new is 'new.target'",
777+
"unsupported-meta-property"
778+
],
779+
["Trailing comma is not allowed in import()", "import-call-arity"],
780+
["Unexpected character '*'", "invalid-or-unexpected-token"],
781+
["Unexpected keyword '*'", "unexpected-keyword"],
782+
["Unexpected token", "unexpected-token"],
783+
["Unsyntactic *", "illegal-break-continue"],
784+
["Unterminated comment", "unterminated-comment"],
785+
["Unterminated regular expression", "unterminated-reg-exp"],
786+
["Unterminated string constant", "unterminated-string"],
787+
["Unterminated template", "unterminated-template"],
788+
["Unterminated template literal", "unterminated-template"],
789+
[
790+
"Using declaration cannot appear in the top level when source type is `script` or in the bare case statement",
791+
"unexpected-using-declaration"
792+
],
793+
["Using declaration is not allowed in for-in loops", "for-in-using"],
794+
[
795+
"Using declaration is not allowed in single-statement positions",
796+
"unexpected-lexical-declaration"
797+
],
798+
["Yield expression cannot be a default value", "yield-in-parameter"],
799+
["getter should have no params", "bad-getter-arity"],
800+
[
801+
"import call cannot be the target of `new`",
802+
"import-call-not-new-expression"
803+
],
804+
["let is disallowed as a lexically bound name", "let-in-lexical-binding"],
805+
["setter should have exactly one param", "bad-setter-arity"],
806+
["super() call outside constructor of a subclass", "super-not-allowed"],
807+
["*", "syntax-error"]
808+
];
809+
567810
module.exports.ASTRAL_IDENTIFIER_PART_RANGES = ASTRAL_IDENTIFIER_PART_RANGES;
568811
module.exports.ASTRAL_IDENTIFIER_START_RANGES = ASTRAL_IDENTIFIER_START_RANGES;
569812
module.exports.IDENTIFIER_PART_RANGES = IDENTIFIER_PART_RANGES;
@@ -574,6 +817,7 @@ module.exports.Field = Field;
574817
module.exports.Flag = Flag;
575818
module.exports.NODE_FIELDS = NODE_FIELDS;
576819
module.exports.NodeType = NodeType;
820+
module.exports.PARSE_ERROR_CODES = PARSE_ERROR_CODES;
577821
module.exports.Part = Part;
578822
module.exports.UNICODE_BINARY_PROPERTIES = UNICODE_BINARY_PROPERTIES;
579823
module.exports.UNICODE_BINARY_PROPERTIES_OF_STRINGS =

0 commit comments

Comments
 (0)