Skip to content

Commit a40cbb3

Browse files
authored
Preserve Loc when emitting module prefix (microsoft#451)
1 parent 0e7b287 commit a40cbb3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

internal/transformers/commonjsmodule.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,7 @@ func (tx *CommonJSModuleTransformer) visitExpressionIdentifier(node *ast.Identif
19101910
ast.NodeFlagsNone,
19111911
)
19121912
tx.emitContext.AssignCommentAndSourceMapRanges(reference, node)
1913+
reference.Loc = node.Loc
19131914
return reference
19141915
}
19151916

@@ -1923,6 +1924,7 @@ func (tx *CommonJSModuleTransformer) visitExpressionIdentifier(node *ast.Identif
19231924
ast.NodeFlagsNone,
19241925
)
19251926
tx.emitContext.AssignCommentAndSourceMapRanges(reference, node)
1927+
reference.Loc = node.Loc
19261928
return reference
19271929
}
19281930
if ast.IsImportSpecifier(importDeclaration) {
@@ -1946,6 +1948,7 @@ func (tx *CommonJSModuleTransformer) visitExpressionIdentifier(node *ast.Identif
19461948
)
19471949
}
19481950
tx.emitContext.AssignCommentAndSourceMapRanges(reference, node)
1951+
reference.Loc = node.Loc
19491952
return reference
19501953
}
19511954
}

internal/transformers/commonjsmodule_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,17 @@ var E;
947947
})(E || (exports.E = E = {}));
948948
E.A;`,
949949
},
950+
{
951+
title: "Identifier#4 (preserve location)",
952+
input: `import { a } from "other";
953+
x ||
954+
a`,
955+
output: `"use strict";
956+
Object.defineProperty(exports, "__esModule", { value: true });
957+
const other_1 = require("other");
958+
x ||
959+
other_1.a;`,
960+
},
950961

951962
{
952963
title: "Other",

0 commit comments

Comments
 (0)