Skip to content

Commit 2f48ec3

Browse files
committed
messing around with printhelpers
1 parent 28dfb91 commit 2f48ec3

File tree

4 files changed

+67
-55
lines changed

4 files changed

+67
-55
lines changed

src/compiler/emitter.ts

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,11 +1347,11 @@ const printerCache = new Map<string, Printer>();
13471347

13481348
// If updating this, make sure the cache key is updated too.
13491349
/** @internal */
1350-
export type ReusablePrinterOptions = Pick<PrinterOptions, "removeComments" | "neverAsciiEscape" | "omitTrailingSemicolon" | "module" | "target" | "newLine">;
1350+
export type ReusablePrinterOptions = Pick<PrinterOptions, "removeComments" | "neverAsciiEscape" | "omitTrailingSemicolon" | "module" | "target" | "newLine" | "preserveSourceNewlines" | "terminateUnterminatedLiterals">;
13511351

13521352
/** @internal */
13531353
export function createOrReusePrinter(o: ReusablePrinterOptions = {}) {
1354-
const key = `${keyBool(o.removeComments)}|${keyBool(o.neverAsciiEscape)}|${keyBool(o.omitTrailingSemicolon)}|${keyNum(o.module)}|${keyNum(o.target)}|${keyNum(o.newLine)}`;
1354+
const key = `${keyBool(o.removeComments)}|${keyBool(o.neverAsciiEscape)}|${keyBool(o.omitTrailingSemicolon)}|${keyNum(o.module)}|${keyNum(o.target)}|${keyNum(o.newLine)}|${keyBool(o.preserveSourceNewlines)}|${keyBool(o.terminateUnterminatedLiterals)}`;
13551355
let printer = printerCache.get(key);
13561356
if (!printer) {
13571357
printerCache.set(key, printer = createPrinter(o));
@@ -1368,18 +1368,19 @@ export function createOrReusePrinter(o: ReusablePrinterOptions = {}) {
13681368
}
13691369

13701370
export function createPrinter(printerOptions: PrinterOptions = {}, handlers: PrintHandlers = {}): Printer {
1371-
const {
1372-
hasGlobalName,
1373-
onEmitNode = noEmitNotification,
1374-
isEmitNotificationEnabled,
1375-
substituteNode = noEmitSubstitution,
1376-
onBeforeEmitNode,
1377-
onAfterEmitNode,
1378-
onBeforeEmitNodeArray,
1379-
onAfterEmitNodeArray,
1380-
onBeforeEmitToken,
1381-
onAfterEmitToken
1382-
} = handlers;
1371+
let theHandlers!: PrintHandlers & Required<Pick<PrintHandlers, "onEmitNode" | "substituteNode">>;
1372+
1373+
function setHandlers(newHandlers?: PrintHandlers) {
1374+
if (newHandlers) {
1375+
theHandlers = {
1376+
onEmitNode: noEmitNotification,
1377+
substituteNode: noEmitSubstitution,
1378+
...newHandlers,
1379+
};
1380+
}
1381+
}
1382+
1383+
setHandlers(handlers);
13831384

13841385
const extendedDiagnostics = !!printerOptions.extendedDiagnostics;
13851386
const newLine = getNewLineCharacter(printerOptions);
@@ -1499,25 +1500,31 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
14991500
/**
15001501
* If `sourceFile` is `undefined`, `node` must be a synthesized `TypeNode`.
15011502
*/
1502-
function writeNode(hint: EmitHint, node: TypeNode, sourceFile: undefined, output: EmitTextWriter): void;
1503-
function writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile, output: EmitTextWriter): void;
1504-
function writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined, output: EmitTextWriter) {
1503+
function writeNode(hint: EmitHint, node: TypeNode, sourceFile: undefined, output: EmitTextWriter, handlers?: PrintHandlers): void;
1504+
function writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile, output: EmitTextWriter, handlers?: PrintHandlers): void;
1505+
function writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined, output: EmitTextWriter, handlers?: PrintHandlers) {
15051506
const previousWriter = writer;
1507+
const previousHandlers = theHandlers;
1508+
setHandlers(handlers);
15061509
setWriter(output, /*_sourceMapGenerator*/ undefined);
15071510
print(hint, node, sourceFile);
15081511
reset();
15091512
writer = previousWriter;
1513+
theHandlers = previousHandlers;
15101514
}
15111515

1512-
function writeList<T extends Node>(format: ListFormat, nodes: NodeArray<T>, sourceFile: SourceFile | undefined, output: EmitTextWriter) {
1516+
function writeList<T extends Node>(format: ListFormat, nodes: NodeArray<T>, sourceFile: SourceFile | undefined, output: EmitTextWriter, handlers?: PrintHandlers) {
15131517
const previousWriter = writer;
1518+
const previousHandlers = theHandlers;
1519+
setHandlers(handlers);
15141520
setWriter(output, /*_sourceMapGenerator*/ undefined);
15151521
if (sourceFile) {
15161522
setSourceFile(sourceFile);
15171523
}
15181524
emitList(/*parentNode*/ undefined, nodes, format);
15191525
reset();
15201526
writer = previousWriter;
1527+
theHandlers = previousHandlers;
15211528
}
15221529

15231530
function getTextPosWithWriteLine() {
@@ -1566,9 +1573,11 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
15661573
return false;
15671574
}
15681575

1569-
function writeBundle(bundle: Bundle, output: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined) {
1576+
function writeBundle(bundle: Bundle, output: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined, handlers?: PrintHandlers) {
15701577
isOwnFileEmit = false;
15711578
const previousWriter = writer;
1579+
const previousHandlers = theHandlers;
1580+
setHandlers(handlers);
15721581
setWriter(output, sourceMapGenerator);
15731582
emitShebangIfNeeded(bundle);
15741583
emitPrologueDirectivesIfNeeded(bundle);
@@ -1623,6 +1632,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
16231632

16241633
reset();
16251634
writer = previousWriter;
1635+
theHandlers = previousHandlers;
16261636
}
16271637

16281638
function writeUnparsedSource(unparsed: UnparsedSource, output: EmitTextWriter) {
@@ -1633,15 +1643,18 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
16331643
writer = previousWriter;
16341644
}
16351645

1636-
function writeFile(sourceFile: SourceFile, output: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined) {
1646+
function writeFile(sourceFile: SourceFile, output: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined, handlers?: PrintHandlers) {
16371647
isOwnFileEmit = true;
16381648
const previousWriter = writer;
1649+
const previousHandlers = theHandlers;
1650+
setHandlers(handlers);
16391651
setWriter(output, sourceMapGenerator);
16401652
emitShebangIfNeeded(sourceFile);
16411653
emitPrologueDirectivesIfNeeded(sourceFile);
16421654
print(EmitHint.SourceFile, sourceFile, sourceFile);
16431655
reset();
16441656
writer = previousWriter;
1657+
theHandlers = previousHandlers;
16451658
}
16461659

16471660
function beginPrint() {
@@ -1765,12 +1778,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
17651778
function getPipelinePhase(phase: PipelinePhase, emitHint: EmitHint, node: Node) {
17661779
switch (phase) {
17671780
case PipelinePhase.Notification:
1768-
if (onEmitNode !== noEmitNotification && (!isEmitNotificationEnabled || isEmitNotificationEnabled(node))) {
1781+
if (theHandlers.onEmitNode !== noEmitNotification && (!theHandlers.isEmitNotificationEnabled || theHandlers.isEmitNotificationEnabled(node))) {
17691782
return pipelineEmitWithNotification;
17701783
}
17711784
// falls through
17721785
case PipelinePhase.Substitution:
1773-
if (substituteNode !== noEmitSubstitution && (lastSubstitution = substituteNode(emitHint, node) || node) !== node) {
1786+
if (theHandlers.substituteNode !== noEmitSubstitution && (lastSubstitution = theHandlers.substituteNode(emitHint, node) || node) !== node) {
17741787
if (currentParenthesizerRule) {
17751788
lastSubstitution = currentParenthesizerRule(lastSubstitution);
17761789
}
@@ -1800,11 +1813,11 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
18001813

18011814
function pipelineEmitWithNotification(hint: EmitHint, node: Node) {
18021815
const pipelinePhase = getNextPipelinePhase(PipelinePhase.Notification, hint, node);
1803-
onEmitNode(hint, node, pipelinePhase);
1816+
theHandlers.onEmitNode(hint, node, pipelinePhase);
18041817
}
18051818

18061819
function pipelineEmitWithHint(hint: EmitHint, node: Node): void {
1807-
onBeforeEmitNode?.(node);
1820+
theHandlers.onBeforeEmitNode?.(node);
18081821
if (preserveSourceNewlines) {
18091822
const savedPreserveSourceNewlines = preserveSourceNewlines;
18101823
beforeEmitNode(node);
@@ -1814,7 +1827,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
18141827
else {
18151828
pipelineEmitWithHintWorker(hint, node);
18161829
}
1817-
onAfterEmitNode?.(node);
1830+
theHandlers.onAfterEmitNode?.(node);
18181831
// clear the parenthesizer rule as we ascend
18191832
currentParenthesizerRule = undefined;
18201833
}
@@ -2185,8 +2198,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
21852198
}
21862199
if (isExpression(node)) {
21872200
hint = EmitHint.Expression;
2188-
if (substituteNode !== noEmitSubstitution) {
2189-
const substitute = substituteNode(hint, node) || node;
2201+
if (theHandlers.substituteNode !== noEmitSubstitution) {
2202+
const substitute = theHandlers.substituteNode(hint, node) || node;
21902203
if (substitute !== node) {
21912204
node = substitute;
21922205
if (currentParenthesizerRule) {
@@ -3227,7 +3240,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
32273240
state.declarationListContainerEndStack[state.stackIndex] = declarationListContainerEnd;
32283241
const emitComments = state.shouldEmitCommentsStack[state.stackIndex] = shouldEmitComments(node);
32293242
const emitSourceMaps = state.shouldEmitSourceMapsStack[state.stackIndex] = shouldEmitSourceMaps(node);
3230-
onBeforeEmitNode?.(node);
3243+
theHandlers.onBeforeEmitNode?.(node);
32313244
if (emitComments) emitCommentsBeforeNode(node);
32323245
if (emitSourceMaps) emitSourceMapsBeforeNode(node);
32333246
beforeEmitNode(node);
@@ -3279,7 +3292,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
32793292
afterEmitNode(savedPreserveSourceNewlines);
32803293
if (shouldEmitSourceMaps) emitSourceMapsAfterNode(node);
32813294
if (shouldEmitComments) emitCommentsAfterNode(node, savedContainerPos, savedContainerEnd, savedDeclarationListContainerEnd);
3282-
onAfterEmitNode?.(node);
3295+
theHandlers.onAfterEmitNode?.(node);
32833296
state.stackIndex--;
32843297
}
32853298
}
@@ -3780,7 +3793,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
37803793
}
37813794

37823795
function emitBlockFunctionBody(body: Block) {
3783-
onBeforeEmitNode?.(body);
3796+
theHandlers.onBeforeEmitNode?.(body);
37843797
writeSpace();
37853798
writePunctuation("{");
37863799
increaseIndent();
@@ -3793,7 +3806,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
37933806

37943807
decreaseIndent();
37953808
writeToken(SyntaxKind.CloseBraceToken, body.statements.end, writePunctuation, body);
3796-
onAfterEmitNode?.(body);
3809+
theHandlers.onAfterEmitNode?.(body);
37973810
}
37983811

37993812
function emitBlockFunctionBodyOnSingleLine(body: Block) {
@@ -4761,7 +4774,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
47614774
return node.pos;
47624775
}
47634776

4764-
onBeforeEmitNodeArray?.(modifiers);
4777+
theHandlers.onBeforeEmitNodeArray?.(modifiers);
47654778

47664779
// partition modifiers into contiguous chunks of `Modifier` or `Decorator`
47674780
let lastMode: "modifiers" | "decorators" | undefined;
@@ -4803,7 +4816,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
48034816
pos++;
48044817
}
48054818

4806-
onAfterEmitNodeArray?.(modifiers);
4819+
theHandlers.onAfterEmitNodeArray?.(modifiers);
48074820

48084821
if (lastModifier && !positionIsSynthesized(lastModifier.end)) {
48094822
return lastModifier.end;
@@ -4978,8 +4991,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
49784991

49794992
const isEmpty = children === undefined || start >= children.length || count === 0;
49804993
if (isEmpty && format & ListFormat.OptionalIfEmpty) {
4981-
onBeforeEmitNodeArray?.(children);
4982-
onAfterEmitNodeArray?.(children);
4994+
theHandlers.onBeforeEmitNodeArray?.(children);
4995+
theHandlers.onAfterEmitNodeArray?.(children);
49834996
return;
49844997
}
49854998

@@ -4990,7 +5003,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
49905003
}
49915004
}
49925005

4993-
onBeforeEmitNodeArray?.(children);
5006+
theHandlers.onBeforeEmitNodeArray?.(children);
49945007

49955008
if (isEmpty) {
49965009
// Write a line terminator if the parent node was multi-line
@@ -5005,7 +5018,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
50055018
emitNodeListItems(emit, parentNode, children, format, parenthesizerRule, start, count, children.hasTrailingComma, children);
50065019
}
50075020

5008-
onAfterEmitNodeArray?.(children);
5021+
theHandlers.onAfterEmitNodeArray?.(children);
50095022

50105023
if (format & ListFormat.BracketsMask) {
50115024
if (isEmpty && children) {
@@ -5231,12 +5244,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
52315244
}
52325245

52335246
function writeTokenNode(node: Node, writer: (s: string) => void) {
5234-
if (onBeforeEmitToken) {
5235-
onBeforeEmitToken(node);
5247+
if (theHandlers.onBeforeEmitToken) {
5248+
theHandlers.onBeforeEmitToken(node);
52365249
}
52375250
writer(tokenToString(node.kind)!);
5238-
if (onAfterEmitToken) {
5239-
onAfterEmitToken(node);
5251+
if (theHandlers.onAfterEmitToken) {
5252+
theHandlers.onAfterEmitToken(node);
52405253
}
52415254
}
52425255

@@ -5778,7 +5791,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
57785791
* when `isfileLevelUniqueName` is passed as a callback to `makeUniqueName`.
57795792
*/
57805793
function isFileLevelUniqueName(name: string, _isPrivate: boolean) {
5781-
return currentSourceFile ? ts.isFileLevelUniqueName(currentSourceFile, name, hasGlobalName) : true;
5794+
return currentSourceFile ? ts.isFileLevelUniqueName(currentSourceFile, name, theHandlers.hasGlobalName) : true;
57825795
}
57835796

57845797
/**

src/compiler/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9160,10 +9160,10 @@ export interface Printer {
91609160
* Prints a bundle of source files as-is, without any emit transformations.
91619161
*/
91629162
printBundle(bundle: Bundle): string;
9163-
/** @internal */ writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined, writer: EmitTextWriter): void;
9164-
/** @internal */ writeList<T extends Node>(format: ListFormat, list: NodeArray<T> | undefined, sourceFile: SourceFile | undefined, writer: EmitTextWriter): void;
9165-
/** @internal */ writeFile(sourceFile: SourceFile, writer: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined): void;
9166-
/** @internal */ writeBundle(bundle: Bundle, writer: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined): void;
9163+
/** @internal */ writeNode(hint: EmitHint, node: Node, sourceFile: SourceFile | undefined, writer: EmitTextWriter, handlers?: PrintHandlers): void;
9164+
/** @internal */ writeList<T extends Node>(format: ListFormat, list: NodeArray<T> | undefined, sourceFile: SourceFile | undefined, writer: EmitTextWriter, handlers?: PrintHandlers): void;
9165+
/** @internal */ writeFile(sourceFile: SourceFile, writer: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined, handlers?: PrintHandlers): void;
9166+
/** @internal */ writeBundle(bundle: Bundle, writer: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined, handlers?: PrintHandlers): void;
91679167
/** @deprecated @internal */ bundleFileInfo?: BundleFileInfo;
91689168
}
91699169

src/services/completions.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
createModuleSpecifierResolutionHost,
3737
createOrReusePrinter,
3838
createPackageJsonImportFilter,
39-
createPrinter,
4039
createSortedArray,
4140
createTextRangeFromSpan,
4241
createTextSpanFromBounds,
@@ -292,7 +291,6 @@ import {
292291
positionBelongsToNode,
293292
positionIsASICandidate,
294293
positionsAreOnSameLine,
295-
PrinterOptions,
296294
probablyUsesSemicolons,
297295
Program,
298296
programContainsModules,
@@ -308,6 +306,7 @@ import {
308306
rangeContainsPosition,
309307
rangeContainsPositionExclusive,
310308
rangeIsOnSingleLine,
309+
ReusablePrinterOptions,
311310
ScriptElementKind,
312311
ScriptElementKindModifier,
313312
ScriptTarget,
@@ -1851,11 +1850,11 @@ function createObjectLiteralMethod(
18511850
}
18521851

18531852
function createSnippetPrinter(
1854-
printerOptions: PrinterOptions,
1853+
printerOptions: ReusablePrinterOptions,
18551854
) {
18561855
let escapes: TextChange[] | undefined;
18571856
const baseWriter = textChanges.createWriter(getNewLineCharacter(printerOptions));
1858-
const printer = createPrinter(printerOptions, baseWriter);
1857+
const printer = createOrReusePrinter(printerOptions);
18591858
const writer: EmitTextWriter = {
18601859
...baseWriter,
18611860
write: s => escapingWrite(s, () => baseWriter.write(s)),
@@ -1909,7 +1908,7 @@ function createSnippetPrinter(
19091908
): string {
19101909
escapes = undefined;
19111910
writer.clear();
1912-
printer.writeList(format, list, sourceFile, writer);
1911+
printer.writeList(format, list, sourceFile, writer, baseWriter);
19131912
return writer.getText();
19141913
}
19151914

@@ -1956,7 +1955,7 @@ function createSnippetPrinter(
19561955
function printUnescapedNode(hint: EmitHint, node: Node, sourceFile: SourceFile): string {
19571956
escapes = undefined;
19581957
writer.clear();
1959-
printer.writeNode(hint, node, sourceFile, writer);
1958+
printer.writeNode(hint, node, sourceFile, writer, baseWriter);
19601959
return writer.getText();
19611960
}
19621961

src/services/textChanges.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ConstructorDeclaration,
1212
contains,
1313
createNodeFactory,
14-
createPrinter,
14+
createOrReusePrinter,
1515
createRange,
1616
createSourceFile,
1717
createTextChange,
@@ -1301,12 +1301,12 @@ namespace changesToText {
13011301
export function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLineCharacter: string): { text: string, node: Node } {
13021302
const writer = createWriter(newLineCharacter);
13031303
const newLine = getNewLineKind(newLineCharacter);
1304-
createPrinter({
1304+
createOrReusePrinter({
13051305
newLine,
13061306
neverAsciiEscape: true,
13071307
preserveSourceNewlines: true,
13081308
terminateUnterminatedLiterals: true
1309-
}, writer).writeNode(EmitHint.Unspecified, node, sourceFile, writer);
1309+
}).writeNode(EmitHint.Unspecified, node, sourceFile, writer, writer);
13101310
return { text: writer.getText(), node: assignPositionsToNode(node) };
13111311
}
13121312
}

0 commit comments

Comments
 (0)