Skip to content

Commit

Permalink
allow string names in exports
Browse files Browse the repository at this point in the history
  • Loading branch information
jackjennings committed May 13, 2024
1 parent 00b01ff commit 7205cb1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 20.9.0
16 changes: 16 additions & 0 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} }
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/string-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const foo = 41

export { foo as "non-valid-identifier" }
15 changes: 15 additions & 0 deletions test/hook/v15-string-export.mjs
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7205cb1

Please sign in to comment.