55 ObjectCreate,
66 ObjectValues,
77 ObjectPrototypeHasOwnProperty,
8- Symbol,
98} = primordials ;
109const { validateString } = require ( 'internal/validators' ) ;
1110
@@ -15,23 +14,25 @@ const {
1514 ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED ,
1615} = require ( 'internal/errors' ) . codes ;
1716
18- const kImplicitAssertType = Symbol ( 'implicit assert type' ) ;
17+ // Per the HTML spec, import statements without an assertion type imply a
18+ // `type` of `'javascript'`.
19+ const kImplicitAssertType = 'javascript' ;
1920
2021/**
21- * Define a map of module formats to import assertion types (the value of `type`
22- * in `assert { type: 'json' }`).
23- * @type {Map<string, string | typeof kImplicitAssertType }
22+ * Define a map of module formats to import assertion types (the value of
23+ * `type` in `assert { type: 'json' }`).
24+ * @type {Map<string, string> }
2425 */
2526const formatTypeMap = {
2627 '__proto__' : null ,
2728 'builtin' : kImplicitAssertType ,
2829 'commonjs' : kImplicitAssertType ,
2930 'json' : 'json' ,
3031 'module' : kImplicitAssertType ,
31- 'wasm' : kImplicitAssertType , // Should probably be 'webassembly' per https://github.com/tc39/proposal-import-assertions
32+ 'wasm' : 'webassembly' ,
3233} ;
3334
34- /** @type {Array< string, string | typeof kImplicitAssertType } */
35+ /** @type {string[] } */
3536const supportedAssertionTypes = ObjectValues ( formatTypeMap ) ;
3637
3738
@@ -50,25 +51,29 @@ function validateAssertions(url, format,
5051
5152 switch ( validType ) {
5253 case undefined :
53- // Ignore assertions for module types we don't recognize, to allow new
54+ // Ignore assertions for module formats we don't recognize, to allow new
5455 // formats in the future.
5556 return true ;
5657
5758 case importAssertions . type :
5859 // The asserted type is the valid type for this format.
60+ // This case also covers when the implicit type is declared explicitly
61+ // (`assert { type: 'javascript' }`).
62+ process . emitWarning ( 'Import assertions are experimental.' ,
63+ 'ExperimentalWarning' ) ;
5964 return true ;
6065
6166 case kImplicitAssertType :
62- // This format doesn't allow an import assertion type, so the property
63- // must not be set on the import assertions object.
67+ // This format allows the type to be implied. (If it were declared
68+ // explicitly, it would have been caught already by the previous case.)
6469 if ( ! ObjectPrototypeHasOwnProperty ( importAssertions , 'type' ) ) {
6570 return true ;
6671 }
6772 return handleInvalidType ( url , importAssertions . type ) ;
6873
6974 default :
7075 // There is an expected type for this format, but the value of
71- // `importAssertions.type` was not it.
76+ // `importAssertions.type` might not have been it.
7277 if ( ! ObjectPrototypeHasOwnProperty ( importAssertions , 'type' ) ) {
7378 // `type` wasn't specified at all.
7479 throw new ERR_IMPORT_ASSERTION_TYPE_MISSING ( url , validType ) ;
@@ -86,7 +91,7 @@ function handleInvalidType(url, type) {
8691 // `type` might have not been a string.
8792 validateString ( type , 'type' ) ;
8893
89- // `type` was not one of the types we understand.
94+ // `type` might not have been one of the types we understand.
9095 if ( ! ArrayPrototypeIncludes ( supportedAssertionTypes , type ) ) {
9196 throw new ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED ( type ) ;
9297 }
0 commit comments