_id
stringlengths
2
6
title
stringlengths
0
58
partition
stringclasses
3 values
text
stringlengths
27
233k
language
stringclasses
1 value
meta_information
dict
q1500
isSymbolAccessible
train
function isSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasesToMakeVisible) { if (symbol && enclosingDeclaration && !(symbol.flags & 262144 /* TypeParameter */)) { var initialSymbol = symbol; var meaningToLook = meaning; while (symbol) { // Symbol is accessible if it by itself is accessible var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook, /*useOnlyExternalAliasing*/ false); if (accessibleSymbolChain) { var hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible); if (!hasAccessibleDeclarations) { return { accessibility: 1 /* NotAccessible */, errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, 1920 /* Namespace */) : undefined }; } return hasAccessibleDeclarations; } // If we haven't got the accessible symbol, it doesn't mean the symbol is actually inaccessible. // It could be a qualified symbol and hence verify the path // e.g.: // module m { // export class c { // } // } // const x: typeof m.c // In the above example when we start with checking if typeof m.c symbol is accessible, // we are going to see if c can be accessed in scope directly. // But it can't, hence the accessible is going to be undefined, but that doesn't mean m.c is inaccessible // It is accessible if the parent m is accessible because then m.c can be accessed through qualification meaningToLook = getQualifiedLeftMeaning(meaning); symbol
javascript
{ "resource": "" }
q1501
appendPropertyOrElementAccessForSymbol
train
function appendPropertyOrElementAccessForSymbol(symbol, writer) { var symbolName = getNameOfSymbol(symbol); var firstChar = symbolName.charCodeAt(0); var needsElementAccess = !ts.isIdentifierStart(firstChar, languageVersion); if (needsElementAccess) { writePunctuation(writer, 19 /* OpenBracketToken */);
javascript
{ "resource": "" }
q1502
buildSymbolDisplay
train
function buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning, flags, typeFlags) { var parentSymbol; function appendParentTypeArgumentsAndSymbolName(symbol) { if (parentSymbol) { // Write type arguments of instantiated class/interface here if (flags & 1 /* WriteTypeParametersOrArguments */) { if (symbol.flags & 16777216 /* Instantiated */) { buildDisplayForTypeArgumentsAndDelimiters(getTypeParametersOfClassOrInterface(parentSymbol), symbol.mapper, writer, enclosingDeclaration); } else { buildTypeParameterDisplayFromSymbol(parentSymbol, writer, enclosingDeclaration); } } appendPropertyOrElementAccessForSymbol(symbol, writer); } else { appendSymbolNameOnly(symbol, writer); } parentSymbol = symbol; } // const the writer know we just wrote out a symbol. The declaration emitter writer uses // this to determine if an import it has previously seen (and not written out) needs // to be written to the file once the walk of the tree is complete. // // NOTE(cyrusn): This approach feels somewhat unfortunate. A simple pass over the tree // up front (for example, during checking) could determine if we need to emit the imports // and we could then access that data during declaration emit. writer.trackSymbol(symbol, enclosingDeclaration, meaning); function walkSymbol(symbol, meaning) { if (symbol) { var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2 /* UseOnlyExternalAliasing */)); if (!accessibleSymbolChain || needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { // Go up and add our parent. walkSymbol(getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol), getQualifiedLeftMeaning(meaning)); } if (accessibleSymbolChain) {
javascript
{ "resource": "" }
q1503
pushTypeResolution
train
function pushTypeResolution(target, propertyName) { var resolutionCycleStartIndex = findResolutionCycleStartIndex(target, propertyName); if (resolutionCycleStartIndex >= 0) { // A cycle was found var length_2 = resolutionTargets.length; for (var i = resolutionCycleStartIndex; i < length_2; i++) { resolutionResults[i] = false; }
javascript
{ "resource": "" }
q1504
getTypeForBindingElement
train
function getTypeForBindingElement(declaration) { var pattern = declaration.parent; var parentType = getTypeForBindingElementParent(pattern.parent); // If parent has the unknown (error) type, then so does this binding element if (parentType === unknownType) { return unknownType; } // If no type was specified or inferred for parent, or if the specified or inferred type is any, // infer from the initializer of the binding element if one is present. Otherwise, go with the // undefined or any type of the parent. if (!parentType || isTypeAny(parentType)) { if (declaration.initializer) { return checkExpressionCached(declaration.initializer); } return parentType; } var type; if (pattern.kind === 167 /* ObjectBindingPattern */) { // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) var name_11 = declaration.propertyName || declaration.name; if (isComputedNonLiteralName(name_11)) { // computed properties with non-literal names are treated as 'any' return anyType; } if (declaration.initializer) { getContextualType(declaration.initializer); } // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, // or otherwise the type of the string index signature. var text = getTextOfPropertyName(name_11); type = getTypeOfPropertyOfType(parentType, text) || isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1 /* Number */) || getIndexTypeOfType(parentType, 0 /* String */); if (!type) { error(name_11, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_11)); return unknownType; } } else { // This elementType will be used if the specific property corresponding to this index is not // present (aka the tuple element property). This call also checks that the parentType is in // fact an iterable or array (depending on target language). var elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false);
javascript
{ "resource": "" }
q1505
getTypeForVariableLikeDeclaration
train
function getTypeForVariableLikeDeclaration(declaration, includeOptionality) { if (declaration.flags & 134217728 /* JavaScriptFile */) { // If this is a variable in a JavaScript file, then use the JSDoc type (if it has // one as its type), otherwise fallback to the below standard TS codepaths to // try to figure it out. var type = getTypeForVariableLikeDeclarationFromJSDocComment(declaration); if (type && type !== unknownType) { return type; } } // A variable declared in a for..in statement is always of type string if (declaration.parent.parent.kind === 207 /* ForInStatement */) { return stringType; } if (declaration.parent.parent.kind === 208 /* ForOfStatement */) { // checkRightHandSideOfForOf will return undefined if the for-of expression type was // missing properties/signatures required to get its iteratedType (like // [Symbol.iterator] or next). This may be because we accessed properties from anyType, // or it may have led to an error inside getElementTypeOfIterable. return checkRightHandSideOfForOf(declaration.parent.parent.expression) || anyType; } if (ts.isBindingPattern(declaration.parent)) { return getTypeForBindingElement(declaration); } // Use type from type annotation if one is present if (declaration.type) { return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality); } if (declaration.kind === 142 /* Parameter */) {
javascript
{ "resource": "" }
q1506
getTypeFromBindingElement
train
function getTypeFromBindingElement(element, includePatternInType, reportErrors) { if (element.initializer) { return checkExpressionCached(element.initializer); } if (ts.isBindingPattern(element.name)) { return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); }
javascript
{ "resource": "" }
q1507
getTypeFromObjectBindingPattern
train
function getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) { var members = ts.createMap(); var hasComputedProperties = false; ts.forEach(pattern.elements, function (e) { var name = e.propertyName || e.name; if (isComputedNonLiteralName(name)) { // do not include computed properties in the implied type hasComputedProperties = true; return; } var text = getTextOfPropertyName(name); var flags = 4 /* Property */ | 67108864 /* Transient */ | (e.initializer ? 536870912 /* Optional */ : 0); var symbol = createSymbol(flags, text);
javascript
{ "resource": "" }
q1508
getTypeFromArrayBindingPattern
train
function getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors) { var elements = pattern.elements; if (elements.length === 0 || elements[elements.length - 1].dotDotDotToken) { return languageVersion >= 2 /* ES6 */ ? createIterableType(anyType) : anyArrayType; } // If the pattern has at least one element, and no rest element, then it should imply a tuple type. var elementTypes = ts.map(elements, function (e)
javascript
{ "resource": "" }
q1509
appendTypeParameters
train
function appendTypeParameters(typeParameters, declarations) { for (var _i = 0, declarations_2 = declarations; _i < declarations_2.length; _i++) { var declaration = declarations_2[_i]; var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration));
javascript
{ "resource": "" }
q1510
appendOuterTypeParameters
train
function appendOuterTypeParameters(typeParameters, node) { while (true) { node = node.parent; if (!node) { return typeParameters; } if (node.kind === 221 /* ClassDeclaration */ || node.kind === 192 /* ClassExpression */ || node.kind === 220 /* FunctionDeclaration */ || node.kind === 179 /* FunctionExpression */ || node.kind === 147 /* MethodDeclaration */ || node.kind === 180 /* ArrowFunction */) {
javascript
{ "resource": "" }
q1511
getOuterTypeParametersOfClassOrInterface
train
function getOuterTypeParametersOfClassOrInterface(symbol) { var declaration = symbol.flags & 32 /* Class
javascript
{ "resource": "" }
q1512
isIndependentInterface
train
function isIndependentInterface(symbol) { for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { var declaration = _a[_i]; if (declaration.kind === 222 /* InterfaceDeclaration */) { if (declaration.flags & 16384 /* ContainsThis */) { return false; } var baseTypeNodes = ts.getInterfaceBaseTypeNodes(declaration); if (baseTypeNodes) {
javascript
{ "resource": "" }
q1513
isIndependentType
train
function isIndependentType(node) { switch (node.kind) { case 117 /* AnyKeyword */: case 132 /* StringKeyword */: case 130 /* NumberKeyword */: case 120 /* BooleanKeyword */: case 133 /* SymbolKeyword */: case 103 /* VoidKeyword */: case 135 /* UndefinedKeyword */: case 93 /* NullKeyword */: case 127 /* NeverKeyword */:
javascript
{ "resource": "" }
q1514
isIndependentMember
train
function isIndependentMember(symbol) { if (symbol.declarations && symbol.declarations.length === 1) { var declaration = symbol.declarations[0]; if (declaration) { switch (declaration.kind) { case 145 /* PropertyDeclaration */: case 144 /* PropertySignature */: return isIndependentVariableLikeDeclaration(declaration);
javascript
{ "resource": "" }
q1515
createInstantiatedSymbolTable
train
function createInstantiatedSymbolTable(symbols, mapper, mappingThisOnly) { var result = ts.createMap(); for (var _i = 0, symbols_2 = symbols; _i < symbols_2.length; _i++) { var symbol = symbols_2[_i];
javascript
{ "resource": "" }
q1516
getUnionSignatures
train
function getUnionSignatures(types, kind) { var signatureLists = ts.map(types, function (t) { return getSignaturesOfType(t, kind); }); var result = undefined; for (var i = 0; i < signatureLists.length; i++) { for (var _i = 0, _a = signatureLists[i]; _i < _a.length; _i++) { var signature = _a[_i]; // Only process signatures with parameter lists that aren't already in the result list if (!result || !findMatchingSignature(result, signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true)) { var unionSignatures = findMatchingSignatures(signatureLists, signature, i); if (unionSignatures) { var s = signature; // Union the result types when more than one signature matches if (unionSignatures.length > 1) { s = cloneSignature(signature);
javascript
{ "resource": "" }
q1517
getApparentTypeOfTypeParameter
train
function getApparentTypeOfTypeParameter(type) { if (!type.resolvedApparentType) { var constraintType = getConstraintOfTypeParameter(type); while (constraintType && constraintType.flags & 16384 /* TypeParameter */) { constraintType = getConstraintOfTypeParameter(constraintType);
javascript
{ "resource": "" }
q1518
getApparentType
train
function getApparentType(type) { if (type.flags & 16384 /* TypeParameter */) { type = getApparentTypeOfTypeParameter(type); } if (type.flags & 34 /* StringLike */) { type = globalStringType; } else if (type.flags & 340 /* NumberLike */) { type = globalNumberType; }
javascript
{ "resource": "" }
q1519
getPropagatingFlagsOfTypes
train
function getPropagatingFlagsOfTypes(types, excludeKinds) { var result = 0; for (var _i = 0, types_3 = types; _i < types_3.length; _i++) { var type = types_3[_i];
javascript
{ "resource": "" }
q1520
getUnionTypeFromSortedList
train
function getUnionTypeFromSortedList(types, aliasSymbol, aliasTypeArguments) { if (types.length === 0) { return neverType; } if (types.length === 1) { return types[0]; } var id = getTypeListId(types); var type =
javascript
{ "resource": "" }
q1521
isKnownProperty
train
function isKnownProperty(type, name) { if (type.flags & 2588672 /* ObjectType */) { var resolved = resolveStructuredTypeMembers(type); if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyObjectType(resolved)) || resolved.stringIndexInfo || (resolved.numberIndexInfo && isNumericLiteralName(name)) || getPropertyOfType(type, name)) { return true; } } else if (type.flags & 1572864 /* UnionOrIntersection */) { for
javascript
{ "resource": "" }
q1522
objectTypeRelatedTo
train
function objectTypeRelatedTo(source, originalSource, target, reportErrors) { if (overflow) { return 0 /* False */; } var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; var related = relation[id]; if (related !== undefined) { if (reportErrors && related === 2 /* Failed */) { // We are elaborating errors and the cached result is an unreported failure. Record the result as a reported // failure and continue computing the relation such that errors get reported. relation[id] = 3 /* FailedAndReported */; } else { return related === 1 /* Succeeded */ ? -1 /* True */ : 0 /* False */; } } if (depth > 0) { for (var i = 0; i < depth; i++) { // If source and target are already being compared, consider them related with assumptions if (maybeStack[i][id]) { return 1 /* Maybe */; } } if (depth === 100) { overflow = true; return 0 /* False */; } } else { sourceStack = []; targetStack = []; maybeStack = []; expandingFlags = 0; }
javascript
{ "resource": "" }
q1523
signatureRelatedTo
train
function signatureRelatedTo(source, target, reportErrors) { return compareSignaturesRelated(source, target,
javascript
{ "resource": "" }
q1524
isAbstractConstructorType
train
function isAbstractConstructorType(type) { if (type.flags & 2097152 /* Anonymous */) { var symbol = type.symbol; if (symbol && symbol.flags & 32 /* Class */) { var declaration = getClassLikeDeclarationOfSymbol(symbol);
javascript
{ "resource": "" }
q1525
isObjectLiteralType
train
function isObjectLiteralType(type) { return type.symbol && (type.symbol.flags & (4096 /* ObjectLiteral */ | 2048 /* TypeLiteral */)) !== 0 &&
javascript
{ "resource": "" }
q1526
getRegularTypeOfObjectLiteral
train
function getRegularTypeOfObjectLiteral(type) { if (!(type.flags & 16777216 /* FreshObjectLiteral */)) { return type; } var regularType = type.regularType; if (regularType) { return regularType; } var resolved = type; var members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral); var regularNew = createAnonymousType(resolved.symbol,
javascript
{ "resource": "" }
q1527
removeTypesFromUnionOrIntersection
train
function removeTypesFromUnionOrIntersection(type, typesToRemove) { var reducedTypes = []; for (var _i = 0, _a = type.types; _i < _a.length; _i++) { var t = _a[_i]; if (!typeIdenticalToSomeType(t, typesToRemove)) {
javascript
{ "resource": "" }
q1528
getResolvedSymbol
train
function getResolvedSymbol(node) { var links = getNodeLinks(node); if (!links.resolvedSymbol) { links.resolvedSymbol = !ts.nodeIsMissing(node) && resolveName(node, node.text, 107455 /* Value */
javascript
{ "resource": "" }
q1529
containsMatchingReferenceDiscriminant
train
function containsMatchingReferenceDiscriminant(source, target) { return target.kind === 172 /* PropertyAccessExpression */ && containsMatchingReference(source, target.expression) &&
javascript
{ "resource": "" }
q1530
getAssignmentReducedType
train
function getAssignmentReducedType(declaredType, assignedType) { if (declaredType !== assignedType) { var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); });
javascript
{ "resource": "" }
q1531
narrowType
train
function narrowType(type, expr, assumeTrue) { switch (expr.kind) { case 69 /* Identifier */: case 97 /* ThisKeyword */: case 172 /* PropertyAccessExpression */: return narrowTypeByTruthiness(type, expr, assumeTrue); case 174 /* CallExpression */: return narrowTypeByTypePredicate(type, expr, assumeTrue); case 178 /* ParenthesizedExpression */: return narrowType(type, expr.expression, assumeTrue);
javascript
{ "resource": "" }
q1532
isParameterAssigned
train
function isParameterAssigned(symbol) { var func = ts.getRootDeclaration(symbol.valueDeclaration).parent; var links = getNodeLinks(func); if (!(links.flags & 4194304 /* AssignmentsMarked */)) { links.flags |= 4194304 /* AssignmentsMarked */;
javascript
{ "resource": "" }
q1533
getSuperCallInConstructor
train
function getSuperCallInConstructor(constructor) { var links = getNodeLinks(constructor); // Only trying to find super-call if we haven't yet tried to find one. Once we try, we will record the result if (links.hasSuperCall === undefined) {
javascript
{ "resource": "" }
q1534
classDeclarationExtendsNull
train
function classDeclarationExtendsNull(classDecl) { var classSymbol = getSymbolOfNode(classDecl); var classInstanceType =
javascript
{ "resource": "" }
q1535
getContextuallyTypedParameterType
train
function getContextuallyTypedParameterType(parameter) { var func = parameter.parent; if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) { var iife = ts.getImmediatelyInvokedFunctionExpression(func); if (iife) { var indexOfParameter = ts.indexOf(func.parameters, parameter); if (iife.arguments && indexOfParameter < iife.arguments.length) { if (parameter.dotDotDotToken) { var restTypes = []; for (var i = indexOfParameter; i < iife.arguments.length; i++) { restTypes.push(getTypeOfExpression(iife.arguments[i])); } return createArrayType(getUnionType(restTypes)); } var links = getNodeLinks(iife);
javascript
{ "resource": "" }
q1536
getContextualTypeForInitializerExpression
train
function getContextualTypeForInitializerExpression(node) { var declaration = node.parent; if (node === declaration.initializer) { if (declaration.type) { return getTypeFromTypeNode(declaration.type); } if (declaration.kind === 142 /* Parameter */) { var type = getContextuallyTypedParameterType(declaration); if (type) { return type; } } if (ts.isBindingPattern(declaration.name)) {
javascript
{ "resource": "" }
q1537
getContextualTypeForArgument
train
function getContextualTypeForArgument(callTarget, arg) { var args = getEffectiveCallArguments(callTarget); var argIndex = ts.indexOf(args, arg); if (argIndex >= 0) { var signature
javascript
{ "resource": "" }
q1538
contextualTypeIsTupleLikeType
train
function contextualTypeIsTupleLikeType(type) { return !!(type.flags & 524288 /* Union
javascript
{ "resource": "" }
q1539
getContextualSignature
train
function getContextualSignature(node) { ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); var type = getContextualTypeForFunctionLikeDeclaration(node); if (!type) { return undefined; } if (!(type.flags & 524288 /* Union */)) { return getNonGenericSignature(type); } var signatureList; var types = type.types; for (var _i = 0, types_12 = types; _i < types_12.length; _i++) { var current = types_12[_i]; var signature = getNonGenericSignature(current); if (signature) { if (!signatureList) { // This signature will contribute to contextual union signature signatureList = [signature]; } else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { // Signatures aren't identical, do not use return undefined;
javascript
{ "resource": "" }
q1540
isJsxIntrinsicIdentifier
train
function isJsxIntrinsicIdentifier(tagName) { // TODO (yuisu): comment if (tagName.kind === 172 /* PropertyAccessExpression */ || tagName.kind === 97 /* ThisKeyword */) { return false;
javascript
{ "resource": "" }
q1541
checkClassPropertyAccess
train
function checkClassPropertyAccess(node, left, type, prop) { var flags = getDeclarationFlagsFromSymbol(prop); var declaringClass = getDeclaredTypeOfSymbol(getParentOfSymbol(prop)); var errorNode = node.kind === 172 /* PropertyAccessExpression */ || node.kind === 218 /* VariableDeclaration */ ? node.name : node.right; if (left.kind === 95 /* SuperKeyword */) { // TS 1.0 spec (April 2014): 4.8.2 // - In a constructor, instance member function, instance member accessor, or // instance member variable initializer where this references a derived class instance, // a super property access is permitted and must specify a public instance member function of the base class. // - In a static member function or static member accessor // where this references the constructor function object of a derived class, // a super property access is permitted and must specify a public static member function of the base class. if (languageVersion < 2 /* ES6 */ && getDeclarationKindFromSymbol(prop) !== 147 /* MethodDeclaration */) { // `prop` refers to a *property* declared in the super class // rather than a *method*, so it does not satisfy the above criteria. error(errorNode, ts.Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); return false; } if (flags & 128 /* Abstract */) { // A method cannot be accessed in a super property access if the method is abstract. // This error could mask a private property access error. But, a member // cannot simultaneously be private and abstract, so this will trigger an // additional error elsewhere. error(errorNode, ts.Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(declaringClass)); return false; } } // Public properties are otherwise accessible. if (!(flags & (8 /* Private */ | 16 /* Protected */))) { return true; } // Property is known to be private or protected at this point // Private property is accessible if the property is within the declaring class if (flags & 8 /* Private */) { var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)); if (!isNodeWithinClass(node, declaringClassDeclaration)) { error(errorNode, ts.Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass));
javascript
{ "resource": "" }
q1542
getForInVariableSymbol
train
function getForInVariableSymbol(node) { var initializer = node.initializer; if (initializer.kind === 219 /* VariableDeclarationList */) { var variable = initializer.declarations[0]; if (variable && !ts.isBindingPattern(variable.name)) { return getSymbolOfNode(variable); } }
javascript
{ "resource": "" }
q1543
getSingleCallSignature
train
function getSingleCallSignature(type) { if (type.flags & 2588672 /* ObjectType */) { var resolved = resolveStructuredTypeMembers(type); if (resolved.callSignatures.length === 1
javascript
{ "resource": "" }
q1544
getEffectiveDecoratorFirstArgumentType
train
function getEffectiveDecoratorFirstArgumentType(node) { // The first argument to a decorator is its `target`. if (node.kind === 221 /* ClassDeclaration */) { // For a class decorator, the `target` is the type of the class (e.g. the // "static" or "constructor" side of the class) var classSymbol = getSymbolOfNode(node); return getTypeOfSymbol(classSymbol); } if (node.kind === 142 /* Parameter */) { // For a parameter decorator, the `target` is the parent type of the // parameter's containing method. node = node.parent; if (node.kind === 148 /* Constructor */) { var classSymbol = getSymbolOfNode(node); return getTypeOfSymbol(classSymbol); } } if (node.kind === 145 /* PropertyDeclaration */ ||
javascript
{ "resource": "" }
q1545
getEffectiveArgumentType
train
function getEffectiveArgumentType(node, argIndex, arg) { // Decorators provide special arguments, a tagged template expression provides // a special first argument, and string literals get string literal types // unless we're reporting errors if (node.kind === 143 /* Decorator */) { return getEffectiveDecoratorArgumentType(node, argIndex); } else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) {
javascript
{ "resource": "" }
q1546
getResolvedSignature
train
function getResolvedSignature(node, candidatesOutArray) { var links = getNodeLinks(node); // If getResolvedSignature has already been called, we will have cached the resolvedSignature. // However, it is possible that either candidatesOutArray was not passed in the first time, // or that a different candidatesOutArray was passed in. Therefore, we need to redo the work // to correctly fill the candidatesOutArray. var cached = links.resolvedSignature; if (cached && cached !== resolvingSignature && !candidatesOutArray) { return cached; }
javascript
{ "resource": "" }
q1547
checkCallExpression
train
function checkCallExpression(node) { // Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments); var signature = getResolvedSignature(node); if (node.expression.kind === 95 /* SuperKeyword */) { return voidType; } if (node.kind === 175 /* NewExpression */) { var declaration = signature.declaration; if (declaration && declaration.kind !== 148 /* Constructor */ && declaration.kind !== 152 /* ConstructSignature */ && declaration.kind !== 157 /* ConstructorType */ && !ts.isJSDocConstructSignature(declaration)) { // When resolved signature is a call signature (and not a construct signature) the result type is any, unless // the declaring function had members created through 'x.prototype.y = expr' or 'this.y = expr' psuedodeclarations // in a JS file // Note:JS inferred classes might come from a variable declaration instead of a function declaration. // In this case, using getResolvedSymbol directly is required to avoid losing the members from the declaration. var funcSymbol = node.expression.kind === 69 /* Identifier */ ?
javascript
{ "resource": "" }
q1548
maybeTypeOfKind
train
function maybeTypeOfKind(type, kind) { if (type.flags & kind) { return true; } if (type.flags & 1572864 /* UnionOrIntersection */) { var types = type.types; for (var _i = 0, types_13 = types; _i < types_13.length; _i++) { var t = types_13[_i];
javascript
{ "resource": "" }
q1549
isTypeOfKind
train
function isTypeOfKind(type, kind) { if (type.flags & kind) { return true; } if (type.flags & 524288 /* Union */) { var types = type.types; for (var _i = 0, types_14 = types; _i < types_14.length; _i++) { var t = types_14[_i]; if (!isTypeOfKind(t, kind)) { return false; } } return true; } if (type.flags & 1048576 /* Intersection */) { var types = type.types;
javascript
{ "resource": "" }
q1550
checkForDisallowedESSymbolOperand
train
function checkForDisallowedESSymbolOperand(operator) { var offendingSymbolOperand = maybeTypeOfKind(leftType, 512 /* ESSymbol */) ? left : maybeTypeOfKind(rightType, 512 /* ESSymbol */) ? right :
javascript
{ "resource": "" }
q1551
checkTypeParameter
train
function checkTypeParameter(node) { // Grammar Checking if (node.expression) { grammarErrorOnFirstToken(node.expression, ts.Diagnostics.Type_expected); }
javascript
{ "resource": "" }
q1552
getPromisedType
train
function getPromisedType(promise) { // // { // promise // then( // thenFunction // onfulfilled: ( // onfulfilledParameterType // value: T // valueParameterType // ) => any // ): any; // } // if (isTypeAny(promise)) { return undefined; } if (promise.flags & 131072 /* Reference */) { if (promise.target === tryGetGlobalPromiseType() || promise.target === getGlobalPromiseLikeType()) { return promise.typeArguments[0]; } } var globalPromiseLikeType = getInstantiatedGlobalPromiseLikeType(); if (globalPromiseLikeType === emptyObjectType || !isTypeAssignableTo(promise, globalPromiseLikeType)) { return undefined; } var thenFunction = getTypeOfPropertyOfType(promise, "then"); if (!thenFunction || isTypeAny(thenFunction)) { return undefined; } var thenSignatures = getSignaturesOfType(thenFunction, 0 /* Call */);
javascript
{ "resource": "" }
q1553
checkAsyncFunctionReturnType
train
function checkAsyncFunctionReturnType(node) { if (languageVersion >= 2 /* ES6 */) { var returnType = getTypeFromTypeNode(node.type); return checkCorrectPromiseType(returnType, node.type); } var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType(); if (globalPromiseConstructorLikeType === emptyObjectType) { // If we couldn't resolve the global PromiseConstructorLike type we cannot verify // compatibility with __awaiter. return unknownType; } // As part of our emit for an async function, we will need to emit the entity name of // the return type annotation as an expression. To meet the necessary runtime semantics // for __awaiter, we must also check that the type of the declaration (e.g. the static // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`. // // An example might be (from lib.es6.d.ts): // // interface Promise<T> { ... } // interface PromiseConstructor { // new <T>(...): Promise<T>; // } // declare var Promise: PromiseConstructor; // // When an async function declares a return type annotation of `Promise<T>`, we // need to get the type of the `Promise` variable declaration above, which would // be `PromiseConstructor`. // // The same case applies to a class: // // declare class Promise<T> { // constructor(...); // then<U>(...): Promise<U>; // } // // When we get the type of the `Promise` symbol here, we get the type of the static // side of the `Promise` class, which would be `{ new <T>(...): Promise<T> }`. var promiseType = getTypeFromTypeNode(node.type); if (promiseType === unknownType && compilerOptions.isolatedModules) { // If we are compiling with isolatedModules, we may not be able to resolve the // type as a value. As such, we will just return unknownType; return unknownType; } var promiseConstructor = getNodeLinks(node.type).resolvedSymbol; if (!promiseConstructor || !symbolIsValue(promiseConstructor)) { var typeName = promiseConstructor
javascript
{ "resource": "" }
q1554
checkDecorators
train
function checkDecorators(node) { if (!node.decorators) { return; } // skip this check for nodes that cannot have decorators. These should have already had an error reported by // checkGrammarDecorators. if (!ts.nodeCanBeDecorated(node)) { return; } if (!compilerOptions.experimentalDecorators) { error(node, ts.Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning); } if (compilerOptions.emitDecoratorMetadata) { // we only need to perform these checks if we are emitting serialized type metadata for the target of a decorator. switch (node.kind) { case 221 /* ClassDeclaration */: var constructor = ts.getFirstConstructorWithBody(node); if (constructor) { checkParameterTypeAnnotationsAsExpressions(constructor);
javascript
{ "resource": "" }
q1555
checkParameterInitializer
train
function checkParameterInitializer(node) { if (ts.getRootDeclaration(node).kind !== 142 /* Parameter */) { return; } var func = ts.getContainingFunction(node); visit(node.initializer); function visit(n) { if (ts.isTypeNode(n) || ts.isDeclarationName(n)) { // do not dive in types // skip declaration names (i.e. in object literal expressions) return; } if (n.kind === 172 /* PropertyAccessExpression */) { // skip property names in property access expression return visit(n.expression); } else if (n.kind === 69 /* Identifier */) { // check FunctionLikeDeclaration.locals (stores parameters\function local variable) // if it contains entry with a specified name var symbol = resolveName(n, n.text, 107455 /* Value */ | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); if (!symbol || symbol === unknownSymbol || !symbol.valueDeclaration) { return; } if (symbol.valueDeclaration === node) { error(n, ts.Diagnostics.Parameter_0_cannot_be_referenced_in_its_initializer, ts.declarationNameToString(node.name)); return; } // locals map for function contain both parameters and function locals // so we need to do a bit of extra work to check if reference is legal var enclosingContainer = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); if (enclosingContainer === func) { if (symbol.valueDeclaration.kind === 142 /* Parameter */) { // it is ok to reference parameter in initializer if either // - parameter is located strictly on the left of current parameter declaration
javascript
{ "resource": "" }
q1556
checkTypeParameterListsIdentical
train
function checkTypeParameterListsIdentical(node, symbol) { if (symbol.declarations.length === 1) { return; } var firstDecl; for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { var declaration = _a[_i]; if (declaration.kind === 221 /* ClassDeclaration */ || declaration.kind === 222 /* InterfaceDeclaration */) { if (!firstDecl) { firstDecl = declaration;
javascript
{ "resource": "" }
q1557
checkSourceFileWorker
train
function checkSourceFileWorker(node) { var links = getNodeLinks(node); if (!(links.flags & 1 /* TypeChecked */)) { // If skipLibCheck is enabled, skip type checking if file is a declaration file. // If skipDefaultLibCheck is enabled, skip type checking if file contains a // '/// <reference no-default-lib="true"/>' directive. if (compilerOptions.skipLibCheck && node.isDeclarationFile || compilerOptions.skipDefaultLibCheck && node.hasNoDefaultLib) { return; } // Grammar checking checkGrammarSourceFile(node); potentialThisCollisions.length = 0; deferredNodes = []; deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined; ts.forEach(node.statements, checkSourceElement); checkDeferredNodes(); if (ts.isExternalModule(node)) { registerForUnusedIdentifiersCheck(node); }
javascript
{ "resource": "" }
q1558
isInsideWithStatementBody
train
function isInsideWithStatementBody(node) { if (node) { while (node.parent) { if (node.parent.kind === 212 /* WithStatement */ && node.parent.statement === node) { return true;
javascript
{ "resource": "" }
q1559
copySymbol
train
function copySymbol(symbol, meaning) { if (symbol.flags & meaning) { var id = symbol.name; // We will copy all symbol regardless of its reserved name because // symbolsToArray will check whether the key is a reserved name and
javascript
{ "resource": "" }
q1560
isTypeReferenceIdentifier
train
function isTypeReferenceIdentifier(entityName) { var node = entityName; while (node.parent && node.parent.kind === 139 /* QualifiedName */) { node = node.parent; }
javascript
{ "resource": "" }
q1561
getExportSpecifierLocalTargetSymbol
train
function getExportSpecifierLocalTargetSymbol(node) { return node.parent.parent.moduleSpecifier ? getExternalModuleMember(node.parent.parent, node) :
javascript
{ "resource": "" }
q1562
getReferencedDeclarationWithCollidingName
train
function getReferencedDeclarationWithCollidingName(node) { var symbol = getReferencedValueSymbol(node);
javascript
{ "resource": "" }
q1563
encodeLastRecordedSourceMapSpan
train
function encodeLastRecordedSourceMapSpan() { if (!lastRecordedSourceMapSpan || lastRecordedSourceMapSpan === lastEncodedSourceMapSpan) { return; } var prevEncodedEmittedColumn = lastEncodedSourceMapSpan.emittedColumn; // Line/Comma delimiters if (lastEncodedSourceMapSpan.emittedLine === lastRecordedSourceMapSpan.emittedLine) { // Emit comma to separate the entry if (sourceMapData.sourceMapMappings) { sourceMapData.sourceMapMappings += ","; } } else { // Emit line delimiters for (var encodedLine = lastEncodedSourceMapSpan.emittedLine; encodedLine < lastRecordedSourceMapSpan.emittedLine; encodedLine++) { sourceMapData.sourceMapMappings += ";"; } prevEncodedEmittedColumn = 1; }
javascript
{ "resource": "" }
q1564
getExportDefaultTempVariableName
train
function getExportDefaultTempVariableName() { var baseName = "_default"; if (!(baseName in currentIdentifiers)) { return baseName; } var count = 0; while (true) { count++; var name_23 = baseName + "_"
javascript
{ "resource": "" }
q1565
writeReferencePath
train
function writeReferencePath(referencedFile, addBundledFileReference, emitOnlyDtsFiles) { var declFileName; var addedBundledEmitReference = false; if (ts.isDeclarationFile(referencedFile)) { // Declaration file, use declaration file name declFileName = referencedFile.fileName; } else { // Get the declaration file path ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile, emitOnlyDtsFiles); } if (declFileName) { declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, /*isAbsolutePathAnUrl*/ false); referencesOutput += "/// <reference path=\"" + declFileName + "\" />" + newLine; }
javascript
{ "resource": "" }
q1566
emitFiles
train
function emitFiles(resolver, host, targetSourceFile, emitOnlyDtsFiles) { // emit output for the __extends helper function var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; var assignHelper = "\nvar __assign = (this && this.__assign) || Object.assign || function(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))\n t[p] = s[p];\n }\n return t;\n};"; // emit output for the __decorate helper function var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; // emit output for the __metadata helper function var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; // emit output for the __param helper function var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; var compilerOptions = host.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); var modulekind = ts.getEmitModuleKind(compilerOptions); var sourceMapDataList = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; var emitterDiagnostics = ts.createDiagnosticCollection(); var emitSkipped = false; var newLine = host.getNewLine(); var emitJavaScript = createFileEmitter(); ts.forEachExpectedEmitFile(host, emitFile, targetSourceFile, emitOnlyDtsFiles); return { emitSkipped: emitSkipped, diagnostics: emitterDiagnostics.getDiagnostics(), emittedFiles: emittedFilesList, sourceMaps: sourceMapDataList }; function isUniqueLocalName(name, container) { for (var node = container; ts.isNodeDescendentOf(node, container); node = node.nextContainer) { if (node.locals && name in node.locals) { // We conservatively include alias symbols to cover cases where they're emitted as locals if (node.locals[name].flags & (107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */)) { return false; } } } return true; } function setLabeledJump(state, isBreak, labelText, labelMarker) { if (isBreak) { if (!state.labeledNonLocalBreaks) { state.labeledNonLocalBreaks = ts.createMap(); } state.labeledNonLocalBreaks[labelText] = labelMarker; } else { if (!state.labeledNonLocalContinues) { state.labeledNonLocalContinues = ts.createMap(); } state.labeledNonLocalContinues[labelText] = labelMarker; } } function hoistVariableDeclarationFromLoop(state, declaration) { if (!state.hoistedLocalVariables) { state.hoistedLocalVariables = []; } visit(declaration.name); function visit(node) { if (node.kind === 69 /* Identifier */) { state.hoistedLocalVariables.push(node); } else { for (var _a = 0, _b = node.elements; _a < _b.length; _a++) { var element = _b[_a]; visit(element.name); } } } } function createFileEmitter() { var writer = ts.createTextWriter(newLine); var write = writer.write, writeTextOfNode = writer.writeTextOfNode, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; var sourceMap = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? ts.createSourceMapWriter(host, writer) : ts.getNullSourceMapWriter(); var setSourceFile = sourceMap.setSourceFile, emitStart = sourceMap.emitStart, emitEnd = sourceMap.emitEnd, emitPos = sourceMap.emitPos; var currentSourceFile; var currentText; var currentLineMap; var currentFileIdentifiers; var renamedDependencies; var isEs6Module; var isCurrentFileExternalModule; // name of an exporter function if file is a System external module // System.register([...], function (<exporter>) {...}) // exporting in System modules looks like: // export var x; ... x = 1 // => // var x;... exporter("x", x = 1) var exportFunctionForFile; var contextObjectForFile; var generatedNameSet; var nodeToGeneratedName; var computedPropertyNamesToGeneratedNames; var decoratedClassAliases; var convertedLoopState; var extendsEmitted; var assignEmitted; var decorateEmitted; var paramEmitted; var awaiterEmitted; var tempFlags = 0; var tempVariables; var tempParameters; var externalImports; var exportSpecifiers; var exportEquals; var hasExportStarsToExportValues; var detachedCommentsInfo; /** Sourcemap data that will get encoded */ var sourceMapData; /** Is the file being emitted into its own file */ var isOwnFileEmit; /** If removeComments is true, no leading-comments needed to be emitted **/ var emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos) { } : emitLeadingCommentsOfPositionWorker; var setSourceMapWriterEmit = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? changeSourceMapEmit : function (writer) { }; var moduleEmitDelegates = ts.createMap((_a = {}, _a[ts.ModuleKind.ES6] = emitES6Module, _a[ts.ModuleKind.AMD] = emitAMDModule, _a[ts.ModuleKind.System] = emitSystemModule, _a[ts.ModuleKind.UMD] = emitUMDModule, _a[ts.ModuleKind.CommonJS] = emitCommonJSModule, _a )); var bundleEmitDelegates = ts.createMap((_b = {}, _b[ts.ModuleKind.ES6] = function () { }, _b[ts.ModuleKind.AMD] = emitAMDModule, _b[ts.ModuleKind.System] = emitSystemModule, _b[ts.ModuleKind.UMD] = function () { }, _b[ts.ModuleKind.CommonJS] = function () { }, _b )); return doEmit; function doEmit(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit) { sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); generatedNameSet = ts.createMap(); nodeToGeneratedName = []; decoratedClassAliases = []; isOwnFileEmit = !isBundledEmit; // Emit helpers from all the files if (isBundledEmit && modulekind) { ts.forEach(sourceFiles, emitEmitHelpers); } // Do not call emit directly. It does not set the currentSourceFile. ts.forEach(sourceFiles, emitSourceFile); writeLine(); var sourceMappingURL = sourceMap.getSourceMappingURL(); if (sourceMappingURL) { write("//# " + "sourceMappingURL" + "=" + sourceMappingURL); // Sometimes tools can sometimes see this line as a source mapping url comment } writeEmittedFiles(writer.getText(), jsFilePath, sourceMapFilePath, /*writeByteOrderMark*/ compilerOptions.emitBOM, sourceFiles); // reset the state sourceMap.reset(); writer.reset(); currentSourceFile = undefined; currentText = undefined; currentLineMap = undefined; exportFunctionForFile = undefined; contextObjectForFile = undefined; generatedNameSet = undefined; nodeToGeneratedName = undefined; decoratedClassAliases = undefined; computedPropertyNamesToGeneratedNames = undefined; convertedLoopState = undefined; extendsEmitted = false; decorateEmitted = false; paramEmitted = false; awaiterEmitted = false; assignEmitted = false; tempFlags = 0; tempVariables = undefined; tempParameters = undefined; externalImports = undefined; exportSpecifiers = undefined; exportEquals = undefined; hasExportStarsToExportValues = undefined; detachedCommentsInfo = undefined; sourceMapData = undefined; isEs6Module = false; renamedDependencies = undefined; isCurrentFileExternalModule = false; } function emitSourceFile(sourceFile) { currentSourceFile = sourceFile; currentText = sourceFile.text; currentLineMap = ts.getLineStarts(sourceFile); exportFunctionForFile = undefined; contextObjectForFile = undefined; isEs6Module = sourceFile.symbol && sourceFile.symbol.exports && !!sourceFile.symbol.exports["___esModule"]; renamedDependencies = sourceFile.renamedDependencies; currentFileIdentifiers = sourceFile.identifiers; isCurrentFileExternalModule = ts.isExternalModule(sourceFile); setSourceFile(sourceFile); emitNodeWithCommentsAndWithoutSourcemap(sourceFile); } function isUniqueName(name) { return !resolver.hasGlobalName(name) && !(name in currentFileIdentifiers) && !(name in generatedNameSet); } // Return the next available name in the pattern _a ... _z, _0, _1, ... // TempFlags._i or TempFlags._n may be used to express a preference for that dedicated name. // Note that names generated by makeTempVariableName and makeUniqueName will never conflict. function makeTempVariableName(flags) { if (flags && !(tempFlags & flags)) { var name_24 = flags === 268435456 /* _i */ ? "_i" : "_n"; if (isUniqueName(name_24)) { tempFlags |= flags; return name_24; } } while (true) { var count = tempFlags & 268435455 /* CountMask */; tempFlags++; // Skip over 'i' and 'n' if (count !== 8 && count !== 13) { var name_25 = count < 26 ? "_" + String.fromCharCode(97 /* a */ + count) : "_" + (count - 26); if (isUniqueName(name_25)) { return name_25; } } } } // Generate a name that is unique within the current file and doesn't conflict with any names // in global scope. The name is formed by adding an '_n' suffix to the specified base name, // where n is a positive integer. Note that names generated by makeTempVariableName and // makeUniqueName are guaranteed to never conflict. function makeUniqueName(baseName) { // Find the first unique 'name_n', where n is a positive number if (baseName.charCodeAt(baseName.length - 1) !== 95 /* _ */) { baseName += "_"; } var i = 1; while (true) { var generatedName = baseName + i; if (isUniqueName(generatedName)) { return generatedNameSet[generatedName] = generatedName; } i++; } } function generateNameForModuleOrEnum(node) { var name = node.name.text; // Use module/enum name itself if it is unique, otherwise make a unique variation return isUniqueLocalName(name, node) ? name : makeUniqueName(name); } function generateNameForImportOrExportDeclaration(node) { var expr = ts.getExternalModuleName(node); var baseName = expr.kind === 9 /* StringLiteral */ ? ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; return makeUniqueName(baseName); } function generateNameForExportDefault() { return makeUniqueName("default"); } function generateNameForClassExpression() { return makeUniqueName("class"); } function generateNameForNode(node) { switch (node.kind) { case 69 /* Identifier */: return makeUniqueName(node.text); case 225 /* ModuleDeclaration */: case 224 /* EnumDeclaration */: return generateNameForModuleOrEnum(node); case 230 /* ImportDeclaration */: case 236 /* ExportDeclaration */: return generateNameForImportOrExportDeclaration(node); case 220 /* FunctionDeclaration */: case 221 /* ClassDeclaration */: case 235 /* ExportAssignment */: return generateNameForExportDefault(); case 192 /* ClassExpression */: return generateNameForClassExpression(); default: ts.Debug.fail(); } } function getGeneratedNameForNode(node) { var id = ts.getNodeId(node); return nodeToGeneratedName[id] || (nodeToGeneratedName[id] = ts.unescapeIdentifier(generateNameForNode(node))); } /** Write emitted output to disk */ function writeEmittedFiles(emitOutput, jsFilePath, sourceMapFilePath, writeByteOrderMark, sourceFiles) { if (compilerOptions.sourceMap && !compilerOptions.inlineSourceMap) { ts.writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false, sourceFiles); } if (sourceMapDataList) { sourceMapDataList.push(sourceMap.getSourceMapData()); } ts.writeFile(host, emitterDiagnostics, jsFilePath, emitOutput, writeByteOrderMark, sourceFiles); } // Create a temporary variable with a unique unused name. function createTempVariable(flags) { var result = ts.createSynthesizedNode(69 /* Identifier */); result.text = makeTempVariableName(flags); return result; } function recordTempDeclaration(name) { if (!tempVariables) { tempVariables = []; } tempVariables.push(name); } function createAndRecordTempVariable(flags) { var temp = createTempVariable(flags); recordTempDeclaration(temp); return temp; } function emitTempDeclarations(newLine) { if (tempVariables) { if (newLine) { writeLine(); } else { write(" "); } write("var "); emitCommaList(tempVariables); write(";"); } } /** Emit the text for the given token that comes after startPos * This by default writes the text provided with the given tokenKind * but if optional emitFn callback is provided the text is emitted using the callback instead of default text * @param tokenKind the kind of the token to search and emit * @param startPos the position in the source to start searching for the token * @param emitFn if given will be invoked to emit the text instead of actual token emit */ function emitToken(tokenKind, startPos, emitFn) { var tokenStartPos = ts.skipTrivia(currentText, startPos); emitPos(tokenStartPos); var tokenString = ts.tokenToString(tokenKind); if (emitFn) { emitFn(); } else { write(tokenString); } var tokenEndPos = tokenStartPos + tokenString.length; emitPos(tokenEndPos); return tokenEndPos; } function emitOptional(prefix, node) { if (node) { write(prefix); emit(node); } } function emitParenthesizedIf(node, parenthesized) { if (parenthesized) { write("("); } emit(node); if (parenthesized) { write(")"); } } function emitLinePreservingList(parent, nodes, allowTrailingComma, spacesBetweenBraces) { ts.Debug.assert(nodes.length > 0); increaseIndent(); if (nodeStartPositionsAreOnSameLine(parent, nodes[0])) { if (spacesBetweenBraces) { write(" "); } } else { writeLine(); } for (var i = 0, n = nodes.length; i < n; i++) { if (i) { if (nodeEndIsOnSameLineAsNodeStart(nodes[i - 1], nodes[i])) { write(", "); } else { write(","); writeLine(); } } emit(nodes[i]); } if (nodes.hasTrailingComma && allowTrailingComma) { write(","); } decreaseIndent(); if (nodeEndPositionsAreOnSameLine(parent, ts.lastOrUndefined(nodes))) { if (spacesBetweenBraces) { write(" "); } } else { writeLine(); } } function emitList(nodes, start, count, multiLine, trailingComma, leadingComma, noTrailingNewLine, emitNode) { if (!emitNode) { emitNode = emit; } for (var i = 0; i < count; i++) { if (multiLine) { if (i || leadingComma) { write(","); } writeLine(); } else { if (i || leadingComma) { write(", "); } } var node = nodes[start + i]; // This emitting is to make sure we emit following comment properly // ...(x, /*comment1*/ y)... // ^ => node.pos // "comment1" is not considered leading comment for "y" but rather // considered as trailing comment of the previous node. emitTrailingCommentsOfPosition(node.pos); emitNode(node); leadingComma = true; } if (trailingComma) { write(","); } if (multiLine && !noTrailingNewLine) { writeLine(); } return count; } function emitCommaList(nodes) { if (nodes) { emitList(nodes, 0, nodes.length, /*multiLine*/ false, /*trailingComma*/ false); } } function emitLines(nodes) { emitLinesStartingAt(nodes, /*startIndex*/ 0); } function emitLinesStartingAt(nodes, startIndex) { for (var i = startIndex; i < nodes.length; i++) { writeLine(); emit(nodes[i]); } } function isBinaryOrOctalIntegerLiteral(node, text) { if (node.kind === 8 /* NumericLiteral */ && text.length > 1) { switch (text.charCodeAt(1)) { case 98 /* b */: case 66 /* B */: case 111 /* o */: case 79 /* O */: return true; } } return false; } function emitLiteral(node) { var text = getLiteralText(node); if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) && (node.kind === 9 /* StringLiteral */ || ts.isTemplateLiteralKind(node.kind))) { writer.writeLiteral(text); } else if (languageVersion < 2 /* ES6 */ && isBinaryOrOctalIntegerLiteral(node, text)) { write(node.text); } else { write(text); } } function getLiteralText(node) { // Any template literal or string literal with an extended escape // (e.g. "\u{0067}") will need to be downleveled as a escaped string literal. if (languageVersion < 2 /* ES6 */ && (ts.isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) { return getQuotedEscapedLiteralText('"', node.text, '"'); } // If we don't need to downlevel and we can reach the original source text using // the node's parent reference, then simply get the text as it was originally written. if (node.parent) { return ts.getTextOfNodeFromSourceText(currentText, node); } // If we can't reach the original source text, use the canonical form if it's a number, // or an escaped quoted form of the original text if it's string-like. switch (node.kind) { case 9 /* StringLiteral */: return getQuotedEscapedLiteralText('"', node.text, '"'); case 11 /* NoSubstitutionTemplateLiteral */: return getQuotedEscapedLiteralText("`", node.text, "`"); case 12 /* TemplateHead */: return getQuotedEscapedLiteralText("`", node.text, "${"); case 13 /* TemplateMiddle */: return getQuotedEscapedLiteralText("}", node.text, "${"); case 14 /* TemplateTail */: return getQuotedEscapedLiteralText("}", node.text, "`"); case 8 /* NumericLiteral */: return node.text; } ts.Debug.fail("Literal kind '" + node.kind + "' not accounted for."); } function getQuotedEscapedLiteralText(leftQuote, text, rightQuote) { return leftQuote + ts.escapeNonAsciiCharacters(ts.escapeString(text)) + rightQuote; } function emitDownlevelRawTemplateLiteral(node) { // Find original source text, since we need to emit the raw strings of the tagged template. // The raw strings contain the (escaped) strings of what the user wrote. // Examples: `\n` is converted to "\\n", a template string with a newline to "\n". var text = ts.getTextOfNodeFromSourceText(currentText, node); // text contains the original source, it will also contain quotes ("`"), dollar signs and braces ("${" and "}"), // thus we need to remove those characters. // First template piece starts with "`", others with "}" // Last template piece ends with "`", others with "${" var isLast = node.kind === 11 /* NoSubstitutionTemplateLiteral */ || node.kind === 14 /* TemplateTail */; text = text.substring(1, text.length - (isLast ? 1 : 2)); // Newline normalization: // ES6 Spec 11.8.6.1 - Static Semantics of TV's and TRV's // <CR><LF> and <CR> LineTerminatorSequences are normalized to <LF> for both TV and TRV. text = text.replace(/\r\n?/g, "\n"); text = ts.escapeString(text); write("\"" + text + "\""); } function emitDownlevelTaggedTemplateArray(node, literalEmitter) { write("["); if (node.template.kind === 11 /* NoSubstitutionTemplateLiteral */) { literalEmitter(node.template); } else { literalEmitter(node.template.head); ts.forEach(node.template.templateSpans, function (child) { write(", "); literalEmitter(child.literal); }); } write("]"); } function emitDownlevelTaggedTemplate(node) { var tempVariable = createAndRecordTempVariable(0 /* Auto */); write("("); emit(tempVariable); write(" = "); emitDownlevelTaggedTemplateArray(node, emit); write(", "); emit(tempVariable); write(".raw = "); emitDownlevelTaggedTemplateArray(node, emitDownlevelRawTemplateLiteral); write(", "); emitParenthesizedIf(node.tag, needsParenthesisForPropertyAccessOrInvocation(node.tag)); write("("); emit(tempVariable); // Now we emit the expressions if (node.template.kind === 189 /* TemplateExpression */) { ts.forEach(node.template.templateSpans, function (templateSpan) { write(", "); var needsParens = templateSpan.expression.kind === 187 /* BinaryExpression */ && templateSpan.expression.operatorToken.kind === 24 /* CommaToken */; emitParenthesizedIf(templateSpan.expression, needsParens); }); } write("))"); } function emitTemplateExpression(node) { // In ES6 mode and above, we can simply emit each portion of a template in order, but in // ES3 & ES5 we must convert the template expression into a series of string concatenations. if (languageVersion >= 2 /* ES6 */) { ts.forEachChild(node, emit); return; } var emitOuterParens = ts.isExpression(node.parent) && templateNeedsParens(node, node.parent); if (emitOuterParens) { write("("); } var headEmitted = false; if (shouldEmitTemplateHead()) { emitLiteral(node.head); headEmitted = true; } for (var i = 0, n = node.templateSpans.length; i < n; i++) { var templateSpan = node.templateSpans[i]; // Check if the expression has operands and binds its operands less closely than binary '+'. // If it does, we need to wrap the expression in parentheses. Otherwise, something like // `abc${ 1 << 2 }` // becomes // "abc" + 1 << 2 + "" // which is really // ("abc" + 1) << (2 + "") // rather than // "abc" + (1 << 2) + "" var needsParens = templateSpan.expression.kind !== 178 /* ParenthesizedExpression */ && comparePrecedenceToBinaryPlus(templateSpan.expression) !== 1 /* GreaterThan */; if (i > 0 || headEmitted) { // If this is the first span and the head was not emitted, then this templateSpan's // expression will be the first to be emitted. Don't emit the preceding ' + ' in that // case. write(" + "); } emitParenthesizedIf(templateSpan.expression, needsParens); // Only emit if the literal is non-empty. // The binary '+' operator is left-associative, so the first string concatenation // with the head will force the result up to this point to be a string. // Emitting a '+ ""' has no semantic effect for middles and tails. if (templateSpan.literal.text.length !== 0) { write(" + "); emitLiteral(templateSpan.literal); } } if (emitOuterParens) { write(")"); } function shouldEmitTemplateHead() { // If this expression has an empty head literal and the first template span has a non-empty // literal, then emitting the empty head literal is not necessary. // `${ foo } and ${ bar }` // can be emitted as // foo + " and " + bar // This is because it is only required that one of the first two operands in the emit // output must be a string literal, so that the other operand and all following operands // are forced into strings. // // If the first template span has an empty literal, then the head must still be emitted. // `${ foo }${ bar }` // must still be emitted as // "" + foo + bar // There is always atleast one templateSpan in this code path, since // NoSubstitutionTemplateLiterals are directly emitted via emitLiteral() ts.Debug.assert(node.templateSpans.length !== 0); return node.head.text.length !== 0 || node.templateSpans[0].literal.text.length === 0; } function templateNeedsParens(template, parent) { switch (parent.kind) { case 174 /* CallExpression */: case 175 /* NewExpression */: return parent.expression === template; case 176 /* TaggedTemplateExpression */: case 178 /* ParenthesizedExpression */: return false; default: return comparePrecedenceToBinaryPlus(parent) !== -1 /* LessThan */; } } /** * Returns whether the expression has lesser, greater, * or equal precedence to the binary '+' operator */ function comparePrecedenceToBinaryPlus(expression) { // All binary expressions have lower precedence than '+' apart from '*', '/', and '%' // which have greater precedence and '-' which has equal precedence. // All unary operators have a higher precedence apart from yield. // Arrow functions and conditionals have a lower precedence, // although we convert the former into regular function expressions in ES5 mode, // and in ES6 mode this function won't get called anyway. // // TODO (drosen): Note that we need to account for the upcoming 'yield' and // spread ('...') unary operators that are anticipated for ES6. switch (expression.kind) { case 187 /* BinaryExpression */: switch (expression.operatorToken.kind) { case 37 /* AsteriskToken */: case 39 /* SlashToken */: case 40 /* PercentToken */: return 1 /* GreaterThan */; case 35 /* PlusToken */: case 36 /* MinusToken */: return 0 /* EqualTo */; default: return -1 /* LessThan */; } case 190 /* YieldExpression */: case 188 /* ConditionalExpression */: return -1 /* LessThan */; default: return 1 /* GreaterThan */; } } } function emitTemplateSpan(span) { emit(span.expression); emit(span.literal); } function jsxEmitReact(node) { /// Emit a tag name, which is either '"div"' for lower-cased names, or /// 'Div' for upper-cased or dotted names function emitTagName(name) { if (name.kind === 69 /* Identifier */ && ts.isIntrinsicJsxName(name.text)) { write('"'); emit(name); write('"'); } else { emit(name); } } /// Emit an attribute name, which is quoted if it needs to be quoted. Because /// these emit into an object literal property name, we don't need to be worried /// about keywords, just non-identifier characters function emitAttributeName(name) { if (/^[A-Za-z_]\w*$/.test(name.text)) { emit(name); } else { write('"'); emit(name); write('"'); } } /// Emit an name/value pair for an attribute (e.g. "x: 3") function emitJsxAttribute(node) { emitAttributeName(node.name); write(": "); if (node.initializer) { emit(node.initializer); } else { write("true"); } } function emitJsxElement(openingNode, children) { var syntheticReactRef = ts.createSynthesizedNode(69 /* Identifier */); syntheticReactRef.text = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React"; syntheticReactRef.parent = openingNode; // Call React.createElement(tag, ... emitLeadingComments(openingNode); emitExpressionIdentifier(syntheticReactRef); write(".createElement("); emitTagName(openingNode.tagName); write(", "); // Attribute list if (openingNode.attributes.length === 0) { // When there are no attributes, React wants "null" write("null"); } else { // Either emit one big object literal (no spread attribs), or // a call to the __assign helper var attrs = openingNode.attributes; if (ts.forEach(attrs, function (attr) { return attr.kind === 247 /* JsxSpreadAttribute */; })) { write("__assign("); var haveOpenedObjectLiteral = false; for (var i = 0; i < attrs.length; i++) { if (attrs[i].kind === 247 /* JsxSpreadAttribute */) { // If this is the first argument, we need to emit a {} as the first argument if (i === 0) { write("{}, "); } if (haveOpenedObjectLiteral) { write("}"); haveOpenedObjectLiteral = false; } if (i > 0) { write(", "); } emit(attrs[i].expression); } else { ts.Debug.assert(attrs[i].kind === 246 /* JsxAttribute */); if (haveOpenedObjectLiteral) { write(", "); } else { haveOpenedObjectLiteral = true; if (i > 0) { write(", "); } write("{"); } emitJsxAttribute(attrs[i]); } } if (haveOpenedObjectLiteral) write("}"); write(")"); // closing paren to React.__spread( } else { // One object literal with all the attributes in them write("{"); for (var i = 0, n = attrs.length; i < n; i++) { if (i > 0) { write(", "); } emitJsxAttribute(attrs[i]); } write("}"); } } // Children if (children) { var firstChild = void 0; var multipleEmittableChildren = false; for (var i = 0, n = children.length; i < n; i++) { var jsxChild = children[i]; if (isJsxChildEmittable(jsxChild)) { // we need to decide whether to emit in single line or multiple lines as indented list // store firstChild reference, if we see another emittable child, then emit accordingly if (!firstChild) { write(", "); firstChild = jsxChild; } else { // more than one emittable child, emit indented list if (!multipleEmittableChildren) { multipleEmittableChildren = true; increaseIndent(); writeLine(); emit(firstChild); } write(", "); writeLine(); emit(jsxChild); } } } if (multipleEmittableChildren) { decreaseIndent(); } else if (firstChild) { if (firstChild.kind !== 241 /* JsxElement */ && firstChild.kind !== 242 /* JsxSelfClosingElement */) { emit(firstChild); } else { // If the only child is jsx element, put it on a new indented line increaseIndent(); writeLine(); emit(firstChild); writeLine(); decreaseIndent(); } } } // Closing paren write(")"); // closes "React.createElement(" emitTrailingComments(openingNode); } if (node.kind === 241 /* JsxElement */) { emitJsxElement(node.openingElement, node.children); } else { ts.Debug.assert(node.kind === 242 /* JsxSelfClosingElement */); emitJsxElement(node); } } function jsxEmitPreserve(node) { function emitJsxAttribute(node) { emit(node.name); if (node.initializer) { write("="); emit(node.initializer); } } function emitJsxSpreadAttribute(node) { write("{..."); emit(node.expression); write("}"); } function emitAttributes(attribs) { for (var i = 0, n = attribs.length; i < n; i++) { if (i > 0) { write(" "); } if (attribs[i].kind === 247 /* JsxSpreadAttribute */) { emitJsxSpreadAttribute(attribs[i]); } else { ts.Debug.assert(attribs[i].kind === 246 /* JsxAttribute */); emitJsxAttribute(attribs[i]); } } } function emitJsxOpeningOrSelfClosingElement(node) { write("<"); emit(node.tagName); if (node.attributes.length > 0 || (node.kind === 242 /* JsxSelfClosingElement */)) { write(" "); } emitAttributes(node.attributes); if (node.kind === 242 /* JsxSelfClosingElement */) { write("/>"); } else { write(">"); } } function emitJsxClosingElement(node) { write("</"); emit(node.tagName); write(">"); } function emitJsxElement(node) { emitJsxOpeningOrSelfClosingElement(node.openingElement); for (var i = 0, n = node.children.length; i < n; i++) { emit(node.children[i]); } emitJsxClosingElement(node.closingElement); } if (node.kind === 241 /* JsxElement */) { emitJsxElement(node); } else { ts.Debug.assert(node.kind === 242 /* JsxSelfClosingElement */); emitJsxOpeningOrSelfClosingElement(node); } } // This function specifically handles numeric/string literals for enum and accessor 'identifiers'. // In a sense, it does not actually emit identifiers as much as it declares a name for a specific property. // For example, this is utilized when feeding in a result to Object.defineProperty. function emitExpressionForPropertyName(node) { ts.Debug.assert(node.kind !== 169 /* BindingElement */); if (node.kind === 9 /* StringLiteral */) { emitLiteral(node); } else if (node.kind === 140 /* ComputedPropertyName */) { // if this is a decorated computed property, we will need to capture the result // of the property expression so that we can apply decorators later. This is to ensure // we don't introduce unintended side effects: // // class C { // [_a = x]() { } // } // // The emit for the decorated computed property decorator is: // // __decorate([dec], C.prototype, _a, Object.getOwnPropertyDescriptor(C.prototype, _a)); // if (ts.nodeIsDecorated(node.parent)) { if (!computedPropertyNamesToGeneratedNames) { computedPropertyNamesToGeneratedNames = []; } var generatedName = computedPropertyNamesToGeneratedNames[ts.getNodeId(node)]; if (generatedName) { // we have already generated a variable for this node, write that value instead. write(generatedName); return; } generatedName = createAndRecordTempVariable(0 /* Auto */).text; computedPropertyNamesToGeneratedNames[ts.getNodeId(node)] = generatedName; write(generatedName); write(" = "); } emit(node.expression); } else { write('"'); if (node.kind === 8 /* NumericLiteral */) { write(node.text); } else { writeTextOfNode(currentText, node); } write('"'); } } function isExpressionIdentifier(node) { var parent = node.parent; switch (parent.kind) { case 170 /* ArrayLiteralExpression */: case 195 /* AsExpression */: case 184 /* AwaitExpression */: case 187 /* BinaryExpression */: case 174 /* CallExpression */: case 249 /* CaseClause */: case 140 /* ComputedPropertyName */: case 188 /* ConditionalExpression */: case 143 /* Decorator */: case 181 /* DeleteExpression */: case 204 /* DoStatement */: case 173 /* ElementAccessExpression */: case 235 /* ExportAssignment */: case 202 /* ExpressionStatement */: case 194 /* ExpressionWithTypeArguments */: case 206 /* ForStatement */: case 207 /* ForInStatement */: case 208 /* ForOfStatement */: case 203 /* IfStatement */: case 245 /* JsxClosingElement */: case 242 /* JsxSelfClosingElement */: case 243 /* JsxOpeningElement */: case 247 /* JsxSpreadAttribute */: case 248 /* JsxExpression */: case 175 /* NewExpression */: case 196 /* NonNullExpression */: case 178 /* ParenthesizedExpression */: case 186 /* PostfixUnaryExpression */: case 185 /* PrefixUnaryExpression */: case 211 /* ReturnStatement */: case 254 /* ShorthandPropertyAssignment */: case 191 /* SpreadElementExpression */: case 213 /* SwitchStatement */: case 176 /* TaggedTemplateExpression */: case 197 /* TemplateSpan */: case 215 /* ThrowStatement */: case 177 /* TypeAssertionExpression */: case 182 /* TypeOfExpression */: case 183 /* VoidExpression */: case 205 /* WhileStatement */: case 212 /* WithStatement */: case 190 /* YieldExpression */: return true; case 169 /* BindingElement */: case 255 /* EnumMember */: case 142 /* Parameter */: case 253 /* PropertyAssignment */: case 145 /* PropertyDeclaration */: case 218 /* VariableDeclaration */: return parent.initializer === node; case 172 /* PropertyAccessExpression */: return parent.expression === node; case 180 /* ArrowFunction */: case 179 /* FunctionExpression */: return parent.body === node; case 229 /* ImportEqualsDeclaration */: return parent.moduleReference === node; case 139 /* QualifiedName */: return parent.left === node; } return false; } function emitExpressionIdentifier(node) { var container = resolver.getReferencedExportContainer(node); if (container) { if (container.kind === 256 /* SourceFile */) { // Identifier references module export if (modulekind !== ts.ModuleKind.ES6 && modulekind !== ts.ModuleKind.System) { write("exports."); } } else { // Identifier references namespace export write(getGeneratedNameForNode(container)); write("."); } } else { if (modulekind !== ts.ModuleKind.ES6) { var declaration = resolver.getReferencedImportDeclaration(node); if (declaration) { if (declaration.kind === 231 /* ImportClause */) { // Identifier references default import write(getGeneratedNameForNode(declaration.parent)); write(languageVersion === 0 /* ES3 */ ? '["default"]' : ".default"); return; } else if (declaration.kind === 234 /* ImportSpecifier */) { // Identifier references named import write(getGeneratedNameForNode(declaration.parent.parent.parent)); var name_26 = declaration.propertyName || declaration.name; var identifier = ts.getTextOfNodeFromSourceText(currentText, name_26); if (languageVersion === 0 /* ES3 */ && identifier === "default") { write('["default"]'); } else { write("."); write(identifier); } return; } } } if (languageVersion < 2 /* ES6 */) { var declaration = resolver.getReferencedDeclarationWithCollidingName(node); if (declaration) { write(getGeneratedNameForNode(declaration.name)); return; } } else if (resolver.getNodeCheckFlags(node) & 1048576 /* BodyScopedClassBinding */) { // Due to the emit for class decorators, any reference to the class from inside of the class body // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind // behavior of class names in ES6. var declaration = resolver.getReferencedValueDeclaration(node); if (declaration) { var classAlias = decoratedClassAliases[ts.getNodeId(declaration)]; if (classAlias !== undefined) { write(classAlias); return; } } } } if (ts.nodeIsSynthesized(node)) { write(node.text); } else { writeTextOfNode(currentText, node); } } function isNameOfNestedBlockScopedRedeclarationOrCapturedBinding(node) { if (languageVersion < 2 /* ES6 */) { var parent_13 = node.parent; switch (parent_13.kind) { case 169 /* BindingElement */: case 221 /* ClassDeclaration */: case 224 /* EnumDeclaration */: case 218 /* VariableDeclaration */: return parent_13.name === node && resolver.isDeclarationWithCollidingName(parent_13); } } return false; } function getClassExpressionInPropertyAccessInStaticPropertyDeclaration(node) { if (languageVersion >= 2 /* ES6 */) { var parent_14 = node.parent; if (parent_14.kind === 172 /* PropertyAccessExpression */ && parent_14.expression === node) { parent_14 = parent_14.parent; while (parent_14 && parent_14.kind !== 145 /* PropertyDeclaration */) { parent_14 = parent_14.parent; } return parent_14 && parent_14.kind === 145 /* PropertyDeclaration */ && (parent_14.flags & 32 /* Static */) !== 0 && parent_14.parent.kind === 192 /* ClassExpression */ ? parent_14.parent : undefined; } } return undefined; } function emitIdentifier(node) { if (convertedLoopState) { if (node.text == "arguments" && resolver.isArgumentsLocalBinding(node)) { // in converted loop body arguments cannot be used directly. var name_27 = convertedLoopState.argumentsName || (convertedLoopState.argumentsName = makeUniqueName("arguments")); write(name_27); return; } } if (!node.parent) { write(node.text); } else if (isExpressionIdentifier(node)) { var classExpression = getClassExpressionInPropertyAccessInStaticPropertyDeclaration(node); if (classExpression) { var declaration = resolver.getReferencedValueDeclaration(node); if (declaration === classExpression) { write(getGeneratedNameForNode(declaration.name)); return; } } emitExpressionIdentifier(node); } else if (isNameOfNestedBlockScopedRedeclarationOrCapturedBinding(node)) { write(getGeneratedNameForNode(node)); } else if (ts.nodeIsSynthesized(node)) { write(node.text); } else { writeTextOfNode(currentText, node); } } function emitThis(node) { if (resolver.getNodeCheckFlags(node) & 2 /* LexicalThis */) { write("_this"); } else if (convertedLoopState) { write(convertedLoopState.thisName || (convertedLoopState.thisName = makeUniqueName("this"))); } else { write("this"); } } function emitSuper(node) { if (languageVersion >= 2 /* ES6 */) { write("super"); } else { var flags = resolver.getNodeCheckFlags(node); if (flags & 256 /* SuperInstance */) { write("_super.prototype"); } else { write("_super"); } } } function emitObjectBindingPattern(node) { write("{ "); var elements = node.elements; emitList(elements, 0, elements.length, /*multiLine*/ false, /*trailingComma*/ elements.hasTrailingComma); write(" }"); } function emitArrayBindingPattern(node) { write("["); var elements = node.elements; emitList(elements, 0, elements.length, /*multiLine*/ false, /*trailingComma*/ elements.hasTrailingComma); write("]"); } function emitBindingElement(node) { if (node.propertyName) { emit(node.propertyName); write(": "); } if (node.dotDotDotToken) { write("..."); } if (ts.isBindingPattern(node.name)) { emit(node.name); } else { emitModuleMemberName(node); } emitOptional(" = ", node.initializer); } function emitSpreadElementExpression(node) { write("..."); emit(node.expression); } function emitYieldExpression(node) { write(ts.tokenToString(114 /* YieldKeyword */)); if (node.asteriskToken) { write("*"); } if (node.expression) { write(" "); emit(node.expression); } } function emitAwaitExpression(node) { var needsParenthesis = needsParenthesisForAwaitExpressionAsYield(node); if (needsParenthesis) { write("("); } write(ts.tokenToString(114 /* YieldKeyword */)); write(" "); emit(node.expression); if (needsParenthesis) { write(")"); } } function needsParenthesisForAwaitExpressionAsYield(node) { if (node.parent.kind === 187 /* BinaryExpression */ && !ts.isAssignmentOperator(node.parent.operatorToken.kind)) { return true; } else if (node.parent.kind === 188 /* ConditionalExpression */ && node.parent.condition === node) { return true; } else if (node.parent.kind === 185 /* PrefixUnaryExpression */ || node.parent.kind === 181 /* DeleteExpression */ || node.parent.kind === 182 /* TypeOfExpression */ || node.parent.kind === 183 /* VoidExpression */) { return true; } return false; } function needsParenthesisForPropertyAccessOrInvocation(node) { switch (node.kind) { case 69 /* Identifier */: case 170 /* ArrayLiteralExpression */: case 172 /* PropertyAccessExpression */: case 173 /* ElementAccessExpression */: case 174 /* CallExpression */: case 178 /* ParenthesizedExpression */: // This list is not exhaustive and only includes those cases that are relevant // to the check in emitArrayLiteral. More cases can be added as needed. return false; } return true; } function emitListWithSpread(elements, needsUniqueCopy, multiLine, trailingComma, useConcat) { var pos = 0; var group = 0; var length = elements.length; while (pos < length) { // Emit using the pattern <group0>.concat(<group1>, <group2>, ...) if (group === 1 && useConcat) { write(".concat("); } else if (group > 0) { write(", "); } var e = elements[pos]; if (e.kind === 191 /* SpreadElementExpression */) { e = e.expression; emitParenthesizedIf(e, /*parenthesized*/ group === 0 && needsParenthesisForPropertyAccessOrInvocation(e)); pos++; if (pos === length && group === 0 && needsUniqueCopy && e.kind !== 170 /* ArrayLiteralExpression */) { write(".slice()"); } } else { var i = pos; while (i < length && elements[i].kind !== 191 /* SpreadElementExpression */) { i++; } write("["); if (multiLine) { increaseIndent(); } emitList(elements, pos, i - pos, multiLine, trailingComma && i === length); if (multiLine) { decreaseIndent(); } write("]"); pos = i; } group++; } if (group > 1) { if (useConcat) { write(")"); } } } function isSpreadElementExpression(node) { return node.kind === 191 /* SpreadElementExpression */; } function emitArrayLiteral(node) { var elements = node.elements; if (elements.length === 0) { write("[]"); } else if (languageVersion >= 2 /* ES6 */ || !ts.forEach(elements, isSpreadElementExpression)) { write("["); emitLinePreservingList(node, node.elements, elements.hasTrailingComma, /*spacesBetweenBraces*/ false); write("]"); } else { emitListWithSpread(elements, /*needsUniqueCopy*/ true, /*multiLine*/ node.multiLine, /*trailingComma*/ elements.hasTrailingComma, /*useConcat*/ true); } } function emitObjectLiteralBody(node, numElements) { if (numElements === 0) { write("{}"); return; } write("{"); if (numElements > 0) { var properties = node.properties; // If we are not doing a downlevel transformation for object literals, // then try to preserve the original shape of the object literal. // Otherwise just try to preserve the formatting. if (numElements === properties.length) { emitLinePreservingList(node, properties, /*allowTrailingComma*/ languageVersion >= 1 /* ES5 */, /*spacesBetweenBraces*/ true); } else { var multiLine = node.multiLine; if (!multiLine) { write(" "); } else { increaseIndent(); } emitList(properties, 0, numElements, /*multiLine*/ multiLine, /*trailingComma*/ false); if (!multiLine) { write(" "); } else { decreaseIndent(); } } } write("}"); } function emitDownlevelObjectLiteralWithComputedProperties(node, firstComputedPropertyIndex) { var multiLine = node.multiLine; var properties = node.properties; write("("); if (multiLine) { increaseIndent(); } // For computed properties, we need to create a unique handle to the object // literal so we can modify it without risking internal assignments tainting the object. var tempVar = createAndRecordTempVariable(0 /* Auto */); // Write out the first non-computed properties // (or all properties if none of them are computed), // then emit the rest through indexing on the temp variable. emit(tempVar); write(" = "); emitObjectLiteralBody(node, firstComputedPropertyIndex); for (var i = firstComputedPropertyIndex, n = properties.length; i < n; i++) { var property = properties[i]; if (property.kind === 149 /* GetAccessor */ || property.kind === 150 /* SetAccessor */) { // TODO (drosen): Reconcile with 'emitMemberFunctions'. var accessors = ts.getAllAccessorDeclarations(node.properties, property); if (property !== accessors.firstAccessor) { continue; } writeComma(); emitStart(property); write("Object.defineProperty("); emit(tempVar); write(", "); emitStart(property.name); emitExpressionForPropertyName(property.name); emitEnd(property.name); write(", {"); increaseIndent(); if (accessors.getAccessor) { writeLine(); emitLeadingComments(accessors.getAccessor); write("get: "); emitStart(accessors.getAccessor); write("function "); emitSignatureAndBody(accessors.getAccessor); emitEnd(accessors.getAccessor); emitTrailingComments(accessors.getAccessor); write(","); } if (accessors.setAccessor) { writeLine(); emitLeadingComments(accessors.setAccessor); write("set: "); emitStart(accessors.setAccessor); write("function "); emitSignatureAndBody(accessors.setAccessor); emitEnd(accessors.setAccessor); emitTrailingComments(accessors.setAccessor); write(","); } writeLine(); write("enumerable: true,"); writeLine(); write("configurable: true"); decreaseIndent(); writeLine(); write("})"); emitEnd(property); } else { writeComma(); emitStart(property); emitLeadingComments(property); emitStart(property.name); emit(tempVar); emitMemberAccessForPropertyName(property.name); emitEnd(property.name); write(" = "); if (property.kind === 253 /* PropertyAssignment */) { emit(property.initializer); } else if (property.kind === 254 /* ShorthandPropertyAssignment */) { emitExpressionIdentifier(property.name); } else if (property.kind === 147 /* MethodDeclaration */) { emitFunctionDeclaration(property); } else { ts.Debug.fail("ObjectLiteralElement type not accounted for: " + property.kind); } emitEnd(property); } } writeComma(); emit(tempVar); if (multiLine) { decreaseIndent(); writeLine(); } write(")"); function writeComma() { if (multiLine) { write(","); writeLine(); } else { write(", "); } } } function emitObjectLiteral(node) { var properties = node.properties; if (languageVersion < 2 /* ES6 */) { var numProperties = properties.length; // Find the first computed property. // Everything until that point can be emitted as part of the initial object literal. var numInitialNonComputedProperties = numProperties; for (var i = 0, n = properties.length; i < n; i++) { if (properties[i].name.kind === 140 /* ComputedPropertyName */) { numInitialNonComputedProperties = i; break; } } var hasComputedProperty = numInitialNonComputedProperties !== properties.length; if (hasComputedProperty) { emitDownlevelObjectLiteralWithComputedProperties(node, numInitialNonComputedProperties); return; } } // Ordinary case: either the object has no computed properties // or we're compiling with an ES6+ target. emitObjectLiteralBody(node, properties.length); } function createBinaryExpression(left, operator, right, startsOnNewLine) { var result = ts.createSynthesizedNode(187 /* BinaryExpression */, startsOnNewLine); result.operatorToken = ts.createSynthesizedNode(operator); result.left = left; result.right = right; return result; } function createPropertyAccessExpression(expression, name) { var result = ts.createSynthesizedNode(172 /* PropertyAccessExpression */); result.expression = parenthesizeForAccess(expression); result.name = name; return result; } function createElementAccessExpression(expression, argumentExpression) { var result = ts.createSynthesizedNode(173 /* ElementAccessExpression */); result.expression = parenthesizeForAccess(expression); result.argumentExpression = argumentExpression; return result; } function parenthesizeForAccess(expr) { // When diagnosing whether the expression needs parentheses, the decision should be based // on the innermost expression in a chain of nested type assertions. while (expr.kind === 177 /* TypeAssertionExpression */ || expr.kind === 195 /* AsExpression */ || expr.kind === 196 /* NonNullExpression */) { expr = expr.expression; } // isLeftHandSideExpression is almost the correct criterion for when it is not necessary // to parenthesize the expression before a dot. The known exceptions are: // // NewExpression: // new C.x -> not the same as (new C).x // NumberLiteral // 1.x -> not the same as (1).x // if (ts.isLeftHandSideExpression(expr) && expr.kind !== 175 /* NewExpression */ && expr.kind !== 8 /* NumericLiteral */) { return expr; } var node = ts.createSynthesizedNode(178 /* ParenthesizedExpression */); node.expression = expr; return node; } function emitComputedPropertyName(node) { write("["); emitExpressionForPropertyName(node); write("]"); } function emitMethod(node) { if (languageVersion >= 2 /* ES6 */ && node.asteriskToken) { write("*"); } emit(node.name); if (languageVersion < 2 /* ES6 */) { write(": function "); } emitSignatureAndBody(node); } function emitPropertyAssignment(node) { emit(node.name); write(": "); // This is to ensure that we emit comment in the following case: // For example: // obj = { // id: /*comment1*/ ()=>void // } // "comment1" is not considered to be leading comment for node.initializer // but rather a trailing comment on the previous node. emitTrailingCommentsOfPosition(node.initializer.pos); emit(node.initializer); } // Return true if identifier resolves to an exported member of a namespace function isExportReference(node) { var container = resolver.getReferencedExportContainer(node); return !!container; } // Return true if identifier resolves to an imported identifier function isImportedReference(node) { var declaration = resolver.getReferencedImportDeclaration(node); return declaration && (declaration.kind === 231 /* ImportClause */ || declaration.kind === 234 /* ImportSpecifier */); } function emitShorthandPropertyAssignment(node) { // The name property of a short-hand property assignment is considered an expression position, so here // we manually emit the identifier to avoid rewriting. writeTextOfNode(currentText, node.name); // If emitting pre-ES6 code, or if the name requires rewriting when resolved as an expression identifier, // we emit a normal property assignment. For example: // module m { // export let y; // } // module m { // let obj = { y }; // } // Here we need to emit obj = { y : m.y } regardless of the output target. // The same rules apply for imported identifiers when targeting module formats with indirect access to // the imported identifiers. For example, when targeting CommonJS: // // import {foo} from './foo'; // export const baz = { foo }; // // Must be transformed into: // // const foo_1 = require('./foo'); // exports.baz = { foo: foo_1.foo }; // if (languageVersion < 2 /* ES6 */ || (modulekind !== ts.ModuleKind.ES6 && isImportedReference(node.name)) || isExportReference(node.name)) { // Emit identifier as an identifier write(": "); emitExpressionIdentifier(node.name); } if (languageVersion >= 2 /* ES6 */ && node.objectAssignmentInitializer) { write(" = "); emit(node.objectAssignmentInitializer); } } function tryEmitConstantValue(node) { var constantValue = tryGetConstEnumValue(node); if (constantValue !== undefined) { write(constantValue.toString()); if (!compilerOptions.removeComments) { var propertyName = node.kind === 172 /* PropertyAccessExpression */ ? ts.declarationNameToString(node.name) : ts.getTextOfNode(node.argumentExpression); write(" /* " + propertyName + " */"); } return true; } return false; } function tryGetConstEnumValue(node) { if (compilerOptions.isolatedModules) { return undefined; } return node.kind === 172 /* PropertyAccessExpression */ || node.kind === 173 /* ElementAccessExpression */ ? resolver.getConstantValue(node) : undefined; } // Returns 'true' if the code was actually indented, false otherwise. // If the code is not indented, an optional valueToWriteWhenNotIndenting will be // emitted instead. function indentIfOnDifferentLines(parent, node1, node2, valueToWriteWhenNotIndenting) { var realNodesAreOnDifferentLines = !ts.nodeIsSynthesized(parent) && !nodeEndIsOnSameLineAsNodeStart(node1, node2); // Always use a newline for synthesized code if the synthesizer desires it. var synthesizedNodeIsOnDifferentLine = synthesizedNodeStartsOnNewLine(node2); if (realNodesAreOnDifferentLines || synthesizedNodeIsOnDifferentLine) { increaseIndent(); writeLine(); return true; } else { if (valueToWriteWhenNotIndenting) { write(valueToWriteWhenNotIndenting); } return false; } } function emitPropertyAccess(node) { if (tryEmitConstantValue(node)) { return; } if (languageVersion === 2 /* ES6 */ && node.expression.kind === 95 /* SuperKeyword */ && isInAsyncMethodWithSuperInES6(node)) { var name_28 = ts.createSynthesizedNode(9 /* StringLiteral */); name_28.text = node.name.text; emitSuperAccessInAsyncMethod(node.expression, name_28); return; } emit(node.expression); var dotRangeStart = ts.nodeIsSynthesized(node.expression) ? -1 : node.expression.end; var dotRangeEnd = ts.nodeIsSynthesized(node.expression) ? -1 : ts.skipTrivia(currentText, node.expression.end) + 1; var dotToken = { pos: dotRangeStart, end: dotRangeEnd }; var indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, dotToken); // 1 .toString is a valid property access, emit a space after the literal // Also emit a space if expression is a integer const enum value - it will appear in generated code as numeric literal var shouldEmitSpace = false; if (!indentedBeforeDot) { if (node.expression.kind === 8 /* NumericLiteral */) { // check if numeric literal was originally written with a dot var text = ts.getTextOfNodeFromSourceText(currentText, node.expression); shouldEmitSpace = text.indexOf(ts.tokenToString(21 /* DotToken */)) < 0; } else { // check if constant enum value is integer var constantValue = tryGetConstEnumValue(node.expression); // isFinite handles cases when constantValue is undefined shouldEmitSpace = isFinite(constantValue) && Math.floor(constantValue) === constantValue; } } if (shouldEmitSpace) { write(" ."); } else { write("."); } var indentedAfterDot = indentIfOnDifferentLines(node, dotToken, node.name); emit(node.name); decreaseIndentIf(indentedBeforeDot, indentedAfterDot); } function emitQualifiedName(node) { emit(node.left); write("."); emit(node.right); } function emitQualifiedNameAsExpression(node, useFallback) { if (node.left.kind === 69 /* Identifier */) { emitEntityNameAsExpression(node.left, useFallback); } else if (useFallback) { var temp = createAndRecordTempVariable(0 /* Auto */); write("("); emitNodeWithoutSourceMap(temp); write(" = "); emitEntityNameAsExpression(node.left, /*useFallback*/ true); write(") && "); emitNodeWithoutSourceMap(temp); } else { emitEntityNameAsExpression(node.left, /*useFallback*/ false); } write("."); emit(node.right); } function emitEntityNameAsExpression(node, useFallback) { switch (node.kind) { case 69 /* Identifier */: if (useFallback) { write("typeof "); emitExpressionIdentifier(node); write(" !== 'undefined' && "); } emitExpressionIdentifier(node); break; case 139 /* QualifiedName */: emitQualifiedNameAsExpression(node, useFallback); break; default: emitNodeWithoutSourceMap(node); break; } } function emitIndexedAccess(node) { if (tryEmitConstantValue(node)) { return; } if (languageVersion === 2 /* ES6 */ && node.expression.kind === 95 /* SuperKeyword */ && isInAsyncMethodWithSuperInES6(node)) { emitSuperAccessInAsyncMethod(node.expression, node.argumentExpression); return; } emit(node.expression); write("["); emit(node.argumentExpression); write("]"); } function hasSpreadElement(elements) { return ts.forEach(elements, function (e) { return e.kind === 191 /* SpreadElementExpression */; }); } function skipParentheses(node) { while (node.kind === 178 /* ParenthesizedExpression */ || node.kind === 177 /* TypeAssertionExpression */ || node.kind === 195 /* AsExpression */ || node.kind === 196 /* NonNullExpression */) { node = node.expression; } return node; } function emitCallTarget(node) { if (node.kind === 69 /* Identifier */ || node.kind === 97 /* ThisKeyword */ || node.kind === 95 /* SuperKeyword */) { emit(node); return node; } var temp = createAndRecordTempVariable(0 /* Auto */); write("("); emit(temp); write(" = "); emit(node); write(")"); return temp; } function emitCallWithSpread(node) { var target; var expr = skipParentheses(node.expression); if (expr.kind === 172 /* PropertyAccessExpression */) { // Target will be emitted as "this" argument target = emitCallTarget(expr.expression); write("."); emit(expr.name); } else if (expr.kind === 173 /* ElementAccessExpression */) { // Target will be emitted as "this" argument target = emitCallTarget(expr.expression); write("["); emit(expr.argumentExpression); write("]"); } else if (expr.kind === 95 /* SuperKeyword */) { target = expr; write("_super"); } else { emit(node.expression); } write(".apply("); if (target) { if (target.kind === 95 /* SuperKeyword */) { // Calls of form super(...) and super.foo(...) emitThis(target); } else { // Calls of form obj.foo(...) emit(target); } } else { // Calls of form foo(...) write("void 0"); } write(", "); emitListWithSpread(node.arguments, /*needsUniqueCopy*/ false, /*multiLine*/ false, /*trailingComma*/ false, /*useConcat*/ true); write(")"); } function isInAsyncMethodWithSuperInES6(node) { if (languageVersion === 2 /* ES6 */) { var container = ts.getSuperContainer(node, /*includeFunctions*/ false); if (container && resolver.getNodeCheckFlags(container) & (2048 /* AsyncMethodWithSuper */ | 4096 /* AsyncMethodWithSuperBinding */)) { return true; } } return false; } function emitSuperAccessInAsyncMethod(superNode, argumentExpression) { var container = ts.getSuperContainer(superNode, /*includeFunctions*/ false); var isSuperBinding = resolver.getNodeCheckFlags(container) & 4096 /* AsyncMethodWithSuperBinding */; write("_super("); emit(argumentExpression); write(isSuperBinding ? ").value" : ")"); } function emitCallExpression(node) { if (languageVersion < 2 /* ES6 */ && hasSpreadElement(node.arguments)) { emitCallWithSpread(node); return; } var expression = node.expression; var superCall = false; var isAsyncMethodWithSuper = false; if (expression.kind === 95 /* SuperKeyword */) { emitSuper(expression); superCall = true; } else { superCall = ts.isSuperPropertyOrElementAccess(expression); isAsyncMethodWithSuper = superCall && isInAsyncMethodWithSuperInES6(node); emit(expression); } if (superCall && (languageVersion < 2 /* ES6 */ || isAsyncMethodWithSuper)) { write(".call("); emitThis(expression); if (node.arguments.length) { write(", "); emitCommaList(node.arguments); } write(")"); } else { write("("); emitCommaList(node.arguments); write(")"); } } function emitNewExpression(node) { write("new "); // Spread operator logic is supported in new expressions in ES5 using a combination // of Function.prototype.bind() and Function.prototype.apply(). // // Example: // // var args = [1, 2, 3, 4, 5]; // new Array(...args); // // is compiled into the following ES5: // // var args = [1, 2, 3, 4, 5]; // new (Array.bind.apply(Array, [void 0].concat(args))); // // The 'thisArg' to 'bind' is ignored when invoking the result of 'bind' with 'new', // Thus, we set it to undefined ('void 0'). if (languageVersion === 1 /* ES5 */ && node.arguments && hasSpreadElement(node.arguments)) { write("("); var target = emitCallTarget(node.expression); write(".bind.apply("); emit(target); write(", [void 0].concat("); emitListWithSpread(node.arguments, /*needsUniqueCopy*/ false, /*multiLine*/ false, /*trailingComma*/ false, /*useConcat*/ false); write(")))"); write("()"); } else { emit(node.expression); if (node.arguments) { write("("); emitCommaList(node.arguments); write(")"); } } } function emitTaggedTemplateExpression(node) { if (languageVersion >= 2 /* ES6 */) { emit(node.tag); write(" "); emit(node.template); } else { emitDownlevelTaggedTemplate(node); } } function emitParenExpression(node) { // If the node is synthesized, it means the emitter put the parentheses there, // not the user. If we didn't want them, the emitter would not have put them // there. if (!ts.nodeIsSynthesized(node) && node.parent.kind !== 180 /* ArrowFunction */) { if (node.expression.kind === 177 /* TypeAssertionExpression */ || node.expression.kind === 195 /* AsExpression */ || node.expression.kind === 196 /* NonNullExpression */) { var operand = node.expression.expression; // Make sure we consider all nested cast expressions, e.g.: // (<any><number><any>-A).x; while (operand.kind === 177 /* TypeAssertionExpression */ || operand.kind === 195 /* AsExpression */ || operand.kind === 196 /* NonNullExpression */) { operand = operand.expression; } // We have an expression of the form: (<Type>SubExpr) or (SubExpr as Type) // Emitting this as (SubExpr) is really not desirable. We would like to emit the subexpr as is. // Omitting the parentheses, however, could cause change in the semantics of the generated // code if the casted expression has a lower precedence than the rest of the expression, e.g.: // (<any>new A).foo should be emitted as (new A).foo and not new A.foo // (<any>typeof A).toString() should be emitted as (typeof A).toString() and not typeof A.toString() // new (<any>A()) should be emitted as new (A()) and not new A() // (<any>function foo() { })() should be emitted as an IIF (function foo(){})() and not declaration function foo(){} () if (operand.kind !== 185 /* PrefixUnaryExpression */ && operand.kind !== 183 /* VoidExpression */ && operand.kind !== 182 /* TypeOfExpression */ && operand.kind !== 181 /* DeleteExpression */ && operand.kind !== 186 /* PostfixUnaryExpression */ && operand.kind !== 175 /* NewExpression */ && !(operand.kind === 187 /* BinaryExpression */ && node.expression.kind === 195 /* AsExpression */) && !(operand.kind === 174 /* CallExpression */ && node.parent.kind === 175 /* NewExpression */) && !(operand.kind === 179 /* FunctionExpression */ && node.parent.kind === 174 /* CallExpression */) && !(operand.kind === 8 /* NumericLiteral */ && node.parent.kind === 172 /* PropertyAccessExpression */)) { emit(operand); return; } } } write("("); emit(node.expression); write(")"); } function emitDeleteExpression(node) { write(ts.tokenToString(78 /* DeleteKeyword */)); write(" "); emit(node.expression); } function emitVoidExpression(node) { write(ts.tokenToString(103 /* VoidKeyword */)); write(" "); emit(node.expression); } function emitTypeOfExpression(node) { write(ts.tokenToString(101 /* TypeOfKeyword */)); write(" "); emit(node.expression); } function isNameOfExportedSourceLevelDeclarationInSystemExternalModule(node) { if (!isCurrentFileSystemExternalModule() || node.kind !== 69 /* Identifier */ || ts.nodeIsSynthesized(node)) { return false; } var isVariableDeclarationOrBindingElement = node.parent && (node.parent.kind === 218 /* VariableDeclaration */ || node.parent.kind === 169 /* BindingElement */); var targetDeclaration = isVariableDeclarationOrBindingElement ? node.parent : resolver.getReferencedValueDeclaration(node); return isSourceFileLevelDeclarationInSystemJsModule(targetDeclaration, /*isExported*/ true); } function isNameOfExportedDeclarationInNonES6Module(node) { if (modulekind === ts.ModuleKind.System || node.kind !== 69 /* Identifier */ || ts.nodeIsSynthesized(node)) { return false; } if (exportEquals || !exportSpecifiers || !(node.text in exportSpecifiers)) { return false; } // check that referenced declaration is declared on source file level var declaration = resolver.getReferencedValueDeclaration(node); return declaration && ts.getEnclosingBlockScopeContainer(declaration).kind === 256 /* SourceFile */; } function emitPrefixUnaryExpression(node) { var isPlusPlusOrMinusMinus = (node.operator === 41 /* PlusPlusToken */ || node.operator === 42 /* MinusMinusToken */); var externalExportChanged = isPlusPlusOrMinusMinus && isNameOfExportedSourceLevelDeclarationInSystemExternalModule(node.operand); if (externalExportChanged) { // emit // ++x // as // exports('x', ++x) write(exportFunctionForFile + "(\""); emitNodeWithoutSourceMap(node.operand); write("\", "); } var internalExportChanged = isPlusPlusOrMinusMinus && isNameOfExportedDeclarationInNonES6Module(node.operand); if (internalExportChanged) { emitAliasEqual(node.operand); } write(ts.tokenToString(node.operator)); // In some cases, we need to emit a space between the operator and the operand. One obvious case // is when the operator is an identifier, like delete or typeof. We also need to do this for plus // and minus expressions in certain cases. Specifically, consider the following two cases (parens // are just for clarity of exposition, and not part of the source code): // // (+(+1)) // (+(++1)) // // We need to emit a space in both cases. In the first case, the absence of a space will make // the resulting expression a prefix increment operation. And in the second, it will make the resulting // expression a prefix increment whose operand is a plus expression - (++(+x)) // The same is true of minus of course. if (node.operand.kind === 185 /* PrefixUnaryExpression */) { var operand = node.operand; if (node.operator === 35 /* PlusToken */ && (operand.operator === 35 /* PlusToken */ || operand.operator === 41 /* PlusPlusToken */)) { write(" "); } else if (node.operator === 36 /* MinusToken */ && (operand.operator === 36 /* MinusToken */ || operand.operator === 42 /* MinusMinusToken */)) { write(" "); } } emit(node.operand); if (externalExportChanged) { write(")"); } } function emitPostfixUnaryExpression(node) { var externalExportChanged = isNameOfExportedSourceLevelDeclarationInSystemExternalModule(node.operand); var internalExportChanged = isNameOfExportedDeclarationInNonES6Module(node.operand); if (externalExportChanged) { // export function returns the value that was passes as the second argument // however for postfix unary expressions result value should be the value before modification. // emit 'x++' as '(export('x', ++x) - 1)' and 'x--' as '(export('x', --x) + 1)' write("(" + exportFunctionForFile + "(\""); emitNodeWithoutSourceMap(node.operand); write("\", "); write(ts.tokenToString(node.operator)); emit(node.operand); if (node.operator === 41 /* PlusPlusToken */) { write(") - 1)"); } else { write(") + 1)"); } } else if (internalExportChanged) { emitAliasEqual(node.operand); emit(node.operand); if (node.operator === 41 /* PlusPlusToken */) { write(" += 1"); } else { write(" -= 1"); } } else { emit(node.operand); write(ts.tokenToString(node.operator)); } } function shouldHoistDeclarationInSystemJsModule(node) { return isSourceFileLevelDeclarationInSystemJsModule(node, /*isExported*/ false); } /* * Checks if given node is a source file level declaration (not nested in module/function). * If 'isExported' is true - then declaration must also be exported. * This function is used in two cases: * - check if node is a exported source file level value to determine * if we should also export the value after its it changed * - check if node is a source level declaration to emit it differently, * i.e non-exported variable statement 'var x = 1' is hoisted so * when we emit variable statement 'var' should be dropped. */ function isSourceFileLevelDeclarationInSystemJsModule(node, isExported) { if (!node || !isCurrentFileSystemExternalModule()) { return false; } var current = ts.getRootDeclaration(node).parent; while (current) { if (current.kind === 256 /* SourceFile */) { return !isExported || ((ts.getCombinedNodeFlags(node) & 1 /* Export */) !== 0); } else if (ts.isDeclaration(current)) { return false; } else { current = current.parent; } } } /** * Emit ES7 exponentiation operator downlevel using Math.pow * @param node a binary expression node containing exponentiationOperator (**, **=) */ function emitExponentiationOperator(node) { var leftHandSideExpression = node.left; if (node.operatorToken.kind === 60 /* AsteriskAsteriskEqualsToken */) { var synthesizedLHS = void 0; var shouldEmitParentheses = false; if (ts.isElementAccessExpression(leftHandSideExpression)) { shouldEmitParentheses = true; write("("); synthesizedLHS = ts.createSynthesizedNode(173 /* ElementAccessExpression */, /*startsOnNewLine*/ false); var identifier = emitTempVariableAssignment(leftHandSideExpression.expression, /*canDefineTempVariablesInPlace*/ false, /*shouldEmitCommaBeforeAssignment*/ false); synthesizedLHS.expression = identifier; if (leftHandSideExpression.argumentExpression.kind !== 8 /* NumericLiteral */ && leftHandSideExpression.argumentExpression.kind !== 9 /* StringLiteral */) { var tempArgumentExpression = createAndRecordTempVariable(268435456 /* _i */); synthesizedLHS.argumentExpression = tempArgumentExpression; emitAssignment(tempArgumentExpression, leftHandSideExpression.argumentExpression, /*shouldEmitCommaBeforeAssignment*/ true, leftHandSideExpression.expression); } else { synthesizedLHS.argumentExpression = leftHandSideExpression.argumentExpression; } write(", "); } else if (ts.isPropertyAccessExpression(leftHandSideExpression)) { shouldEmitParentheses = true; write("("); synthesizedLHS = ts.createSynthesizedNode(172 /* PropertyAccessExpression */, /*startsOnNewLine*/ false); var identifier = emitTempVariableAssignment(leftHandSideExpression.expression, /*canDefineTempVariablesInPlace*/ false, /*shouldEmitCommaBeforeAssignment*/ false); synthesizedLHS.expression = identifier; synthesizedLHS.name = leftHandSideExpression.name; write(", "); } emit(synthesizedLHS || leftHandSideExpression); write(" = "); write("Math.pow("); emit(synthesizedLHS || leftHandSideExpression); write(", "); emit(node.right); write(")"); if (shouldEmitParentheses) { write(")"); } } else { write("Math.pow("); emit(leftHandSideExpression); write(", "); emit(node.right); write(")"); } } function emitAliasEqual(name) { for (var _a = 0, _b = exportSpecifiers[name.text]; _a < _b.length; _a++) { var specifier = _b[_a]; emitStart(specifier.name); emitContainingModuleName(specifier); if (languageVersion === 0 /* ES3 */ && name.text === "default") { write('["default"]'); } else { write("."); emitNodeWithCommentsAndWithoutSourcemap(specifier.name); } emitEnd(specifier.name); write(" = "); } return true; } function emitBinaryExpression(node) { if (languageVersion < 2 /* ES6 */ && node.operatorToken.kind === 56 /* EqualsToken */ && (node.left.kind === 171 /* ObjectLiteralExpression */ || node.left.kind === 170 /* ArrayLiteralExpression */)) { emitDestructuring(node, node.parent.kind === 202 /* ExpressionStatement */); } else { var isAssignment = ts.isAssignmentOperator(node.operatorToken.kind); var externalExportChanged = isAssignment && isNameOfExportedSourceLevelDeclarationInSystemExternalModule(node.left); if (externalExportChanged) { // emit assignment 'x <op> y' as 'exports("x", x <op> y)' write(exportFunctionForFile + "(\""); emitNodeWithoutSourceMap(node.left); write("\", "); } var internalExportChanged = isAssignment && isNameOfExportedDeclarationInNonES6Module(node.left); if (internalExportChanged) { // export { foo } // emit foo = 2 as exports.foo = foo = 2 emitAliasEqual(node.left); } if (node.operatorToken.kind === 38 /* AsteriskAsteriskToken */ || node.operatorToken.kind === 60 /* AsteriskAsteriskEqualsToken */) { // Downleveled emit exponentiation operator using Math.pow emitExponentiationOperator(node); } else { emit(node.left); // Add indentation before emit the operator if the operator is on different line // For example: // 3 // + 2; // emitted as // 3 // + 2; var indentedBeforeOperator = indentIfOnDifferentLines(node, node.left, node.operatorToken, node.operatorToken.kind !== 24 /* CommaToken */ ? " " : undefined); write(ts.tokenToString(node.operatorToken.kind)); var indentedAfterOperator = indentIfOnDifferentLines(node, node.operatorToken, node.right, " "); emit(node.right); decreaseIndentIf(indentedBeforeOperator, indentedAfterOperator); } if (externalExportChanged) { write(")"); } } } function synthesizedNodeStartsOnNewLine(node) { return ts.nodeIsSynthesized(node) && node.startsOnNewLine; } function emitConditionalExpression(node) { emit(node.condition); var indentedBeforeQuestion = indentIfOnDifferentLines(node, node.condition, node.questionToken, " "); write("?"); var indentedAfterQuestion = indentIfOnDifferentLines(node, node.questionToken, node.whenTrue, " "); emit(node.whenTrue); decreaseIndentIf(indentedBeforeQuestion, indentedAfterQuestion); var indentedBeforeColon = indentIfOnDifferentLines(node, node.whenTrue, node.colonToken, " "); write(":"); var indentedAfterColon = indentIfOnDifferentLines(node, node.colonToken, node.whenFalse, " "); emit(node.whenFalse); decreaseIndentIf(indentedBeforeColon, indentedAfterColon); } // Helper function to decrease the indent if we previously indented. Allows multiple // previous indent values to be considered at a time. This also allows caller to just // call this once, passing in all their appropriate indent values, instead of needing // to call this helper function multiple times. function decreaseIndentIf(value1, value2) { if (value1) { decreaseIndent(); } if (value2) { decreaseIndent(); } } function isSingleLineEmptyBlock(node) { if (node && node.kind === 199 /* Block */) { var block = node; return block.statements.length === 0 && nodeEndIsOnSameLineAsNodeStart(block, block); } } function emitBlock(node) { if (isSingleLineEmptyBlock(node)) { emitToken(15 /* OpenBraceToken */, node.pos); write(" "); emitToken(16 /* CloseBraceToken */, node.statements.end); return; } emitToken(15 /* OpenBraceToken */, node.pos); increaseIndent(); if (node.kind === 226 /* ModuleBlock */) { ts.Debug.assert(node.parent.kind === 225 /* ModuleDeclaration */); emitCaptureThisForNodeIfNecessary(node.parent); } emitLines(node.statements); if (node.kind === 226 /* ModuleBlock */) { emitTempDeclarations(/*newLine*/ true); } decreaseIndent(); writeLine(); emitToken(16 /* CloseBraceToken */, node.statements.end); } function emitEmbeddedStatement(node) { if (node.kind === 199 /* Block */) { write(" "); emit(node); } else { increaseIndent(); writeLine(); emit(node); decreaseIndent(); } } function emitExpressionStatement(node) { emitParenthesizedIf(node.expression, /*parenthesized*/ node.expression.kind === 180 /* ArrowFunction */); write(";"); } function emitIfStatement(node) { var endPos = emitToken(88 /* IfKeyword */, node.pos); write(" "); endPos = emitToken(17 /* OpenParenToken */, endPos); emit(node.expression); emitToken(18 /* CloseParenToken */, node.expression.end); emitEmbeddedStatement(node.thenStatement); if (node.elseStatement) { writeLine(); emitToken(80 /* ElseKeyword */, node.thenStatement.end); if (node.elseStatement.kind === 203 /* IfStatement */) { write(" "); emit(node.elseStatement); } else { emitEmbeddedStatement(node.elseStatement); } } } function emitDoStatement(node) { emitLoop(node, emitDoStatementWorker); } function emitDoStatementWorker(node, loop) { write("do"); if (loop) { emitConvertedLoopCall(loop, /*emitAsBlock*/ true); } else { emitNormalLoopBody(node, /*emitAsEmbeddedStatement*/ true); } if (node.statement.kind === 199 /* Block */) { write(" "); } else { writeLine(); } write("while ("); emit(node.expression); write(");"); } function emitWhileStatement(node) { emitLoop(node, emitWhileStatementWorker); } function emitWhileStatementWorker(node, loop) { write("while ("); emit(node.expression); write(")"); if (loop) { emitConvertedLoopCall(loop, /*emitAsBlock*/ true); } else { emitNormalLoopBody(node, /*emitAsEmbeddedStatement*/ true); } } /** * Returns true if start of variable declaration list was emitted. * Returns false if nothing was written - this can happen for source file level variable declarations * in system modules where such variable declarations are hoisted. */ function tryEmitStartOfVariableDeclarationList(decl) { if (shouldHoistVariable(decl, /*checkIfSourceFileLevelDecl*/ true)) { // variables in variable declaration list were already hoisted return false; } if (convertedLoopState && (ts.getCombinedNodeFlags(decl) & 3072 /* BlockScoped */) === 0) { // we are inside a converted loop - this can only happen in downlevel scenarios // record names for all variable declarations for (var _a = 0, _b = decl.declarations; _a < _b.length; _a++) { var varDecl = _b[_a]; hoistVariableDeclarationFromLoop(convertedLoopState, varDecl); } return false; } emitStart(decl); if (decl && languageVersion >= 2 /* ES6 */) { if (ts.isLet(decl)) { write("let "); } else if (ts.isConst(decl)) { write("const "); } else { write("var "); } } else { write("var "); } // Note here we specifically dont emit end so that if we are going to emit binding pattern // we can alter the source map correctly return true; } function emitVariableDeclarationListSkippingUninitializedEntries(list) { var started = false; for (var _a = 0, _b = list.declarations; _a < _b.length; _a++) { var decl = _b[_a]; if (!decl.initializer) { continue; } if (!started) { started = true; } else { write(", "); } emit(decl); } return started; } function shouldConvertLoopBody(node) { return languageVersion < 2 /* ES6 */ && (resolver.getNodeCheckFlags(node) & 65536 /* LoopWithCapturedBlockScopedBinding */) !== 0; } function emitLoop(node, loopEmitter) { var shouldConvert = shouldConvertLoopBody(node); if (!shouldConvert) { loopEmitter(node, /* convertedLoop*/ undefined); } else { var loop = convertLoopBody(node); if (node.parent.kind === 214 /* LabeledStatement */) { // if parent of the loop was labeled statement - attach the label to loop skipping converted loop body emitLabelAndColon(node.parent); } loopEmitter(node, loop); } } function convertLoopBody(node) { var functionName = makeUniqueName("_loop"); var loopInitializer; switch (node.kind) { case 206 /* ForStatement */: case 207 /* ForInStatement */: case 208 /* ForOfStatement */: var initializer = node.initializer; if (initializer && initializer.kind === 219 /* VariableDeclarationList */) { loopInitializer = node.initializer; } break; } var loopParameters; var loopOutParameters; if (loopInitializer && (ts.getCombinedNodeFlags(loopInitializer) & 3072 /* BlockScoped */)) { // if loop initializer contains block scoped variables - they should be passed to converted loop body as parameters loopParameters = []; for (var _a = 0, _b = loopInitializer.declarations; _a < _b.length; _a++) { var varDeclaration = _b[_a]; processVariableDeclaration(varDeclaration.name); } } var bodyIsBlock = node.statement.kind === 199 /* Block */; var paramList = loopParameters ? loopParameters.join(", ") : ""; writeLine(); write("var " + functionName + " = function(" + paramList + ")"); var convertedOuterLoopState = convertedLoopState; convertedLoopState = { loopOutParameters: loopOutParameters }; if (convertedOuterLoopState) { // convertedOuterLoopState !== undefined means that this converted loop is nested in another converted loop. // if outer converted loop has already accumulated some state - pass it through if (convertedOuterLoopState.argumentsName) { // outer loop has already used 'arguments' so we've already have some name to alias it // use the same name in all nested loops convertedLoopState.argumentsName = convertedOuterLoopState.argumentsName; } if (convertedOuterLoopState.thisName) { // outer loop has already used 'this' so we've already have some name to alias it // use the same name in all nested loops convertedLoopState.thisName = convertedOuterLoopState.thisName; } if (convertedOuterLoopState.hoistedLocalVariables) { // we've already collected some non-block scoped variable declarations in enclosing loop // use the same storage in nested loop convertedLoopState.hoistedLocalVariables = convertedOuterLoopState.hoistedLocalVariables; } } write(" {"); writeLine(); increaseIndent(); if (bodyIsBlock) { emitLines(node.statement.statements); } else { emit(node.statement); } writeLine(); // end of loop body -> copy out parameter copyLoopOutParameters(convertedLoopState, 1 /* ToOutParameter */, /*emitAsStatements*/ true); decreaseIndent(); writeLine(); write("};"); writeLine(); if (loopOutParameters) { // declare variables to hold out params for loop body write("var "); for (var i = 0; i < loopOutParameters.length; i++) { if (i !== 0) { write(", "); } write(loopOutParameters[i].outParamName); } write(";"); writeLine(); } if (convertedLoopState.argumentsName) { // if alias for arguments is set if (convertedOuterLoopState) { // pass it to outer converted loop convertedOuterLoopState.argumentsName = convertedLoopState.argumentsName; } else { // this is top level converted loop and we need to create an alias for 'arguments' object write("var " + convertedLoopState.argumentsName + " = arguments;"); writeLine(); } } if (convertedLoopState.thisName) { // if alias for this is set if (convertedOuterLoopState) { // pass it to outer converted loop convertedOuterLoopState.thisName = convertedLoopState.thisName; } else { // this is top level converted loop so we need to create an alias for 'this' here // NOTE: // if converted loops were all nested in arrow function then we'll always emit '_this' so convertedLoopState.thisName will not be set. // If it is set this means that all nested loops are not nested in arrow function and it is safe to capture 'this'. write("var " + convertedLoopState.thisName + " = this;"); writeLine(); } } if (convertedLoopState.hoistedLocalVariables) { // if hoistedLocalVariables !== undefined this means that we've possibly collected some variable declarations to be hoisted later if (convertedOuterLoopState) { // pass them to outer converted loop convertedOuterLoopState.hoistedLocalVariables = convertedLoopState.hoistedLocalVariables; } else { // deduplicate and hoist collected variable declarations write("var "); var seen = void 0; for (var _c = 0, _d = convertedLoopState.hoistedLocalVariables; _c < _d.length; _c++) { var id = _d[_c]; // Don't initialize seen unless we have at least one element. // Emit a comma to separate for all but the first element. if (!seen) { seen = ts.createMap(); } else { write(", "); } if (!(id.text in seen)) { emit(id); seen[id.text] = id.text; } } write(";"); writeLine(); } } var currentLoopState = convertedLoopState; convertedLoopState = convertedOuterLoopState; return { functionName: functionName, paramList: paramList, state: currentLoopState }; function processVariableDeclaration(name) { if (name.kind === 69 /* Identifier */) { var nameText = isNameOfNestedBlockScopedRedeclarationOrCapturedBinding(name) ? getGeneratedNameForNode(name) : name.text; loopParameters.push(nameText); if (resolver.getNodeCheckFlags(name.parent) & 2097152 /* NeedsLoopOutParameter */) { var reassignedVariable = { originalName: name, outParamName: makeUniqueName("out_" + nameText) }; (loopOutParameters || (loopOutParameters = [])).push(reassignedVariable); } } else { for (var _a = 0, _b = name.elements; _a < _b.length; _a++) { var element = _b[_a]; processVariableDeclaration(element.name); } } } } function emitNormalLoopBody(node, emitAsEmbeddedStatement) { var saveAllowedNonLabeledJumps; if (convertedLoopState) { // we get here if we are trying to emit normal loop loop inside converted loop // set allowedNonLabeledJumps to Break | Continue to mark that break\continue inside the loop should be emitted as is saveAllowedNonLabeledJumps = convertedLoopState.allowedNonLabeledJumps; convertedLoopState.allowedNonLabeledJumps = 2 /* Break */ | 4 /* Continue */; } if (emitAsEmbeddedStatement) { emitEmbeddedStatement(node.statement); } else if (node.statement.kind === 199 /* Block */) { emitLines(node.statement.statements); } else { writeLine(); emit(node.statement); } if (convertedLoopState) { convertedLoopState.allowedNonLabeledJumps = saveAllowedNonLabeledJumps; } } function copyLoopOutParameters(state, copyDirection, emitAsStatements) { if (state.loopOutParameters) { for (var _a = 0, _b = state.loopOutParameters; _a < _b.length; _a++) { var outParam = _b[_a]; if (copyDirection === 0 /* ToOriginal */) { emitIdentifier(outParam.originalName); write(" = " + outParam.outParamName); } else { write(outParam.outParamName + " = "); emitIdentifier(outParam.originalName); } if (emitAsStatements) { write(";"); writeLine(); } else { write(", "); } } } } function emitConvertedLoopCall(loop, emitAsBlock) { if (emitAsBlock) { write(" {"); writeLine(); increaseIndent(); } // loop is considered simple if it does not have any return statements or break\continue that transfer control outside of the loop // simple loops are emitted as just 'loop()'; // NOTE: if loop uses only 'continue' it still will be emitted as simple loop var isSimpleLoop = !(loop.state.nonLocalJumps & ~4 /* Continue */) && !loop.state.labeledNonLocalBreaks && !loop.state.labeledNonLocalContinues; var loopResult = makeUniqueName("state"); if (!isSimpleLoop) { write("var " + loopResult + " = "); } write(loop.functionName + "(" + loop.paramList + ");"); writeLine(); copyLoopOutParameters(loop.state, 0 /* ToOriginal */, /*emitAsStatements*/ true); if (!isSimpleLoop) { // for non simple loops we need to store result returned from converted loop function and use it to do dispatching // converted loop function can return: // - object - used when body of the converted loop contains return statement. Property "value" of this object stores retuned value // - string - used to dispatch jumps. "break" and "continue" are used to non-labeled jumps, other values are used to transfer control to // different labels writeLine(); if (loop.state.nonLocalJumps & 8 /* Return */) { write("if (typeof " + loopResult + " === \"object\") "); if (convertedLoopState) { // we are currently nested in another converted loop - return unwrapped result write("return " + loopResult + ";"); // propagate 'hasReturn' flag to outer loop convertedLoopState.nonLocalJumps |= 8 /* Return */; } else { // top level converted loop - return unwrapped value write("return " + loopResult + ".value;"); } writeLine(); } if (loop.state.nonLocalJumps & 2 /* Break */) { write("if (" + loopResult + " === \"break\") break;"); writeLine(); } // in case of labeled breaks emit code that either breaks to some known label inside outer loop or delegates jump decision to outer loop emitDispatchTableForLabeledJumps(loopResult, loop.state, convertedLoopState); } if (emitAsBlock) { writeLine(); decreaseIndent(); write("}"); } function emitDispatchTableForLabeledJumps(loopResultVariable, currentLoop, outerLoop) { if (!currentLoop.labeledNonLocalBreaks && !currentLoop.labeledNonLocalContinues) { return; } write("switch(" + loopResultVariable + ") {"); increaseIndent(); emitDispatchEntriesForLabeledJumps(currentLoop.labeledNonLocalBreaks, /*isBreak*/ true, loopResultVariable, outerLoop); emitDispatchEntriesForLabeledJumps(currentLoop.labeledNonLocalContinues, /*isBreak*/ false, loopResultVariable, outerLoop); decreaseIndent(); writeLine(); write("}"); } function emitDispatchEntriesForLabeledJumps(table, isBreak, loopResultVariable, outerLoop) { if (!table) { return; } for (var labelText in table) { var labelMarker = table[labelText]; writeLine(); write("case \"" + labelMarker + "\": "); // if there are no outer converted loop or outer label in question is located inside outer converted loop // then emit labeled break\continue // otherwise propagate pair 'label -> marker' to outer converted loop and emit 'return labelMarker' so outer loop can later decide what to do if (!outerLoop || (outerLoop.labels && outerLoop.labels[labelText])) { if (isBreak) { write("break "); } else { write("continue "); } write(labelText + ";"); } else { setLabeledJump(outerLoop, isBreak, labelText, labelMarker); write("return " + loopResultVariable + ";"); } } } } function emitForStatement(node) { emitLoop(node, emitForStatementWorker); } function emitForStatementWorker(node, loop) { var endPos = emitToken(86 /* ForKeyword */, node.pos); write(" "); endPos = emitToken(17 /* OpenParenToken */, endPos); if (node.initializer && node.initializer.kind === 219 /* VariableDeclarationList */) { var variableDeclarationList = node.initializer; var startIsEmitted = tryEmitStartOfVariableDeclarationList(variableDeclarationList); if (startIsEmitted) { emitCommaList(variableDeclarationList.declarations); } else { emitVariableDeclarationListSkippingUninitializedEntries(variableDeclarationList); } } else if (node.initializer) { emit(node.initializer); } write(";"); emitOptional(" ", node.condition); write(";"); emitOptional(" ", node.incrementor); write(")"); if (loop) { emitConvertedLoopCall(loop, /*emitAsBlock*/ true); } else { emitNormalLoopBody(node, /*emitAsEmbeddedStatement*/ true); } } function emitForInOrForOfStatement(node) { if (languageVersion < 2 /* ES6 */ && node.kind === 208 /* ForOfStatement */) { emitLoop(node, emitDownLevelForOfStatementWorker); } else { emitLoop(node, emitForInOrForOfStatementWorker); } } function emitForInOrForOfStatementWorker(node, loop) { var endPos = emitToken(86 /* ForKeyword */, node.pos); write(" "); endPos = emitToken(17 /* OpenParenToken */, endPos); if (node.initializer.kind === 219 /* VariableDeclarationList */) { var variableDeclarationList = node.initializer; if (variableDeclarationList.declarations.length >= 1) { tryEmitStartOfVariableDeclarationList(variableDeclarationList); emit(variableDeclarationList.declarations[0]); } } else { emit(node.initializer); } if (node.kind === 207 /* ForInStatement */) { write(" in "); } else { write(" of "); } emit(node.expression); emitToken(18 /* CloseParenToken */, node.expression.end); if (loop) { emitConvertedLoopCall(loop, /*emitAsBlock*/ true); } else { emitNormalLoopBody(node, /*emitAsEmbeddedStatement*/ true); } } function emitDownLevelForOfStatementWorker(node, loop) { // The following ES6 code: // // for (let v of expr) { } // // should be emitted as // // for (let _i = 0, _a = expr; _i < _a.length; _i++) { // let v = _a[_i]; // } // // where _a and _i are temps emitted to capture the RHS and the counter, // respectively. // When the left hand side is an expression instead of a let declaration, // the "let v" is not emitted. // When the left hand side is a let/const, the v is renamed if there is // another v in scope. // Note that all assignments to the LHS are emitted in the body, including // all destructuring. // Note also that because an extra statement is needed to assign to the LHS, // for-of bodies are always emitted as blocks. var endPos = emitToken(86 /* ForKeyword */, node.pos); write(" "); endPos = emitToken(17 /* OpenParenToken */, endPos); // Do not emit the LHS let declaration yet, because it might contain destructuring. // Do not call recordTempDeclaration because we are declaring the temps // right here. Recording means they will be declared later. // In the case where the user wrote an identifier as the RHS, like this: // // for (let v of arr) { } // // we can't reuse 'arr' because it might be modified within the body of the loop. var counter = createTempVariable(268435456 /* _i */); var rhsReference = ts.createSynthesizedNode(69 /* Identifier */); rhsReference.text = node.expression.kind === 69 /* Identifier */ ? makeUniqueName(node.expression.text) : makeTempVariableName(0 /* Auto */); // This is the let keyword for the counter and rhsReference. The let keyword for // the LHS will be emitted inside the body. emitStart(node.expression); write("var "); // _i = 0 emitNodeWithoutSourceMap(counter); write(" = 0"); emitEnd(node.expression); // , _a = expr write(", "); emitStart(node.expression); emitNodeWithoutSourceMap(rhsReference); write(" = "); emitNodeWithoutSourceMap(node.expression); emitEnd(node.expression); write("; "); // _i < _a.length; emitStart(node.expression); emitNodeWithoutSourceMap(counter); write(" < "); emitNodeWithCommentsAndWithoutSourcemap(rhsReference); write(".length"); emitEnd(node.expression); write("; "); // _i++) emitStart(node.expression); emitNodeWithoutSourceMap(counter); write("++"); emitEnd(node.expression); emitToken(18 /* CloseParenToken */, node.expression.end); // Body write(" {"); writeLine(); increaseIndent(); // Initialize LHS // let v = _a[_i]; var rhsIterationValue = createElementAccessExpression(rhsReference, counter); emitStart(node.initializer); if (node.initializer.kind === 219 /* VariableDeclarationList */) { write("var "); var variableDeclarationList = node.initializer; if (variableDeclarationList.declarations.length > 0) { var declaration = variableDeclarationList.declarations[0]; if (ts.isBindingPattern(declaration.name)) { // This works whether the declaration is a var, let, or const. // It will use rhsIterationValue _a[_i] as the initializer. emitDestructuring(declaration, /*isAssignmentExpressionStatement*/ false, rhsIterationValue); } else { // The following call does not include the initializer, so we have // to emit it separately. emitNodeWithCommentsAndWithoutSourcemap(declaration); write(" = "); emitNodeWithoutSourceMap(rhsIterationValue); } } else { // It's an empty declaration list. This can only happen in an error case, if the user wrote // for (let of []) {} emitNodeWithoutSourceMap(createTempVariable(0 /* Auto */)); write(" = "); emitNodeWithoutSourceMap(rhsIterationValue); } } else { // Initializer is an expression. Emit the expression in the body, so that it's // evaluated on every iteration. var assignmentExpression = createBinaryExpression(node.initializer, 56 /* EqualsToken */, rhsIterationValue, /*startsOnNewLine*/ false); if (node.initializer.kind === 170 /* ArrayLiteralExpression */ || node.initializer.kind === 171 /* ObjectLiteralExpression */) { // This is a destructuring pattern, so call emitDestructuring instead of emit. Calling emit will not work, because it will cause // the BinaryExpression to be passed in instead of the expression statement, which will cause emitDestructuring to crash. emitDestructuring(assignmentExpression, /*isAssignmentExpressionStatement*/ true, /*value*/ undefined); } else { emitNodeWithCommentsAndWithoutSourcemap(assignmentExpression); } } emitEnd(node.initializer); write(";"); if (loop) { writeLine(); emitConvertedLoopCall(loop, /*emitAsBlock*/ false); } else { emitNormalLoopBody(node, /*emitAsEmbeddedStatement*/ false); } writeLine(); decreaseIndent(); write("}"); } function emitBreakOrContinueStatement(node) { if (convertedLoopState) { // check if we can emit break\continue as is // it is possible if either // - break\continue is statement labeled and label is located inside the converted loop // - break\continue is non-labeled and located in non-converted loop\switch statement var jump = node.kind === 210 /* BreakStatement */ ? 2 /* Break */ : 4 /* Continue */; var canUseBreakOrContinue = (node.label && convertedLoopState.labels && convertedLoopState.labels[node.label.text]) || (!node.label && (convertedLoopState.allowedNonLabeledJumps & jump)); if (!canUseBreakOrContinue) { write("return "); // explicit exit from loop -> copy out parameters copyLoopOutParameters(convertedLoopState, 1 /* ToOutParameter */, /*emitAsStatements*/ false); if (!node.label) { if (node.kind === 210 /* BreakStatement */) { convertedLoopState.nonLocalJumps |= 2 /* Break */; write("\"break\";"); } else { convertedLoopState.nonLocalJumps |= 4 /* Continue */; // note: return value is emitted only to simplify debugging, call to converted loop body does not do any dispatching on it. write("\"continue\";"); } } else { var labelMarker = void 0; if (node.kind === 210 /* BreakStatement */) { labelMarker = "break-" + node.label.text; setLabeledJump(convertedLoopState, /*isBreak*/ true, node.label.text, labelMarker); } else { labelMarker = "continue-" + node.label.text; setLabeledJump(convertedLoopState, /*isBreak*/ false, node.label.text, labelMarker); } write("\"" + labelMarker + "\";"); } return; } } emitToken(node.kind === 210 /* BreakStatement */ ? 70 /* BreakKeyword */ : 75 /* ContinueKeyword */, node.pos); emitOptional(" ", node.label); write(";"); } function emitReturnStatement(node) { if (convertedLoopState) { convertedLoopState.nonLocalJumps |= 8 /* Return */; write("return { value: "); if (node.expression) { emit(node.expression); } else { write("void 0"); } write(" };"); return; } emitToken(94 /* ReturnKeyword */, node.pos); emitOptional(" ", node.expression); write(";"); } function emitWithStatement(node) { write("with ("); emit(node.expression); write(")"); emitEmbeddedStatement(node.statement); } function emitSwitchStatement(node) { var endPos = emitToken(96 /* SwitchKeyword */, node.pos); write(" "); emitToken(17 /* OpenParenToken */, endPos); emit(node.expression); endPos = emitToken(18 /* CloseParenToken */, node.expression.end); write(" "); var saveAllowedNonLabeledJumps; if (convertedLoopState) { saveAllowedNonLabeledJumps = convertedLoopState.allowedNonLabeledJumps; // for switch statement allow only non-labeled break convertedLoopState.allowedNonLabeledJumps |= 2 /* Break */; } emitCaseBlock(node.caseBlock, endPos); if (convertedLoopState) { convertedLoopState.allowedNonLabeledJumps = saveAllowedNonLabeledJumps; } } function emitCaseBlock(node, startPos) { emitToken(15 /* OpenBraceToken */, startPos); increaseIndent(); emitLines(node.clauses); decreaseIndent(); writeLine(); emitToken(16 /* CloseBraceToken */, node.clauses.end); } function nodeStartPositionsAreOnSameLine(node1, node2) { return ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node1.pos)) === ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function nodeEndPositionsAreOnSameLine(node1, node2) { return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === ts.getLineOfLocalPositionFromLineMap(currentLineMap, node2.end); } function nodeEndIsOnSameLineAsNodeStart(node1, node2) { return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function emitCaseOrDefaultClause(node) { if (node.kind === 249 /* CaseClause */) { write("case "); emit(node.expression); write(":"); } else { write("default:"); } if (node.statements.length === 1 && nodeStartPositionsAreOnSameLine(node, node.statements[0])) { write(" "); emit(node.statements[0]); } else { increaseIndent(); emitLines(node.statements); decreaseIndent(); } } function emitThrowStatement(node) { write("throw "); emit(node.expression); write(";"); } function emitTryStatement(node) { write("try "); emit(node.tryBlock); emit(node.catchClause); if (node.finallyBlock) { writeLine(); write("finally "); emit(node.finallyBlock); } } function emitCatchClause(node) { writeLine(); var endPos = emitToken(72 /* CatchKeyword */, node.pos); write(" "); emitToken(17 /* OpenParenToken */, endPos); emit(node.variableDeclaration); emitToken(18 /* CloseParenToken */, node.variableDeclaration ? node.variableDeclaration.end : endPos); write(" "); emitBlock(node.block); } function emitDebuggerStatement(node) { emitToken(76 /* DebuggerKeyword */, node.pos); write(";"); } function emitLabelAndColon(node) { emit(node.label); write(": "); } function emitLabeledStatement(node) { if (!ts.isIterationStatement(node.statement, /* lookInLabeledStatements */ false) || !shouldConvertLoopBody(node.statement)) { emitLabelAndColon(node); } if (convertedLoopState) { if (!convertedLoopState.labels) { convertedLoopState.labels = ts.createMap(); } convertedLoopState.labels[node.label.text] = node.label.text; } emit(node.statement); if (convertedLoopState) { convertedLoopState.labels[node.label.text] = undefined; } } function getContainingModule(node) { do { node = node.parent; } while (node && node.kind !== 225 /* ModuleDeclaration */); return node; } function emitContainingModuleName(node) { var container = getContainingModule(node); write(container ? getGeneratedNameForNode(container) : "exports"); } function emitModuleMemberName(node) { emitStart(node.name); if (ts.getCombinedNodeFlags(node) & 1 /* Export */) { var container = getContainingModule(node); if (container) { write(getGeneratedNameForNode(container)); write("."); } else if (modulekind !== ts.ModuleKind.ES6 && modulekind !== ts.ModuleKind.System) { write("exports."); } } emitNodeWithCommentsAndWithoutSourcemap(node.name); emitEnd(node.name); } function createVoidZero() { var zero = ts.createSynthesizedNode(8 /* NumericLiteral */); zero.text = "0"; var result = ts.createSynthesizedNode(183 /* VoidExpression */); result.expression = zero; return result; } function emitEs6ExportDefaultCompat(node) { if (node.parent.kind === 256 /* SourceFile */) { ts.Debug.assert(!!(node.flags & 512 /* Default */) || node.kind === 235 /* ExportAssignment */); // only allow export default at a source file level if (modulekind === ts.ModuleKind.CommonJS || modulekind === ts.ModuleKind.AMD || modulekind === ts.ModuleKind.UMD) { if (!isEs6Module) { if (languageVersion !== 0 /* ES3 */) { // default value of configurable, enumerable, writable are `false`. write('Object.defineProperty(exports, "__esModule", { value: true });'); writeLine(); } else { write("exports.__esModule = true;"); writeLine(); } } } } } function emitExportMemberAssignment(node) { if (node.flags & 1 /* Export */) { writeLine(); emitStart(node); // emit call to exporter only for top level nodes if (modulekind === ts.ModuleKind.System && node.parent === currentSourceFile) { // emit export default <smth> as // export("default", <smth>) write(exportFunctionForFile + "(\""); if (node.flags & 512 /* Default */) { write("default"); } else { emitNodeWithCommentsAndWithoutSourcemap(node.name); } write("\", "); emitDeclarationName(node); write(")"); } else { if (node.flags & 512 /* Default */) { emitEs6ExportDefaultCompat(node); if (languageVersion === 0 /* ES3 */) { write('exports["default"]'); } else { write("exports.default"); } } else { emitModuleMemberName(node); } write(" = "); emitDeclarationName(node); } emitEnd(node); write(";"); } } function emitExportMemberAssignments(name) { if (modulekind === ts.ModuleKind.System) { return; } if (!exportEquals && exportSpecifiers && name.text in exportSpecifiers) { for (var _a = 0, _b = exportSpecifiers[name.text]; _a < _b.length; _a++) { var specifier = _b[_a]; writeLine(); emitStart(specifier.name); emitContainingModuleName(specifier); write("."); emitNodeWithCommentsAndWithoutSourcemap(specifier.name); emitEnd(specifier.name); write(" = "); emitExpressionIdentifier(name); write(";"); } } } function emitExportSpecifierInSystemModule(specifier) { ts.Debug.assert(modulekind === ts.ModuleKind.System); if (!resolver.getReferencedValueDeclaration(specifier.propertyName || specifier.name) && !resolver.isValueAliasDeclaration(specifier)) { return; } writeLine(); emitStart(specifier.name); write(exportFunctionForFile + "(\""); emitNodeWithCommentsAndWithoutSourcemap(specifier.name); write("\", "); emitExpressionIdentifier(specifier.propertyName || specifier.name); write(")"); emitEnd(specifier.name); write(";"); } /** * Emit an assignment to a given identifier, 'name', with a given expression, 'value'. * @param name an identifier as a left-hand-side operand of the assignment * @param value an expression as a right-hand-side operand of the assignment * @param shouldEmitCommaBeforeAssignment a boolean indicating whether to prefix an assignment with comma */ function emitAssignment(name, value, shouldEmitCommaBeforeAssignment, nodeForSourceMap) { if (shouldEmitCommaBeforeAssignment) { write(", "); } var exportChanged = isNameOfExportedSourceLevelDeclarationInSystemExternalModule(name); if (exportChanged) { write(exportFunctionForFile + "(\""); emitNodeWithCommentsAndWithoutSourcemap(name); write("\", "); } var isVariableDeclarationOrBindingElement = name.parent && (name.parent.kind === 218 /* VariableDeclaration */ || name.parent.kind === 169 /* BindingElement */); // If this is first var declaration, we need to start at var/let/const keyword instead // otherwise use nodeForSourceMap as the start position emitStart(isFirstVariableDeclaration(nodeForSourceMap) ? nodeForSourceMap.parent : nodeForSourceMap); withTemporaryNoSourceMap(function () { if (isVariableDeclarationOrBindingElement) { emitModuleMemberName(name.parent); } else { emit(name); } write(" = "); emit(value); }); emitEnd(nodeForSourceMap, /*stopOverridingSpan*/ true); if (exportChanged) { write(")"); } } /** * Create temporary variable, emit an assignment of the variable the given expression * @param expression an expression to assign to the newly created temporary variable * @param canDefineTempVariablesInPlace a boolean indicating whether you can define the temporary variable at an assignment location * @param shouldEmitCommaBeforeAssignment a boolean indicating whether an assignment should prefix with comma */ function emitTempVariableAssignment(expression, canDefineTempVariablesInPlace, shouldEmitCommaBeforeAssignment, sourceMapNode) { var identifier = createTempVariable(0 /* Auto */); if (!canDefineTempVariablesInPlace) { recordTempDeclaration(identifier); } emitAssignment(identifier, expression, shouldEmitCommaBeforeAssignment, sourceMapNode || expression.parent); return identifier; } function isFirstVariableDeclaration(root) { return root.kind === 218 /* VariableDeclaration */ && root.parent.kind === 219 /* VariableDeclarationList */ && root.parent.declarations[0] === root; } function emitDestructuring(root, isAssignmentExpressionStatement, value) { var emitCount = 0; // An exported declaration is actually emitted as an assignment (to a property on the module object), so // temporary variables in an exported declaration need to have real declarations elsewhere // Also temporary variables should be explicitly allocated for source level declarations when module target is system // because actual variable declarations are hoisted var canDefineTempVariablesInPlace = false; if (root.kind === 218 /* VariableDeclaration */) { var isExported = ts.getCombinedNodeFlags(root) & 1 /* Export */; var isSourceLevelForSystemModuleKind = shouldHoistDeclarationInSystemJsModule(root); canDefineTempVariablesInPlace = !isExported && !isSourceLevelForSystemModuleKind; } else if (root.kind === 142 /* Parameter */) { canDefineTempVariablesInPlace = true; } if (root.kind === 187 /* BinaryExpression */) { emitAssignmentExpression(root); } else { ts.Debug.assert(!isAssignmentExpressionStatement); // If first variable declaration of variable statement correct the start location if (isFirstVariableDeclaration(root)) { // Use emit location of "var " as next emit start entry sourceMap.changeEmitSourcePos(); } emitBindingElement(root, value); } /** * Ensures that there exists a declared identifier whose value holds the given expression. * This function is useful to ensure that the expression's value can be read from in subsequent expressions. * Unless 'reuseIdentifierExpressions' is false, 'expr' will be returned if it is just an identifier. * * @param expr the expression whose value needs to be bound. * @param reuseIdentifierExpressions true if identifier expressions can simply be returned; * false if it is necessary to always emit an identifier. */ function ensureIdentifier(expr, reuseIdentifierExpressions, sourceMapNode) { if (expr.kind === 69 /* Identifier */ && reuseIdentifierExpressions) { return expr; } var identifier = emitTempVariableAssignment(expr, canDefineTempVariablesInPlace, emitCount > 0, sourceMapNode); emitCount++; return identifier; } function createDefaultValueCheck(value, defaultValue, sourceMapNode) { // The value expression will be evaluated twice, so for anything but a simple identifier // we need to generate a temporary variable // If the temporary variable needs to be emitted use the source Map node for assignment of that statement value = ensureIdentifier(value, /*reuseIdentifierExpressions*/ true, sourceMapNode); // Return the expression 'value === void 0 ? defaultValue : value' var equals = ts.createSynthesizedNode(187 /* BinaryExpression */); equals.left = value; equals.operatorToken = ts.createSynthesizedNode(32 /* EqualsEqualsEqualsToken */); equals.right = createVoidZero(); return createConditionalExpression(equals, defaultValue, value); } function createConditionalExpression(condition, whenTrue, whenFalse) { var cond = ts.createSynthesizedNode(188 /* ConditionalExpression */); cond.condition = condition; cond.questionToken = ts.createSynthesizedNode(53 /* QuestionToken */); cond.whenTrue = whenTrue; cond.colonToken = ts.createSynthesizedNode(54 /* ColonToken */); cond.whenFalse = whenFalse; return cond; } function createNumericLiteral(value) { var node = ts.createSynthesizedNode(8 /* NumericLiteral */); node.text = "" + value; return node; } function createPropertyAccessForDestructuringProperty(object, propName) { var index; var nameIsComputed = propName.kind === 140 /* ComputedPropertyName */; if (nameIsComputed) { // TODO to handle when we look into sourcemaps for computed properties, for now use propName index = ensureIdentifier(propName.expression, /*reuseIdentifierExpressions*/ false, propName); } else { // We create a synthetic copy of the identifier in order to avoid the rewriting that might // otherwise occur when the identifier is emitted. index = ts.createSynthesizedNode(propName.kind); // We need to unescape identifier here because when parsing an identifier prefixing with "__" // the parser need to append "_" in order to escape colliding with magic identifiers such as "__proto__" // Therefore, in order to correctly emit identifiers that are written in original TypeScript file, // we will unescapeIdentifier to remove additional underscore (if no underscore is added, the function will return original input string) index.text = ts.unescapeIdentifier(propName.text); } return !nameIsComputed && index.kind === 69 /* Identifier */ ? createPropertyAccessExpression(object, index) : createElementAccessExpression(object, index); } function createSliceCall(value, sliceIndex) { var call = ts.createSynthesizedNode(174 /* CallExpression */); var sliceIdentifier = ts.createSynthesizedNode(69 /* Identifier */); sliceIdentifier.text = "slice"; call.expression = createPropertyAccessExpression(value, sliceIdentifier); call.arguments = ts.createSynthesizedNodeArray(); call.arguments[0] = createNumericLiteral(sliceIndex); return call; } function emitObjectLiteralAssignment(target, value, sourceMapNode) { var properties = target.properties; if (properties.length !== 1) { // For anything but a single element destructuring we need to generate a temporary // to ensure value is evaluated exactly once. // When doing so we want to highlight the passed in source map node since thats the one needing this temp assignment value = ensureIdentifier(value, /*reuseIdentifierExpressions*/ true, sourceMapNode); } for (var _a = 0, properties_5 = properties; _a < properties_5.length; _a++) { var p = properties_5[_a]; if (p.kind === 253 /* PropertyAssignment */ || p.kind === 254 /* ShorthandPropertyAssignment */) { var propName = p.name; var target_1 = p.kind === 254 /* ShorthandPropertyAssignment */ ? p : p.initializer || propName; // Assignment for target = value.propName should highlight whole property, hence use p as source map node emitDestructuringAssignment(target_1, createPropertyAccessForDestructuringProperty(value, propName), p); } } } function emitArrayLiteralAssignment(target, value, sourceMapNode) { var elements = target.elements; if (elements.length !== 1) { // For anything but a single element destructuring we need to generate a temporary // to ensure value is evaluated exactly once. // When doing so we want to highlight the passed in source map node since thats the one needing this temp assignment value = ensureIdentifier(value, /*reuseIdentifierExpressions*/ true, sourceMapNode); } for (var i = 0; i < elements.length; i++) { var e = elements[i]; if (e.kind !== 193 /* OmittedExpression */) { // Assignment for target = value.propName should highlight whole property, hence use e as source map node if (e.kind !== 191 /* SpreadElementExpression */) { emitDestructuringAssignment(e, createElementAccessExpression(value, createNumericLiteral(i)), e); } else if (i === elements.length - 1) { emitDestructuringAssignment(e.expression, createSliceCall(value, i), e); } } } } function emitDestructuringAssignment(target, value, sourceMapNode) { // When emitting target = value use source map node to highlight, including any temporary assignments needed for this if (target.kind === 254 /* ShorthandPropertyAssignment */) { if (target.objectAssignmentInitializer) { value = createDefaultValueCheck(value, target.objectAssignmentInitializer, sourceMapNode); } target = target.name; } else if (target.kind === 187 /* BinaryExpression */ && target.operatorToken.kind === 56 /* EqualsToken */) { value = createDefaultValueCheck(value, target.right, sourceMapNode); target = target.left; } if (target.kind === 171 /* ObjectLiteralExpression */) { emitObjectLiteralAssignment(target, value, sourceMapNode); } else if (target.kind === 170 /* ArrayLiteralExpression */) { emitArrayLiteralAssignment(target, value, sourceMapNode); } else { emitAssignment(target, value, /*shouldEmitCommaBeforeAssignment*/ emitCount > 0, sourceMapNode); emitCount++; } } function emitAssignmentExpression(root) { var target = root.left; var value = root.right; if (ts.isEmptyObjectLiteralOrArrayLiteral(target)) { emit(value); } else if (isAssignmentExpressionStatement) { // Source map node for root.left = root.right is root // but if root is synthetic, which could be in below case, use the target which is { a } // for ({a} of {a: string}) { // } emitDestructuringAssignment(target, value, ts.nodeIsSynthesized(root) ? target : root); } else { if (root.parent.kind !== 178 /* ParenthesizedExpression */) { write("("); } // Temporary assignment needed to emit root should highlight whole binary expression value = ensureIdentifier(value, /*reuseIdentifierExpressions*/ true, root); // Source map node for root.left = root.right is root emitDestructuringAssignment(target, value, root); write(", "); emit(value); if (root.parent.kind !== 178 /* ParenthesizedExpression */) { write(")"); } } } function emitBindingElement(target, value) { // Any temporary assignments needed to emit target = value should point to target if (target.initializer) { // Combine value and initializer value = value ? createDefaultValueCheck(value, target.initializer, target) : target.initializer; } else if (!value) { // Use 'void 0' in absence of value and initializer value = createVoidZero(); } if (ts.isBindingPattern(target.name)) { var pattern = target.name; var elements = pattern.elements; var numElements = elements.length; if (numElements !== 1) { // For anything other than a single-element destructuring we need to generate a temporary // to ensure value is evaluated exactly once. Additionally, if we have zero elements // we need to emit *something* to ensure that in case a 'var' keyword was already emitted, // so in that case, we'll intentionally create that temporary. value = ensureIdentifier(value, /*reuseIdentifierExpressions*/ numElements !== 0, target); } for (var i = 0; i < numElements; i++) { var element = elements[i]; if (pattern.kind === 167 /* ObjectBindingPattern */) { // Rewrite element to a declaration with an initializer that fetches property var propName = element.propertyName || element.name; emitBindingElement(element, createPropertyAccessForDestructuringProperty(value, propName)); } else if (element.kind !== 193 /* OmittedExpression */) { if (!element.dotDotDotToken) { // Rewrite element to a declaration that accesses array element at index i emitBindingElement(element, createElementAccessExpression(value, createNumericLiteral(i))); } else if (i === numElements - 1) { emitBindingElement(element, createSliceCall(value, i)); } } } } else { emitAssignment(target.name, value, /*shouldEmitCommaBeforeAssignment*/ emitCount > 0, target); emitCount++; } } } function emitVariableDeclaration(node) { if (ts.isBindingPattern(node.name)) { var isExported = ts.getCombinedNodeFlags(node) & 1 /* Export */; if (languageVersion >= 2 /* ES6 */ && (!isExported || modulekind === ts.ModuleKind.ES6)) { // emit ES6 destructuring only if target module is ES6 or variable is not exported // exported variables in CJS/AMD are prefixed with 'exports.' so result javascript { exports.toString } = 1; is illegal var isTopLevelDeclarationInSystemModule = modulekind === ts.ModuleKind.System && shouldHoistVariable(node, /*checkIfSourceFileLevelDecl*/ true); if (isTopLevelDeclarationInSystemModule) { // In System modules top level variables are hoisted // so variable declarations with destructuring are turned into destructuring assignments. // As a result, they will need parentheses to disambiguate object binding assignments from blocks. write("("); } emit(node.name); emitOptional(" = ", node.initializer); if (isTopLevelDeclarationInSystemModule) { write(")"); } } else { emitDestructuring(node, /*isAssignmentExpressionStatement*/ false); } } else { var initializer = node.initializer; if (!initializer && languageVersion < 2 /* ES6 */ && // for names - binding patterns that lack initializer there is no point to emit explicit initializer // since downlevel codegen for destructuring will fail in the absence of initializer so all binding elements will say uninitialized node.name.kind === 69 /* Identifier */) { var container = ts.getEnclosingBlockScopeContainer(node); var flags = resolver.getNodeCheckFlags(node); // nested let bindings might need to be initialized explicitly to preserve ES6 semantic // { let x = 1; } // { let x; } // x here should be undefined. not 1 // NOTES: // Top level bindings never collide with anything and thus don't require explicit initialization. // As for nested let bindings there are two cases: // - nested let bindings that were not renamed definitely should be initialized explicitly // { let x = 1; } // { let x; if (some-condition) { x = 1}; if (x) { /*1*/ } } // Without explicit initialization code in /*1*/ can be executed even if some-condition is evaluated to false // - renaming introduces fresh name that should not collide with any existing names, however renamed bindings sometimes also should be // explicitly initialized. One particular case: non-captured binding declared inside loop body (but not in loop initializer) // let x; // for (;;) { // let x; // } // in downlevel codegen inner 'x' will be renamed so it won't collide with outer 'x' however it will should be reset on every iteration // as if it was declared anew. // * Why non-captured binding - because if loop contains block scoped binding captured in some function then loop body will be rewritten // to have a fresh scope on every iteration so everything will just work. // * Why loop initializer is excluded - since we've introduced a fresh name it already will be undefined. var isCapturedInFunction = flags & 131072 /* CapturedBlockScopedBinding */; var isDeclaredInLoop = flags & 262144 /* BlockScopedBindingInLoop */; var emittedAsTopLevel = ts.isBlockScopedContainerTopLevel(container) || (isCapturedInFunction && isDeclaredInLoop && container.kind === 199 /* Block */ && ts.isIterationStatement(container.parent, /*lookInLabeledStatements*/ false)); var emittedAsNestedLetDeclaration = ts.getCombinedNodeFlags(node) & 1024 /* Let */ && !emittedAsTopLevel; var emitExplicitInitializer = emittedAsNestedLetDeclaration && container.kind !== 207 /* ForInStatement */ && container.kind !== 208 /* ForOfStatement */ && (!resolver.isDeclarationWithCollidingName(node) || (isDeclaredInLoop && !isCapturedInFunction && !ts.isIterationStatement(container, /*lookInLabeledStatements*/ false))); if (emitExplicitInitializer) { initializer = createVoidZero(); } } var exportChanged = isNameOfExportedSourceLevelDeclarationInSystemExternalModule(node.name); if (exportChanged) { write(exportFunctionForFile + "(\""); emitNodeWithCommentsAndWithoutSourcemap(node.name); write("\", "); } emitModuleMemberName(node); emitOptional(" = ", initializer); if (exportChanged) { write(")"); } } } function emitExportVariableAssignments(node) { if (node.kind === 193 /* OmittedExpression */) { return; } var name = node.name; if (name.kind === 69 /* Identifier */) { emitExportMemberAssignments(name); } else if (ts.isBindingPattern(name)) { ts.forEach(name.elements, emitExportVariableAssignments); } } function isES6ExportedDeclaration(node) { return !!(node.flags & 1 /* Export */) && modulekind === ts.ModuleKind.ES6 && node.parent.kind === 256 /* SourceFile */; } function emitVariableStatement(node) { var startIsEmitted = false; if (node.flags & 1 /* Export */) { if (isES6ExportedDeclaration(node)) { // Exported ES6 module member write("export "); startIsEmitted = tryEmitStartOfVariableDeclarationList(node.declarationList); } } else { startIsEmitted = tryEmitStartOfVariableDeclarationList(node.declarationList); } if (startIsEmitted) { emitCommaList(node.declarationList.declarations); write(";"); } else { var atLeastOneItem = emitVariableDeclarationListSkippingUninitializedEntries(node.declarationList); if (atLeastOneItem) { write(";"); } } if (modulekind !== ts.ModuleKind.ES6 && node.parent === currentSourceFile) { ts.forEach(node.declarationList.declarations, emitExportVariableAssignments); } } function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node) { // If we're not exporting the variables, there's nothing special here. // Always emit comments for these nodes. if (!(node.flags & 1 /* Export */)) { return true; } // If we are exporting, but it's a top-level ES6 module exports, // we'll emit the declaration list verbatim, so emit comments too. if (isES6ExportedDeclaration(node)) { return true; } // Otherwise, only emit if we have at least one initializer present. for (var _a = 0, _b = node.declarationList.declarations; _a < _b.length; _a++) { var declaration = _b[_a]; if (declaration.initializer) { return true; } } return false; } function emitParameter(node) { if (languageVersion < 2 /* ES6 */) { if (ts.isBindingPattern(node.name)) { var name_29 = createTempVariable(0 /* Auto */); if (!tempParameters) { tempParameters = []; } tempParameters.push(name_29); emit(name_29); } else { emit(node.name); } } else { if (node.dotDotDotToken) { write("..."); } emit(node.name); emitOptional(" = ", node.initializer); } } function emitDefaultValueAssignments(node) { if (languageVersion < 2 /* ES6 */) { var tempIndex_1 = 0; ts.forEach(node.parameters, function (parameter) { // A rest parameter cannot have a binding pattern or an initializer, // so let's just ignore it. if (parameter.dotDotDotToken) { return; } var paramName = parameter.name, initializer = parameter.initializer; if (ts.isBindingPattern(paramName)) { // In cases where a binding pattern is simply '[]' or '{}', // we usually don't want to emit a var declaration; however, in the presence // of an initializer, we must emit that expression to preserve side effects. var hasBindingElements = paramName.elements.length > 0; if (hasBindingElements || initializer) { writeLine(); write("var "); if (hasBindingElements) { emitDestructuring(parameter, /*isAssignmentExpressionStatement*/ false, tempParameters[tempIndex_1]); } else { emit(tempParameters[tempIndex_1]); write(" = "); emit(initializer); } write(";"); } // Regardless of whether we will emit a var declaration for the binding pattern, we generate the temporary // variable for the parameter (see: emitParameter) tempIndex_1++; } else if (initializer) { writeLine(); emitStart(parameter); write("if ("); emitNodeWithoutSourceMap(paramName); write(" === void 0)"); emitEnd(parameter); write(" { "); emitStart(parameter); emitNodeWithCommentsAndWithoutSourcemap(paramName); write(" = "); emitNodeWithCommentsAndWithoutSourcemap(initializer); emitEnd(parameter); write("; }"); } }); } } function emitRestParameter(node) { if (languageVersion < 2 /* ES6 */ && ts.hasDeclaredRestParameter(node)) { var restParam = node.parameters[node.parameters.length - 1]; // A rest parameter cannot have a binding pattern, so let's just ignore it if it does. if (ts.isBindingPattern(restParam.name)) { return; } var skipThisCount = node.parameters.length && node.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */ ? 1 : 0; var restIndex = node.parameters.length - 1 - skipThisCount; var tempName = createTempVariable(268435456 /* _i */).text; writeLine(); emitLeadingComments(restParam); emitStart(restParam); write("var "); emitNodeWithCommentsAndWithoutSourcemap(restParam.name); write(" = [];"); emitEnd(restParam); emitTrailingComments(restParam); writeLine(); write("for ("); emitStart(restParam); write("var " + tempName + " = " + restIndex + ";"); emitEnd(restParam); write(" "); emitStart(restParam); write(tempName + " < arguments.length;"); emitEnd(restParam); write(" "); emitStart(restParam); write(tempName + "++"); emitEnd(restParam); write(") {"); increaseIndent(); writeLine(); emitStart(restParam); emitNodeWithCommentsAndWithoutSourcemap(restParam.name); write("[" + tempName + " - " + restIndex + "] = arguments[" + tempName + "];"); emitEnd(restParam); decreaseIndent(); writeLine(); write("}"); } } function emitAccessor(node) { write(node.kind === 149 /* GetAccessor */ ? "get " : "set "); emit(node.name); emitSignatureAndBody(node); } function shouldEmitAsArrowFunction(node) { return node.kind === 180 /* ArrowFunction */ && languageVersion >= 2 /* ES6 */; } function emitDeclarationName(node) { if (node.name) { emitNodeWithCommentsAndWithoutSourcemap(node.name); } else { write(getGeneratedNameForNode(node)); } } function shouldEmitFunctionName(node) { if (node.kind === 179 /* FunctionExpression */) { // Emit name if one is present return !!node.name; } if (node.kind === 220 /* FunctionDeclaration */) { // Emit name if one is present, or emit generated name in down-level case (for export default case) return !!node.name || modulekind !== ts.ModuleKind.ES6; } } function emitFunctionDeclaration(node) { if (ts.nodeIsMissing(node.body)) { return emitCommentsOnNotEmittedNode(node); } // TODO (yuisu) : we should not have special cases to condition emitting comments // but have one place to fix check for these conditions. var kind = node.kind, parent = node.parent; if (kind !== 147 /* MethodDeclaration */ && kind !== 146 /* MethodSignature */ && parent && parent.kind !== 253 /* PropertyAssignment */ && parent.kind !== 174 /* CallExpression */ && parent.kind !== 170 /* ArrayLiteralExpression */) { // 1. Methods will emit comments at their assignment declaration sites. // // 2. If the function is a property of object literal, emitting leading-comments // is done by emitNodeWithoutSourceMap which then call this function. // In particular, we would like to avoid emit comments twice in following case: // // var obj = { // id: // /*comment*/ () => void // } // // 3. If the function is an argument in call expression, emitting of comments will be // taken care of in emit list of arguments inside of 'emitCallExpression'. // // 4. If the function is in an array literal, 'emitLinePreservingList' will take care // of leading comments. emitLeadingComments(node); } emitStart(node); // For targeting below es6, emit functions-like declaration including arrow function using function keyword. // When targeting ES6, emit arrow function natively in ES6 by omitting function keyword and using fat arrow instead if (!shouldEmitAsArrowFunction(node)) { if (isES6ExportedDeclaration(node)) { write("export "); if (node.flags & 512 /* Default */) { write("default "); } } write("function"); if (languageVersion >= 2 /* ES6 */ && node.asteriskToken) { write("*"); } write(" "); } if (shouldEmitFunctionName(node)) { emitDeclarationName(node); } emitSignatureAndBody(node); if (modulekind !== ts.ModuleKind.ES6 && kind === 220 /* FunctionDeclaration */ && parent === currentSourceFile && node.name) { emitExportMemberAssignments(node.name); } emitEnd(node); if (kind !== 147 /* MethodDeclaration */ && kind !== 146 /* MethodSignature */ && kind !== 180 /* ArrowFunction */) { emitTrailingComments(node); } } function emitCaptureThisForNodeIfNecessary(node) { if (resolver.getNodeCheckFlags(node) & 4 /* CaptureThis */) { writeLine(); emitStart(node); write("var _this = this;"); emitEnd(node); } } function emitSignatureParameters(node) { increaseIndent(); write("("); if (node) { var parameters = node.parameters; var skipCount = node.parameters.length && node.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */ ? 1 : 0; var omitCount = languageVersion < 2 /* ES6 */ && ts.hasDeclaredRestParameter(node) ? 1 : 0; emitList(parameters, skipCount, parameters.length - omitCount - skipCount, /*multiLine*/ false, /*trailingComma*/ false); } write(")"); decreaseIndent(); } function emitSignatureParametersForArrow(node) { // Check whether the parameter list needs parentheses and preserve no-parenthesis if (node.parameters.length === 1 && node.pos === node.parameters[0].pos) { emit(node.parameters[0]); return; } emitSignatureParameters(node); } function emitAsyncFunctionBodyForES6(node) { var promiseConstructor = ts.getEntityNameFromTypeNode(node.type); var isArrowFunction = node.kind === 180 /* ArrowFunction */; var hasLexicalArguments = (resolver.getNodeCheckFlags(node) & 8192 /* CaptureArguments */) !== 0; // An async function is emit as an outer function that calls an inner // generator function. To preserve lexical bindings, we pass the current // `this` and `arguments` objects to `__awaiter`. The generator function // passed to `__awaiter` is executed inside of the callback to the // promise constructor. // // The emit for an async arrow without a lexical `arguments` binding might be: // // // input // let a = async (b) => { await b; } // // // output // let a = (b) => __awaiter(this, void 0, void 0, function* () { // yield b; // }); // // The emit for an async arrow with a lexical `arguments` binding might be: // // // input // let a = async (b) => { await arguments[0]; } // // // output // let a = (b) => __awaiter(this, arguments, void 0, function* (arguments) { // yield arguments[0]; // }); // // The emit for an async function expression without a lexical `arguments` binding // might be: // // // input // let a = async function (b) { // await b; // } // // // output // let a = function (b) { // return __awaiter(this, void 0, void 0, function* () { // yield b; // }); // } // // The emit for an async function expression with a lexical `arguments` binding // might be: // // // input // let a = async function (b) { // await arguments[0]; // } // // // output // let a = function (b) { // return __awaiter(this, arguments, void 0, function* (_arguments) { // yield _arguments[0]; // }); // } // // The emit for an async function expression with a lexical `arguments` binding // and a return type annotation might be: // // // input // let a = async function (b): MyPromise<any> { // await arguments[0]; // } // // // output // let a = function (b) { // return __awaiter(this, arguments, MyPromise, function* (_arguments) { // yield _arguments[0]; // }); // } // // If this is not an async arrow, emit the opening brace of the function body // and the start of the return statement. if (!isArrowFunction) { write(" {"); increaseIndent(); writeLine(); if (resolver.getNodeCheckFlags(node) & 4096 /* AsyncMethodWithSuperBinding */) { writeLines("\nconst _super = (function (geti, seti) {\n const cache = Object.create(null);\n return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } });\n})(name => super[name], (name, value) => super[name] = value);"); writeLine(); } else if (resolver.getNodeCheckFlags(node) & 2048 /* AsyncMethodWithSuper */) { write("const _super = name => super[name];"); writeLine(); } write("return"); } write(" __awaiter(this"); if (hasLexicalArguments) { write(", arguments, "); } else { write(", void 0, "); } if (languageVersion >= 2 /* ES6 */ || !promiseConstructor) { write("void 0"); } else { emitEntityNameAsExpression(promiseConstructor, /*useFallback*/ false); } // Emit the call to __awaiter. write(", function* ()"); // Emit the signature and body for the inner generator function. emitFunctionBody(node); write(")"); // If this is not an async arrow, emit the closing brace of the outer function body. if (!isArrowFunction) { write(";"); decreaseIndent(); writeLine(); write("}"); } } function emitFunctionBody(node) { if (!node.body) { // There can be no body when there are parse errors. Just emit an empty block // in that case. write(" { }"); } else { if (node.body.kind === 199 /* Block */) { emitBlockFunctionBody(node, node.body); } else { emitExpressionFunctionBody(node, node.body); } } } function emitSignatureAndBody(node) { var saveConvertedLoopState = convertedLoopState; var saveTempFlags = tempFlags; var saveTempVariables = tempVariables; var saveTempParameters = tempParameters; convertedLoopState = undefined; tempFlags = 0; tempVariables = undefined; tempParameters = undefined; // When targeting ES6, emit arrow function natively in ES6 if (shouldEmitAsArrowFunction(node)) { emitSignatureParametersForArrow(node); write(" =>"); } else { emitSignatureParameters(node); } var isAsync = ts.isAsyncFunctionLike(node); if (isAsync) { emitAsyncFunctionBodyForES6(node); } else { emitFunctionBody(node); } if (!isES6ExportedDeclaration(node)) { emitExportMemberAssignment(node); } ts.Debug.assert(convertedLoopState === undefined); convertedLoopState = saveConvertedLoopState; tempFlags = saveTempFlags; tempVariables = saveTempVariables; tempParameters = saveTempParameters; } // Returns true if any preamble code was emitted. function emitFunctionBodyPreamble(node) { emitCaptureThisForNodeIfNecessary(node); emitDefaultValueAssignments(node); emitRestParameter(node); } function emitExpressionFunctionBody(node, body) { if (languageVersion < 2 /* ES6 */ || node.flags & 256 /* Async */) { emitDownLevelExpressionFunctionBody(node, body); return; } // For es6 and higher we can emit the expression as is. However, in the case // where the expression might end up looking like a block when emitted, we'll // also wrap it in parentheses first. For example if you have: a => <foo>{} // then we need to generate: a => ({}) write(" "); // Unwrap all type assertions. var current = body; while (current.kind === 177 /* TypeAssertionExpression */) { current = current.expression; } emitParenthesizedIf(body, current.kind === 171 /* ObjectLiteralExpression */); } function emitDownLevelExpressionFunctionBody(node, body) { write(" {"); increaseIndent(); var outPos = writer.getTextPos(); emitDetachedCommentsAndUpdateCommentsInfo(node.body); emitFunctionBodyPreamble(node); var preambleEmitted = writer.getTextPos() !== outPos; decreaseIndent(); // If we didn't have to emit any preamble code, then attempt to keep the arrow // function on one line. if (!preambleEmitted && nodeStartPositionsAreOnSameLine(node, body)) { write(" "); emitStart(body); write("return "); emit(body); emitEnd(body); write(";"); emitTempDeclarations(/*newLine*/ false); write(" "); } else { increaseIndent(); writeLine(); emitLeadingComments(node.body); emitStart(body); write("return "); emit(body); emitEnd(body); write(";"); emitTrailingComments(node.body); emitTempDeclarations(/*newLine*/ true); decreaseIndent(); writeLine(); } emitStart(node.body); write("}"); emitEnd(node.body); } function emitBlockFunctionBody(node, body) { write(" {"); var initialTextPos = writer.getTextPos(); increaseIndent(); emitDetachedCommentsAndUpdateCommentsInfo(body.statements); // Emit all the directive prologues (like "use strict"). These have to come before // any other preamble code we write (like parameter initializers). var startIndex = emitDirectivePrologues(body.statements, /*startWithNewLine*/ true); emitFunctionBodyPreamble(node); decreaseIndent(); var preambleEmitted = writer.getTextPos() !== initialTextPos; if (!preambleEmitted && nodeEndIsOnSameLineAsNodeStart(body, body)) { for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { var statement = _b[_a]; write(" "); emit(statement); } emitTempDeclarations(/*newLine*/ false); write(" "); emitLeadingCommentsOfPosition(body.statements.end); } else { increaseIndent(); emitLinesStartingAt(body.statements, startIndex); emitTempDeclarations(/*newLine*/ true); writeLine(); emitLeadingCommentsOfPosition(body.statements.end); decreaseIndent(); } emitToken(16 /* CloseBraceToken */, body.statements.end); } /** * Return the statement at a given index if it is a super-call statement * @param ctor a constructor declaration * @param index an index to constructor's body to check */ function getSuperCallAtGivenIndex(ctor, index) { if (!ctor.body) { return undefined; } var statements = ctor.body.statements; if (!statements || index >= statements.length) { return undefined; } var statement = statements[index]; if (statement.kind === 202 /* ExpressionStatement */) { return ts.isSuperCallExpression(statement.expression) ? statement : undefined; } } function emitParameterPropertyAssignments(node) { ts.forEach(node.parameters, function (param) { if (param.flags & 92 /* ParameterPropertyModifier */) { writeLine(); emitStart(param); emitStart(param.name); write("this."); emitNodeWithoutSourceMap(param.name); emitEnd(param.name); write(" = "); emit(param.name); write(";"); emitEnd(param); } }); } function emitMemberAccessForPropertyName(memberName) { // This does not emit source map because it is emitted by caller as caller // is aware how the property name changes to the property access // eg. public x = 10; becomes this.x and static x = 10 becomes className.x if (memberName.kind === 9 /* StringLiteral */ || memberName.kind === 8 /* NumericLiteral */) { write("["); emitNodeWithCommentsAndWithoutSourcemap(memberName); write("]"); } else if (memberName.kind === 140 /* ComputedPropertyName */) { emitComputedPropertyName(memberName); } else { write("."); emitNodeWithCommentsAndWithoutSourcemap(memberName); } } function getInitializedProperties(node, isStatic) { var properties = []; for (var _a = 0, _b = node.members; _a < _b.length; _a++) { var member = _b[_a]; if (member.kind === 145 /* PropertyDeclaration */ && isStatic === ((member.flags & 32 /* Static */) !== 0) && member.initializer) { properties.push(member); } } return properties; } function emitPropertyDeclarations(node, properties) { for (var _a = 0, properties_6 = properties; _a < properties_6.length; _a++) { var property = properties_6[_a]; emitPropertyDeclaration(node, property); } } function emitPropertyDeclaration(node, property, receiver, isExpression) { writeLine(); emitLeadingComments(property); emitStart(property); emitStart(property.name); if (receiver) { write(receiver); } else { if (property.flags & 32 /* Static */) { emitDeclarationName(node); } else { write("this"); } } emitMemberAccessForPropertyName(property.name); emitEnd(property.name); write(" = "); emit(property.initializer); if (!isExpression) { write(";"); } emitEnd(property); emitTrailingComments(property); } function emitMemberFunctionsForES5AndLower(node) { ts.forEach(node.members, function (member) { if (member.kind === 198 /* SemicolonClassElement */) { writeLine(); write(";"); } else if (member.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */) { if (!member.body) { return emitCommentsOnNotEmittedNode(member); } writeLine(); emitLeadingComments(member); emitStart(member); emitStart(member.name); emitClassMemberPrefix(node, member); emitMemberAccessForPropertyName(member.name); emitEnd(member.name); write(" = "); emitFunctionDeclaration(member); emitEnd(member); write(";"); emitTrailingComments(member); } else if (member.kind === 149 /* GetAccessor */ || member.kind === 150 /* SetAccessor */) { var accessors = ts.getAllAccessorDeclarations(node.members, member); if (member === accessors.firstAccessor) { writeLine(); emitStart(member); write("Object.defineProperty("); emitStart(member.name); emitClassMemberPrefix(node, member); write(", "); emitExpressionForPropertyName(member.name); emitEnd(member.name); write(", {"); increaseIndent(); if (accessors.getAccessor) { writeLine(); emitLeadingComments(accessors.getAccessor); write("get: "); emitStart(accessors.getAccessor); write("function "); emitSignatureAndBody(accessors.getAccessor); emitEnd(accessors.getAccessor); emitTrailingComments(accessors.getAccessor); write(","); } if (accessors.setAccessor) { writeLine(); emitLeadingComments(accessors.setAccessor); write("set: "); emitStart(accessors.setAccessor); write("function "); emitSignatureAndBody(accessors.setAccessor); emitEnd(accessors.setAccessor); emitTrailingComments(accessors.setAccessor); write(","); } writeLine(); write("enumerable: true,"); writeLine(); write("configurable: true"); decreaseIndent(); writeLine(); write("});"); emitEnd(member); } } }); } function emitMemberFunctionsForES6AndHigher(node) { for (var _a = 0, _b = node.members; _a < _b.length; _a++) { var member = _b[_a]; if ((member.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */) && !member.body) { emitCommentsOnNotEmittedNode(member); } else if (member.kind === 147 /* MethodDeclaration */ || member.kind === 149 /* GetAccessor */ || member.kind === 150 /* SetAccessor */) { writeLine(); emitLeadingComments(member); emitStart(member); if (member.flags & 32 /* Static */) { write("static "); } if (member.kind === 149 /* GetAccessor */) { write("get "); } else if (member.kind === 150 /* SetAccessor */) { write("set "); } if (member.asteriskToken) { write("*"); } emit(member.name); emitSignatureAndBody(member); emitEnd(member); emitTrailingComments(member); } else if (member.kind === 198 /* SemicolonClassElement */) { writeLine(); write(";"); } } } function emitConstructor(node, baseTypeElement) { var saveConvertedLoopState = convertedLoopState; var saveTempFlags = tempFlags; var saveTempVariables = tempVariables; var saveTempParameters = tempParameters; convertedLoopState = undefined; tempFlags = 0; tempVariables = undefined; tempParameters = undefined; emitConstructorWorker(node, baseTypeElement); ts.Debug.assert(convertedLoopState === undefined); convertedLoopState = saveConvertedLoopState; tempFlags = saveTempFlags; tempVariables = saveTempVariables; tempParameters = saveTempParameters; } function emitConstructorWorker(node, baseTypeElement) { // Check if we have property assignment inside class declaration. // If there is property assignment, we need to emit constructor whether users define it or not // If there is no property assignment, we can omit constructor if users do not define it var hasInstancePropertyWithInitializer = false; // Emit the constructor overload pinned comments ts.forEach(node.members, function (member) { if (member.kind === 148 /* Constructor */ && !member.body) { emitCommentsOnNotEmittedNode(member); } // Check if there is any non-static property assignment if (member.kind === 145 /* PropertyDeclaration */ && member.initializer && (member.flags & 32 /* Static */) === 0) { hasInstancePropertyWithInitializer = true; } }); var ctor = ts.getFirstConstructorWithBody(node); // For target ES6 and above, if there is no user-defined constructor and there is no property assignment // do not emit constructor in class declaration. if (languageVersion >= 2 /* ES6 */ && !ctor && !hasInstancePropertyWithInitializer) { return; } if (ctor) { emitLeadingComments(ctor); } emitStart(ctor || node); if (languageVersion < 2 /* ES6 */) { write("function "); emitDeclarationName(node); emitSignatureParameters(ctor); } else { write("constructor"); if (ctor) { emitSignatureParameters(ctor); } else { // The ES2015 spec specifies in 14.5.14. Runtime Semantics: ClassDefinitionEvaluation: // If constructor is empty, then // If ClassHeritag_eopt is present and protoParent is not null, then // Let constructor be the result of parsing the source text // constructor(...args) { super (...args);} // using the syntactic grammar with the goal symbol MethodDefinition[~Yield]. // Else, // Let constructor be the result of parsing the source text // constructor( ){ } // using the syntactic grammar with the goal symbol MethodDefinition[~Yield]. // // While we could emit the '...args' rest parameter, certain later tools in the pipeline might // downlevel the '...args' portion less efficiently by naively copying the contents of 'arguments' to an array. // Instead, we'll avoid using a rest parameter and spread into the super call as // 'super(...arguments)' instead of 'super(...args)', as you can see below. write("()"); } } var startIndex = 0; write(" {"); increaseIndent(); if (ctor) { // Emit all the directive prologues (like "use strict"). These have to come before // any other preamble code we write (like parameter initializers). startIndex = emitDirectivePrologues(ctor.body.statements, /*startWithNewLine*/ true); emitDetachedCommentsAndUpdateCommentsInfo(ctor.body.statements); } emitCaptureThisForNodeIfNecessary(node); var superCall; if (ctor) { emitDefaultValueAssignments(ctor); emitRestParameter(ctor); if (baseTypeElement) { superCall = getSuperCallAtGivenIndex(ctor, startIndex); if (superCall) { writeLine(); emit(superCall); } } emitParameterPropertyAssignments(ctor); } else { if (baseTypeElement) { writeLine(); emitStart(baseTypeElement); if (languageVersion < 2 /* ES6 */) { write("_super.apply(this, arguments);"); } else { // See comment above on using '...arguments' instead of '...args'. write("super(...arguments);"); } emitEnd(baseTypeElement); } } emitPropertyDeclarations(node, getInitializedProperties(node, /*isStatic*/ false)); if (ctor) { var statements = ctor.body.statements; if (superCall) { statements = statements.slice(1); } emitLinesStartingAt(statements, startIndex); } emitTempDeclarations(/*newLine*/ true); writeLine(); if (ctor) { emitLeadingCommentsOfPosition(ctor.body.statements.end); } decreaseIndent(); emitToken(16 /* CloseBraceToken */, ctor ? ctor.body.statements.end : node.members.end); emitEnd(ctor || node); if (ctor) { emitTrailingComments(ctor); } } function emitClassExpression(node) { return emitClassLikeDeclaration(node); } function emitClassDeclaration(node) { return emitClassLikeDeclaration(node); } function emitClassLikeDeclaration(node) { if (languageVersion < 2 /* ES6 */) { emitClassLikeDeclarationBelowES6(node); } else { emitClassLikeDeclarationForES6AndHigher(node); } if (modulekind !== ts.ModuleKind.ES6 && node.parent === currentSourceFile && node.name) { emitExportMemberAssignments(node.name); } } function emitClassLikeDeclarationForES6AndHigher(node) { var decoratedClassAlias; var isHoistedDeclarationInSystemModule = shouldHoistDeclarationInSystemJsModule(node); var isDecorated = ts.nodeIsDecorated(node); var rewriteAsClassExpression = isDecorated || isHoistedDeclarationInSystemModule; if (node.kind === 221 /* ClassDeclaration */) { if (rewriteAsClassExpression) { // When we emit an ES6 class that has a class decorator, we must tailor the // emit to certain specific cases. // // In the simplest case, we emit the class declaration as a let declaration, and // evaluate decorators after the close of the class body: // // TypeScript
javascript
{ "resource": "" }
q1567
makeTempVariableName
train
function makeTempVariableName(flags) { if (flags && !(tempFlags & flags)) { var name_24 = flags === 268435456 /* _i */ ? "_i" : "_n"; if (isUniqueName(name_24)) { tempFlags |= flags; return name_24; } }
javascript
{ "resource": "" }
q1568
writeEmittedFiles
train
function writeEmittedFiles(emitOutput, jsFilePath, sourceMapFilePath, writeByteOrderMark, sourceFiles) { if (compilerOptions.sourceMap && !compilerOptions.inlineSourceMap) { ts.writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false, sourceFiles); }
javascript
{ "resource": "" }
q1569
emitToken
train
function emitToken(tokenKind, startPos, emitFn) { var tokenStartPos = ts.skipTrivia(currentText, startPos); emitPos(tokenStartPos); var tokenString = ts.tokenToString(tokenKind); if (emitFn) { emitFn(); }
javascript
{ "resource": "" }
q1570
isImportedReference
train
function isImportedReference(node) { var declaration = resolver.getReferencedImportDeclaration(node); return declaration
javascript
{ "resource": "" }
q1571
tryEmitStartOfVariableDeclarationList
train
function tryEmitStartOfVariableDeclarationList(decl) { if (shouldHoistVariable(decl, /*checkIfSourceFileLevelDecl*/ true)) { // variables in variable declaration list were already hoisted return false; } if (convertedLoopState && (ts.getCombinedNodeFlags(decl) & 3072 /* BlockScoped */) === 0) { // we are inside a converted loop - this can only happen in downlevel scenarios // record names for all variable declarations for (var _a = 0, _b = decl.declarations; _a < _b.length; _a++) { var varDecl = _b[_a]; hoistVariableDeclarationFromLoop(convertedLoopState, varDecl); } return false; } emitStart(decl);
javascript
{ "resource": "" }
q1572
emitAssignment
train
function emitAssignment(name, value, shouldEmitCommaBeforeAssignment, nodeForSourceMap) { if (shouldEmitCommaBeforeAssignment) { write(", "); } var exportChanged = isNameOfExportedSourceLevelDeclarationInSystemExternalModule(name); if (exportChanged) { write(exportFunctionForFile + "(\""); emitNodeWithCommentsAndWithoutSourcemap(name); write("\", "); } var isVariableDeclarationOrBindingElement = name.parent && (name.parent.kind === 218 /* VariableDeclaration */ || name.parent.kind === 169 /* BindingElement */); // If this is first var declaration, we need to start at var/let/const keyword instead // otherwise use nodeForSourceMap as the start position
javascript
{ "resource": "" }
q1573
getSuperCallAtGivenIndex
train
function getSuperCallAtGivenIndex(ctor, index) { if (!ctor.body) { return undefined; } var statements = ctor.body.statements; if (!statements || index >= statements.length) { return undefined; } var statement = statements[index];
javascript
{ "resource": "" }
q1574
isTripleSlashComment
train
function isTripleSlashComment(comment) { // Verify this is /// comment, but do the regexp match only when we first can find /// in the comment text // so that we don't end up computing comment string and doing match for all // comments if (currentText.charCodeAt(comment.pos + 1) === 47 /* slash */ && comment.pos + 2 < comment.end && currentText.charCodeAt(comment.pos + 2) === 47 /* slash */) {
javascript
{ "resource": "" }
q1575
getAutomaticTypeDirectiveNames
train
function getAutomaticTypeDirectiveNames(options, host) { // Use explicit type list from tsconfig.json if (options.types) { return options.types; } // Walk the primary type lookup locations var result = []; if (host.directoryExists && host.getDirectories) { var typeRoots = getEffectiveTypeRoots(options, host); if (typeRoots) { for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { var root = typeRoots_1[_i]; if (host.directoryExists(root)) { for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { var typeDirectivePath = _b[_a]; var normalized = ts.normalizePath(typeDirectivePath); var packageJsonPath = ts.pathToPackageJson(ts.combinePaths(root, normalized));
javascript
{ "resource": "" }
q1576
findSourceFile
train
function findSourceFile(fileName, path, isDefaultLib, isReference, refFile, refPos, refEnd) { if (filesByName.contains(path)) { var file_1 = filesByName.get(path); // try to check if we've already seen this file but with a different casing in path // NOTE: this only makes sense for case-insensitive file systems if (file_1 && options.forceConsistentCasingInFileNames && ts.getNormalizedAbsolutePath(file_1.fileName, currentDirectory) !== ts.getNormalizedAbsolutePath(fileName, currentDirectory)) { reportFileNamesDifferOnlyInCasingError(fileName, file_1.fileName, refFile, refPos, refEnd); } // If the file was previously found via a node_modules search, but is now being processed as a root file, // then everything it sucks in may also be marked incorrectly, and needs to be checked again. if (file_1 && sourceFilesFoundSearchingNodeModules[file_1.path] && currentNodeModulesDepth == 0) { sourceFilesFoundSearchingNodeModules[file_1.path] = false; if (!options.noResolve) { processReferencedFiles(file_1, ts.getDirectoryPath(fileName), isDefaultLib); processTypeReferenceDirectives(file_1); } modulesWithElidedImports[file_1.path] = false; processImportedModules(file_1, ts.getDirectoryPath(fileName)); } else if (file_1 && modulesWithElidedImports[file_1.path]) { if (currentNodeModulesDepth < maxNodeModulesJsDepth) { modulesWithElidedImports[file_1.path] = false; processImportedModules(file_1, ts.getDirectoryPath(fileName)); } } return file_1; } // We haven't looked for this file, do so now and cache result var file = host.getSourceFile(fileName, options.target, function (hostErrorMessage) { if (refFile !== undefined && refPos !== undefined && refEnd !== undefined) { fileProcessingDiagnostics.add(ts.createFileDiagnostic(refFile, refPos, refEnd - refPos, ts.Diagnostics.Cannot_read_file_0_Colon_1, fileName, hostErrorMessage)); } else { fileProcessingDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_read_file_0_Colon_1,
javascript
{ "resource": "" }
q1577
verifyEmitFilePath
train
function verifyEmitFilePath(emitFileName, emitFilesSeen) { if (emitFileName) { var emitFilePath = ts.toPath(emitFileName, currentDirectory, getCanonicalFileName); // Report error if the output overwrites input file if (filesByName.contains(emitFilePath)) { createEmitBlockingDiagnostics(emitFileName, emitFilePath, ts.Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file);
javascript
{ "resource": "" }
q1578
parseConfigFileTextToJson
train
function parseConfigFileTextToJson(fileName, jsonText, stripComments) { if (stripComments === void 0) { stripComments = true; } try { var jsonTextToParse = stripComments ? removeComments(jsonText) : jsonText; return { config: /\S/.test(jsonTextToParse) ? JSON.parse(jsonTextToParse) : {} }; }
javascript
{ "resource": "" }
q1579
matchFileNames
train
function matchFileNames(fileNames, include, exclude, basePath, options, host, errors) { basePath = ts.normalizePath(basePath); // The exclude spec list is converted into a regular expression, which allows us to quickly // test whether a file or directory should be excluded before recursively traversing the // file system. var keyMapper = host.useCaseSensitiveFileNames ? caseSensitiveKeyMapper : caseInsensitiveKeyMapper; // Literal file names (provided via the "files" array in tsconfig.json) are stored in a // file map with a possibly case insensitive key. We use this map later when when including // wildcard paths. var literalFileMap = ts.createMap(); // Wildcard paths (provided via the "includes" array in tsconfig.json) are stored in a // file map with a possibly case insensitive key. We use this map to store paths matched // via wildcard, and to handle extension priority. var wildcardFileMap = ts.createMap(); if (include) { include = validateSpecs(include, errors, /*allowTrailingRecursion*/ false); } if (exclude) { exclude = validateSpecs(exclude, errors, /*allowTrailingRecursion*/ true); } // Wildcard directories (provided as part of a wildcard path) are stored in a // file map that marks whether it was a regular wildcard match (with a `*` or `?` token), // or a recursive directory. This information is used by filesystem watchers to monitor for // new entries in these paths. var wildcardDirectories = getWildcardDirectories(include, exclude, basePath, host.useCaseSensitiveFileNames); // Rather than requery this for each file and filespec, we query the supported extensions // once and store it on the expansion context. var supportedExtensions = ts.getSupportedExtensions(options); // Literal files are always included verbatim. An "include" or "exclude" specification cannot // remove a literal file. if (fileNames) { for (var _i = 0, fileNames_1 = fileNames; _i < fileNames_1.length; _i++) { var fileName = fileNames_1[_i]; var file = ts.combinePaths(basePath, fileName); literalFileMap[keyMapper(file)] = file; } } if (include && include.length > 0) { for (var _a = 0, _b = host.readDirectory(basePath, supportedExtensions, exclude, include); _a < _b.length; _a++) { var file = _b[_a]; // If we have already included a literal or
javascript
{ "resource": "" }
q1580
getWildcardDirectories
train
function getWildcardDirectories(include, exclude, path, useCaseSensitiveFileNames) { // We watch a directory recursively if it contains a wildcard anywhere in a directory segment // of the pattern: // // /a/b/**/d - Watch /a/b recursively to catch changes to any d in any subfolder recursively // /a/b/*/d - Watch /a/b recursively to catch any d in any immediate subfolder, even if a new subfolder is added // // We watch a directory without recursion if it contains a wildcard in the file segment of // the pattern: // // /a/b/* - Watch /a/b directly to catch any new file // /a/b/a?z - Watch /a/b directly to catch any new file matching a?z var rawExcludeRegex = ts.getRegularExpressionForWildcard(exclude, path, "exclude"); var excludeRegex = rawExcludeRegex && new RegExp(rawExcludeRegex, useCaseSensitiveFileNames ? "" : "i"); var wildcardDirectories = ts.createMap(); if (include !== undefined) { var recursiveKeys = []; for (var _i = 0, include_1 = include; _i < include_1.length; _i++) { var file = include_1[_i]; var name_36 = ts.normalizePath(ts.combinePaths(path, file)); if (excludeRegex && excludeRegex.test(name_36)) { continue; } var match = wildcardDirectoryPattern.exec(name_36); if (match) { var key = useCaseSensitiveFileNames ? match[0] : match[0].toLowerCase(); var flags = watchRecursivePattern.test(name_36) ? 1 /* Recursive */ : 0 /* None */; var existingFlags = wildcardDirectories[key]; if (existingFlags === undefined || existingFlags < flags) {
javascript
{ "resource": "" }
q1581
hasFileWithHigherPriorityExtension
train
function hasFileWithHigherPriorityExtension(file, literalFiles, wildcardFiles, extensions, keyMapper) { var extensionPriority = ts.getExtensionPriority(file, extensions); var adjustedExtensionPriority = ts.adjustExtensionPriority(extensionPriority); for (var i = 0 /* Highest */; i < adjustedExtensionPriority; i++) { var higherPriorityExtension = extensions[i];
javascript
{ "resource": "" }
q1582
removeWildcardFilesWithLowerPriorityExtension
train
function removeWildcardFilesWithLowerPriorityExtension(file, wildcardFiles, extensions, keyMapper) { var extensionPriority = ts.getExtensionPriority(file, extensions); var nextExtensionPriority = ts.getNextLowestExtensionPriority(extensionPriority);
javascript
{ "resource": "" }
q1583
startNode
train
function startNode(node) { var navNode = emptyNavigationBarNode(node); pushChild(parent, navNode); // Save the old
javascript
{ "resource": "" }
q1584
endNode
train
function endNode() { if (parent.children) { mergeChildren(parent.children);
javascript
{ "resource": "" }
q1585
mergeChildren
train
function mergeChildren(children) { var nameToItems = ts.createMap(); ts.filterMutate(children, function (child) { var decl = child.node; var name = decl.name && nodeText(decl.name); if (!name) { // Anonymous items are never merged. return true; } var itemsWithSameName = nameToItems[name]; if (!itemsWithSameName) { nameToItems[name] = child; return true; } if (itemsWithSameName instanceof Array) { for (var _i = 0, itemsWithSameName_1 = itemsWithSameName; _i < itemsWithSameName_1.length; _i++) { var itemWithSameName = itemsWithSameName_1[_i]; if (tryMerge(itemWithSameName, child)) { return false; } } itemsWithSameName.push(child); return true; } else { var itemWithSameName = itemsWithSameName; if (tryMerge(itemWithSameName, child)) { return false; } nameToItems[name] = [itemWithSameName, child]; return true; } function tryMerge(a, b) { if (shouldReallyMerge(a.node, b.node)) { merge(a, b); return true; } return false; } }); /** a and b have the same name, but they may not be mergeable. */ function shouldReallyMerge(a, b) {
javascript
{ "resource": "" }
q1586
shouldReallyMerge
train
function shouldReallyMerge(a, b) { return a.kind === b.kind && (a.kind !== 225 /* ModuleDeclaration */ || areSameModule(a, b)); // We use 1 NavNode to represent 'A.B.C', but there are multiple source nodes. // Only merge module nodes that have the same chain. Don't merge 'A.B.C' with 'A'! function areSameModule(a, b) { if (a.body.kind !== b.body.kind) {
javascript
{ "resource": "" }
q1587
areSameModule
train
function areSameModule(a, b) { if (a.body.kind !== b.body.kind) { return false; } if (a.body.kind !== 225 /* ModuleDeclaration */) {
javascript
{ "resource": "" }
q1588
merge
train
function merge(target, source) { target.additionalNodes = target.additionalNodes || []; target.additionalNodes.push(source.node); if (source.additionalNodes) { (_a = target.additionalNodes).push.apply(_a, source.additionalNodes); } target.children =
javascript
{ "resource": "" }
q1589
topLevelItems
train
function topLevelItems(root) { var topLevel = []; function recur(item) { if (isTopLevel(item)) { topLevel.push(item); if (item.children) { for (var _i = 0, _a = item.children; _i < _a.length; _i++) { var child = _a[_i]; recur(child); } } } } recur(root); return topLevel; function isTopLevel(item) { switch (navigationBarNodeKind(item)) { case 221 /* ClassDeclaration */: case 192 /* ClassExpression */: case 224 /* EnumDeclaration */: case 222 /* InterfaceDeclaration */: case 225 /* ModuleDeclaration */: case 256 /* SourceFile */: case 223 /* TypeAliasDeclaration */: case 279 /* JSDocTypedefTag */: return true; case 148 /* Constructor */: case 147 /* MethodDeclaration */: case 149 /* GetAccessor */: case 150 /* SetAccessor */: return hasSomeImportantChild(item); case 180 /* ArrowFunction */: case 220 /* FunctionDeclaration */: case 179 /* FunctionExpression */: return isTopLevelFunctionDeclaration(item); default: return false;
javascript
{ "resource": "" }
q1590
getArgumentIndexForTemplatePiece
train
function getArgumentIndexForTemplatePiece(spanIndex, node, position) { // Because the TemplateStringsArray is the first argument, we have to offset each substitution expression by 1. // There are three cases we can encounter: // 1. We are precisely in the template literal (argIndex = 0). // 2. We are in or to the right of the substitution expression (argIndex = spanIndex + 1). // 3. We are directly to the right of the template literal, but because we look for the token on the left, // not enough to put us in the substitution expression; we should consider ourselves part of // the *next* span's expression by offsetting the index (argIndex = (spanIndex + 1) + 1). // // Example: f `# abcd $#{# 1 + 1# }# efghi ${ #"#hello"# } # ` // ^
javascript
{ "resource": "" }
q1591
getTokenAtPosition
train
function getTokenAtPosition(sourceFile, position, includeJsDocComment) { if (includeJsDocComment === void 0) { includeJsDocComment = false; }
javascript
{ "resource": "" }
q1592
isInsideJsxElementOrAttribute
train
function isInsideJsxElementOrAttribute(sourceFile, position) { var token = getTokenAtPosition(sourceFile, position); if (!token) { return false; } if (token.kind === 244 /* JsxText */) { return true; } // <div>Hello |</div> if (token.kind === 25 /* LessThanToken */ && token.parent.kind === 244 /* JsxText */) { return true; } // <div> { | </div> or <div a={| </div> if (token.kind === 25 /* LessThanToken */ && token.parent.kind === 248 /* JsxExpression */) { return true;
javascript
{ "resource": "" }
q1593
isInCommentHelper
train
function isInCommentHelper(sourceFile, position, predicate) { var token = getTokenAtPosition(sourceFile, position); if (token && position <= token.getStart(sourceFile)) { var commentRanges = ts.getLeadingCommentRanges(sourceFile.text, token.pos); // The end marker of a single-line comment does not include the newline character. // In the following case, we are inside a comment (^ denotes the cursor position): // // // asdf ^\n // // But for multi-line comments, we don't want to be inside the comment in the following case: // // /* asdf */^ // // Internally, we represent the end of the comment at the newline and closing '/', respectively. return predicate ? ts.forEach(commentRanges, function (c) { return
javascript
{ "resource": "" }
q1594
mergeTypings
train
function mergeTypings(typingNames) { if (!typingNames) { return; } for (var _i = 0, typingNames_1 = typingNames; _i < typingNames_1.length; _i++) { var typing = typingNames_1[_i];
javascript
{ "resource": "" }
q1595
getTypingNamesFromJson
train
function getTypingNamesFromJson(jsonPath, filesToWatch) { if (host.fileExists(jsonPath)) { filesToWatch.push(jsonPath); } var result = ts.readConfigFile(jsonPath, function (path) { return host.readFile(path); }); if (result.config) { var jsonConfig = result.config; if (jsonConfig.dependencies) {
javascript
{ "resource": "" }
q1596
getTypingNamesFromSourceFileNames
train
function getTypingNamesFromSourceFileNames(fileNames) { var jsFileNames = ts.filter(fileNames, ts.hasJavaScriptFileExtension); var inferredTypingNames = ts.map(jsFileNames, function (f) { return ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())); }); var cleanedTypingNames = ts.map(inferredTypingNames, function (f) { return f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""); }); if (safeList !== EmptySafeList) { mergeTypings(ts.filter(cleanedTypingNames, function (f) { return f in safeList; }));
javascript
{ "resource": "" }
q1597
getTypingNamesFromNodeModuleFolder
train
function getTypingNamesFromNodeModuleFolder(nodeModulesPath) { // Todo: add support for ModuleResolutionHost too if (!host.directoryExists(nodeModulesPath)) { return; } var typingNames = []; var fileNames = host.readDirectory(nodeModulesPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2); for (var _i = 0, fileNames_2 = fileNames; _i < fileNames_2.length; _i++) { var fileName = fileNames_2[_i]; var normalizedFileName = ts.normalizePath(fileName); if (ts.getBaseFileName(normalizedFileName) !== "package.json") { continue; } var result = ts.readConfigFile(normalizedFileName, function (path) { return host.readFile(path); }); if (!result.config) { continue; } var packageJson = result.config; // npm 3's package.json contains a "_requiredBy" field // we should include all the top level module names for npm 2, and only module names whose // "_requiredBy" field starts with "#" or equals "/" for npm 3. if (packageJson._requiredBy && ts.filter(packageJson._requiredBy, function (r) { return r[0] === "#" || r === "/"; }).length === 0) { continue; }
javascript
{ "resource": "" }
q1598
getScanStartPosition
train
function getScanStartPosition(enclosingNode, originalRange, sourceFile) { var start = enclosingNode.getStart(sourceFile); if (start === originalRange.pos && enclosingNode.end === originalRange.end) { return start; } var precedingToken = ts.findPrecedingToken(originalRange.pos, sourceFile); if (!precedingToken) { // no preceding token found - start from the beginning of enclosing node return enclosingNode.pos; }
javascript
{ "resource": "" }
q1599
tryComputeIndentationForListItem
train
function tryComputeIndentationForListItem(startPos, endPos, parentStartLine, range, inheritedIndentation) { if (ts.rangeOverlapsWithStartEnd(range, startPos, endPos) || ts.rangeContainsStartEnd(range, startPos, endPos) /* Not to miss zero-range nodes e.g. JsxText */) { if (inheritedIndentation !== -1 /* Unknown */) { return inheritedIndentation; } } else { var startLine = sourceFile.getLineAndCharacterOfPosition(startPos).line; var startLinePosition = ts.getLineStartPositionForPosition(startPos, sourceFile); var column = formatting.SmartIndenter.findFirstNonWhitespaceColumn(startLinePosition, startPos, sourceFile, options); if
javascript
{ "resource": "" }