WICG/import-maps

What should 'parse an import map string' return when it throws an Error?

allstarschh opened this issue · 8 comments

From the spec parse an import map string
it will throw SyntaxError if it's an invalid JSON, or TypeError if there's any invalid type in the map.

But what should the return value be? A null? or an empty import map?
For example, what will the 'import map' be in the Step 1 of create an import map parse result ?

v8 returns an empty import map.
ImportMap::Parse

@hiroshige-g, can you help to clarify this? As you implement the Import map spec in v8.
I am not sure whether the spec or your implementation is wrong.

See https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result

A script element has a result, which is either "uninitialized", null (representing an error), or a script. It is initially "uninitialized".

If an algorithm throws an exception, then it does not return anything! That's how throwing exceptions works.

https://wicg.github.io/import-maps/#ref-for-parse-an-import-map-string is quite clear on this. It handles the thrown exception case specifically. And it is the only caller of "parse an import map string".

I see, perhaps it is confusing because you are wondering what the "import map parse result"'s "import map" field should be in that case. I agree the spec is not 100% clear; I will make it clearer. However, the answer is that it doesn't matter. The only caller that consults and import map parse results' import map is https://wicg.github.io/import-maps/#ref-for-import-map-parse-result-import-map, and if the error to rethrow is set, then it will not touch that field.

@domenic
Could you also explain when the script's result would be null for import maps?

Per import-maps spec:

https://wicg.github.io/import-maps/#register-an-import-map
If element’s the script’s result is null, then fire an event named error at element, and return.

As the script's result will be set to import map parse result in Step 2
https://wicg.github.io/import-maps/#ref-for-create-an-import-map-parse-result

  1. Set the script’s result to import map parse result.

And from the spec, import map parse result is never null. (If it throws, error to rethrow will be set)
https://wicg.github.io/import-maps/#create-an-import-map-parse-result

Also from whatwg spec:

https://html.spec.whatwg.org/multipage/scripting.html#concept-script-result
A script element has a result, which is either "uninitialized", null (representing an error), or a script.

https://wicg.github.io/import-maps/#the-scripts-result
the script’s result, which can be either a script or an import map parse result.

That means if we want to know the script's result is valid:

  • If the type is "importmap", we need to check the 'error to rethrow' in script's result (which is an import map parse result).
  • Otherwise we check whether script's result is null.

Shouldn't the script's result also be null when there's an error when processing import maps?

The script's result will never be null for import maps, it appears.

Thanks for explaning,
then the Step 1 in register an import map
should assert that the script's result is never null.