Skip to content

Commit c1ee534

Browse files
committed
Clean up SourceMapWriter and emitter.
1 parent f2de450 commit c1ee534

File tree

3 files changed

+136
-609
lines changed

3 files changed

+136
-609
lines changed

src/compiler/emitter.ts

Lines changed: 14 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,8 @@ const _super = (function (geti, seti) {
203203

204204
const sourceMap = createSourceMapWriter(host, writer);
205205
const {
206-
emitStart,
207-
emitEnd,
208-
emitTokenStart,
209-
emitTokenEnd
206+
emitNodeWithSourceMap,
207+
emitTokenWithSourceMap
210208
} = sourceMap;
211209

212210
const comments = createCommentWriter(host, writer, sourceMap);
@@ -244,10 +242,8 @@ const _super = (function (geti, seti) {
244242

245243
// Extract helpers from the result
246244
const {
247-
isSubstitutionEnabled,
248-
isEmitNotificationEnabled,
249-
onSubstituteNode,
250-
onEmitNode
245+
emitNodeWithSubstitution,
246+
emitNodeWithNotification
251247
} = transformed;
252248

253249
performance.mark("beforePrint");
@@ -448,101 +444,6 @@ const _super = (function (geti, seti) {
448444
emitNodeWithSubstitution(node, /*isExpression*/ true, emitExpressionWorker);
449445
}
450446

451-
/**
452-
* Emits a node with possible emit notification.
453-
*/
454-
// TODO(rbuckton): Move this into transformer.ts
455-
function emitNodeWithNotification(node: Node, emitCallback: (node: Node) => void) {
456-
if (node) {
457-
if (isEmitNotificationEnabled(node)) {
458-
onEmitNode(node, emitCallback);
459-
}
460-
else {
461-
emitCallback(node);
462-
}
463-
}
464-
}
465-
466-
/**
467-
* Emits a node with possible source maps.
468-
*/
469-
// TODO(rbuckton): Move this into sourcemap.ts
470-
function emitNodeWithSourceMap(node: Node, emitCallback: (node: Node) => void) {
471-
if (node) {
472-
emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
473-
emitCallback(node);
474-
emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
475-
}
476-
}
477-
478-
/**
479-
* Determines whether to skip leading comment emit for a node.
480-
*
481-
* We do not emit comments for NotEmittedStatement nodes or any node that has
482-
* NodeEmitFlags.NoLeadingComments.
483-
*
484-
* @param node A Node.
485-
*/
486-
// TODO(rbuckton): Move this into comments.ts
487-
function shouldSkipLeadingCommentsForNode(node: Node) {
488-
return isNotEmittedStatement(node)
489-
|| (getEmitFlags(node) & EmitFlags.NoLeadingComments) !== 0;
490-
}
491-
492-
/**
493-
* Determines whether to skip source map emit for the start position of a node.
494-
*
495-
* We do not emit source maps for NotEmittedStatement nodes or any node that
496-
* has NodeEmitFlags.NoLeadingSourceMap.
497-
*
498-
* @param node A Node.
499-
*/
500-
// TODO(rbuckton): Move this into sourcemap.ts
501-
function shouldSkipLeadingSourceMapForNode(node: Node) {
502-
return isNotEmittedStatement(node)
503-
|| (getEmitFlags(node) & EmitFlags.NoLeadingSourceMap) !== 0;
504-
}
505-
506-
/**
507-
* Determines whether to skip source map emit for the end position of a node.
508-
*
509-
* We do not emit source maps for NotEmittedStatement nodes or any node that
510-
* has NodeEmitFlags.NoTrailingSourceMap.
511-
*
512-
* @param node A Node.
513-
*/
514-
// TODO(rbuckton): Move this into sourcemap.ts
515-
function shouldSkipTrailingSourceMapForNode(node: Node) {
516-
return isNotEmittedStatement(node)
517-
|| (getEmitFlags(node) & EmitFlags.NoTrailingSourceMap) !== 0;
518-
}
519-
520-
/**
521-
* Determines whether to skip source map emit for a node and its children.
522-
*
523-
* We do not emit source maps for a node that has NodeEmitFlags.NoNestedSourceMaps.
524-
*/
525-
// TODO(rbuckton): Move this into sourcemap.ts
526-
function shouldSkipSourceMapForChildren(node: Node) {
527-
return (getEmitFlags(node) & EmitFlags.NoNestedSourceMaps) !== 0;
528-
}
529-
530-
/**
531-
* Emits a node with possible substitution.
532-
*/
533-
// TODO(rbuckton): Move this into transformer.ts
534-
function emitNodeWithSubstitution(node: Node, isExpression: boolean, emitCallback: (node: Node) => void) {
535-
if (isSubstitutionEnabled(node) && (getEmitFlags(node) & EmitFlags.NoSubstitution) === 0) {
536-
const substitute = onSubstituteNode(node, isExpression);
537-
if (substitute !== node) {
538-
emitCallback(substitute);
539-
return;
540-
}
541-
}
542-
543-
emitCallback(node);
544-
}
545-
546447
/**
547448
* Emits a node.
548449
*
@@ -585,7 +486,7 @@ const _super = (function (geti, seti) {
585486
case SyntaxKind.StringKeyword:
586487
case SyntaxKind.SymbolKeyword:
587488
case SyntaxKind.GlobalKeyword:
588-
return writeTokenNode(node);
489+
return emitTokenNode(node);
589490

590491
// Parse tree nodes
591492

@@ -832,7 +733,7 @@ const _super = (function (geti, seti) {
832733
case SyntaxKind.SuperKeyword:
833734
case SyntaxKind.TrueKeyword:
834735
case SyntaxKind.ThisKeyword:
835-
return writeTokenNode(node);
736+
return emitTokenNode(node);
836737

837738
// Expressions
838739
case SyntaxKind.ArrayLiteralExpression:
@@ -2129,7 +2030,7 @@ const _super = (function (geti, seti) {
21292030
// "comment1" is not considered to be leading comment for node.initializer
21302031
// but rather a trailing comment on the previous node.
21312032
const initializer = node.initializer;
2132-
if (!shouldSkipLeadingCommentsForNode(initializer)) {
2033+
if ((getEmitFlags(initializer) & EmitFlags.NoLeadingComments) === 0) {
21332034
const commentRange = getCommentRange(initializer);
21342035
emitTrailingCommentsOfPosition(commentRange.pos);
21352036
}
@@ -2537,17 +2438,7 @@ const _super = (function (geti, seti) {
25372438
}
25382439

25392440
function writeToken(token: SyntaxKind, pos: number, contextNode?: Node) {
2540-
const tokenStartPos = emitTokenStart(token, pos, contextNode, shouldSkipLeadingSourceMapForToken, getTokenSourceMapRange);
2541-
const tokenEndPos = writeTokenText(token, tokenStartPos);
2542-
return emitTokenEnd(token, tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken, getTokenSourceMapRange);
2543-
}
2544-
2545-
function shouldSkipLeadingSourceMapForToken(contextNode: Node) {
2546-
return (getEmitFlags(contextNode) & EmitFlags.NoTokenLeadingSourceMaps) !== 0;
2547-
}
2548-
2549-
function shouldSkipTrailingSourceMapForToken(contextNode: Node) {
2550-
return (getEmitFlags(contextNode) & EmitFlags.NoTokenTrailingSourceMaps) !== 0;
2441+
return emitTokenWithSourceMap(contextNode, token, pos, writeTokenText);
25512442
}
25522443

25532444
function writeTokenText(token: SyntaxKind, pos?: number) {
@@ -2556,12 +2447,12 @@ const _super = (function (geti, seti) {
25562447
return positionIsSynthesized(pos) ? -1 : pos + tokenString.length;
25572448
}
25582449

2559-
function writeTokenNode(node: Node) {
2560-
if (node) {
2561-
emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
2562-
writeTokenText(node.kind);
2563-
emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
2564-
}
2450+
function emitTokenNode(node: Node) {
2451+
emitNodeWithSourceMap(node, emitTokenNodeWorker);
2452+
}
2453+
2454+
function emitTokenNodeWorker(node: Node) {
2455+
writeTokenText(node.kind);
25652456
}
25662457

25672458
function increaseIndentIf(value: boolean, valueToWriteWhenNotIndenting?: string) {

0 commit comments

Comments
 (0)