Reject an external @scope whose global JavaScript cannot name
Why
An external's @scope was only checked for being strings, and without @module the first one names a global, printed as is: @scope("class") and @scope("foo-bar") on a @val emitted class.b, a syntax error, and foo-bar.d, a subtraction. #9309 checks the @scope of an @instanceOf type, and its review suggested sharing that parsing with externals. Ast_payload.scopes_of_payload now reads the payload for both and checks the global the properties start at.
addedDiagnostic an external @scope whose global JavaScript cannot name
@scope("class") @val external b: int = "b"@scope("foo-bar") @val external d: int = "d"let x = b + d
js
let x = class.b + foo-bar.d | 0;diagnostic
Without @module, the first property of @scope is a global, which cannot be named like the keyword "class". To read a property of the global object of that name, put "globalThis" first in @scope.Without @module, the first property of @scope is a global, so it must be an identifier, and "foo-bar" is not one. To read a property of the global object of that name, put "globalThis" first in @scope.
Following the advice, @scope(("globalThis", "class")) and @scope(("globalThis", "foo-bar")) compile to globalThis.class.b + globalThis["foo-bar"].d | 0.
modifiedDiagnostic an external @scope that is not strings
@scope @val external x: int = "x"@scope(("window", 3)) @val external y: int = "y"
diagnostic
modifiedRepresentation which properties of a @scope are checked
representation
The properties after the global may be any string for an external, as before: they are read with a computed access where they need one, as in a["foo-bar"]. A class keeps its own rules: its path is a string of identifiers and dots, and globalThis in front would give one class two identities. @scope("globalThis") stays valid on an external. Class errors are unchanged, except that @scope(("import", "meta")), which an external may use, is reported at the @scope of a class rather than as an invalid @instanceOf. The external's @scope is now read once the kind of external is known, since @module can follow it.
Review
- Why accept
importas the first property of an external's @scope?@scope(("import", "meta"))readsimport.meta, which is valid JavaScript and how bindings readimport.meta.urlorimport.meta.env(tests/docstring_tests/Node.res does). - Why is the first property of a @send, @get or @set not checked? It is a property of the object the external is called on, as in
o.class.f(), never a global.