Skip to content

fix: duplicate exports from the same source file are excluded #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,13 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
const exportNames = await getExports(srcUrl, context, parentGetSource)
const starExports = new Set()
const setters = new Map()
const starExportSources = new Map()

const addSetter = (name, setter, isStarExport = false) => {
const addSetter = (name, source, setter, isStarExport = false) => {
if (setters.has(name)) {
if (isStarExport) {
// If there's already a matching star export, delete it
if (starExports.has(name)) {
// If there's already a matching star export and it comes from a different source, delete it
if (starExports.has(name) && !starExportSources.get(name).has(source)) {
setters.delete(name)
}
// and return so this is excluded
Expand All @@ -220,6 +221,9 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
// named exports
if (isStarExport) {
starExports.add(name)
const sources = starExportSources.get(name) || new Set()
sources.add(source)
starExportSources.set(name, sources)
}

setters.set(name, setter)
Expand Down Expand Up @@ -249,10 +253,10 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
})

for (const [name, setter] of subSetters.entries()) {
addSetter(name, setter, true)
addSetter(name, srcUrl, setter, true)
}
} else {
addSetter(n, `
addSetter(n, srcUrl, `
let $${n}
try {
$${n} = _.${n} = namespace.${n}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/duplicate-d.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './duplicate-c.mjs'
2 changes: 2 additions & 0 deletions test/fixtures/duplicate-same-source.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './duplicate-c.mjs'

Check failure on line 1 in test/fixtures/duplicate-same-source.mjs

View workflow job for this annotation

GitHub Actions / lint

Multiple exports of name 'foo'
export * from './duplicate-d.mjs'

Check failure on line 2 in test/fixtures/duplicate-same-source.mjs

View workflow job for this annotation

GitHub Actions / lint

Multiple exports of name 'foo'
15 changes: 15 additions & 0 deletions test/hook/duplicate-exports-same-source.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as lib from '../fixtures/duplicate-same-source.mjs'
import { notEqual, strictEqual } from 'assert'
import Hook from '../../index.js'

Hook((exports, name) => {
if (name.match(/duplicate-same-source\.mjs/)) {
// foo should be exported because it comes from the same source
strictEqual('foo' in exports, true)
}
})

notEqual(lib, undefined)

// foo should be exported because it comes from the same source
strictEqual('foo' in lib, true)
Loading