rjsx-mode icon indicating copy to clipboard operation
rjsx-mode copied to clipboard

Better recovery from malformed attribute expressions

Open felipeochoa opened this issue 7 years ago • 0 comments

There are three possibilities for recovering from a missing RC:

  1. Do nothing This works best for attributes in the middle when they have a value (e.g., a={123 b="...") since the js2 expression parser won't consume extra tokens, letting additional attributes be tokenized properly. In contrast, when the attribute is the last one in the tag the parser will happily give us a greater-than expression using the closing GT from the tag, messing everything up. There's not much we can do to distinguish between correct and incorrect parsing there. In self-closing tags js2 would parse the / as division, then > as an error primary expression, and then who knows.

  2. Un-parse the expression This works best for attributes where there's not even a value If we're a middle attribute, js2 will try to parse the next attribute as an assignment of an object literal or string. It can be a valid assignment (a={ b={abc}, a={ b="str"), an error due to a spread op (a={ {...abc}), or an error due to an invalid object literal body (a={ b={a > b}. When the assignment is valid, js2 may keep going if we're at the end of the tag, using the / or > to form expressions.

    If we're at the end of a tag, js2 will parse either a regex (for self-closing tags) or anything goes for an expression starting with > (which is de facto treated as its own primary expression).

  3. Consume up to the next RC. This fixes malformed/partially formed expressions inside the curlies. E.g., a={pred ? b}

felipeochoa avatar Nov 03 '16 16:11 felipeochoa