Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor declaration emitter into declaration transformer #21930

Merged
merged 21 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
62ee91d
Refactor declaration emitter into declaration transformer
weswigham Jan 18, 2018
9f42eb8
Merge branch 'master' into declaration-emit-refactor
weswigham Feb 15, 2018
4c639b8
Merge branch 'master' into declaration-emit-refactor
weswigham Feb 21, 2018
3adcbc7
Slight cleanup from code review feedback
weswigham Feb 21, 2018
9e34a3f
Merge branch 'master' into declaration-emit-refactor
weswigham Feb 26, 2018
db8e239
Merge branch 'master' into declaration-emit-refactor
weswigham Feb 27, 2018
7844547
Merge branch 'master' into declaration-emit-refactor
weswigham Mar 1, 2018
d1359ee
Merge branch 'master' into declaration-emit-refactor
weswigham Mar 3, 2018
165e6bb
Incorporate fix for new test
weswigham Mar 3, 2018
b84cbe4
Merge branch 'master' into declaration-emit-refactor
weswigham Mar 8, 2018
63923e2
Merge branch 'declaration-emit-refactor' of github.com:weswigham/Type…
weswigham Mar 8, 2018
e8a64bd
Merge branch 'master' into declaration-emit-refactor
weswigham Mar 9, 2018
d67ddc1
Merge branch 'master' into declaration-emit-refactor
weswigham Mar 15, 2018
a2f1a8a
Swaths of PR feedback
weswigham Mar 15, 2018
036c65a
Merge public methods
weswigham Mar 15, 2018
289e3e5
Per-file output
weswigham Mar 15, 2018
08ffcc8
Preserve input import ordering more often
weswigham Mar 15, 2018
cb69963
Unify jsdoc comment start detection under more lenient rule
weswigham Mar 15, 2018
1a699b8
Merge branch 'per-file-output' into declaration-emit-refactor
weswigham Mar 15, 2018
f83bc81
Move to per-file transformations to reduce the memory that msut be re…
weswigham Mar 15, 2018
6d729cf
Fix typo
weswigham Mar 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3684,10 +3684,6 @@ namespace ts {
isExternalModuleAugmentation(node.parent.parent);
}

function literalTypeToString(type: LiteralType) {
return type.flags & TypeFlags.StringLiteral ? '"' + escapeString((<StringLiteralType>type).value) + '"' : "" + (<NumberLiteralType>type).value;
}

interface NodeBuilderContext {
enclosingDeclaration: Node | undefined;
flags: NodeBuilderFlags | undefined;
Expand Down Expand Up @@ -3748,7 +3744,7 @@ namespace ts {
return symbolName(symbol);
}

function isDeclarationVisible(node: Declaration): boolean {
function isDeclarationVisible(node: Declaration | AnyImportSyntax): boolean {
if (node) {
const links = getNodeLinks(node);
if (links.isVisible === undefined) {
Expand Down Expand Up @@ -25496,6 +25492,7 @@ namespace ts {

function isImplementationOfOverload(node: SignatureDeclaration) {
if (nodeIsPresent((node as FunctionLikeDeclaration).body)) {
if (isGetAccessor(node) || isSetAccessor(node)) return false; // Get or set accessors can never be overload implementations, but can have up to 2 signatures
const symbol = getSymbolOfNode(node);
const signaturesOfSymbol = getSignaturesOfSymbol(symbol);
// If this function body corresponds to function with multiple signature, it is implementation of overload
Expand Down Expand Up @@ -25635,30 +25632,42 @@ namespace ts {
}
}

function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: EmitTextWriter) {
function createTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: NodeBuilderFlags, tracker: SymbolTracker, addUndefined?: boolean) {
declaration = getParseTreeNode(declaration, isVariableLikeOrAccessor);
if (!declaration) {
return createToken(SyntaxKind.AnyKeyword) as KeywordTypeNode;
}
// Get type of the symbol if this is the valid symbol otherwise get type at location
const symbol = getSymbolOfNode(declaration);
let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
? getWidenedLiteralType(getTypeOfSymbol(symbol))
: unknownType;
if (type.flags & TypeFlags.UniqueESSymbol &&
type.symbol === symbol) {
flags |= TypeFormatFlags.AllowUniqueESSymbolType;
flags |= NodeBuilderFlags.AllowUniqueESSymbolType;
}
if (flags & TypeFormatFlags.AddUndefined) {
if (addUndefined) {
type = getOptionalType(type);
}
typeToString(type, enclosingDeclaration, flags | TypeFormatFlags.MultilineObjectLiterals, writer);
return nodeBuilder.typeToTypeNode(type, enclosingDeclaration, flags | NodeBuilderFlags.MultilineObjectLiterals, tracker);
}

function writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: EmitTextWriter) {
function createReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: NodeBuilderFlags, tracker: SymbolTracker) {
signatureDeclaration = getParseTreeNode(signatureDeclaration, isFunctionLike);
if (!signatureDeclaration) {
return createToken(SyntaxKind.AnyKeyword) as KeywordTypeNode;
}
const signature = getSignatureFromDeclaration(signatureDeclaration);
typeToString(getReturnTypeOfSignature(signature), enclosingDeclaration, flags | TypeFormatFlags.MultilineObjectLiterals, writer);
return nodeBuilder.typeToTypeNode(getReturnTypeOfSignature(signature), enclosingDeclaration, flags | NodeBuilderFlags.MultilineObjectLiterals, tracker);
}

function writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: EmitTextWriter) {
function createTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: NodeBuilderFlags, tracker: SymbolTracker) {
expr = getParseTreeNode(expr, isExpression);
if (!expr) {
return createToken(SyntaxKind.AnyKeyword) as KeywordTypeNode;
}
const type = getWidenedType(getRegularTypeOfExpression(expr));
typeToString(type, enclosingDeclaration, flags | TypeFormatFlags.MultilineObjectLiterals, writer);
return nodeBuilder.typeToTypeNode(type, enclosingDeclaration, flags | NodeBuilderFlags.MultilineObjectLiterals, tracker);
}

function hasGlobalName(name: string): boolean {
Expand Down Expand Up @@ -25706,9 +25715,13 @@ namespace ts {
return false;
}

function writeLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration, writer: EmitTextWriter) {
function literalTypeToNode(type: LiteralType): Expression {
return createLiteral(type.value);
}

function createLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration) {
const type = getTypeOfSymbol(getSymbolOfNode(node));
writer.writeStringLiteral(literalTypeToString(<LiteralType>type));
return literalTypeToNode(<LiteralType>type);
}

function createResolver(): EmitResolver {
Expand Down Expand Up @@ -25752,9 +25765,10 @@ namespace ts {
isImplementationOfOverload,
isRequiredInitializedParameter,
isOptionalUninitializedParameterProperty,
writeTypeOfDeclaration,
writeReturnTypeOfSignatureDeclaration,
writeTypeOfExpression,
createTypeOfDeclaration,
createReturnTypeOfSignatureDeclaration,
createTypeOfExpression,
createLiteralConstValue,
isSymbolAccessible,
isEntityNameVisible,
getConstantValue: node => {
Expand All @@ -25776,7 +25790,6 @@ namespace ts {
const symbol = node && getSymbolOfNode(node);
return !!(symbol && getCheckFlags(symbol) & CheckFlags.Late);
},
writeLiteralConstValue,
getJsxFactoryEntity: location => location ? (getJsxNamespace(location), (getSourceFileOfNode(location).localJsxFactory || _jsxFactoryEntity)) : _jsxFactoryEntity
};

Expand Down
10 changes: 10 additions & 0 deletions src/compiler/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,15 @@ namespace ts {
}
}

function shouldWriteComment(text: string, pos: number) {
if (printerOptions.onlyPrintJsDocStyle) {
return (isJSDocLikeText(text, pos) || isPinnedComment(text, pos));
}
return true;
}

function emitLeadingComment(commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) {
if (!shouldWriteComment(currentText, commentPos)) return;
if (!hasWrittenComment) {
emitNewLineBeforeLeadingCommentOfPosition(currentLineMap, writer, rangePos, commentPos);
hasWrittenComment = true;
Expand Down Expand Up @@ -292,6 +300,7 @@ namespace ts {
}

function emitTrailingComment(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) {
if (!shouldWriteComment(currentText, commentPos)) return;
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment2*/
if (!writer.isAtStartOfLine()) {
writer.write(" ");
Expand Down Expand Up @@ -404,6 +413,7 @@ namespace ts {
}

function writeComment(text: string, lineMap: number[], writer: EmitTextWriter, commentPos: number, commentEnd: number, newLine: string) {
if (!shouldWriteComment(currentText, commentPos)) return;
if (emitPos) emitPos(commentPos);
writeCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine);
if (emitPos) emitPos(commentEnd);
Expand Down
Loading