Read the class of an @instanceOf type from its @module and @scope
Why
@module and @scope on an @instanceOf type were silently ignored (#9239), so the class check of an untagged variant read a global of the class's name. For a class of a JavaScript library or a local file that global does not exist, and the check threw a ReferenceError. Like an external, the type now says where its class is found: @module("m") reads it among the exports of m, "default" for the default export, imported as a @module external imports it, and @scope puts its properties in front of the path.
modifiedJS class check of an @instanceOf type
@instanceOf("Object3D") type object3D@module("three") @instanceOf("Mesh") type mesh = private object3D@scope("THREE") @instanceOf("Line") type line = private object3D @unboxedtype shape = Mesh(mesh) | Line(line) | Other(object3D) let kind = s => switch s { | Mesh(_) => "mesh" | Line(_) => "line" | Other(_) => "other" }
js
modifiedRepresentation where the class of an @instanceOf type is found
representation
removedDiagnostic two classes of one name in a variant
@instanceOf("Mesh") type globalMesh@module("three") @instanceOf("Mesh") type mesh @unboxedtype t = Global(globalMesh) | Three(mesh) | Plain({"plain": bool}) let kind = x => switch x { | Global(_) => "global" | Three(_) => "three" | Plain(_) => "plain" }
diagnostic
This untagged variant definition is invalid: At most one case can be an instance of Mesh.js
let Three = require("three"); function kind(x) { if (x instanceof Mesh) { return "global"; } else if (x instanceof Three.Mesh) { return "three"; } else { return "plain"; }}
addedDiagnostic an interface that states another module
module M: { @module("three") @instanceOf("Mesh") type mesh} = { @instanceOf("Mesh") type mesh}
diagnostic
modifiedDiagnostic an @instanceOf payload that is not a string
@module("three") @instanceOf(Mesh) type meshdiagnostic
addedDiagnostic a @scope that cannot lead to a class
@scope("default") @instanceOf("Mesh") type meshdiagnostic
addedDiagnostic a @module that names no module
@module(("three", "THREE")) @instanceOf("Mesh") type meshdiagnostic
Review
- Why are @module and @scope still ignored on a @typeOf or @notUndefined type? @notUndefined shipped in v12, so an error there would break bindings that compile today, and without a class the attributes cannot make a check read the wrong value, which was the harm in #9239.
- Where is a relative path such as @module("./x.js") resolved? It is emitted as written, relative to the module that compiles the check, as for a @module external used from another module.
- Why does a class of a module skip the checks of global paths such as Object or Array? Those name the builtin classes only as globals; an export can be named anything, also like a keyword such as default.