diff --git a/packages/babel-generator/src/buffer.ts b/packages/babel-generator/src/buffer.ts index a83a847c22b7..ee2cccdbf951 100644 --- a/packages/babel-generator/src/buffer.ts +++ b/packages/babel-generator/src/buffer.ts @@ -263,18 +263,6 @@ export default class Buffer { sourcePos.identifierNamePos = undefined; } - // Fast path for multi-line - if (maybeNewline && identifierNamePos && this._map?._inputMap) { - const name = originalPositionFor( - this._map._inputMap, - identifierNamePos, - ).name; - if (name) { - identifierName = name; - } - identifierNamePos = undefined; - } - // Search for newline chars. We search only for `\n`, since both `\r` and // `\r\n` are normalized to `\n` during parse. We exclude `\u2028` and // `\u2029` for performance reasons, they're so uncommon that it's probably @@ -297,7 +285,7 @@ export default class Buffer { // We mark the start of each line, which happens directly after this newline char // unless this is the last char. if (last < len) { - this._mark(++line, 0, identifierName, identifierNamePos, filename); + this._mark(++line, 0, null, null, filename); } i = str.indexOf("\n", last); } diff --git a/packages/babel-generator/src/source-map.ts b/packages/babel-generator/src/source-map.ts index 9c8145238ab2..634b41fb60e0 100644 --- a/packages/babel-generator/src/source-map.ts +++ b/packages/babel-generator/src/source-map.ts @@ -107,40 +107,40 @@ export default class SourceMap { ) { this._rawMappings = undefined; - let original; - let source; + let originalMapping: { + source: string | null; + name?: string | null; + line: number | null; + column: number | null; + }; if (line != null) { if (this._inputMap) { - if (identifierNamePos) { - const name = originalPositionFor(this._inputMap, { - line, - column, - }).name; - if (name) { - identifierName = name; - } - } - - const originalMapping = originalPositionFor(this._inputMap, { + // This is the lookup for this mark + originalMapping = originalPositionFor(this._inputMap, { line, column, }); - source = originalMapping.source; - - if (source && originalMapping.line != null) { - original = { - line: originalMapping.line, - column: originalMapping.column, - }; - if (originalMapping.name) { - identifierName = originalMapping.name; + // If the we found a name, nothing else needs to be done + // Maybe we're marking a `(` and the input map already had a name attached there, + // or we're marking a `(` and the sourcemap spanned a `foo(`, + // or we're marking an identifier, etc. + if (!originalMapping.name && identifierNamePos) { + // We're trying to mark a `(` (as that's the only thing that provides + // an identifierNamePos currently), and we the AST had an identifier attached. + // Lookup it's original name. + const originalIdentifierMapping = originalPositionFor( + this._inputMap, + identifierNamePos, + ); + if (originalIdentifierMapping.name) { + identifierName = originalIdentifierMapping.name; } } } else { - source = filename?.replace(/\\/g, "/") || this._sourceFileName; - original = { + originalMapping = { + source: filename?.replace(/\\/g, "/") || this._sourceFileName, line: line, column: column, }; @@ -150,8 +150,8 @@ export default class SourceMap { maybeAddMapping(this._map, { name: identifierName, generated, - source: original == null ? undefined : source, - original: original, + source: originalMapping?.source, + original: originalMapping, }); } }