Skip to content

Commit 877631e

Browse files
authored
fix: map default export bindings without source mappings (#337)
1 parent 6caa0fb commit 877631e

15 files changed

Lines changed: 175 additions & 1 deletion

‎src/fake-js/index.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ import {
2828
} from './runtime-binding.ts'
2929
import {
3030
collectReferenceDirectives,
31+
findDefaultExportRef,
3132
getIdentifierIndex,
3233
getIdFromTSEntityName,
3334
inheritNodeComments,
3435
isCjsDtsInputSyntax,
3536
isHelperImport,
3637
isInfer,
38+
isLineMapped,
3739
overwriteNode,
3840
pragmaComments,
3941
} from './utils.ts'
@@ -214,6 +216,22 @@ export function createFakeJsPlugin({
214216
const appendStmts: t.ProgramStatement[] = []
215217
const namespaceStmts: NamespaceMap = new Map()
216218

219+
// Generators map different statements of `export default <expr>`: tsc and
220+
// tsgo only map `export default _default`, oxc only maps the declaration.
221+
// The former is dropped below, so a binding on a line without mappings
222+
// takes the position of its default export reference.
223+
const defaultExportRef = sourcemap
224+
? findDefaultExportRef(program.body)
225+
: undefined
226+
const inputMappings = defaultExportRef
227+
? this.getCombinedSourcemap().mappings
228+
: ''
229+
const getBindingRange = (binding: t.Identifier) =>
230+
binding.name === defaultExportRef?.name &&
231+
!isLineMapped(inputMappings, code, binding.start)
232+
? { start: defaultExportRef.start, end: defaultExportRef.end }
233+
: undefined
234+
217235
for (const [i, stmt] of program.body.entries()) {
218236
const setStmt = (stmt: t.ProgramStatement) => (program.body[i] = stmt)
219237
if (rewriteImportExport(stmt, setStmt, appendStmts)) {
@@ -378,6 +396,7 @@ export function createFakeJsPlugin({
378396
id: b.ArrayPattern({
379397
elements: bindings.map((binding) => ({
380398
...binding,
399+
...getBindingRange(binding),
381400
typeAnnotation: null,
382401
})),
383402
}),

‎src/fake-js/utils.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ export function isCjsDtsInputSyntax(node: t.ProgramStatement): boolean {
8282
)
8383
}
8484

85+
export function findDefaultExportRef(
86+
body: t.ProgramStatement[],
87+
): t.Identifier | undefined {
88+
for (const stmt of body) {
89+
const ref =
90+
stmt.type === 'ExportDefaultDeclaration'
91+
? stmt.declaration
92+
: stmt.type === 'TSExportAssignment'
93+
? stmt.expression
94+
: undefined
95+
if (ref) return ref.type === 'Identifier' ? ref : undefined
96+
}
97+
}
98+
99+
export function isLineMapped(
100+
mappings: string,
101+
code: string,
102+
offset: number,
103+
): boolean {
104+
const line = code.slice(0, offset).split('\n').length - 1
105+
return !!mappings.split(';')[line]
106+
}
107+
85108
export function overwriteNode<T>(node: t.Node, newNode: T): T {
86109
// clear object keys
87110
for (const key of Object.keys(node)) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const value = 1
2+
3+
export = value + 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const value: number = 1
2+
3+
export default value + 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { o } from './member-source'
2+
3+
export default o.a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const o: { a: number } = { a: 1 }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const label: '中文' = '中文'
2+
3+
export default label.length
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// the literal keeps its members in the declaration
2+
3+
export default {
4+
a: 1,
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "esnext",
5+
"moduleResolution": "bundler",
6+
"strict": true,
7+
"declaration": true,
8+
"emitDeclarationOnly": true,
9+
"declarationMap": true
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface Foo {
2+
a: 1
3+
}
4+
const v = { a: 1 } as const
5+
6+
export default v as Foo

0 commit comments

Comments
 (0)