From 7205cb1aaceb96429b3c6e3b26891bd6c29d150b Mon Sep 17 00:00:00 2001 From: Jack Jennings Date: Wed, 15 Nov 2023 13:04:21 -0800 Subject: [PATCH] allow string names in exports --- .tool-versions | 1 + hook.js | 16 ++++++++++++++++ test/fixtures/string-export.mjs | 3 +++ test/hook/v15-string-export.mjs | 15 +++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 .tool-versions create mode 100644 test/fixtures/string-export.mjs create mode 100644 test/hook/v15-string-export.mjs diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..c2ca3d3 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +nodejs 20.9.0 diff --git a/hook.js b/hook.js index 79a2e5b..3a12ea7 100644 --- a/hook.js +++ b/hook.js @@ -191,6 +191,22 @@ async function processModule ({ continue } + if (NODE_MAJOR >= 15) { + // const modName = `v${Buffer.from(n, 'utf-8').toString('hex')}` + const modName = normalizeModName(n) + + setters.set(`$${modName}` + ns, ` + let $${modName} = ${ns}["${n}"] + export { $${modName} as "${n}" } + set["${n}"] = (v) => { + $${modName} = v + return true + } + `) + + continue + } + setters.set(`$${n}` + ns, ` let $${n} = ${ns}.${n} export { $${n} as ${n} } diff --git a/test/fixtures/string-export.mjs b/test/fixtures/string-export.mjs new file mode 100644 index 0000000..c92ced4 --- /dev/null +++ b/test/fixtures/string-export.mjs @@ -0,0 +1,3 @@ +export const foo = 41 + +export { foo as "non-valid-identifier" } diff --git a/test/hook/v15-string-export.mjs b/test/hook/v15-string-export.mjs new file mode 100644 index 0000000..20837f7 --- /dev/null +++ b/test/hook/v15-string-export.mjs @@ -0,0 +1,15 @@ +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License. +// +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. + +import Hook from '../../index.js' +import * as stringExportMjs from '../fixtures/string-export.mjs' +import { strictEqual } from 'assert' + +Hook((exports, name) => { + if (name.match(/fixtures\/string-export\.mjs/)) { + exports['non-valid-identifier'] += 1 + } +}) + +strictEqual(stringExportMjs['non-valid-identifier'], 42)