Repository: nodejs/node
Pull request: esm: fix inconsistency with importAssertion in resolve hook

As the [documentation](https://nodejs.org/docs/latest/api/module.html#customization-hooks:~:text=The%20property%20context.importAssertions%20is%20replaced%20with%20context.importAttributes.%20Using%20the%20old%20name%20is%20still%20supported%20and%20will%20emit%20an%20experimental%20warning.) says, the `context.importAssertions` should be still supported and emit a warning. This is true for the load hook, but not correct for context of the resolve hook.

This PR is tring to fix the inconsistency.

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: lib/internal/modules/esm/hooks.js
patch: @@ -237,7 +237,7 @@ class Hooks {
 
     const nextResolve = nextHookFactory(chain[chain.length - 1], meta, { validateArgs, validateOutput });
 
-    const resolution = await nextResolve(originalSpecifier, context);
+    const resolution = await nextResolve(originalSpecifier, defineImportAssertionAlias(context));
     const { hookErrIdentifier } = meta; // Retrieve the value after all settled
 
     validateOutput(hookErrIdentifier, resolution);

filename: test/es-module/test-esm-import-assertion-warning.mjs
patch: @@ -7,6 +7,11 @@ await Promise.all([
   `data:text/javascript,export ${encodeURIComponent(function resolve() {
     return { shortCircuit: true, url: 'data:application/json,1', importAssertions: { type: 'json' } };
   })}`,
+  // Using importAssertions on the context object of the resolve hook should warn but still work.
+  `data:text/javascript,export ${encodeURIComponent(function resolve(s, c, n) {
+    const type = c.importAssertions.type;
+    return { shortCircuit: true, url: 'data:application/json,1', importAttributes: { type: type ?? 'json' } };
+  })}`,
   // Setting importAssertions on the context object of the load hook should warn but still work.
   `data:text/javascript,export ${encodeURIComponent(function load(u, c, n) {
     c.importAssertions = { type: 'json' };
@@ -22,9 +27,9 @@ await Promise.all([
     '--eval', `
     import assert from 'node:assert';
     import { register } from 'node:module';
-    
+
     register(${JSON.stringify(loaderURL)});
-    
+
     assert.deepStrictEqual(
       { ...await import('data:') },
       { default: 1 }

filename: test/fixtures/es-module-loaders/hooks-input.mjs
patch: @@ -37,6 +37,7 @@ export async function resolve(specifier, context, next) {
     'conditions',
     'importAttributes',
     'parentURL',
+    'importAssertions',
   ]);
   assert.ok(Array.isArray(context.conditions));
   assert.strictEqual(typeof next, 'function');
@@ -71,9 +72,10 @@ export async function load(url, context, next) {
 
   assert.ok(new URL(url));
   // Ensure `context` has all and only the properties it's supposed to
-  assert.deepStrictEqual(Object.keys(context), [
+  assert.deepStrictEqual(Reflect.ownKeys(context), [
     'format',
     'importAttributes',
+    'importAssertions',
   ]);
   assert.strictEqual(context.format, 'test');
   assert.strictEqual(typeof next, 'function');