Use same property name for BundleFileSection and UnparsedNode for easy node creation

This commit is contained in:
Sheetal Nandi
2019-01-31 11:45:42 -08:00
parent 38e0d4df0f
commit f7281e3fb5
19 changed files with 288 additions and 221 deletions

View File

@@ -562,7 +562,7 @@ namespace ts {
writeLine();
const pos = getTextPosWithWriteLine();
print(EmitHint.Unspecified, prepend, /*sourceFile*/ undefined);
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Prepend, fileName: (prepend as UnparsedSource).fileName });
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Prepend, data: (prepend as UnparsedSource).fileName });
}
const pos = getTextPosWithWriteLine();
@@ -1182,7 +1182,7 @@ namespace ts {
else {
writeLines(helper.text(makeFileLevelOptimisticUniqueName));
}
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.EmitHelpers, name: helper.name });
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.EmitHelpers, data: helper.name });
helpersEmitted = true;
}
}
@@ -2974,19 +2974,19 @@ namespace ts {
for (const directive of files) {
const pos = writer.getTextPos();
writeComment(`/// <reference path="${directive.fileName}" />`);
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Reference, fileName: directive.fileName });
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Reference, data: directive.fileName });
writeLine();
}
for (const directive of types) {
const pos = writer.getTextPos();
writeComment(`/// <reference types="${directive.fileName}" />`);
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Type, fileName: directive.fileName });
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Type, data: directive.fileName });
writeLine();
}
for (const directive of libs) {
const pos = writer.getTextPos();
writeComment(`/// <reference lib="${directive.fileName}" />`);
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Lib, fileName: directive.fileName });
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Lib, data: directive.fileName });
writeLine();
}
}
@@ -3030,7 +3030,7 @@ namespace ts {
writeLine();
const pos = writer.getTextPos();
emit(statement);
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Prologue, text: statement.expression.text });
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Prologue, data: statement.expression.text });
if (seenPrologueDirectives) {
seenPrologueDirectives.set(statement.expression.text, true);
}
@@ -3047,13 +3047,13 @@ namespace ts {
function emitUnparsedPrologues(prologues: ReadonlyArray<UnparsedPrologue>, seenPrologueDirectives: Map<true>) {
for (const prologue of prologues) {
if (!seenPrologueDirectives.has(prologue.text)) {
if (!seenPrologueDirectives.has(prologue.data)) {
writeLine();
const pos = writer.getTextPos();
emit(prologue);
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Prologue, text: prologue.text });
if (bundleFileInfo) bundleFileInfo.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Prologue, data: prologue.data });
if (seenPrologueDirectives) {
seenPrologueDirectives.set(prologue.text, true);
seenPrologueDirectives.set(prologue.data, true);
}
}
}

View File

@@ -2678,35 +2678,27 @@ namespace ts {
for (const section of sections) {
switch (section.kind) {
case BundleFileSectionKind.Prologue:
const unparsedPrologue = <UnparsedPrologue>createUnparsedNode(SyntaxKind.UnparsedPrologue, section.pos, section.end, node);
unparsedPrologue.text = section.text;
(prologues || (prologues = [])).push(unparsedPrologue);
(prologues || (prologues = [])).push(createUnparsedNode(section, node) as UnparsedPrologue);
break;
case BundleFileSectionKind.EmitHelpers:
(helpers || (helpers = [])).push(getAllUnscopedEmitHelpers().get(section.name)!);
(helpers || (helpers = [])).push(getAllUnscopedEmitHelpers().get(section.data)!);
break;
case BundleFileSectionKind.NoDefaultLib:
node.hasNoDefaultLib = true;
break;
case BundleFileSectionKind.Reference:
(referencedFiles || (referencedFiles = [])).push({ pos: -1, end: -1, fileName: section.fileName });
(referencedFiles || (referencedFiles = [])).push({ pos: -1, end: -1, fileName: section.data });
break;
case BundleFileSectionKind.Type:
(typeReferenceDirectives || (typeReferenceDirectives = [])).push(section.fileName);
(typeReferenceDirectives || (typeReferenceDirectives = [])).push(section.data);
break;
case BundleFileSectionKind.Lib:
(libReferenceDirectives || (libReferenceDirectives = [])).push({ pos: -1, end: -1, fileName: section.fileName });
(libReferenceDirectives || (libReferenceDirectives = [])).push({ pos: -1, end: -1, fileName: section.data });
break;
case BundleFileSectionKind.Prepend:
const unparsedPrependText = <UnparsedPrependText>createUnparsedNode(SyntaxKind.UnparsedPrependText, section.pos, section.end, node);
unparsedPrependText.fileName = section.fileName;
(texts || (texts = [])).push(unparsedPrependText);
break;
case BundleFileSectionKind.Text:
(texts || (texts = [])).push(<UnparsedText>createUnparsedNode(SyntaxKind.UnparsedText, section.pos, section.end, node));
break;
case BundleFileSectionKind.SourceMapUrl:
(texts || (texts = [])).push(<UnparsedSourceMapUrl>createUnparsedNode(SyntaxKind.UnparsedSourceMapUrl, section.pos, section.end, node));
(texts || (texts = [])).push(createUnparsedNode(section, node) as UnparsedSourceText);
break;
default:
Debug.assertNever(section);
@@ -2725,14 +2717,34 @@ namespace ts {
node.referencedFiles = referencedFiles || emptyArray;
node.typeReferenceDirectives = typeReferenceDirectives;
node.libReferenceDirectives = libReferenceDirectives || emptyArray;
node.texts = texts || [<UnparsedText>createUnparsedNode(SyntaxKind.UnparsedText, 0, node.text.length, node)];
node.texts = texts || [<UnparsedText>createUnparsedNode({ kind: BundleFileSectionKind.Text, pos: 0, end: node.text.length }, node)];
node.getLineAndCharacterOfPosition = pos => getLineAndCharacterOfPosition(node, pos);
return node;
}
function createUnparsedNode(kind: UnparsedNode["kind"], pos: number, end: number, parent: UnparsedSource) {
const node = createNode(kind, pos, end) as UnparsedNode;
function mapBundleFileSectionKindToSyntaxKind(kind: BundleFileSectionKind): SyntaxKind {
switch (kind) {
case BundleFileSectionKind.Prologue: return SyntaxKind.UnparsedPrologue;
case BundleFileSectionKind.Prepend: return SyntaxKind.UnparsedPrependText;
case BundleFileSectionKind.Text: return SyntaxKind.UnparsedText;
case BundleFileSectionKind.SourceMapUrl: return SyntaxKind.UnparsedSourceMapUrl;
case BundleFileSectionKind.EmitHelpers:
case BundleFileSectionKind.NoDefaultLib:
case BundleFileSectionKind.Reference:
case BundleFileSectionKind.Type:
case BundleFileSectionKind.Lib:
return Debug.fail(`BundleFileSectionKind: ${kind} not yet mapped to SyntaxKind`);
default:
return Debug.assertNever(kind);
}
}
function createUnparsedNode(section: BundleFileSection, parent: UnparsedSource): UnparsedNode {
const node = createNode(mapBundleFileSectionKindToSyntaxKind(section.kind), section.pos, section.end) as UnparsedNode;
node.parent = parent;
node.data = section.data;
return node;
}

View File

@@ -2801,24 +2801,30 @@ namespace ts {
export type UnparsedSourceText = UnparsedPrependText | UnparsedText | UnparsedSourceMapUrl;
export type UnparsedNode = UnparsedPrologue | UnparsedSourceText;
export interface UnparsedPrologue extends Node {
export interface UnparsedSection extends Node {
kind: SyntaxKind;
data?: string;
parent: UnparsedSource;
}
export interface UnparsedPrologue extends UnparsedSection {
kind: SyntaxKind.UnparsedPrologue;
text: string;
data: string;
parent: UnparsedSource;
}
export interface UnparsedPrependText extends Node {
export interface UnparsedPrependText extends UnparsedSection {
kind: SyntaxKind.UnparsedPrependText;
fileName: string;
data: string;
parent: UnparsedSource;
}
export interface UnparsedText extends Node {
export interface UnparsedText extends UnparsedSection {
kind: SyntaxKind.UnparsedText;
parent: UnparsedSource;
}
export interface UnparsedSourceMapUrl extends Node {
export interface UnparsedSourceMapUrl extends UnparsedSection {
kind: SyntaxKind.UnparsedSourceMapUrl;
parent: UnparsedSource;
}
@@ -5474,18 +5480,19 @@ namespace ts {
/*@internal*/
export interface BundleFileSectionBase extends TextRange {
kind: BundleFileSectionKind;
data?: string;
}
/*@internal*/
export interface BundleFilePrologue extends BundleFileSectionBase {
kind: BundleFileSectionKind.Prologue;
text: string;
data: string;
}
/*@internal*/
export interface BundleFileEmitHelpers extends BundleFileSectionBase {
kind: BundleFileSectionKind.EmitHelpers;
name: string;
data: string;
}
/*@internal*/
@@ -5496,13 +5503,13 @@ namespace ts {
/*@internal*/
export interface BundleFileReference extends BundleFileSectionBase {
kind: BundleFileSectionKind.Reference | BundleFileSectionKind.Type | BundleFileSectionKind.Lib;
fileName: string;
data: string;
}
/*@internal*/
export interface BundleFilePrepend extends BundleFileSectionBase {
kind: BundleFileSectionKind.Prepend;
fileName: string;
data: string;
}
/*@internal*/