Ensure that emitter calls callbacks (#18284)

* Ensure that emitter calls calbacks

* Move new parameter to end of parameters

* Fix for ConditionalExpression

* Make suggested changes to emitter

* Fix parameter ordering

* Respond to minor comments

* Remove potentially expensive assertion

* More emitter cleanup
This commit is contained in:
Andy 2017-09-07 14:30:19 -07:00 committed by GitHub
parent 8c64937888
commit ed4e2e6e3b
20 changed files with 325 additions and 197 deletions

View file

@ -406,6 +406,14 @@ namespace ts {
setWriter(/*output*/ undefined);
}
// TODO: Should this just be `emit`?
// See https://github.com/Microsoft/TypeScript/pull/18284#discussion_r137611034
function emitIfPresent(node: Node | undefined) {
if (node) {
emit(node);
}
}
function emit(node: Node) {
pipelineEmitWithNotification(EmitHint.Unspecified, node);
}
@ -451,6 +459,7 @@ namespace ts {
case EmitHint.SourceFile: return pipelineEmitSourceFile(node);
case EmitHint.IdentifierName: return pipelineEmitIdentifierName(node);
case EmitHint.Expression: return pipelineEmitExpression(node);
case EmitHint.MappedTypeParameter: return emitMappedTypeParameter(cast(node, isTypeParameterDeclaration));
case EmitHint.Unspecified: return pipelineEmitUnspecified(node);
}
}
@ -465,6 +474,12 @@ namespace ts {
emitIdentifier(<Identifier>node);
}
function emitMappedTypeParameter(node: TypeParameterDeclaration): void {
emit(node.name);
write(" in ");
emit(node.constraint);
}
function pipelineEmitUnspecified(node: Node): void {
const kind = node.kind;
@ -898,9 +913,9 @@ namespace ts {
function emitParameter(node: ParameterDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
writeIfPresent(node.dotDotDotToken, "...");
emitIfPresent(node.dotDotDotToken);
emit(node.name);
writeIfPresent(node.questionToken, "?");
emitIfPresent(node.questionToken);
emitWithPrefix(": ", node.type);
emitExpressionWithPrefix(" = ", node.initializer);
}
@ -918,7 +933,7 @@ namespace ts {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emit(node.name);
writeIfPresent(node.questionToken, "?");
emitIfPresent(node.questionToken);
emitWithPrefix(": ", node.type);
write(";");
}
@ -927,7 +942,7 @@ namespace ts {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emit(node.name);
writeIfPresent(node.questionToken, "?");
emitIfPresent(node.questionToken);
emitWithPrefix(": ", node.type);
emitExpressionWithPrefix(" = ", node.initializer);
write(";");
@ -937,7 +952,7 @@ namespace ts {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emit(node.name);
writeIfPresent(node.questionToken, "?");
emitIfPresent(node.questionToken);
emitTypeParameters(node, node.typeParameters);
emitParameters(node, node.parameters);
emitWithPrefix(": ", node.type);
@ -947,9 +962,9 @@ namespace ts {
function emitMethodDeclaration(node: MethodDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
writeIfPresent(node.asteriskToken, "*");
emitIfPresent(node.asteriskToken);
emit(node.name);
writeIfPresent(node.questionToken, "?");
emitIfPresent(node.questionToken);
emitSignatureAndBody(node, emitSignatureHead);
}
@ -1035,10 +1050,8 @@ namespace ts {
function emitTypeLiteral(node: TypeLiteralNode) {
write("{");
// If the literal is empty, do not add spaces between braces.
if (node.members.length > 0) {
emitList(node, node.members, getEmitFlags(node) & EmitFlags.SingleLine ? ListFormat.SingleLineTypeLiteralMembers : ListFormat.MultiLineTypeLiteralMembers);
}
const flags = getEmitFlags(node) & EmitFlags.SingleLine ? ListFormat.SingleLineTypeLiteralMembers : ListFormat.MultiLineTypeLiteralMembers;
emitList(node, node.members, flags | ListFormat.NoSpaceIfEmpty);
write("}");
}
@ -1094,13 +1107,16 @@ namespace ts {
writeLine();
increaseIndent();
}
writeIfPresent(node.readonlyToken, "readonly ");
if (node.readonlyToken) {
emit(node.readonlyToken);
write(" ");
}
write("[");
emit(node.typeParameter.name);
write(" in ");
emit(node.typeParameter.constraint);
pipelineEmitWithNotification(EmitHint.MappedTypeParameter, node.typeParameter);
write("]");
writeIfPresent(node.questionToken, "?");
emitIfPresent(node.questionToken);
write(": ");
emit(node.type);
write(";");
@ -1148,7 +1164,7 @@ namespace ts {
function emitBindingElement(node: BindingElement) {
emitWithSuffix(node.propertyName, ": ");
writeIfPresent(node.dotDotDotToken, "...");
emitIfPresent(node.dotDotDotToken);
emit(node.name);
emitExpressionWithPrefix(" = ", node.initializer);
}
@ -1159,33 +1175,22 @@ namespace ts {
function emitArrayLiteralExpression(node: ArrayLiteralExpression) {
const elements = node.elements;
if (elements.length === 0) {
write("[]");
}
else {
const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None;
emitExpressionList(node, elements, ListFormat.ArrayLiteralExpressionElements | preferNewLine);
}
const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None;
emitExpressionList(node, elements, ListFormat.ArrayLiteralExpressionElements | preferNewLine);
}
function emitObjectLiteralExpression(node: ObjectLiteralExpression) {
const properties = node.properties;
if (properties.length === 0) {
write("{}");
const indentedFlag = getEmitFlags(node) & EmitFlags.Indented;
if (indentedFlag) {
increaseIndent();
}
else {
const indentedFlag = getEmitFlags(node) & EmitFlags.Indented;
if (indentedFlag) {
increaseIndent();
}
const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None;
const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 ? ListFormat.AllowTrailingComma : ListFormat.None;
emitList(node, properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine);
const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None;
const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 ? ListFormat.AllowTrailingComma : ListFormat.None;
emitList(node, node.properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine);
if (indentedFlag) {
decreaseIndent();
}
if (indentedFlag) {
decreaseIndent();
}
}
@ -1286,7 +1291,8 @@ namespace ts {
emitTypeParameters(node, node.typeParameters);
emitParametersForArrow(node, node.parameters);
emitWithPrefix(": ", node.type);
write(" =>");
write(" ");
emit(node.equalsGreaterThanToken);
}
function emitDeleteExpression(node: DeleteExpression) {
@ -1364,13 +1370,13 @@ namespace ts {
emitExpression(node.condition);
increaseIndentIf(indentBeforeQuestion, " ");
write("?");
emit(node.questionToken);
increaseIndentIf(indentAfterQuestion, " ");
emitExpression(node.whenTrue);
decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion);
increaseIndentIf(indentBeforeColon, " ");
write(":");
emit(node.colonToken);
increaseIndentIf(indentAfterColon, " ");
emitExpression(node.whenFalse);
decreaseIndentIf(indentBeforeColon, indentAfterColon);
@ -1382,7 +1388,8 @@ namespace ts {
}
function emitYieldExpression(node: YieldExpression) {
write(node.asteriskToken ? "yield*" : "yield");
write("yield");
emit(node.asteriskToken);
emitExpressionWithPrefix(" ", node.expression);
}
@ -1662,7 +1669,9 @@ namespace ts {
function emitFunctionDeclarationOrExpression(node: FunctionDeclaration | FunctionExpression) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
write(node.asteriskToken ? "function* " : "function ");
write("function");
emitIfPresent(node.asteriskToken);
write(" ");
emitIdentifierName(node.name);
emitSignatureAndBody(node, emitSignatureHead);
}
@ -2068,9 +2077,7 @@ namespace ts {
function emitJsxExpression(node: JsxExpression) {
if (node.expression) {
write("{");
if (node.dotDotDotToken) {
write("...");
}
emitIfPresent(node.dotDotDotToken);
emitExpression(node.expression);
write("}");
}
@ -2128,13 +2135,12 @@ namespace ts {
emitTrailingCommentsOfPosition(statements.pos);
}
let format = ListFormat.CaseOrDefaultClauseStatements;
if (emitAsSingleStatement) {
write(" ");
emit(statements[0]);
}
else {
emitList(parentNode, statements, ListFormat.CaseOrDefaultClauseStatements);
format &= ~(ListFormat.MultiLine | ListFormat.Indented);
}
emitList(parentNode, statements, format);
}
function emitHeritageClause(node: HeritageClause) {
@ -2384,7 +2390,7 @@ namespace ts {
function emitParametersForArrow(parentNode: FunctionTypeNode | ArrowFunction, parameters: NodeArray<ParameterDeclaration>) {
if (canEmitSimpleArrowHead(parentNode, parameters)) {
emit(parameters[0]);
emitList(parentNode, parameters, ListFormat.Parameters & ~ListFormat.Parenthesis);
}
else {
emitParameters(parentNode, parameters);
@ -2427,7 +2433,7 @@ namespace ts {
if (format & ListFormat.MultiLine) {
writeLine();
}
else if (format & ListFormat.SpaceBetweenBraces) {
else if (format & ListFormat.SpaceBetweenBraces && !(format & ListFormat.NoSpaceIfEmpty)) {
write(" ");
}
}
@ -2568,12 +2574,6 @@ namespace ts {
}
}
function writeIfPresent(node: Node, text: string) {
if (node) {
write(text);
}
}
function writeToken(token: SyntaxKind, pos: number, contextNode?: Node) {
return onEmitSourceMapOfToken
? onEmitSourceMapOfToken(contextNode, token, pos, writeTokenText)
@ -2584,7 +2584,7 @@ namespace ts {
if (onBeforeEmitToken) {
onBeforeEmitToken(node);
}
writeTokenText(node.kind);
write(tokenToString(node.kind));
if (onAfterEmitToken) {
onAfterEmitToken(node);
}
@ -3107,6 +3107,9 @@ namespace ts {
NoTrailingNewLine = 1 << 16, // Do not emit a trailing NewLine for a MultiLine list.
NoInterveningComments = 1 << 17, // Do not emit comments between each node
NoSpaceIfEmpty = 1 << 18, // If the literal is empty, do not add spaces between braces.
SingleElement = 1 << 19,
// Precomputed Formats
Modifiers = SingleLine | SpaceBetweenSiblings | NoInterveningComments,
HeritageClauses = SingleLine | SpaceBetweenSiblings,
@ -3118,7 +3121,7 @@ namespace ts {
IntersectionTypeConstituents = AmpersandDelimited | SpaceBetweenSiblings | SingleLine,
ObjectBindingPatternElements = SingleLine | AllowTrailingComma | SpaceBetweenBraces | CommaDelimited | SpaceBetweenSiblings,
ArrayBindingPatternElements = SingleLine | AllowTrailingComma | CommaDelimited | SpaceBetweenSiblings,
ObjectLiteralExpressionProperties = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces,
ObjectLiteralExpressionProperties = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces | NoSpaceIfEmpty,
ArrayLiteralExpressionElements = PreserveLines | CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | Indented | SquareBrackets,
CommaListElements = CommaDelimited | SpaceBetweenSiblings | SingleLine,
CallExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis,

View file

@ -281,7 +281,7 @@ namespace ts {
|| node.questionToken !== questionToken
|| node.type !== type
|| node.initializer !== initializer
? updateNode(createParameter(decorators, modifiers, dotDotDotToken, name, node.questionToken, type, initializer), node)
? updateNode(createParameter(decorators, modifiers, dotDotDotToken, name, questionToken, type, initializer), node)
: node;
}
@ -1016,19 +1016,49 @@ namespace ts {
return node;
}
/* @deprecated */ export function updateArrowFunction(
node: ArrowFunction,
modifiers: ReadonlyArray<Modifier> | undefined,
typeParameters: ReadonlyArray<TypeParameterDeclaration> | undefined,
parameters: ReadonlyArray<ParameterDeclaration>,
type: TypeNode | undefined,
body: ConciseBody): ArrowFunction;
export function updateArrowFunction(
node: ArrowFunction,
modifiers: ReadonlyArray<Modifier> | undefined,
typeParameters: ReadonlyArray<TypeParameterDeclaration> | undefined,
parameters: ReadonlyArray<ParameterDeclaration>,
type: TypeNode | undefined,
body: ConciseBody) {
equalsGreaterThanToken: Token<SyntaxKind.EqualsGreaterThanToken>,
body: ConciseBody): ArrowFunction;
export function updateArrowFunction(
node: ArrowFunction,
modifiers: ReadonlyArray<Modifier> | undefined,
typeParameters: ReadonlyArray<TypeParameterDeclaration> | undefined,
parameters: ReadonlyArray<ParameterDeclaration>,
type: TypeNode | undefined,
equalsGreaterThanTokenOrBody: Token<SyntaxKind.EqualsGreaterThanToken> | ConciseBody,
bodyOrUndefined?: ConciseBody,
): ArrowFunction {
let equalsGreaterThanToken: Token<SyntaxKind.EqualsGreaterThanToken>;
let body: ConciseBody;
if (bodyOrUndefined === undefined) {
equalsGreaterThanToken = node.equalsGreaterThanToken;
body = cast(equalsGreaterThanTokenOrBody, isConciseBody);
}
else {
equalsGreaterThanToken = cast(equalsGreaterThanTokenOrBody, (n): n is Token<SyntaxKind.EqualsGreaterThanToken> =>
n.kind === SyntaxKind.EqualsGreaterThanToken);
body = bodyOrUndefined;
}
return node.modifiers !== modifiers
|| node.typeParameters !== typeParameters
|| node.parameters !== parameters
|| node.type !== type
|| node.equalsGreaterThanToken !== equalsGreaterThanToken
|| node.body !== body
? updateNode(createArrowFunction(modifiers, typeParameters, parameters, type, node.equalsGreaterThanToken, body), node)
? updateNode(createArrowFunction(modifiers, typeParameters, parameters, type, equalsGreaterThanToken, body), node)
: node;
}
@ -1135,11 +1165,31 @@ namespace ts {
return node;
}
export function updateConditional(node: ConditionalExpression, condition: Expression, whenTrue: Expression, whenFalse: Expression) {
/* @deprecated */ export function updateConditional(
node: ConditionalExpression,
condition: Expression,
whenTrue: Expression,
whenFalse: Expression): ConditionalExpression;
export function updateConditional(
node: ConditionalExpression,
condition: Expression,
questionToken: Token<SyntaxKind.QuestionToken>,
whenTrue: Expression,
colonToken: Token<SyntaxKind.ColonToken>,
whenFalse: Expression): ConditionalExpression;
export function updateConditional(node: ConditionalExpression, condition: Expression, ...args: any[]) {
if (args.length === 2) {
const [whenTrue, whenFalse] = args;
return updateConditional(node, condition, node.questionToken, whenTrue, node.colonToken, whenFalse);
}
Debug.assert(args.length === 4);
const [questionToken, whenTrue, colonToken, whenFalse] = args;
return node.condition !== condition
|| node.questionToken !== questionToken
|| node.whenTrue !== whenTrue
|| node.colonToken !== colonToken
|| node.whenFalse !== whenFalse
? updateNode(createConditional(condition, node.questionToken, whenTrue, node.colonToken, whenFalse), node)
? updateNode(createConditional(condition, questionToken, whenTrue, colonToken, whenFalse), node)
: node;
}

View file

@ -197,9 +197,10 @@ namespace ts {
/*typeParameters*/ undefined,
visitParameterList(node.parameters, visitor, context),
/*type*/ undefined,
node.equalsGreaterThanToken,
getFunctionFlags(node) & FunctionFlags.Async
? transformAsyncFunctionBody(node)
: visitFunctionBody(node.body, visitor, context)
: visitFunctionBody(node.body, visitor, context),
);
}

View file

@ -595,7 +595,8 @@ namespace ts {
/*typeParameters*/ undefined,
visitParameterList(node.parameters, visitor, context),
/*type*/ undefined,
transformFunctionBody(node)
node.equalsGreaterThanToken,
transformFunctionBody(node),
);
enclosingFunctionFlags = savedEnclosingFunctionFlags;
return updated;

View file

@ -2309,7 +2309,8 @@ namespace ts {
/*typeParameters*/ undefined,
visitParameterList(node.parameters, visitor, context),
/*type*/ undefined,
visitFunctionBody(node.body, visitor, context)
node.equalsGreaterThanToken,
visitFunctionBody(node.body, visitor, context),
);
return updated;
}

View file

@ -4299,10 +4299,11 @@ namespace ts {
}
export const enum EmitHint {
SourceFile, // Emitting a SourceFile
Expression, // Emitting an Expression
IdentifierName, // Emitting an IdentifierName
Unspecified, // Emitting an otherwise unspecified node
SourceFile, // Emitting a SourceFile
Expression, // Emitting an Expression
IdentifierName, // Emitting an IdentifierName
MappedTypeParameter, // Emitting a TypeParameterDeclaration inside of a MappedTypeNode
Unspecified, // Emitting an otherwise unspecified node
}
/* @internal */

View file

@ -4728,8 +4728,7 @@ namespace ts {
/* @internal */
export function isNodeArray<T extends Node>(array: ReadonlyArray<T>): array is NodeArray<T> {
return array.hasOwnProperty("pos")
&& array.hasOwnProperty("end");
return array.hasOwnProperty("pos") && array.hasOwnProperty("end");
}
// Literals

View file

@ -488,6 +488,7 @@ namespace ts {
nodesVisitor((<ArrowFunction>node).typeParameters, visitor, isTypeParameterDeclaration),
visitParameterList((<ArrowFunction>node).parameters, visitor, context, nodesVisitor),
visitNode((<ArrowFunction>node).type, visitor, isTypeNode),
visitNode((<ArrowFunction>node).equalsGreaterThanToken, visitor, isToken),
visitFunctionBody((<ArrowFunction>node).body, visitor, context));
case SyntaxKind.DeleteExpression:
@ -523,7 +524,9 @@ namespace ts {
case SyntaxKind.ConditionalExpression:
return updateConditional(<ConditionalExpression>node,
visitNode((<ConditionalExpression>node).condition, visitor, isExpression),
visitNode((<ConditionalExpression>node).questionToken, visitor, isToken),
visitNode((<ConditionalExpression>node).whenTrue, visitor, isExpression),
visitNode((<ConditionalExpression>node).colonToken, visitor, isToken),
visitNode((<ConditionalExpression>node).whenFalse, visitor, isExpression));
case SyntaxKind.TemplateExpression:

View file

@ -726,6 +726,7 @@ namespace ts.formatting {
parent: Node,
parentStartLine: number,
parentDynamicIndentation: DynamicIndentation): void {
Debug.assert(isNodeArray(nodes));
const listStartToken = getOpenTokenForList(parent, nodes);
const listEndToken = getCloseTokenForOpenToken(listStartToken);

View file

@ -656,11 +656,13 @@ namespace ts.refactor.extractMethod {
const typeParametersAndDeclarations = arrayFrom(typeParameterUsages.values()).map(type => ({ type, declaration: getFirstDeclaration(type) }));
const sortedTypeParametersAndDeclarations = typeParametersAndDeclarations.sort(compareTypesByDeclarationOrder);
const typeParameters: ReadonlyArray<TypeParameterDeclaration> = sortedTypeParametersAndDeclarations.map(t => t.declaration as TypeParameterDeclaration);
const typeParameters: ReadonlyArray<TypeParameterDeclaration> | undefined = sortedTypeParametersAndDeclarations.length === 0
? undefined
: sortedTypeParametersAndDeclarations.map(t => t.declaration as TypeParameterDeclaration);
// Strictly speaking, we should check whether each name actually binds to the appropriate type
// parameter. In cases of shadowing, they may not.
const callTypeArguments: ReadonlyArray<TypeNode> | undefined = typeParameters.length > 0
const callTypeArguments: ReadonlyArray<TypeNode> | undefined = typeParameters !== undefined
? typeParameters.map(decl => createTypeReferenceNode(decl.name, /*typeArguments*/ undefined))
: undefined;

View file

@ -5,19 +5,25 @@ namespace ts.textChanges {
* Currently for simplicity we store recovered positions on the node itself.
* It can be changed to side-table later if we decide that current design is too invasive.
*/
function getPos(n: TextRange) {
return (<any>n)["__pos"];
function getPos(n: TextRange): number {
const result = (<any>n)["__pos"];
Debug.assert(typeof result === "number");
return result;
}
function setPos(n: TextRange, pos: number) {
function setPos(n: TextRange, pos: number): void {
Debug.assert(typeof pos === "number");
(<any>n)["__pos"] = pos;
}
function getEnd(n: TextRange) {
return (<any>n)["__end"];
function getEnd(n: TextRange): number {
const result = (<any>n)["__end"];
Debug.assert(typeof result === "number");
return result;
}
function setEnd(n: TextRange, end: number) {
function setEnd(n: TextRange, end: number): void {
Debug.assert(typeof end === "number");
(<any>n)["__end"] = end;
}
@ -582,7 +588,7 @@ namespace ts.textChanges {
readonly node: Node;
}
export function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLine: NewLineKind): NonFormattedText {
function getNonformattedText(node: Node, sourceFile: SourceFile | undefined, newLine: NewLineKind): NonFormattedText {
const options = { newLine, target: sourceFile && sourceFile.languageVersion };
const writer = new Writer(getNewLineCharacter(options));
const printer = createPrinter(options, writer);
@ -590,7 +596,7 @@ namespace ts.textChanges {
return { text: writer.getText(), node: assignPositionsToNode(node) };
}
export function applyFormatting(nonFormattedText: NonFormattedText, sourceFile: SourceFile, initialIndentation: number, delta: number, rulesProvider: formatting.RulesProvider) {
function applyFormatting(nonFormattedText: NonFormattedText, sourceFile: SourceFile, initialIndentation: number, delta: number, rulesProvider: formatting.RulesProvider) {
const lineMap = computeLineStarts(nonFormattedText.text);
const file: SourceFileLike = {
text: nonFormattedText.text,
@ -616,14 +622,10 @@ namespace ts.textChanges {
function assignPositionsToNode(node: Node): Node {
const visited = visitEachChild(node, assignPositionsToNode, nullTransformationContext, assignPositionsToNodeArray, assignPositionsToNode);
// create proxy node for non synthesized nodes
const newNode = nodeIsSynthesized(visited)
? visited
: (Proxy.prototype = visited, new (<any>Proxy)());
const newNode = nodeIsSynthesized(visited) ? visited : Object.create(visited) as Node;
newNode.pos = getPos(node);
newNode.end = getEnd(node);
return newNode;
function Proxy() { }
}
function assignPositionsToNodeArray(nodes: NodeArray<any>, visitor: Visitor, test?: (node: Node) => boolean, start?: number, count?: number) {

View file

@ -24,9 +24,9 @@ namespace A {
async function newFunction() {
let y = 5;
if(z) {
await z1;
}
if (z) {
await z1;
}
return foo();
}
}
@ -44,9 +44,9 @@ namespace A {
async function newFunction(z: number, z1: any) {
let y = 5;
if(z) {
await z1;
}
if (z) {
await z1;
}
return foo();
}
}
@ -64,9 +64,9 @@ namespace A {
async function newFunction(z: number, z1: any) {
let y = 5;
if(z) {
await z1;
}
if (z) {
await z1;
}
return foo();
}
}
@ -83,8 +83,8 @@ namespace A {
}
async function newFunction(z: number, z1: any, foo: () => void) {
let y = 5;
if(z) {
await z1;
}
if (z) {
await z1;
}
return foo();
}

View file

@ -1,2 +1,2 @@
//// [sourceMapValidationStatements.js.map]
{"version":3,"file":"sourceMapValidationStatements.js","sourceRoot":"","sources":["sourceMapValidationStatements.ts"],"names":[],"mappings":"AAAA;IACI,IAAI,CAAC,CAAC;IACN,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1B,CAAC,IAAI,CAAC,CAAC;QACP,CAAC,IAAI,CAAC,CAAC;IACX,CAAC;IACD,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACT,CAAC,IAAI,CAAC,CAAC;IACX,CAAC;IAAC,IAAI,CAAC,CAAC;QACJ,CAAC,IAAI,EAAE,CAAC;QACR,CAAC,EAAE,CAAC;IACR,CAAC;IACD,IAAI,CAAC,GAAG;QACJ,CAAC;QACD,CAAC;QACD,CAAC;KACJ,CAAC;IACF,IAAI,GAAG,GAAG;QACN,CAAC,EAAE,CAAC;QACJ,CAAC,EAAE,OAAO;KACb,CAAC;IACF,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACd,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACb,IAAI,CAAC,GAAG,EAAE,CAAC;IACf,CAAC;IACD,IAAI,CAAC;QACD,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC;IACnB,CAAC;IAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACT,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACb,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;QACf,CAAC;QAAC,IAAI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X,CAAC;AACD,IAAI,CAAC,GAAG;IACJ,IAAI,CAAC,GAAG,EAAE,CAAC;IACX,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACd,CAAC,CAAC;AACF,CAAC,EAAE,CAAC"}
{"version":3,"file":"sourceMapValidationStatements.js","sourceRoot":"","sources":["sourceMapValidationStatements.ts"],"names":[],"mappings":"AAAA;IACI,IAAI,CAAC,CAAC;IACN,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1B,CAAC,IAAI,CAAC,CAAC;QACP,CAAC,IAAI,CAAC,CAAC;IACX,CAAC;IACD,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACT,CAAC,IAAI,CAAC,CAAC;IACX,CAAC;IAAC,IAAI,CAAC,CAAC;QACJ,CAAC,IAAI,EAAE,CAAC;QACR,CAAC,EAAE,CAAC;IACR,CAAC;IACD,IAAI,CAAC,GAAG;QACJ,CAAC;QACD,CAAC;QACD,CAAC;KACJ,CAAC;IACF,IAAI,GAAG,GAAG;QACN,CAAC,EAAE,CAAC;QACJ,CAAC,EAAE,OAAO;KACb,CAAC;IACF,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACd,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACb,IAAI,CAAC,GAAG,EAAE,CAAC;IACf,CAAC;IACD,IAAI,CAAC;QACD,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC;IACnB,CAAC;IAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACT,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACb,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;QACf,CAAC;QAAC,IAAI,CAAC,CAAC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AAG,CAAC,CAAC;IACV,MAAM,CAAC;AACX,CAAC;AACD,IAAI,CAAC,GAAG;IACJ,IAAI,CAAC,GAAG,EAAE,CAAC;IACX,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACd,CAAC,CAAC;AACF,CAAC,EAAE,CAAC"}

View file

@ -1251,15 +1251,19 @@ sourceFile:sourceMapValidationStatements.ts
7 > ^^^^
8 > ^
9 > ^
10> ^^^
11> ^
12> ^^^
13> ^
14> ^^^
15> ^
16> ^^^
17> ^
18> ^
10> ^
11> ^
12> ^
13> ^
14> ^^^
15> ^
16> ^
17> ^
18> ^
19> ^
20> ^^^
21> ^
22> ^
1->
>
2 > var
@ -1270,15 +1274,19 @@ sourceFile:sourceMapValidationStatements.ts
7 > ==
8 > 1
9 > )
10> ?
11> x
12> +
13> 1
14> :
15> x
16> -
17> 1
18> ;
10>
11> ?
12>
13> x
14> +
15> 1
16>
17> :
18>
19> x
20> -
21> 1
22> ;
1->Emitted(74, 5) Source(72, 5) + SourceIndex(0)
2 >Emitted(74, 9) Source(72, 9) + SourceIndex(0)
3 >Emitted(74, 10) Source(72, 10) + SourceIndex(0)
@ -1288,15 +1296,19 @@ sourceFile:sourceMapValidationStatements.ts
7 >Emitted(74, 19) Source(72, 19) + SourceIndex(0)
8 >Emitted(74, 20) Source(72, 20) + SourceIndex(0)
9 >Emitted(74, 21) Source(72, 21) + SourceIndex(0)
10>Emitted(74, 24) Source(72, 24) + SourceIndex(0)
11>Emitted(74, 25) Source(72, 25) + SourceIndex(0)
12>Emitted(74, 28) Source(72, 28) + SourceIndex(0)
13>Emitted(74, 29) Source(72, 29) + SourceIndex(0)
14>Emitted(74, 32) Source(72, 32) + SourceIndex(0)
15>Emitted(74, 33) Source(72, 33) + SourceIndex(0)
16>Emitted(74, 36) Source(72, 36) + SourceIndex(0)
17>Emitted(74, 37) Source(72, 37) + SourceIndex(0)
18>Emitted(74, 38) Source(72, 38) + SourceIndex(0)
10>Emitted(74, 22) Source(72, 22) + SourceIndex(0)
11>Emitted(74, 23) Source(72, 23) + SourceIndex(0)
12>Emitted(74, 24) Source(72, 24) + SourceIndex(0)
13>Emitted(74, 25) Source(72, 25) + SourceIndex(0)
14>Emitted(74, 28) Source(72, 28) + SourceIndex(0)
15>Emitted(74, 29) Source(72, 29) + SourceIndex(0)
16>Emitted(74, 30) Source(72, 30) + SourceIndex(0)
17>Emitted(74, 31) Source(72, 31) + SourceIndex(0)
18>Emitted(74, 32) Source(72, 32) + SourceIndex(0)
19>Emitted(74, 33) Source(72, 33) + SourceIndex(0)
20>Emitted(74, 36) Source(72, 36) + SourceIndex(0)
21>Emitted(74, 37) Source(72, 37) + SourceIndex(0)
22>Emitted(74, 38) Source(72, 38) + SourceIndex(0)
---
>>> (x == 1) ? x + 1 : x - 1;
1 >^^^^
@ -1305,15 +1317,19 @@ sourceFile:sourceMapValidationStatements.ts
4 > ^^^^
5 > ^
6 > ^
7 > ^^^
8 > ^
9 > ^^^
10> ^
11> ^^^
12> ^
13> ^^^
14> ^
15> ^
7 > ^
8 > ^
9 > ^
10> ^
11> ^^^
12> ^
13> ^
14> ^
15> ^
16> ^
17> ^^^
18> ^
19> ^
1 >
>
2 > (
@ -1321,30 +1337,38 @@ sourceFile:sourceMapValidationStatements.ts
4 > ==
5 > 1
6 > )
7 > ?
8 > x
9 > +
10> 1
11> :
12> x
13> -
14> 1
15> ;
7 >
8 > ?
9 >
10> x
11> +
12> 1
13>
14> :
15>
16> x
17> -
18> 1
19> ;
1 >Emitted(75, 5) Source(73, 5) + SourceIndex(0)
2 >Emitted(75, 6) Source(73, 6) + SourceIndex(0)
3 >Emitted(75, 7) Source(73, 7) + SourceIndex(0)
4 >Emitted(75, 11) Source(73, 11) + SourceIndex(0)
5 >Emitted(75, 12) Source(73, 12) + SourceIndex(0)
6 >Emitted(75, 13) Source(73, 13) + SourceIndex(0)
7 >Emitted(75, 16) Source(73, 16) + SourceIndex(0)
8 >Emitted(75, 17) Source(73, 17) + SourceIndex(0)
9 >Emitted(75, 20) Source(73, 20) + SourceIndex(0)
10>Emitted(75, 21) Source(73, 21) + SourceIndex(0)
11>Emitted(75, 24) Source(73, 24) + SourceIndex(0)
12>Emitted(75, 25) Source(73, 25) + SourceIndex(0)
13>Emitted(75, 28) Source(73, 28) + SourceIndex(0)
14>Emitted(75, 29) Source(73, 29) + SourceIndex(0)
15>Emitted(75, 30) Source(73, 30) + SourceIndex(0)
7 >Emitted(75, 14) Source(73, 14) + SourceIndex(0)
8 >Emitted(75, 15) Source(73, 15) + SourceIndex(0)
9 >Emitted(75, 16) Source(73, 16) + SourceIndex(0)
10>Emitted(75, 17) Source(73, 17) + SourceIndex(0)
11>Emitted(75, 20) Source(73, 20) + SourceIndex(0)
12>Emitted(75, 21) Source(73, 21) + SourceIndex(0)
13>Emitted(75, 22) Source(73, 22) + SourceIndex(0)
14>Emitted(75, 23) Source(73, 23) + SourceIndex(0)
15>Emitted(75, 24) Source(73, 24) + SourceIndex(0)
16>Emitted(75, 25) Source(73, 25) + SourceIndex(0)
17>Emitted(75, 28) Source(73, 28) + SourceIndex(0)
18>Emitted(75, 29) Source(73, 29) + SourceIndex(0)
19>Emitted(75, 30) Source(73, 30) + SourceIndex(0)
---
>>> x === 1;
1 >^^^^

View file

@ -1,2 +1,2 @@
//// [ternaryExpressionSourceMap.js.map]
{"version":3,"file":"ternaryExpressionSourceMap.js","sourceRoot":"","sources":["ternaryExpressionSourceMap.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,CAAC,CAAC;AACV,IAAI,GAAG,GAAG,CAAC,GAAG,cAAM,OAAA,CAAC,EAAD,CAAC,GAAG,cAAM,OAAA,CAAC,EAAD,CAAC,CAAC"}
{"version":3,"file":"ternaryExpressionSourceMap.js","sourceRoot":"","sources":["ternaryExpressionSourceMap.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,CAAC,CAAC;AACV,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,cAAM,OAAA,CAAC,EAAD,CAAC,CAAC,CAAC,CAAC,cAAM,OAAA,CAAC,EAAD,CAAC,CAAC"}

View file

@ -35,55 +35,67 @@ sourceFile:ternaryExpressionSourceMap.ts
3 > ^^^
4 > ^^^
5 > ^
6 > ^^^
7 > ^^^^^^^^^^^^^^
8 > ^^^^^^^
9 > ^
10> ^^
11> ^
12> ^^^
13> ^^^^^^^^^^^^^^
14> ^^^^^^^
15> ^
16> ^^
17> ^
18> ^
6 > ^
7 > ^
8 > ^
9 > ^^^^^^^^^^^^^^
10> ^^^^^^^
11> ^
12> ^^
13> ^
14> ^
15> ^
16> ^
17> ^^^^^^^^^^^^^^
18> ^^^^^^^
19> ^
20> ^^
21> ^
22> ^
1->
>
2 >var
3 > foo
4 > =
5 > x
6 > ?
7 > () =>
8 >
9 > 0
10>
11> 0
12> :
13> () =>
14>
15> 0
16>
17> 0
18> ;
6 >
7 > ?
8 >
9 > () =>
10>
11> 0
12>
13> 0
14>
15> :
16>
17> () =>
18>
19> 0
20>
21> 0
22> ;
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
2 >Emitted(2, 5) Source(2, 5) + SourceIndex(0)
3 >Emitted(2, 8) Source(2, 8) + SourceIndex(0)
4 >Emitted(2, 11) Source(2, 11) + SourceIndex(0)
5 >Emitted(2, 12) Source(2, 12) + SourceIndex(0)
6 >Emitted(2, 15) Source(2, 15) + SourceIndex(0)
7 >Emitted(2, 29) Source(2, 21) + SourceIndex(0)
8 >Emitted(2, 36) Source(2, 21) + SourceIndex(0)
9 >Emitted(2, 37) Source(2, 22) + SourceIndex(0)
10>Emitted(2, 39) Source(2, 21) + SourceIndex(0)
11>Emitted(2, 40) Source(2, 22) + SourceIndex(0)
12>Emitted(2, 43) Source(2, 25) + SourceIndex(0)
13>Emitted(2, 57) Source(2, 31) + SourceIndex(0)
14>Emitted(2, 64) Source(2, 31) + SourceIndex(0)
15>Emitted(2, 65) Source(2, 32) + SourceIndex(0)
16>Emitted(2, 67) Source(2, 31) + SourceIndex(0)
17>Emitted(2, 68) Source(2, 32) + SourceIndex(0)
18>Emitted(2, 69) Source(2, 33) + SourceIndex(0)
6 >Emitted(2, 13) Source(2, 13) + SourceIndex(0)
7 >Emitted(2, 14) Source(2, 14) + SourceIndex(0)
8 >Emitted(2, 15) Source(2, 15) + SourceIndex(0)
9 >Emitted(2, 29) Source(2, 21) + SourceIndex(0)
10>Emitted(2, 36) Source(2, 21) + SourceIndex(0)
11>Emitted(2, 37) Source(2, 22) + SourceIndex(0)
12>Emitted(2, 39) Source(2, 21) + SourceIndex(0)
13>Emitted(2, 40) Source(2, 22) + SourceIndex(0)
14>Emitted(2, 41) Source(2, 23) + SourceIndex(0)
15>Emitted(2, 42) Source(2, 24) + SourceIndex(0)
16>Emitted(2, 43) Source(2, 25) + SourceIndex(0)
17>Emitted(2, 57) Source(2, 31) + SourceIndex(0)
18>Emitted(2, 64) Source(2, 31) + SourceIndex(0)
19>Emitted(2, 65) Source(2, 32) + SourceIndex(0)
20>Emitted(2, 67) Source(2, 31) + SourceIndex(0)
21>Emitted(2, 68) Source(2, 32) + SourceIndex(0)
22>Emitted(2, 69) Source(2, 33) + SourceIndex(0)
---
>>>//# sourceMappingURL=ternaryExpressionSourceMap.js.map

View file

@ -85,6 +85,8 @@ function foo7(x) {
return typeof x !== "string"
&& ((z = x) // number | boolean
&& (typeof x === "number"
// change value of x
? ((x = 10) && x.toString()) // x is number
// do not change value
: ((y = x) && x.toString()))); // x is boolean
}

View file

@ -87,6 +87,8 @@ function foo7(x) {
return typeof x === "string"
|| ((z = x) // number | boolean
|| (typeof x === "number"
// change value of x
? ((x = 10) && x.toString()) // number | boolean | string
// do not change value
: ((y = x) && x.toString()))); // number | boolean | string
}

View file

@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />
////function f(x: number): number {
//// /*start*/switch (x) {case 0:
////return 0;}/*end*/
////}
goTo.select('start', 'end')
edit.applyRefactor({
refactorName: "Extract Method",
actionName: "scope_1",
actionDescription: "Extract function into global scope",
});
verify.currentFileContentIs(
`function f(x: number): number {
return newFunction(x);
}
function newFunction(x: number) {
switch (x) {
case 0:
return 0;
}
}
`);

View file

@ -20,6 +20,6 @@ verify.currentFileContentIs(
var x: 1 | 2 | 3 = newFunction();
function newFunction(): 1 | 2 | 3 {
return 1 + 1 === 2?1: 2;
return 1 + 1 === 2 ? 1 : 2;
}
}`);