Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Oct 15, 2022
1 parent 682e833 commit ed7abbb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
14 changes: 1 addition & 13 deletions packages/babel-generator/src/buffer.ts
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
52 changes: 26 additions & 26 deletions packages/babel-generator/src/source-map.ts
Expand Up @@ -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,
};
Expand All @@ -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,
});
}
}

0 comments on commit ed7abbb

Please sign in to comment.