Skip to content

Commit 8f88df3

Browse files
fix: remove unnecessary export alias. (#35)
1 parent 9d33406 commit 8f88df3

5 files changed

Lines changed: 38 additions & 5 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@knighted/module",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Bidirectional transform for ES modules and CommonJS.",
55
"type": "module",
66
"main": "dist/module.js",

src/format.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,10 @@ const format = async (src: string, ast: ParseResult, opts: FormatterOptions) =>
805805
seen.add(propName)
806806
if (rhs.type === 'Identifier') {
807807
const rhsId = rhsSourceFor(rhs)
808-
if (rhsId === rhs.name) {
808+
const rhsName = rhs.name
809+
if (rhsId === rhsName && rhsName === propName) {
810+
exportsOut.push(`export { ${propName} };`)
811+
} else if (rhsId === rhsName) {
809812
exportsOut.push(`export { ${rhsId} as ${propName} };`)
810813
} else {
811814
exportsOut.push(`export const ${propName} = ${rhsId};`)
@@ -815,7 +818,12 @@ const format = async (src: string, ast: ParseResult, opts: FormatterOptions) =>
815818
}
816819
}
817820

818-
replacements.push({ start: write.start, end: write.end })
821+
// Trim trailing whitespace and one optional semicolon so the idiomatic export
822+
// replacement does not leave the original `;` behind (avoids emitting `;;`).
823+
let end = write.end
824+
while (end < src.length && (src[end] === ' ' || src[end] === '\t')) end++
825+
if (end < src.length && src[end] === ';') end++
826+
replacements.push({ start: write.start, end })
819827
}
820828

821829
if (!seen.size) return { ok: false, reason: 'no-seen' }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function example() {
2+
return 'ok'
3+
}
4+
5+
exports.example = example

test/module.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,26 @@ describe('@knighted/module', () => {
11791179
assert.equal((mod as any).bar(), 'bar')
11801180
})
11811181

1182+
it('emits shorthand exports when names match', async t => {
1183+
const fixturePath = join(fixtures, 'idiomaticShorthand.cjs')
1184+
const outFile = join(fixtures, 'idiomaticShorthand.mjs')
1185+
1186+
t.after(() => rm(outFile, { force: true }))
1187+
1188+
const result = await transform(fixturePath, { target: 'module' })
1189+
await writeFile(outFile, result)
1190+
1191+
assert.equal(result.includes('__exports'), false)
1192+
assert.ok(result.includes('export { example };'))
1193+
assert.equal(result.includes(';;'), false)
1194+
1195+
const { status } = spawnSync('node', [outFile], { stdio: 'inherit' })
1196+
assert.equal(status, 0)
1197+
1198+
const mod = await import(pathToFileURL(outFile).href)
1199+
assert.equal((mod as any).example(), 'ok')
1200+
})
1201+
11821202
it('respects idiomaticExports: off and keeps helper bag', async t => {
11831203
const fixturePath = join(fixtures, 'idiomaticSafe.cjs')
11841204
const outFile = join(fixtures, 'idiomaticOff.mjs')

0 commit comments

Comments
 (0)