diff --git a/lib/tsc.js b/lib/tsc.js index bbbcedf0f47..87a413af729 100644 --- a/lib/tsc.js +++ b/lib/tsc.js @@ -665,7 +665,7 @@ var ts; } ts.fileExtensionIs = fileExtensionIs; ts.supportedExtensions = [".ts", ".tsx", ".d.ts"]; - ts.moduleFileExtensions = ts.supportedExtensions; + ts.supportedJsExtensions = ts.supportedExtensions.concat(".js", ".jsx"); function isSupportedSourceFileName(fileName) { if (!fileName) { return false; @@ -716,17 +716,16 @@ var ts; } function Signature(checker) { } + function Node(kind, pos, end) { + this.kind = kind; + this.pos = pos; + this.end = end; + this.flags = 0; + this.parent = undefined; + } ts.objectAllocator = { - getNodeConstructor: function (kind) { - function Node(pos, end) { - this.pos = pos; - this.end = end; - this.flags = 0; - this.parent = undefined; - } - Node.prototype = { kind: kind }; - return Node; - }, + getNodeConstructor: function () { return Node; }, + getSourceFileConstructor: function () { return Node; }, getSymbolConstructor: function () { return Symbol; }, getTypeConstructor: function () { return Type; }, getSignatureConstructor: function () { return Signature; } @@ -1003,7 +1002,16 @@ var ts; if (writeByteOrderMark) { data = "\uFEFF" + data; } - _fs.writeFileSync(fileName, data, "utf8"); + var fd; + try { + fd = _fs.openSync(fileName, "w"); + _fs.writeSync(fd, data, undefined, "utf8"); + } + finally { + if (fd !== undefined) { + _fs.closeSync(fd); + } + } } function getCanonicalPath(path) { return useCaseSensitiveFileNames ? path.toLowerCase() : path; @@ -1688,6 +1696,7 @@ var ts; Disallow_inconsistently_cased_references_to_the_same_file: { code: 6078, category: ts.DiagnosticCategory.Message, key: "Disallow_inconsistently_cased_references_to_the_same_file_6078", message: "Disallow inconsistently-cased references to the same file." }, Specify_JSX_code_generation_Colon_preserve_or_react: { code: 6080, category: ts.DiagnosticCategory.Message, key: "Specify_JSX_code_generation_Colon_preserve_or_react_6080", message: "Specify JSX code generation: 'preserve' or 'react'" }, Argument_for_jsx_must_be_preserve_or_react: { code: 6081, category: ts.DiagnosticCategory.Message, key: "Argument_for_jsx_must_be_preserve_or_react_6081", message: "Argument for '--jsx' must be 'preserve' or 'react'." }, + Only_amd_and_system_modules_are_supported_alongside_0: { code: 6082, category: ts.DiagnosticCategory.Error, key: "Only_amd_and_system_modules_are_supported_alongside_0_6082", message: "Only 'amd' and 'system' modules are supported alongside --{0}." }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -2148,7 +2157,7 @@ var ts; function getCommentRanges(text, pos, trailing) { var result; var collecting = trailing || pos === 0; - while (true) { + while (pos < text.length) { var ch = text.charCodeAt(pos); switch (ch) { case 13: @@ -2217,6 +2226,7 @@ var ts; } return result; } + return result; } function getLeadingCommentRanges(text, pos) { return getCommentRanges(text, pos, false); @@ -2312,7 +2322,7 @@ var ts; error(ts.Diagnostics.Digit_expected); } } - return +(text.substring(start, end)); + return "" + +(text.substring(start, end)); } function scanOctalDigits() { var start = pos; @@ -2704,7 +2714,7 @@ var ts; return pos++, token = 36; case 46: if (isDigit(text.charCodeAt(pos + 1))) { - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8; } if (text.charCodeAt(pos + 1) === 46 && text.charCodeAt(pos + 2) === 46) { @@ -2801,7 +2811,7 @@ var ts; case 55: case 56: case 57: - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8; case 58: return pos++, token = 54; @@ -3093,1034 +3103,6 @@ var ts; ts.createScanner = createScanner; })(ts || (ts = {})); var ts; -(function (ts) { - ts.bindTime = 0; - function or(state1, state2) { - return (state1 | state2) & 2 - ? 2 - : (state1 & state2) & 8 - ? 8 - : 4; - } - function getModuleInstanceState(node) { - if (node.kind === 215 || node.kind === 216) { - return 0; - } - else if (ts.isConstEnumDeclaration(node)) { - return 2; - } - else if ((node.kind === 222 || node.kind === 221) && !(node.flags & 2)) { - return 0; - } - else if (node.kind === 219) { - var state = 0; - ts.forEachChild(node, function (n) { - switch (getModuleInstanceState(n)) { - case 0: - return false; - case 2: - state = 2; - return false; - case 1: - state = 1; - return true; - } - }); - return state; - } - else if (node.kind === 218) { - return getModuleInstanceState(node.body); - } - else { - return 1; - } - } - ts.getModuleInstanceState = getModuleInstanceState; - var binder = createBinder(); - function bindSourceFile(file, options) { - var start = new Date().getTime(); - binder(file, options); - ts.bindTime += new Date().getTime() - start; - } - ts.bindSourceFile = bindSourceFile; - function createBinder() { - var file; - var options; - var parent; - var container; - var blockScopeContainer; - var lastContainer; - var seenThisKeyword; - var hasExplicitReturn; - var currentReachabilityState; - var labelStack; - var labelIndexMap; - var implicitLabels; - var inStrictMode; - var symbolCount = 0; - var Symbol; - var classifiableNames; - function bindSourceFile(f, opts) { - file = f; - options = opts; - inStrictMode = !!file.externalModuleIndicator; - classifiableNames = {}; - Symbol = ts.objectAllocator.getSymbolConstructor(); - if (!file.locals) { - bind(file); - file.symbolCount = symbolCount; - file.classifiableNames = classifiableNames; - } - parent = undefined; - container = undefined; - blockScopeContainer = undefined; - lastContainer = undefined; - seenThisKeyword = false; - hasExplicitReturn = false; - labelStack = undefined; - labelIndexMap = undefined; - implicitLabels = undefined; - } - return bindSourceFile; - function createSymbol(flags, name) { - symbolCount++; - return new Symbol(flags, name); - } - function addDeclarationToSymbol(symbol, node, symbolFlags) { - symbol.flags |= symbolFlags; - node.symbol = symbol; - if (!symbol.declarations) { - symbol.declarations = []; - } - symbol.declarations.push(node); - if (symbolFlags & 1952 && !symbol.exports) { - symbol.exports = {}; - } - if (symbolFlags & 6240 && !symbol.members) { - symbol.members = {}; - } - if (symbolFlags & 107455 && !symbol.valueDeclaration) { - symbol.valueDeclaration = node; - } - } - function getDeclarationName(node) { - if (node.name) { - if (node.kind === 218 && node.name.kind === 9) { - return "\"" + node.name.text + "\""; - } - if (node.name.kind === 136) { - var nameExpression = node.name.expression; - ts.Debug.assert(ts.isWellKnownSymbolSyntactically(nameExpression)); - return ts.getPropertyNameForKnownSymbolName(nameExpression.name.text); - } - return node.name.text; - } - switch (node.kind) { - case 144: - return "__constructor"; - case 152: - case 147: - return "__call"; - case 153: - case 148: - return "__new"; - case 149: - return "__index"; - case 228: - return "__export"; - case 227: - return node.isExportEquals ? "export=" : "default"; - case 213: - case 214: - return node.flags & 512 ? "default" : undefined; - } - } - function getDisplayName(node) { - return node.name ? ts.declarationNameToString(node.name) : getDeclarationName(node); - } - function declareSymbol(symbolTable, parent, node, includes, excludes) { - ts.Debug.assert(!ts.hasDynamicName(node)); - var isDefaultExport = node.flags & 512; - var name = isDefaultExport && parent ? "default" : getDeclarationName(node); - var symbol; - if (name !== undefined) { - symbol = ts.hasProperty(symbolTable, name) - ? symbolTable[name] - : (symbolTable[name] = createSymbol(0, name)); - if (name && (includes & 788448)) { - classifiableNames[name] = name; - } - if (symbol.flags & excludes) { - if (node.name) { - node.name.parent = node; - } - var message = symbol.flags & 2 - ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 - : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (declaration.flags & 512) { - message = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; - } - }); - ts.forEach(symbol.declarations, function (declaration) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message, getDisplayName(declaration))); - }); - file.bindDiagnostics.push(ts.createDiagnosticForNode(node.name || node, message, getDisplayName(node))); - symbol = createSymbol(0, name); - } - } - else { - symbol = createSymbol(0, "__missing"); - } - addDeclarationToSymbol(symbol, node, includes); - symbol.parent = parent; - return symbol; - } - function declareModuleMember(node, symbolFlags, symbolExcludes) { - var hasExportModifier = ts.getCombinedNodeFlags(node) & 2; - if (symbolFlags & 8388608) { - if (node.kind === 230 || (node.kind === 221 && hasExportModifier)) { - return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - } - else { - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - else { - if (hasExportModifier || container.flags & 131072) { - var exportKind = (symbolFlags & 107455 ? 1048576 : 0) | - (symbolFlags & 793056 ? 2097152 : 0) | - (symbolFlags & 1536 ? 4194304 : 0); - var local = declareSymbol(container.locals, undefined, node, exportKind, symbolExcludes); - local.exportSymbol = declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - node.localSymbol = local; - return local; - } - else { - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - } - function bindChildren(node) { - var saveParent = parent; - var saveContainer = container; - var savedBlockScopeContainer = blockScopeContainer; - parent = node; - var containerFlags = getContainerFlags(node); - if (containerFlags & 1) { - container = blockScopeContainer = node; - if (containerFlags & 4) { - container.locals = {}; - } - addToContainerChain(container); - } - else if (containerFlags & 2) { - blockScopeContainer = node; - blockScopeContainer.locals = undefined; - } - var savedReachabilityState; - var savedLabelStack; - var savedLabels; - var savedImplicitLabels; - var savedHasExplicitReturn; - var kind = node.kind; - var flags = node.flags; - flags &= ~1572864; - if (kind === 215) { - seenThisKeyword = false; - } - var saveState = kind === 248 || kind === 219 || ts.isFunctionLikeKind(kind); - if (saveState) { - savedReachabilityState = currentReachabilityState; - savedLabelStack = labelStack; - savedLabels = labelIndexMap; - savedImplicitLabels = implicitLabels; - savedHasExplicitReturn = hasExplicitReturn; - currentReachabilityState = 2; - hasExplicitReturn = false; - labelStack = labelIndexMap = implicitLabels = undefined; - } - bindReachableStatement(node); - if (currentReachabilityState === 2 && ts.isFunctionLikeKind(kind) && ts.nodeIsPresent(node.body)) { - flags |= 524288; - if (hasExplicitReturn) { - flags |= 1048576; - } - } - if (kind === 215) { - flags = seenThisKeyword ? flags | 262144 : flags & ~262144; - } - node.flags = flags; - if (saveState) { - hasExplicitReturn = savedHasExplicitReturn; - currentReachabilityState = savedReachabilityState; - labelStack = savedLabelStack; - labelIndexMap = savedLabels; - implicitLabels = savedImplicitLabels; - } - container = saveContainer; - parent = saveParent; - blockScopeContainer = savedBlockScopeContainer; - } - function bindReachableStatement(node) { - if (checkUnreachable(node)) { - ts.forEachChild(node, bind); - return; - } - switch (node.kind) { - case 198: - bindWhileStatement(node); - break; - case 197: - bindDoStatement(node); - break; - case 199: - bindForStatement(node); - break; - case 200: - case 201: - bindForInOrForOfStatement(node); - break; - case 196: - bindIfStatement(node); - break; - case 204: - case 208: - bindReturnOrThrow(node); - break; - case 203: - case 202: - bindBreakOrContinueStatement(node); - break; - case 209: - bindTryStatement(node); - break; - case 206: - bindSwitchStatement(node); - break; - case 220: - bindCaseBlock(node); - break; - case 207: - bindLabeledStatement(node); - break; - default: - ts.forEachChild(node, bind); - break; - } - } - function bindWhileStatement(n) { - var preWhileState = n.expression.kind === 84 ? 4 : currentReachabilityState; - var postWhileState = n.expression.kind === 99 ? 4 : currentReachabilityState; - bind(n.expression); - currentReachabilityState = preWhileState; - var postWhileLabel = pushImplicitLabel(); - bind(n.statement); - popImplicitLabel(postWhileLabel, postWhileState); - } - function bindDoStatement(n) { - var preDoState = currentReachabilityState; - var postDoLabel = pushImplicitLabel(); - bind(n.statement); - var postDoState = n.expression.kind === 99 ? 4 : preDoState; - popImplicitLabel(postDoLabel, postDoState); - bind(n.expression); - } - function bindForStatement(n) { - var preForState = currentReachabilityState; - var postForLabel = pushImplicitLabel(); - bind(n.initializer); - bind(n.condition); - bind(n.incrementor); - bind(n.statement); - var isInfiniteLoop = (!n.condition || n.condition.kind === 99); - var postForState = isInfiniteLoop ? 4 : preForState; - popImplicitLabel(postForLabel, postForState); - } - function bindForInOrForOfStatement(n) { - var preStatementState = currentReachabilityState; - var postStatementLabel = pushImplicitLabel(); - bind(n.initializer); - bind(n.expression); - bind(n.statement); - popImplicitLabel(postStatementLabel, preStatementState); - } - function bindIfStatement(n) { - var ifTrueState = n.expression.kind === 84 ? 4 : currentReachabilityState; - var ifFalseState = n.expression.kind === 99 ? 4 : currentReachabilityState; - currentReachabilityState = ifTrueState; - bind(n.expression); - bind(n.thenStatement); - if (n.elseStatement) { - var preElseState = currentReachabilityState; - currentReachabilityState = ifFalseState; - bind(n.elseStatement); - currentReachabilityState = or(currentReachabilityState, preElseState); - } - else { - currentReachabilityState = or(currentReachabilityState, ifFalseState); - } - } - function bindReturnOrThrow(n) { - bind(n.expression); - if (n.kind === 204) { - hasExplicitReturn = true; - } - currentReachabilityState = 4; - } - function bindBreakOrContinueStatement(n) { - bind(n.label); - var isValidJump = jumpToLabel(n.label, n.kind === 203 ? currentReachabilityState : 4); - if (isValidJump) { - currentReachabilityState = 4; - } - } - function bindTryStatement(n) { - var preTryState = currentReachabilityState; - bind(n.tryBlock); - var postTryState = currentReachabilityState; - currentReachabilityState = preTryState; - bind(n.catchClause); - var postCatchState = currentReachabilityState; - currentReachabilityState = preTryState; - bind(n.finallyBlock); - currentReachabilityState = or(postTryState, postCatchState); - } - function bindSwitchStatement(n) { - var preSwitchState = currentReachabilityState; - var postSwitchLabel = pushImplicitLabel(); - bind(n.expression); - bind(n.caseBlock); - var hasDefault = ts.forEach(n.caseBlock.clauses, function (c) { return c.kind === 242; }); - var postSwitchState = hasDefault && currentReachabilityState !== 2 ? 4 : preSwitchState; - popImplicitLabel(postSwitchLabel, postSwitchState); - } - function bindCaseBlock(n) { - var startState = currentReachabilityState; - for (var _i = 0, _a = n.clauses; _i < _a.length; _i++) { - var clause = _a[_i]; - currentReachabilityState = startState; - bind(clause); - if (clause.statements.length && currentReachabilityState === 2 && options.noFallthroughCasesInSwitch) { - errorOnFirstToken(clause, ts.Diagnostics.Fallthrough_case_in_switch); - } - } - } - function bindLabeledStatement(n) { - bind(n.label); - var ok = pushNamedLabel(n.label); - bind(n.statement); - if (ok) { - popNamedLabel(n.label, currentReachabilityState); - } - } - function getContainerFlags(node) { - switch (node.kind) { - case 186: - case 214: - case 215: - case 217: - case 155: - case 165: - return 1; - case 147: - case 148: - case 149: - case 143: - case 142: - case 213: - case 144: - case 145: - case 146: - case 152: - case 153: - case 173: - case 174: - case 218: - case 248: - case 216: - return 5; - case 244: - case 199: - case 200: - case 201: - case 220: - return 2; - case 192: - return ts.isFunctionLike(node.parent) ? 0 : 2; - } - return 0; - } - function addToContainerChain(next) { - if (lastContainer) { - lastContainer.nextContainer = next; - } - lastContainer = next; - } - function declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes) { - declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes); - } - function declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes) { - switch (container.kind) { - case 218: - return declareModuleMember(node, symbolFlags, symbolExcludes); - case 248: - return declareSourceFileMember(node, symbolFlags, symbolExcludes); - case 186: - case 214: - return declareClassMember(node, symbolFlags, symbolExcludes); - case 217: - return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - case 155: - case 165: - case 215: - return declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); - case 152: - case 153: - case 147: - case 148: - case 149: - case 143: - case 142: - case 144: - case 145: - case 146: - case 213: - case 173: - case 174: - case 216: - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - function declareClassMember(node, symbolFlags, symbolExcludes) { - return node.flags & 64 - ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) - : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); - } - function declareSourceFileMember(node, symbolFlags, symbolExcludes) { - return ts.isExternalModule(file) - ? declareModuleMember(node, symbolFlags, symbolExcludes) - : declareSymbol(file.locals, undefined, node, symbolFlags, symbolExcludes); - } - function hasExportDeclarations(node) { - var body = node.kind === 248 ? node : node.body; - if (body.kind === 248 || body.kind === 219) { - for (var _i = 0, _a = body.statements; _i < _a.length; _i++) { - var stat = _a[_i]; - if (stat.kind === 228 || stat.kind === 227) { - return true; - } - } - } - return false; - } - function setExportContextFlag(node) { - if (ts.isInAmbientContext(node) && !hasExportDeclarations(node)) { - node.flags |= 131072; - } - else { - node.flags &= ~131072; - } - } - function bindModuleDeclaration(node) { - setExportContextFlag(node); - if (node.name.kind === 9) { - declareSymbolAndAddToSymbolTable(node, 512, 106639); - } - else { - var state = getModuleInstanceState(node); - if (state === 0) { - declareSymbolAndAddToSymbolTable(node, 1024, 0); - } - else { - declareSymbolAndAddToSymbolTable(node, 512, 106639); - if (node.symbol.flags & (16 | 32 | 256)) { - node.symbol.constEnumOnlyModule = false; - } - else { - var currentModuleIsConstEnumOnly = state === 2; - if (node.symbol.constEnumOnlyModule === undefined) { - node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly; - } - else { - node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly; - } - } - } - } - } - function bindFunctionOrConstructorType(node) { - var symbol = createSymbol(131072, getDeclarationName(node)); - addDeclarationToSymbol(symbol, node, 131072); - var typeLiteralSymbol = createSymbol(2048, "__type"); - addDeclarationToSymbol(typeLiteralSymbol, node, 2048); - typeLiteralSymbol.members = (_a = {}, _a[symbol.name] = symbol, _a); - var _a; - } - function bindObjectLiteralExpression(node) { - if (inStrictMode) { - var seen = {}; - for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { - var prop = _a[_i]; - if (prop.name.kind !== 69) { - continue; - } - var identifier = prop.name; - var currentKind = prop.kind === 245 || prop.kind === 246 || prop.kind === 143 - ? 1 - : 2; - var existingKind = seen[identifier.text]; - if (!existingKind) { - seen[identifier.text] = currentKind; - continue; - } - if (currentKind === 1 && existingKind === 1) { - var span = ts.getErrorSpanForNode(file, identifier); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode)); - } - } - } - return bindAnonymousDeclaration(node, 4096, "__object"); - } - function bindAnonymousDeclaration(node, symbolFlags, name) { - var symbol = createSymbol(symbolFlags, name); - addDeclarationToSymbol(symbol, node, symbolFlags); - } - function bindBlockScopedDeclaration(node, symbolFlags, symbolExcludes) { - switch (blockScopeContainer.kind) { - case 218: - declareModuleMember(node, symbolFlags, symbolExcludes); - break; - case 248: - if (ts.isExternalModule(container)) { - declareModuleMember(node, symbolFlags, symbolExcludes); - break; - } - default: - if (!blockScopeContainer.locals) { - blockScopeContainer.locals = {}; - addToContainerChain(blockScopeContainer); - } - declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - function bindBlockScopedVariableDeclaration(node) { - bindBlockScopedDeclaration(node, 2, 107455); - } - function checkStrictModeIdentifier(node) { - if (inStrictMode && - node.originalKeywordKind >= 106 && - node.originalKeywordKind <= 114 && - !ts.isIdentifierName(node)) { - if (!file.parseDiagnostics.length) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(node, getStrictModeIdentifierMessage(node), ts.declarationNameToString(node))); - } - } - } - function getStrictModeIdentifierMessage(node) { - if (ts.getContainingClass(node)) { - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; - } - if (file.externalModuleIndicator) { - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; - } - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode; - } - function checkStrictModeBinaryExpression(node) { - if (inStrictMode && ts.isLeftHandSideExpression(node.left) && ts.isAssignmentOperator(node.operatorToken.kind)) { - checkStrictModeEvalOrArguments(node, node.left); - } - } - function checkStrictModeCatchClause(node) { - if (inStrictMode && node.variableDeclaration) { - checkStrictModeEvalOrArguments(node, node.variableDeclaration.name); - } - } - function checkStrictModeDeleteExpression(node) { - if (inStrictMode && node.expression.kind === 69) { - var span = ts.getErrorSpanForNode(file, node.expression); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.delete_cannot_be_called_on_an_identifier_in_strict_mode)); - } - } - function isEvalOrArgumentsIdentifier(node) { - return node.kind === 69 && - (node.text === "eval" || node.text === "arguments"); - } - function checkStrictModeEvalOrArguments(contextNode, name) { - if (name && name.kind === 69) { - var identifier = name; - if (isEvalOrArgumentsIdentifier(identifier)) { - var span = ts.getErrorSpanForNode(file, name); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, getStrictModeEvalOrArgumentsMessage(contextNode), identifier.text)); - } - } - } - function getStrictModeEvalOrArgumentsMessage(node) { - if (ts.getContainingClass(node)) { - return ts.Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; - } - if (file.externalModuleIndicator) { - return ts.Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; - } - return ts.Diagnostics.Invalid_use_of_0_in_strict_mode; - } - function checkStrictModeFunctionName(node) { - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.name); - } - } - function checkStrictModeNumericLiteral(node) { - if (inStrictMode && node.flags & 32768) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Octal_literals_are_not_allowed_in_strict_mode)); - } - } - function checkStrictModePostfixUnaryExpression(node) { - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.operand); - } - } - function checkStrictModePrefixUnaryExpression(node) { - if (inStrictMode) { - if (node.operator === 41 || node.operator === 42) { - checkStrictModeEvalOrArguments(node, node.operand); - } - } - } - function checkStrictModeWithStatement(node) { - if (inStrictMode) { - errorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_strict_mode); - } - } - function errorOnFirstToken(node, message, arg0, arg1, arg2) { - var span = ts.getSpanOfTokenAtPosition(file, node.pos); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2)); - } - function getDestructuringParameterName(node) { - return "__" + ts.indexOf(node.parent.parameters, node); - } - function bind(node) { - if (!node) { - return; - } - node.parent = parent; - var savedInStrictMode = inStrictMode; - if (!savedInStrictMode) { - updateStrictMode(node); - } - bindWorker(node); - bindChildren(node); - inStrictMode = savedInStrictMode; - } - function updateStrictMode(node) { - switch (node.kind) { - case 248: - case 219: - updateStrictModeStatementList(node.statements); - return; - case 192: - if (ts.isFunctionLike(node.parent)) { - updateStrictModeStatementList(node.statements); - } - return; - case 214: - case 186: - inStrictMode = true; - return; - } - } - function updateStrictModeStatementList(statements) { - for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) { - var statement = statements_1[_i]; - if (!ts.isPrologueDirective(statement)) { - return; - } - if (isUseStrictPrologueDirective(statement)) { - inStrictMode = true; - return; - } - } - } - function isUseStrictPrologueDirective(node) { - var nodeText = ts.getTextOfNodeFromSourceText(file.text, node.expression); - return nodeText === "\"use strict\"" || nodeText === "'use strict'"; - } - function bindWorker(node) { - switch (node.kind) { - case 69: - return checkStrictModeIdentifier(node); - case 181: - return checkStrictModeBinaryExpression(node); - case 244: - return checkStrictModeCatchClause(node); - case 175: - return checkStrictModeDeleteExpression(node); - case 8: - return checkStrictModeNumericLiteral(node); - case 180: - return checkStrictModePostfixUnaryExpression(node); - case 179: - return checkStrictModePrefixUnaryExpression(node); - case 205: - return checkStrictModeWithStatement(node); - case 97: - seenThisKeyword = true; - return; - case 137: - return declareSymbolAndAddToSymbolTable(node, 262144, 530912); - case 138: - return bindParameter(node); - case 211: - case 163: - return bindVariableDeclarationOrBindingElement(node); - case 141: - case 140: - return bindPropertyOrMethodOrAccessor(node, 4 | (node.questionToken ? 536870912 : 0), 107455); - case 245: - case 246: - return bindPropertyOrMethodOrAccessor(node, 4, 107455); - case 247: - return bindPropertyOrMethodOrAccessor(node, 8, 107455); - case 147: - case 148: - case 149: - return declareSymbolAndAddToSymbolTable(node, 131072, 0); - case 143: - case 142: - return bindPropertyOrMethodOrAccessor(node, 8192 | (node.questionToken ? 536870912 : 0), ts.isObjectLiteralMethod(node) ? 107455 : 99263); - case 213: - checkStrictModeFunctionName(node); - return declareSymbolAndAddToSymbolTable(node, 16, 106927); - case 144: - return declareSymbolAndAddToSymbolTable(node, 16384, 0); - case 145: - return bindPropertyOrMethodOrAccessor(node, 32768, 41919); - case 146: - return bindPropertyOrMethodOrAccessor(node, 65536, 74687); - case 152: - case 153: - return bindFunctionOrConstructorType(node); - case 155: - return bindAnonymousDeclaration(node, 2048, "__type"); - case 165: - return bindObjectLiteralExpression(node); - case 173: - case 174: - checkStrictModeFunctionName(node); - var bindingName = node.name ? node.name.text : "__function"; - return bindAnonymousDeclaration(node, 16, bindingName); - case 186: - case 214: - return bindClassLikeDeclaration(node); - case 215: - return bindBlockScopedDeclaration(node, 64, 792960); - case 216: - return bindBlockScopedDeclaration(node, 524288, 793056); - case 217: - return bindEnumDeclaration(node); - case 218: - return bindModuleDeclaration(node); - case 221: - case 224: - case 226: - case 230: - return declareSymbolAndAddToSymbolTable(node, 8388608, 8388608); - case 223: - return bindImportClause(node); - case 228: - return bindExportDeclaration(node); - case 227: - return bindExportAssignment(node); - case 248: - return bindSourceFileIfExternalModule(); - } - } - function bindSourceFileIfExternalModule() { - setExportContextFlag(file); - if (ts.isExternalModule(file)) { - bindAnonymousDeclaration(file, 512, "\"" + ts.removeFileExtension(file.fileName) + "\""); - } - } - function bindExportAssignment(node) { - if (!container.symbol || !container.symbol.exports) { - bindAnonymousDeclaration(node, 8388608, getDeclarationName(node)); - } - else if (node.expression.kind === 69) { - declareSymbol(container.symbol.exports, container.symbol, node, 8388608, 107455 | 8388608); - } - else { - declareSymbol(container.symbol.exports, container.symbol, node, 4, 107455 | 8388608); - } - } - function bindExportDeclaration(node) { - if (!container.symbol || !container.symbol.exports) { - bindAnonymousDeclaration(node, 1073741824, getDeclarationName(node)); - } - else if (!node.exportClause) { - declareSymbol(container.symbol.exports, container.symbol, node, 1073741824, 0); - } - } - function bindImportClause(node) { - if (node.name) { - declareSymbolAndAddToSymbolTable(node, 8388608, 8388608); - } - } - function bindClassLikeDeclaration(node) { - if (node.kind === 214) { - bindBlockScopedDeclaration(node, 32, 899519); - } - else { - var bindingName = node.name ? node.name.text : "__class"; - bindAnonymousDeclaration(node, 32, bindingName); - if (node.name) { - classifiableNames[node.name.text] = node.name.text; - } - } - var symbol = node.symbol; - var prototypeSymbol = createSymbol(4 | 134217728, "prototype"); - if (ts.hasProperty(symbol.exports, prototypeSymbol.name)) { - if (node.name) { - node.name.parent = node; - } - file.bindDiagnostics.push(ts.createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0], ts.Diagnostics.Duplicate_identifier_0, prototypeSymbol.name)); - } - symbol.exports[prototypeSymbol.name] = prototypeSymbol; - prototypeSymbol.parent = symbol; - } - function bindEnumDeclaration(node) { - return ts.isConst(node) - ? bindBlockScopedDeclaration(node, 128, 899967) - : bindBlockScopedDeclaration(node, 256, 899327); - } - function bindVariableDeclarationOrBindingElement(node) { - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.name); - } - if (!ts.isBindingPattern(node.name)) { - if (ts.isBlockOrCatchScoped(node)) { - bindBlockScopedVariableDeclaration(node); - } - else if (ts.isParameterDeclaration(node)) { - declareSymbolAndAddToSymbolTable(node, 1, 107455); - } - else { - declareSymbolAndAddToSymbolTable(node, 1, 107454); - } - } - } - function bindParameter(node) { - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.name); - } - if (ts.isBindingPattern(node.name)) { - bindAnonymousDeclaration(node, 1, getDestructuringParameterName(node)); - } - else { - declareSymbolAndAddToSymbolTable(node, 1, 107455); - } - if (node.flags & 56 && - node.parent.kind === 144 && - ts.isClassLike(node.parent.parent)) { - var classDeclaration = node.parent.parent; - declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, 4, 107455); - } - } - function bindPropertyOrMethodOrAccessor(node, symbolFlags, symbolExcludes) { - return ts.hasDynamicName(node) - ? bindAnonymousDeclaration(node, symbolFlags, "__computed") - : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); - } - function pushNamedLabel(name) { - initializeReachabilityStateIfNecessary(); - if (ts.hasProperty(labelIndexMap, name.text)) { - return false; - } - labelIndexMap[name.text] = labelStack.push(1) - 1; - return true; - } - function pushImplicitLabel() { - initializeReachabilityStateIfNecessary(); - var index = labelStack.push(1) - 1; - implicitLabels.push(index); - return index; - } - function popNamedLabel(label, outerState) { - var index = labelIndexMap[label.text]; - ts.Debug.assert(index !== undefined); - ts.Debug.assert(labelStack.length == index + 1); - labelIndexMap[label.text] = undefined; - setCurrentStateAtLabel(labelStack.pop(), outerState, label); - } - function popImplicitLabel(implicitLabelIndex, outerState) { - if (labelStack.length !== implicitLabelIndex + 1) { - ts.Debug.assert(false, "Label stack: " + labelStack.length + ", index:" + implicitLabelIndex); - } - var i = implicitLabels.pop(); - if (implicitLabelIndex !== i) { - ts.Debug.assert(false, "i: " + i + ", index: " + implicitLabelIndex); - } - setCurrentStateAtLabel(labelStack.pop(), outerState, undefined); - } - function setCurrentStateAtLabel(innerMergedState, outerState, label) { - if (innerMergedState === 1) { - if (label && !options.allowUnusedLabels) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(label, ts.Diagnostics.Unused_label)); - } - currentReachabilityState = outerState; - } - else { - currentReachabilityState = or(innerMergedState, outerState); - } - } - function jumpToLabel(label, outerState) { - initializeReachabilityStateIfNecessary(); - var index = label ? labelIndexMap[label.text] : ts.lastOrUndefined(implicitLabels); - if (index === undefined) { - return false; - } - var stateAtLabel = labelStack[index]; - labelStack[index] = stateAtLabel === 1 ? outerState : or(stateAtLabel, outerState); - return true; - } - function checkUnreachable(node) { - switch (currentReachabilityState) { - case 4: - var reportError = ts.isStatement(node) || - node.kind === 214 || - (node.kind === 218 && shouldReportErrorOnModuleDeclaration(node)) || - (node.kind === 217 && (!ts.isConstEnumDeclaration(node) || options.preserveConstEnums)); - if (reportError) { - currentReachabilityState = 8; - var reportUnreachableCode = !options.allowUnreachableCode && - !ts.isInAmbientContext(node) && - (node.kind !== 193 || - ts.getCombinedNodeFlags(node.declarationList) & 24576 || - ts.forEach(node.declarationList.declarations, function (d) { return d.initializer; })); - if (reportUnreachableCode) { - errorOnFirstToken(node, ts.Diagnostics.Unreachable_code_detected); - } - } - case 8: - return true; - default: - return false; - } - function shouldReportErrorOnModuleDeclaration(node) { - var instanceState = getModuleInstanceState(node); - return instanceState === 1 || (instanceState === 2 && options.preserveConstEnums); - } - } - function initializeReachabilityStateIfNecessary() { - if (labelIndexMap) { - return; - } - currentReachabilityState = 2; - labelIndexMap = {}; - labelStack = []; - implicitLabels = []; - } - } -})(ts || (ts = {})); -var ts; (function (ts) { function getDeclarationOfKind(symbol, kind) { var declarations = symbol.declarations; @@ -4396,6 +3378,10 @@ var ts; return file.externalModuleIndicator !== undefined; } ts.isExternalModule = isExternalModule; + function isExternalOrCommonJsModule(file) { + return (file.externalModuleIndicator || file.commonJsModuleIndicator) !== undefined; + } + ts.isExternalOrCommonJsModule = isExternalOrCommonJsModule; function isDeclarationFile(file) { return (file.flags & 4096) !== 0; } @@ -4442,18 +3428,26 @@ var ts; return ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos); } ts.getLeadingCommentRangesOfNode = getLeadingCommentRangesOfNode; + function getLeadingCommentRangesOfNodeFromText(node, text) { + return ts.getLeadingCommentRanges(text, node.pos); + } + ts.getLeadingCommentRangesOfNodeFromText = getLeadingCommentRangesOfNodeFromText; function getJsDocComments(node, sourceFileOfNode) { - var commentRanges = (node.kind === 138 || node.kind === 137) ? - ts.concatenate(ts.getTrailingCommentRanges(sourceFileOfNode.text, node.pos), ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos)) : - getLeadingCommentRangesOfNode(node, sourceFileOfNode); - return ts.filter(commentRanges, isJsDocComment); - function isJsDocComment(comment) { - return sourceFileOfNode.text.charCodeAt(comment.pos + 1) === 42 && - sourceFileOfNode.text.charCodeAt(comment.pos + 2) === 42 && - sourceFileOfNode.text.charCodeAt(comment.pos + 3) !== 47; - } + return getJsDocCommentsFromText(node, sourceFileOfNode.text); } ts.getJsDocComments = getJsDocComments; + function getJsDocCommentsFromText(node, text) { + var commentRanges = (node.kind === 138 || node.kind === 137) ? + ts.concatenate(ts.getTrailingCommentRanges(text, node.pos), ts.getLeadingCommentRanges(text, node.pos)) : + getLeadingCommentRangesOfNodeFromText(node, text); + return ts.filter(commentRanges, isJsDocComment); + function isJsDocComment(comment) { + return text.charCodeAt(comment.pos + 1) === 42 && + text.charCodeAt(comment.pos + 2) === 42 && + text.charCodeAt(comment.pos + 3) !== 47; + } + } + ts.getJsDocCommentsFromText = getJsDocCommentsFromText; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; ts.fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*/; function isTypeNode(node) { @@ -4982,6 +3976,41 @@ var ts; return node.kind === 221 && node.moduleReference.kind !== 232; } ts.isInternalModuleImportEqualsDeclaration = isInternalModuleImportEqualsDeclaration; + function isSourceFileJavaScript(file) { + return isInJavaScriptFile(file); + } + ts.isSourceFileJavaScript = isSourceFileJavaScript; + function isInJavaScriptFile(node) { + return node && !!(node.parserContextFlags & 32); + } + ts.isInJavaScriptFile = isInJavaScriptFile; + function isRequireCall(expression) { + return expression.kind === 168 && + expression.expression.kind === 69 && + expression.expression.text === "require" && + expression.arguments.length === 1 && + expression.arguments[0].kind === 9; + } + ts.isRequireCall = isRequireCall; + function isExportsPropertyAssignment(expression) { + return isInJavaScriptFile(expression) && + (expression.kind === 181) && + (expression.operatorToken.kind === 56) && + (expression.left.kind === 166) && + (expression.left.expression.kind === 69) && + ((expression.left.expression).text === "exports"); + } + ts.isExportsPropertyAssignment = isExportsPropertyAssignment; + function isModuleExportsAssignment(expression) { + return isInJavaScriptFile(expression) && + (expression.kind === 181) && + (expression.operatorToken.kind === 56) && + (expression.left.kind === 166) && + (expression.left.expression.kind === 69) && + ((expression.left.expression).text === "module") && + (expression.left.name.text === "exports"); + } + ts.isModuleExportsAssignment = isModuleExportsAssignment; function getExternalModuleName(node) { if (node.kind === 222) { return node.moduleSpecifier; @@ -5293,8 +4322,8 @@ var ts; function getFileReferenceFromReferencePath(comment, commentRange) { var simpleReferenceRegEx = /^\/\/\/\s*/gim; - if (simpleReferenceRegEx.exec(comment)) { - if (isNoDefaultLibRegEx.exec(comment)) { + if (simpleReferenceRegEx.test(comment)) { + if (isNoDefaultLibRegEx.test(comment)) { return { isNoDefaultLib: true }; @@ -5336,12 +4365,20 @@ var ts; return isFunctionLike(node) && (node.flags & 256) !== 0 && !isAccessor(node); } ts.isAsyncFunctionLike = isAsyncFunctionLike; + function isStringOrNumericLiteral(kind) { + return kind === 9 || kind === 8; + } + ts.isStringOrNumericLiteral = isStringOrNumericLiteral; function hasDynamicName(declaration) { - return declaration.name && - declaration.name.kind === 136 && - !isWellKnownSymbolSyntactically(declaration.name.expression); + return declaration.name && isDynamicName(declaration.name); } ts.hasDynamicName = hasDynamicName; + function isDynamicName(name) { + return name.kind === 136 && + !isStringOrNumericLiteral(name.expression.kind) && + !isWellKnownSymbolSyntactically(name.expression); + } + ts.isDynamicName = isDynamicName; function isWellKnownSymbolSyntactically(node) { return isPropertyAccessExpression(node) && isESSymbolIdentifier(node.expression); } @@ -5562,11 +4599,11 @@ var ts; } ts.getIndentSize = getIndentSize; function createTextWriter(newLine) { - var output = ""; - var indent = 0; - var lineStart = true; - var lineCount = 0; - var linePos = 0; + var output; + var indent; + var lineStart; + var lineCount; + var linePos; function write(s) { if (s && s.length) { if (lineStart) { @@ -5576,6 +4613,13 @@ var ts; output += s; } } + function reset() { + output = ""; + indent = 0; + lineStart = true; + lineCount = 0; + linePos = 0; + } function rawWrite(s) { if (s !== undefined) { if (lineStart) { @@ -5602,9 +4646,10 @@ var ts; lineStart = true; } } - function writeTextOfNode(sourceFile, node) { - write(getSourceTextOfNodeFromSourceFile(sourceFile, node)); + function writeTextOfNode(text, node) { + write(getTextOfNodeFromSourceText(text, node)); } + reset(); return { write: write, rawWrite: rawWrite, @@ -5617,10 +4662,17 @@ var ts; getTextPos: function () { return output.length; }, getLine: function () { return lineCount + 1; }, getColumn: function () { return lineStart ? indent * getIndentSize() + 1 : output.length - linePos + 1; }, - getText: function () { return output; } + getText: function () { return output; }, + reset: reset }; } ts.createTextWriter = createTextWriter; + function getExternalModuleNameFromPath(host, fileName) { + var dir = host.getCurrentDirectory(); + var relativePath = ts.getRelativePathToDirectoryOrUrl(dir, fileName, dir, function (f) { return host.getCanonicalFileName(f); }, false); + return ts.removeFileExtension(relativePath); + } + ts.getExternalModuleNameFromPath = getExternalModuleNameFromPath; function getOwnEmitOutputFilePath(sourceFile, host, extension) { var compilerOptions = host.getCompilerOptions(); var emitOutputFilePathWithoutExtension; @@ -5649,6 +4701,10 @@ var ts; return ts.getLineAndCharacterOfPosition(currentSourceFile, pos).line; } ts.getLineOfLocalPosition = getLineOfLocalPosition; + function getLineOfLocalPositionFromLineMap(lineMap, pos) { + return ts.computeLineAndCharacterOfPosition(lineMap, pos).line; + } + ts.getLineOfLocalPositionFromLineMap = getLineOfLocalPositionFromLineMap; function getFirstConstructorWithBody(node) { return ts.forEach(node.members, function (member) { if (member.kind === 144 && nodeIsPresent(member.body)) { @@ -5719,21 +4775,21 @@ var ts; }; } ts.getAllAccessorDeclarations = getAllAccessorDeclarations; - function emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments) { + function emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments) { if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos && - getLineOfLocalPosition(currentSourceFile, node.pos) !== getLineOfLocalPosition(currentSourceFile, leadingComments[0].pos)) { + getLineOfLocalPositionFromLineMap(lineMap, node.pos) !== getLineOfLocalPositionFromLineMap(lineMap, leadingComments[0].pos)) { writer.writeLine(); } } ts.emitNewLineBeforeLeadingComments = emitNewLineBeforeLeadingComments; - function emitComments(currentSourceFile, writer, comments, trailingSeparator, newLine, writeComment) { + function emitComments(text, lineMap, writer, comments, trailingSeparator, newLine, writeComment) { var emitLeadingSpace = !trailingSeparator; ts.forEach(comments, function (comment) { if (emitLeadingSpace) { writer.write(" "); emitLeadingSpace = false; } - writeComment(currentSourceFile, writer, comment, newLine); + writeComment(text, lineMap, writer, comment, newLine); if (comment.hasTrailingNewLine) { writer.writeLine(); } @@ -5746,16 +4802,16 @@ var ts; }); } ts.emitComments = emitComments; - function emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, removeComments) { + function emitDetachedComments(text, lineMap, writer, writeComment, node, newLine, removeComments) { var leadingComments; var currentDetachedCommentInfo; if (removeComments) { if (node.pos === 0) { - leadingComments = ts.filter(ts.getLeadingCommentRanges(currentSourceFile.text, node.pos), isPinnedComment); + leadingComments = ts.filter(ts.getLeadingCommentRanges(text, node.pos), isPinnedComment); } } else { - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + leadingComments = ts.getLeadingCommentRanges(text, node.pos); } if (leadingComments) { var detachedComments = []; @@ -5763,8 +4819,8 @@ var ts; for (var _i = 0, leadingComments_1 = leadingComments; _i < leadingComments_1.length; _i++) { var comment = leadingComments_1[_i]; if (lastComment) { - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastComment.end); - var commentLine = getLineOfLocalPosition(currentSourceFile, comment.pos); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, lastComment.end); + var commentLine = getLineOfLocalPositionFromLineMap(lineMap, comment.pos); if (commentLine >= lastCommentLine + 2) { break; } @@ -5773,37 +4829,37 @@ var ts; lastComment = comment; } if (detachedComments.length) { - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, ts.lastOrUndefined(detachedComments).end); - var nodeLine = getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node.pos)); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, ts.lastOrUndefined(detachedComments).end); + var nodeLine = getLineOfLocalPositionFromLineMap(lineMap, ts.skipTrivia(text, node.pos)); if (nodeLine >= lastCommentLine + 2) { - emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); - emitComments(currentSourceFile, writer, detachedComments, true, newLine, writeComment); + emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments); + emitComments(text, lineMap, writer, detachedComments, true, newLine, writeComment); currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: ts.lastOrUndefined(detachedComments).end }; } } } return currentDetachedCommentInfo; function isPinnedComment(comment) { - return currentSourceFile.text.charCodeAt(comment.pos + 1) === 42 && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 33; + return text.charCodeAt(comment.pos + 1) === 42 && + text.charCodeAt(comment.pos + 2) === 33; } } ts.emitDetachedComments = emitDetachedComments; - function writeCommentRange(currentSourceFile, writer, comment, newLine) { - if (currentSourceFile.text.charCodeAt(comment.pos + 1) === 42) { - var firstCommentLineAndCharacter = ts.getLineAndCharacterOfPosition(currentSourceFile, comment.pos); - var lineCount = ts.getLineStarts(currentSourceFile).length; + function writeCommentRange(text, lineMap, writer, comment, newLine) { + if (text.charCodeAt(comment.pos + 1) === 42) { + var firstCommentLineAndCharacter = ts.computeLineAndCharacterOfPosition(lineMap, comment.pos); + var lineCount = lineMap.length; var firstCommentLineIndent; for (var pos = comment.pos, currentLine = firstCommentLineAndCharacter.line; pos < comment.end; currentLine++) { var nextLineStart = (currentLine + 1) === lineCount - ? currentSourceFile.text.length + 1 - : getStartPositionOfLine(currentLine + 1, currentSourceFile); + ? text.length + 1 + : lineMap[currentLine + 1]; if (pos !== comment.pos) { if (firstCommentLineIndent === undefined) { - firstCommentLineIndent = calculateIndent(getStartPositionOfLine(firstCommentLineAndCharacter.line, currentSourceFile), comment.pos); + firstCommentLineIndent = calculateIndent(text, lineMap[firstCommentLineAndCharacter.line], comment.pos); } var currentWriterIndentSpacing = writer.getIndent() * getIndentSize(); - var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(pos, nextLineStart); + var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(text, pos, nextLineStart); if (spacesToEmit > 0) { var numberOfSingleSpacesToEmit = spacesToEmit % getIndentSize(); var indentSizeSpaceString = getIndentString((spacesToEmit - numberOfSingleSpacesToEmit) / getIndentSize()); @@ -5817,40 +4873,40 @@ var ts; writer.rawWrite(""); } } - writeTrimmedCurrentLine(pos, nextLineStart); + writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart); pos = nextLineStart; } } else { - writer.write(currentSourceFile.text.substring(comment.pos, comment.end)); - } - function writeTrimmedCurrentLine(pos, nextLineStart) { - var end = Math.min(comment.end, nextLineStart - 1); - var currentLineText = currentSourceFile.text.substring(pos, end).replace(/^\s+|\s+$/g, ""); - if (currentLineText) { - writer.write(currentLineText); - if (end !== comment.end) { - writer.writeLine(); - } - } - else { - writer.writeLiteral(newLine); - } - } - function calculateIndent(pos, end) { - var currentLineIndent = 0; - for (; pos < end && ts.isWhiteSpace(currentSourceFile.text.charCodeAt(pos)); pos++) { - if (currentSourceFile.text.charCodeAt(pos) === 9) { - currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); - } - else { - currentLineIndent++; - } - } - return currentLineIndent; + writer.write(text.substring(comment.pos, comment.end)); } } ts.writeCommentRange = writeCommentRange; + function writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart) { + var end = Math.min(comment.end, nextLineStart - 1); + var currentLineText = text.substring(pos, end).replace(/^\s+|\s+$/g, ""); + if (currentLineText) { + writer.write(currentLineText); + if (end !== comment.end) { + writer.writeLine(); + } + } + else { + writer.writeLiteral(newLine); + } + } + function calculateIndent(text, pos, end) { + var currentLineIndent = 0; + for (; pos < end && ts.isWhiteSpace(text.charCodeAt(pos)); pos++) { + if (text.charCodeAt(pos) === 9) { + currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); + } + else { + currentLineIndent++; + } + } + return currentLineIndent; + } function modifierToFlag(token) { switch (token) { case 113: return 64; @@ -5944,14 +5000,14 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 512) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function isJavaScript(fileName) { - return ts.fileExtensionIs(fileName, ".js"); + function hasJavaScriptFileExtension(fileName) { + return ts.fileExtensionIs(fileName, ".js") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isJavaScript = isJavaScript; - function isTsx(fileName) { - return ts.fileExtensionIs(fileName, ".tsx"); + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function allowsJsxExpressions(fileName) { + return ts.fileExtensionIs(fileName, ".tsx") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isTsx = isTsx; + ts.allowsJsxExpressions = allowsJsxExpressions; function getExpandedCharCodes(input) { var output = []; var length = input.length; @@ -6161,14 +5217,16 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - var nodeConstructors = new Array(272); ts.parseTime = 0; - function getNodeConstructor(kind) { - return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); - } - ts.getNodeConstructor = getNodeConstructor; + var NodeConstructor; + var SourceFileConstructor; function createNode(kind, pos, end) { - return new (getNodeConstructor(kind))(pos, end); + if (kind === 248) { + return new (SourceFileConstructor || (SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor()))(kind, pos, end); + } + else { + return new (NodeConstructor || (NodeConstructor = ts.objectAllocator.getNodeConstructor()))(kind, pos, end); + } } ts.createNode = createNode; function visitNode(cbNode, node) { @@ -6567,6 +5625,8 @@ var ts; (function (Parser) { var scanner = ts.createScanner(2, true); var disallowInAndDecoratorContext = 1 | 4; + var NodeConstructor; + var SourceFileConstructor; var sourceFile; var parseDiagnostics; var syntaxCursor; @@ -6579,13 +5639,16 @@ var ts; var contextFlags; var parseErrorBeforeNextFinishedNode = false; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { - initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var isJavaScriptFile = ts.hasJavaScriptFileExtension(fileName) || _sourceText.lastIndexOf("// @language=javascript", 0) === 0; + initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor); var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); clearState(); return result; } Parser.parseSourceFile = parseSourceFile; - function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { + function initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor) { + NodeConstructor = ts.objectAllocator.getNodeConstructor(); + SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor(); sourceText = _sourceText; syntaxCursor = _syntaxCursor; parseDiagnostics = []; @@ -6593,12 +5656,12 @@ var ts; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = ts.isJavaScript(fileName) ? 32 : 0; + contextFlags = isJavaScriptFile ? 32 : 0; parseErrorBeforeNextFinishedNode = false; scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); - scanner.setLanguageVariant(ts.isTsx(fileName) ? 1 : 0); + scanner.setLanguageVariant(ts.allowsJsxExpressions(fileName) ? 1 : 0); } function clearState() { scanner.setText(""); @@ -6611,6 +5674,9 @@ var ts; } function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { sourceFile = createSourceFile(fileName, languageVersion); + if (contextFlags & 32) { + sourceFile.parserContextFlags = 32; + } token = nextToken(); processReferenceComments(sourceFile); sourceFile.statements = parseList(0, parseStatement); @@ -6624,7 +5690,7 @@ var ts; if (setParentNodes) { fixupParentReferences(sourceFile); } - if (ts.isJavaScript(fileName)) { + if (ts.isSourceFileJavaScript(sourceFile)) { addJSDocComments(); } return sourceFile; @@ -6670,15 +5736,14 @@ var ts; } Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - var sourceFile = createNode(248, 0); - sourceFile.pos = 0; - sourceFile.end = sourceText.length; + var sourceFile = new SourceFileConstructor(248, 0, sourceText.length); + nodeCount++; sourceFile.text = sourceText; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 4096 : 0; - sourceFile.languageVariant = ts.isTsx(sourceFile.fileName) ? 1 : 0; + sourceFile.languageVariant = ts.allowsJsxExpressions(sourceFile.fileName) ? 1 : 0; return sourceFile; } function setContextFlag(val, flag) { @@ -6900,7 +5965,7 @@ var ts; if (!(pos >= 0)) { pos = scanner.getStartPos(); } - return new (nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)))(pos, pos); + return new NodeConstructor(kind, pos, pos); } function finishNode(node, end) { node.end = end === undefined ? scanner.getStartPos() : end; @@ -7038,7 +6103,7 @@ var ts; case 12: return token === 19 || token === 37 || isLiteralPropertyName(); case 9: - return isLiteralPropertyName(); + return token === 19 || isLiteralPropertyName(); case 7: if (token === 15) { return lookAhead(isValidHeritageClauseObjectLiteral); @@ -7559,9 +6624,7 @@ var ts; } function parseParameterType() { if (parseOptional(54)) { - return token === 9 - ? parseLiteralNode(true) - : parseType(); + return parseType(); } return undefined; } @@ -7818,6 +6881,8 @@ var ts; case 131: var node = tryParse(parseKeywordAndNoDot); return node || parseTypeReferenceOrTypePredicate(); + case 9: + return parseLiteralNode(true); case 103: case 97: return parseTokenNode(); @@ -7847,6 +6912,7 @@ var ts; case 19: case 25: case 92: + case 9: return true; case 17: return lookAhead(isStartOfParenthesizedOrFunctionType); @@ -8360,7 +7426,6 @@ var ts; var unaryOperator = token; var simpleUnaryExpression = parseSimpleUnaryExpression(); if (token === 38) { - var diagnostic; var start = ts.skipTrivia(sourceText, simpleUnaryExpression.pos); if (simpleUnaryExpression.kind === 171) { parseErrorAtPosition(start, simpleUnaryExpression.end - start, ts.Diagnostics.A_type_assertion_expression_is_not_allowed_in_the_left_hand_side_of_an_exponentiation_expression_Consider_enclosing_the_expression_in_parentheses); @@ -10024,7 +9089,7 @@ var ts; } JSDocParser.isJSDocType = isJSDocType; function parseJSDocTypeExpressionForTests(content, start, length) { - initializeState("file.js", content, 2, undefined); + initializeState("file.js", content, 2, true, undefined); var jsDocTypeExpression = parseJSDocTypeExpression(start, length); var diagnostics = parseDiagnostics; clearState(); @@ -10275,7 +9340,7 @@ var ts; } } function parseIsolatedJSDocComment(content, start, length) { - initializeState("file.js", content, 2, undefined); + initializeState("file.js", content, 2, true, undefined); var jsDocComment = parseJSDocComment(undefined, start, length); var diagnostics = parseDiagnostics; clearState(); @@ -10794,6 +9859,1075 @@ var ts; })(IncrementalParser || (IncrementalParser = {})); })(ts || (ts = {})); var ts; +(function (ts) { + ts.bindTime = 0; + function or(state1, state2) { + return (state1 | state2) & 2 + ? 2 + : (state1 & state2) & 8 + ? 8 + : 4; + } + function getModuleInstanceState(node) { + if (node.kind === 215 || node.kind === 216) { + return 0; + } + else if (ts.isConstEnumDeclaration(node)) { + return 2; + } + else if ((node.kind === 222 || node.kind === 221) && !(node.flags & 2)) { + return 0; + } + else if (node.kind === 219) { + var state = 0; + ts.forEachChild(node, function (n) { + switch (getModuleInstanceState(n)) { + case 0: + return false; + case 2: + state = 2; + return false; + case 1: + state = 1; + return true; + } + }); + return state; + } + else if (node.kind === 218) { + return getModuleInstanceState(node.body); + } + else { + return 1; + } + } + ts.getModuleInstanceState = getModuleInstanceState; + var binder = createBinder(); + function bindSourceFile(file, options) { + var start = new Date().getTime(); + binder(file, options); + ts.bindTime += new Date().getTime() - start; + } + ts.bindSourceFile = bindSourceFile; + function createBinder() { + var file; + var options; + var parent; + var container; + var blockScopeContainer; + var lastContainer; + var seenThisKeyword; + var hasExplicitReturn; + var currentReachabilityState; + var labelStack; + var labelIndexMap; + var implicitLabels; + var inStrictMode; + var symbolCount = 0; + var Symbol; + var classifiableNames; + function bindSourceFile(f, opts) { + file = f; + options = opts; + inStrictMode = !!file.externalModuleIndicator; + classifiableNames = {}; + Symbol = ts.objectAllocator.getSymbolConstructor(); + if (!file.locals) { + bind(file); + file.symbolCount = symbolCount; + file.classifiableNames = classifiableNames; + } + parent = undefined; + container = undefined; + blockScopeContainer = undefined; + lastContainer = undefined; + seenThisKeyword = false; + hasExplicitReturn = false; + labelStack = undefined; + labelIndexMap = undefined; + implicitLabels = undefined; + } + return bindSourceFile; + function createSymbol(flags, name) { + symbolCount++; + return new Symbol(flags, name); + } + function addDeclarationToSymbol(symbol, node, symbolFlags) { + symbol.flags |= symbolFlags; + node.symbol = symbol; + if (!symbol.declarations) { + symbol.declarations = []; + } + symbol.declarations.push(node); + if (symbolFlags & 1952 && !symbol.exports) { + symbol.exports = {}; + } + if (symbolFlags & 6240 && !symbol.members) { + symbol.members = {}; + } + if (symbolFlags & 107455 && !symbol.valueDeclaration) { + symbol.valueDeclaration = node; + } + } + function getDeclarationName(node) { + if (node.name) { + if (node.kind === 218 && node.name.kind === 9) { + return "\"" + node.name.text + "\""; + } + if (node.name.kind === 136) { + var nameExpression = node.name.expression; + if (ts.isStringOrNumericLiteral(nameExpression.kind)) { + return nameExpression.text; + } + ts.Debug.assert(ts.isWellKnownSymbolSyntactically(nameExpression)); + return ts.getPropertyNameForKnownSymbolName(nameExpression.name.text); + } + return node.name.text; + } + switch (node.kind) { + case 144: + return "__constructor"; + case 152: + case 147: + return "__call"; + case 153: + case 148: + return "__new"; + case 149: + return "__index"; + case 228: + return "__export"; + case 227: + return node.isExportEquals ? "export=" : "default"; + case 181: + return "export="; + case 213: + case 214: + return node.flags & 512 ? "default" : undefined; + } + } + function getDisplayName(node) { + return node.name ? ts.declarationNameToString(node.name) : getDeclarationName(node); + } + function declareSymbol(symbolTable, parent, node, includes, excludes) { + ts.Debug.assert(!ts.hasDynamicName(node)); + var isDefaultExport = node.flags & 512; + var name = isDefaultExport && parent ? "default" : getDeclarationName(node); + var symbol; + if (name !== undefined) { + symbol = ts.hasProperty(symbolTable, name) + ? symbolTable[name] + : (symbolTable[name] = createSymbol(0, name)); + if (name && (includes & 788448)) { + classifiableNames[name] = name; + } + if (symbol.flags & excludes) { + if (node.name) { + node.name.parent = node; + } + var message = symbol.flags & 2 + ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 + : ts.Diagnostics.Duplicate_identifier_0; + ts.forEach(symbol.declarations, function (declaration) { + if (declaration.flags & 512) { + message = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + }); + ts.forEach(symbol.declarations, function (declaration) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message, getDisplayName(declaration))); + }); + file.bindDiagnostics.push(ts.createDiagnosticForNode(node.name || node, message, getDisplayName(node))); + symbol = createSymbol(0, name); + } + } + else { + symbol = createSymbol(0, "__missing"); + } + addDeclarationToSymbol(symbol, node, includes); + symbol.parent = parent; + return symbol; + } + function declareModuleMember(node, symbolFlags, symbolExcludes) { + var hasExportModifier = ts.getCombinedNodeFlags(node) & 2; + if (symbolFlags & 8388608) { + if (node.kind === 230 || (node.kind === 221 && hasExportModifier)) { + return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + } + else { + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + else { + if (hasExportModifier || container.flags & 131072) { + var exportKind = (symbolFlags & 107455 ? 1048576 : 0) | + (symbolFlags & 793056 ? 2097152 : 0) | + (symbolFlags & 1536 ? 4194304 : 0); + var local = declareSymbol(container.locals, undefined, node, exportKind, symbolExcludes); + local.exportSymbol = declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + node.localSymbol = local; + return local; + } + else { + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + } + function bindChildren(node) { + var saveParent = parent; + var saveContainer = container; + var savedBlockScopeContainer = blockScopeContainer; + parent = node; + var containerFlags = getContainerFlags(node); + if (containerFlags & 1) { + container = blockScopeContainer = node; + if (containerFlags & 4) { + container.locals = {}; + } + addToContainerChain(container); + } + else if (containerFlags & 2) { + blockScopeContainer = node; + blockScopeContainer.locals = undefined; + } + var savedReachabilityState; + var savedLabelStack; + var savedLabels; + var savedImplicitLabels; + var savedHasExplicitReturn; + var kind = node.kind; + var flags = node.flags; + flags &= ~1572864; + if (kind === 215) { + seenThisKeyword = false; + } + var saveState = kind === 248 || kind === 219 || ts.isFunctionLikeKind(kind); + if (saveState) { + savedReachabilityState = currentReachabilityState; + savedLabelStack = labelStack; + savedLabels = labelIndexMap; + savedImplicitLabels = implicitLabels; + savedHasExplicitReturn = hasExplicitReturn; + currentReachabilityState = 2; + hasExplicitReturn = false; + labelStack = labelIndexMap = implicitLabels = undefined; + } + bindReachableStatement(node); + if (currentReachabilityState === 2 && ts.isFunctionLikeKind(kind) && ts.nodeIsPresent(node.body)) { + flags |= 524288; + if (hasExplicitReturn) { + flags |= 1048576; + } + } + if (kind === 215) { + flags = seenThisKeyword ? flags | 262144 : flags & ~262144; + } + node.flags = flags; + if (saveState) { + hasExplicitReturn = savedHasExplicitReturn; + currentReachabilityState = savedReachabilityState; + labelStack = savedLabelStack; + labelIndexMap = savedLabels; + implicitLabels = savedImplicitLabels; + } + container = saveContainer; + parent = saveParent; + blockScopeContainer = savedBlockScopeContainer; + } + function bindReachableStatement(node) { + if (checkUnreachable(node)) { + ts.forEachChild(node, bind); + return; + } + switch (node.kind) { + case 198: + bindWhileStatement(node); + break; + case 197: + bindDoStatement(node); + break; + case 199: + bindForStatement(node); + break; + case 200: + case 201: + bindForInOrForOfStatement(node); + break; + case 196: + bindIfStatement(node); + break; + case 204: + case 208: + bindReturnOrThrow(node); + break; + case 203: + case 202: + bindBreakOrContinueStatement(node); + break; + case 209: + bindTryStatement(node); + break; + case 206: + bindSwitchStatement(node); + break; + case 220: + bindCaseBlock(node); + break; + case 207: + bindLabeledStatement(node); + break; + default: + ts.forEachChild(node, bind); + break; + } + } + function bindWhileStatement(n) { + var preWhileState = n.expression.kind === 84 ? 4 : currentReachabilityState; + var postWhileState = n.expression.kind === 99 ? 4 : currentReachabilityState; + bind(n.expression); + currentReachabilityState = preWhileState; + var postWhileLabel = pushImplicitLabel(); + bind(n.statement); + popImplicitLabel(postWhileLabel, postWhileState); + } + function bindDoStatement(n) { + var preDoState = currentReachabilityState; + var postDoLabel = pushImplicitLabel(); + bind(n.statement); + var postDoState = n.expression.kind === 99 ? 4 : preDoState; + popImplicitLabel(postDoLabel, postDoState); + bind(n.expression); + } + function bindForStatement(n) { + var preForState = currentReachabilityState; + var postForLabel = pushImplicitLabel(); + bind(n.initializer); + bind(n.condition); + bind(n.incrementor); + bind(n.statement); + var isInfiniteLoop = (!n.condition || n.condition.kind === 99); + var postForState = isInfiniteLoop ? 4 : preForState; + popImplicitLabel(postForLabel, postForState); + } + function bindForInOrForOfStatement(n) { + var preStatementState = currentReachabilityState; + var postStatementLabel = pushImplicitLabel(); + bind(n.initializer); + bind(n.expression); + bind(n.statement); + popImplicitLabel(postStatementLabel, preStatementState); + } + function bindIfStatement(n) { + var ifTrueState = n.expression.kind === 84 ? 4 : currentReachabilityState; + var ifFalseState = n.expression.kind === 99 ? 4 : currentReachabilityState; + currentReachabilityState = ifTrueState; + bind(n.expression); + bind(n.thenStatement); + if (n.elseStatement) { + var preElseState = currentReachabilityState; + currentReachabilityState = ifFalseState; + bind(n.elseStatement); + currentReachabilityState = or(currentReachabilityState, preElseState); + } + else { + currentReachabilityState = or(currentReachabilityState, ifFalseState); + } + } + function bindReturnOrThrow(n) { + bind(n.expression); + if (n.kind === 204) { + hasExplicitReturn = true; + } + currentReachabilityState = 4; + } + function bindBreakOrContinueStatement(n) { + bind(n.label); + var isValidJump = jumpToLabel(n.label, n.kind === 203 ? currentReachabilityState : 4); + if (isValidJump) { + currentReachabilityState = 4; + } + } + function bindTryStatement(n) { + var preTryState = currentReachabilityState; + bind(n.tryBlock); + var postTryState = currentReachabilityState; + currentReachabilityState = preTryState; + bind(n.catchClause); + var postCatchState = currentReachabilityState; + currentReachabilityState = preTryState; + bind(n.finallyBlock); + currentReachabilityState = or(postTryState, postCatchState); + } + function bindSwitchStatement(n) { + var preSwitchState = currentReachabilityState; + var postSwitchLabel = pushImplicitLabel(); + bind(n.expression); + bind(n.caseBlock); + var hasDefault = ts.forEach(n.caseBlock.clauses, function (c) { return c.kind === 242; }); + var postSwitchState = hasDefault && currentReachabilityState !== 2 ? 4 : preSwitchState; + popImplicitLabel(postSwitchLabel, postSwitchState); + } + function bindCaseBlock(n) { + var startState = currentReachabilityState; + for (var _i = 0, _a = n.clauses; _i < _a.length; _i++) { + var clause = _a[_i]; + currentReachabilityState = startState; + bind(clause); + if (clause.statements.length && currentReachabilityState === 2 && options.noFallthroughCasesInSwitch) { + errorOnFirstToken(clause, ts.Diagnostics.Fallthrough_case_in_switch); + } + } + } + function bindLabeledStatement(n) { + bind(n.label); + var ok = pushNamedLabel(n.label); + bind(n.statement); + if (ok) { + popNamedLabel(n.label, currentReachabilityState); + } + } + function getContainerFlags(node) { + switch (node.kind) { + case 186: + case 214: + case 215: + case 217: + case 155: + case 165: + return 1; + case 147: + case 148: + case 149: + case 143: + case 142: + case 213: + case 144: + case 145: + case 146: + case 152: + case 153: + case 173: + case 174: + case 218: + case 248: + case 216: + return 5; + case 244: + case 199: + case 200: + case 201: + case 220: + return 2; + case 192: + return ts.isFunctionLike(node.parent) ? 0 : 2; + } + return 0; + } + function addToContainerChain(next) { + if (lastContainer) { + lastContainer.nextContainer = next; + } + lastContainer = next; + } + function declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes) { + declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes); + } + function declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes) { + switch (container.kind) { + case 218: + return declareModuleMember(node, symbolFlags, symbolExcludes); + case 248: + return declareSourceFileMember(node, symbolFlags, symbolExcludes); + case 186: + case 214: + return declareClassMember(node, symbolFlags, symbolExcludes); + case 217: + return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + case 155: + case 165: + case 215: + return declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); + case 152: + case 153: + case 147: + case 148: + case 149: + case 143: + case 142: + case 144: + case 145: + case 146: + case 213: + case 173: + case 174: + case 216: + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + function declareClassMember(node, symbolFlags, symbolExcludes) { + return node.flags & 64 + ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) + : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); + } + function declareSourceFileMember(node, symbolFlags, symbolExcludes) { + return ts.isExternalModule(file) + ? declareModuleMember(node, symbolFlags, symbolExcludes) + : declareSymbol(file.locals, undefined, node, symbolFlags, symbolExcludes); + } + function hasExportDeclarations(node) { + var body = node.kind === 248 ? node : node.body; + if (body.kind === 248 || body.kind === 219) { + for (var _i = 0, _a = body.statements; _i < _a.length; _i++) { + var stat = _a[_i]; + if (stat.kind === 228 || stat.kind === 227) { + return true; + } + } + } + return false; + } + function setExportContextFlag(node) { + if (ts.isInAmbientContext(node) && !hasExportDeclarations(node)) { + node.flags |= 131072; + } + else { + node.flags &= ~131072; + } + } + function bindModuleDeclaration(node) { + setExportContextFlag(node); + if (node.name.kind === 9) { + declareSymbolAndAddToSymbolTable(node, 512, 106639); + } + else { + var state = getModuleInstanceState(node); + if (state === 0) { + declareSymbolAndAddToSymbolTable(node, 1024, 0); + } + else { + declareSymbolAndAddToSymbolTable(node, 512, 106639); + if (node.symbol.flags & (16 | 32 | 256)) { + node.symbol.constEnumOnlyModule = false; + } + else { + var currentModuleIsConstEnumOnly = state === 2; + if (node.symbol.constEnumOnlyModule === undefined) { + node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly; + } + else { + node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly; + } + } + } + } + } + function bindFunctionOrConstructorType(node) { + var symbol = createSymbol(131072, getDeclarationName(node)); + addDeclarationToSymbol(symbol, node, 131072); + var typeLiteralSymbol = createSymbol(2048, "__type"); + addDeclarationToSymbol(typeLiteralSymbol, node, 2048); + typeLiteralSymbol.members = (_a = {}, _a[symbol.name] = symbol, _a); + var _a; + } + function bindObjectLiteralExpression(node) { + if (inStrictMode) { + var seen = {}; + for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { + var prop = _a[_i]; + if (prop.name.kind !== 69) { + continue; + } + var identifier = prop.name; + var currentKind = prop.kind === 245 || prop.kind === 246 || prop.kind === 143 + ? 1 + : 2; + var existingKind = seen[identifier.text]; + if (!existingKind) { + seen[identifier.text] = currentKind; + continue; + } + if (currentKind === 1 && existingKind === 1) { + var span = ts.getErrorSpanForNode(file, identifier); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode)); + } + } + } + return bindAnonymousDeclaration(node, 4096, "__object"); + } + function bindAnonymousDeclaration(node, symbolFlags, name) { + var symbol = createSymbol(symbolFlags, name); + addDeclarationToSymbol(symbol, node, symbolFlags); + } + function bindBlockScopedDeclaration(node, symbolFlags, symbolExcludes) { + switch (blockScopeContainer.kind) { + case 218: + declareModuleMember(node, symbolFlags, symbolExcludes); + break; + case 248: + if (ts.isExternalModule(container)) { + declareModuleMember(node, symbolFlags, symbolExcludes); + break; + } + default: + if (!blockScopeContainer.locals) { + blockScopeContainer.locals = {}; + addToContainerChain(blockScopeContainer); + } + declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + function bindBlockScopedVariableDeclaration(node) { + bindBlockScopedDeclaration(node, 2, 107455); + } + function checkStrictModeIdentifier(node) { + if (inStrictMode && + node.originalKeywordKind >= 106 && + node.originalKeywordKind <= 114 && + !ts.isIdentifierName(node)) { + if (!file.parseDiagnostics.length) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(node, getStrictModeIdentifierMessage(node), ts.declarationNameToString(node))); + } + } + } + function getStrictModeIdentifierMessage(node) { + if (ts.getContainingClass(node)) { + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; + } + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode; + } + function checkStrictModeBinaryExpression(node) { + if (inStrictMode && ts.isLeftHandSideExpression(node.left) && ts.isAssignmentOperator(node.operatorToken.kind)) { + checkStrictModeEvalOrArguments(node, node.left); + } + } + function checkStrictModeCatchClause(node) { + if (inStrictMode && node.variableDeclaration) { + checkStrictModeEvalOrArguments(node, node.variableDeclaration.name); + } + } + function checkStrictModeDeleteExpression(node) { + if (inStrictMode && node.expression.kind === 69) { + var span = ts.getErrorSpanForNode(file, node.expression); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.delete_cannot_be_called_on_an_identifier_in_strict_mode)); + } + } + function isEvalOrArgumentsIdentifier(node) { + return node.kind === 69 && + (node.text === "eval" || node.text === "arguments"); + } + function checkStrictModeEvalOrArguments(contextNode, name) { + if (name && name.kind === 69) { + var identifier = name; + if (isEvalOrArgumentsIdentifier(identifier)) { + var span = ts.getErrorSpanForNode(file, name); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, getStrictModeEvalOrArgumentsMessage(contextNode), identifier.text)); + } + } + } + function getStrictModeEvalOrArgumentsMessage(node) { + if (ts.getContainingClass(node)) { + return ts.Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { + return ts.Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; + } + return ts.Diagnostics.Invalid_use_of_0_in_strict_mode; + } + function checkStrictModeFunctionName(node) { + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.name); + } + } + function checkStrictModeNumericLiteral(node) { + if (inStrictMode && node.flags & 32768) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Octal_literals_are_not_allowed_in_strict_mode)); + } + } + function checkStrictModePostfixUnaryExpression(node) { + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.operand); + } + } + function checkStrictModePrefixUnaryExpression(node) { + if (inStrictMode) { + if (node.operator === 41 || node.operator === 42) { + checkStrictModeEvalOrArguments(node, node.operand); + } + } + } + function checkStrictModeWithStatement(node) { + if (inStrictMode) { + errorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_strict_mode); + } + } + function errorOnFirstToken(node, message, arg0, arg1, arg2) { + var span = ts.getSpanOfTokenAtPosition(file, node.pos); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2)); + } + function getDestructuringParameterName(node) { + return "__" + ts.indexOf(node.parent.parameters, node); + } + function bind(node) { + if (!node) { + return; + } + node.parent = parent; + var savedInStrictMode = inStrictMode; + if (!savedInStrictMode) { + updateStrictMode(node); + } + bindWorker(node); + bindChildren(node); + inStrictMode = savedInStrictMode; + } + function updateStrictMode(node) { + switch (node.kind) { + case 248: + case 219: + updateStrictModeStatementList(node.statements); + return; + case 192: + if (ts.isFunctionLike(node.parent)) { + updateStrictModeStatementList(node.statements); + } + return; + case 214: + case 186: + inStrictMode = true; + return; + } + } + function updateStrictModeStatementList(statements) { + for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) { + var statement = statements_1[_i]; + if (!ts.isPrologueDirective(statement)) { + return; + } + if (isUseStrictPrologueDirective(statement)) { + inStrictMode = true; + return; + } + } + } + function isUseStrictPrologueDirective(node) { + var nodeText = ts.getTextOfNodeFromSourceText(file.text, node.expression); + return nodeText === "\"use strict\"" || nodeText === "'use strict'"; + } + function bindWorker(node) { + switch (node.kind) { + case 69: + return checkStrictModeIdentifier(node); + case 181: + if (ts.isInJavaScriptFile(node)) { + if (ts.isExportsPropertyAssignment(node)) { + bindExportsPropertyAssignment(node); + } + else if (ts.isModuleExportsAssignment(node)) { + bindModuleExportsAssignment(node); + } + } + return checkStrictModeBinaryExpression(node); + case 244: + return checkStrictModeCatchClause(node); + case 175: + return checkStrictModeDeleteExpression(node); + case 8: + return checkStrictModeNumericLiteral(node); + case 180: + return checkStrictModePostfixUnaryExpression(node); + case 179: + return checkStrictModePrefixUnaryExpression(node); + case 205: + return checkStrictModeWithStatement(node); + case 97: + seenThisKeyword = true; + return; + case 137: + return declareSymbolAndAddToSymbolTable(node, 262144, 530912); + case 138: + return bindParameter(node); + case 211: + case 163: + return bindVariableDeclarationOrBindingElement(node); + case 141: + case 140: + return bindPropertyOrMethodOrAccessor(node, 4 | (node.questionToken ? 536870912 : 0), 107455); + case 245: + case 246: + return bindPropertyOrMethodOrAccessor(node, 4, 107455); + case 247: + return bindPropertyOrMethodOrAccessor(node, 8, 107455); + case 147: + case 148: + case 149: + return declareSymbolAndAddToSymbolTable(node, 131072, 0); + case 143: + case 142: + return bindPropertyOrMethodOrAccessor(node, 8192 | (node.questionToken ? 536870912 : 0), ts.isObjectLiteralMethod(node) ? 107455 : 99263); + case 213: + checkStrictModeFunctionName(node); + return declareSymbolAndAddToSymbolTable(node, 16, 106927); + case 144: + return declareSymbolAndAddToSymbolTable(node, 16384, 0); + case 145: + return bindPropertyOrMethodOrAccessor(node, 32768, 41919); + case 146: + return bindPropertyOrMethodOrAccessor(node, 65536, 74687); + case 152: + case 153: + return bindFunctionOrConstructorType(node); + case 155: + return bindAnonymousDeclaration(node, 2048, "__type"); + case 165: + return bindObjectLiteralExpression(node); + case 173: + case 174: + checkStrictModeFunctionName(node); + var bindingName = node.name ? node.name.text : "__function"; + return bindAnonymousDeclaration(node, 16, bindingName); + case 168: + if (ts.isInJavaScriptFile(node)) { + bindCallExpression(node); + } + break; + case 186: + case 214: + return bindClassLikeDeclaration(node); + case 215: + return bindBlockScopedDeclaration(node, 64, 792960); + case 216: + return bindBlockScopedDeclaration(node, 524288, 793056); + case 217: + return bindEnumDeclaration(node); + case 218: + return bindModuleDeclaration(node); + case 221: + case 224: + case 226: + case 230: + return declareSymbolAndAddToSymbolTable(node, 8388608, 8388608); + case 223: + return bindImportClause(node); + case 228: + return bindExportDeclaration(node); + case 227: + return bindExportAssignment(node); + case 248: + return bindSourceFileIfExternalModule(); + } + } + function bindSourceFileIfExternalModule() { + setExportContextFlag(file); + if (ts.isExternalModule(file)) { + bindSourceFileAsExternalModule(); + } + } + function bindSourceFileAsExternalModule() { + bindAnonymousDeclaration(file, 512, "\"" + ts.removeFileExtension(file.fileName) + "\""); + } + function bindExportAssignment(node) { + var boundExpression = node.kind === 227 ? node.expression : node.right; + if (!container.symbol || !container.symbol.exports) { + bindAnonymousDeclaration(node, 8388608, getDeclarationName(node)); + } + else if (boundExpression.kind === 69) { + declareSymbol(container.symbol.exports, container.symbol, node, 8388608, 107455 | 8388608); + } + else { + declareSymbol(container.symbol.exports, container.symbol, node, 4, 107455 | 8388608); + } + } + function bindExportDeclaration(node) { + if (!container.symbol || !container.symbol.exports) { + bindAnonymousDeclaration(node, 1073741824, getDeclarationName(node)); + } + else if (!node.exportClause) { + declareSymbol(container.symbol.exports, container.symbol, node, 1073741824, 0); + } + } + function bindImportClause(node) { + if (node.name) { + declareSymbolAndAddToSymbolTable(node, 8388608, 8388608); + } + } + function setCommonJsModuleIndicator(node) { + if (!file.commonJsModuleIndicator) { + file.commonJsModuleIndicator = node; + bindSourceFileAsExternalModule(); + } + } + function bindExportsPropertyAssignment(node) { + setCommonJsModuleIndicator(node); + declareSymbol(file.symbol.exports, file.symbol, node.left, 4 | 7340032, 0); + } + function bindModuleExportsAssignment(node) { + setCommonJsModuleIndicator(node); + bindExportAssignment(node); + } + function bindCallExpression(node) { + if (!file.commonJsModuleIndicator && ts.isRequireCall(node)) { + setCommonJsModuleIndicator(node); + } + } + function bindClassLikeDeclaration(node) { + if (node.kind === 214) { + bindBlockScopedDeclaration(node, 32, 899519); + } + else { + var bindingName = node.name ? node.name.text : "__class"; + bindAnonymousDeclaration(node, 32, bindingName); + if (node.name) { + classifiableNames[node.name.text] = node.name.text; + } + } + var symbol = node.symbol; + var prototypeSymbol = createSymbol(4 | 134217728, "prototype"); + if (ts.hasProperty(symbol.exports, prototypeSymbol.name)) { + if (node.name) { + node.name.parent = node; + } + file.bindDiagnostics.push(ts.createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0], ts.Diagnostics.Duplicate_identifier_0, prototypeSymbol.name)); + } + symbol.exports[prototypeSymbol.name] = prototypeSymbol; + prototypeSymbol.parent = symbol; + } + function bindEnumDeclaration(node) { + return ts.isConst(node) + ? bindBlockScopedDeclaration(node, 128, 899967) + : bindBlockScopedDeclaration(node, 256, 899327); + } + function bindVariableDeclarationOrBindingElement(node) { + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.name); + } + if (!ts.isBindingPattern(node.name)) { + if (ts.isBlockOrCatchScoped(node)) { + bindBlockScopedVariableDeclaration(node); + } + else if (ts.isParameterDeclaration(node)) { + declareSymbolAndAddToSymbolTable(node, 1, 107455); + } + else { + declareSymbolAndAddToSymbolTable(node, 1, 107454); + } + } + } + function bindParameter(node) { + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.name); + } + if (ts.isBindingPattern(node.name)) { + bindAnonymousDeclaration(node, 1, getDestructuringParameterName(node)); + } + else { + declareSymbolAndAddToSymbolTable(node, 1, 107455); + } + if (node.flags & 56 && + node.parent.kind === 144 && + ts.isClassLike(node.parent.parent)) { + var classDeclaration = node.parent.parent; + declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, 4, 107455); + } + } + function bindPropertyOrMethodOrAccessor(node, symbolFlags, symbolExcludes) { + return ts.hasDynamicName(node) + ? bindAnonymousDeclaration(node, symbolFlags, "__computed") + : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); + } + function pushNamedLabel(name) { + initializeReachabilityStateIfNecessary(); + if (ts.hasProperty(labelIndexMap, name.text)) { + return false; + } + labelIndexMap[name.text] = labelStack.push(1) - 1; + return true; + } + function pushImplicitLabel() { + initializeReachabilityStateIfNecessary(); + var index = labelStack.push(1) - 1; + implicitLabels.push(index); + return index; + } + function popNamedLabel(label, outerState) { + var index = labelIndexMap[label.text]; + ts.Debug.assert(index !== undefined); + ts.Debug.assert(labelStack.length == index + 1); + labelIndexMap[label.text] = undefined; + setCurrentStateAtLabel(labelStack.pop(), outerState, label); + } + function popImplicitLabel(implicitLabelIndex, outerState) { + if (labelStack.length !== implicitLabelIndex + 1) { + ts.Debug.assert(false, "Label stack: " + labelStack.length + ", index:" + implicitLabelIndex); + } + var i = implicitLabels.pop(); + if (implicitLabelIndex !== i) { + ts.Debug.assert(false, "i: " + i + ", index: " + implicitLabelIndex); + } + setCurrentStateAtLabel(labelStack.pop(), outerState, undefined); + } + function setCurrentStateAtLabel(innerMergedState, outerState, label) { + if (innerMergedState === 1) { + if (label && !options.allowUnusedLabels) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(label, ts.Diagnostics.Unused_label)); + } + currentReachabilityState = outerState; + } + else { + currentReachabilityState = or(innerMergedState, outerState); + } + } + function jumpToLabel(label, outerState) { + initializeReachabilityStateIfNecessary(); + var index = label ? labelIndexMap[label.text] : ts.lastOrUndefined(implicitLabels); + if (index === undefined) { + return false; + } + var stateAtLabel = labelStack[index]; + labelStack[index] = stateAtLabel === 1 ? outerState : or(stateAtLabel, outerState); + return true; + } + function checkUnreachable(node) { + switch (currentReachabilityState) { + case 4: + var reportError = (ts.isStatement(node) && node.kind !== 194) || + node.kind === 214 || + (node.kind === 218 && shouldReportErrorOnModuleDeclaration(node)) || + (node.kind === 217 && (!ts.isConstEnumDeclaration(node) || options.preserveConstEnums)); + if (reportError) { + currentReachabilityState = 8; + var reportUnreachableCode = !options.allowUnreachableCode && + !ts.isInAmbientContext(node) && + (node.kind !== 193 || + ts.getCombinedNodeFlags(node.declarationList) & 24576 || + ts.forEach(node.declarationList.declarations, function (d) { return d.initializer; })); + if (reportUnreachableCode) { + errorOnFirstToken(node, ts.Diagnostics.Unreachable_code_detected); + } + } + case 8: + return true; + default: + return false; + } + function shouldReportErrorOnModuleDeclaration(node) { + var instanceState = getModuleInstanceState(node); + return instanceState === 1 || (instanceState === 2 && options.preserveConstEnums); + } + } + function initializeReachabilityStateIfNecessary() { + if (labelIndexMap) { + return; + } + currentReachabilityState = 2; + labelIndexMap = {}; + labelStack = []; + implicitLabels = []; + } + } +})(ts || (ts = {})); +var ts; (function (ts) { var nextSymbolId = 1; var nextNodeId = 1; @@ -10853,7 +10987,7 @@ var ts; symbolToString: symbolToString, getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, getRootSymbols: getRootSymbols, - getContextualType: getContextualType, + getContextualType: getApparentTypeOfContextualType, getFullyQualifiedName: getFullyQualifiedName, getResolvedSignature: getResolvedSignature, getConstantValue: getConstantValue, @@ -11109,7 +11243,7 @@ var ts; return ts.getAncestor(node, 248); } function isGlobalSourceFile(node) { - return node.kind === 248 && !ts.isExternalModule(node); + return node.kind === 248 && !ts.isExternalOrCommonJsModule(node); } function getSymbol(symbols, name, meaning) { if (meaning && ts.hasProperty(symbols, name)) { @@ -11195,23 +11329,24 @@ var ts; } switch (location.kind) { case 248: - if (!ts.isExternalModule(location)) + if (!ts.isExternalOrCommonJsModule(location)) break; case 218: var moduleExports = getSymbolOfNode(location).exports; if (location.kind === 248 || (location.kind === 218 && location.name.kind === 9)) { + if (result = moduleExports["default"]) { + var localSymbol = ts.getLocalSymbolForExportDefault(result); + if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { + break loop; + } + result = undefined; + } if (ts.hasProperty(moduleExports, name) && moduleExports[name].flags === 8388608 && ts.getDeclarationOfKind(moduleExports[name], 230)) { break; } - result = moduleExports["default"]; - var localSymbol = ts.getLocalSymbolForExportDefault(result); - if (result && localSymbol && (result.flags & meaning) && localSymbol.name === name) { - break loop; - } - result = undefined; } if (result = getSymbol(moduleExports, name, meaning & 8914931)) { break loop; @@ -11569,6 +11704,9 @@ var ts; if (moduleName === undefined) { return; } + if (moduleName.indexOf("!") >= 0) { + moduleName = moduleName.substr(0, moduleName.indexOf("!")); + } var isRelative = ts.isExternalModuleNameRelative(moduleName); if (!isRelative) { var symbol = getSymbol(globals, "\"" + moduleName + "\"", 512); @@ -11739,7 +11877,7 @@ var ts; } switch (location_1.kind) { case 248: - if (!ts.isExternalModule(location_1)) { + if (!ts.isExternalOrCommonJsModule(location_1)) { break; } case 218: @@ -11866,7 +12004,7 @@ var ts; } function hasExternalModuleSymbol(declaration) { return (declaration.kind === 218 && declaration.name.kind === 9) || - (declaration.kind === 248 && ts.isExternalModule(declaration)); + (declaration.kind === 248 && ts.isExternalOrCommonJsModule(declaration)); } function hasVisibleDeclarations(symbol) { var aliasesToMakeVisible; @@ -12064,7 +12202,7 @@ var ts; writeAnonymousType(type, flags); } else if (type.flags & 256) { - writer.writeStringLiteral(type.text); + writer.writeStringLiteral("\"" + ts.escapeString(type.text) + "\""); } else { writePunctuation(writer, 15); @@ -12428,7 +12566,7 @@ var ts; } } else if (node.kind === 248) { - return ts.isExternalModule(node) ? node : undefined; + return ts.isExternalOrCommonJsModule(node) ? node : undefined; } } ts.Debug.fail("getContainingModule cant reach here"); @@ -12633,6 +12771,23 @@ var ts; var symbol = getSymbolOfNode(node); return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node); } + function getTextOfPropertyName(name) { + switch (name.kind) { + case 69: + return name.text; + case 9: + case 8: + return name.text; + case 136: + if (ts.isStringOrNumericLiteral(name.expression.kind)) { + return name.expression.text; + } + } + return undefined; + } + function isComputedNonLiteralName(name) { + return name.kind === 136 && !ts.isStringOrNumericLiteral(name.expression.kind); + } function getTypeForBindingElement(declaration) { var pattern = declaration.parent; var parentType = getTypeForBindingElementParent(pattern.parent); @@ -12648,8 +12803,12 @@ var ts; var type; if (pattern.kind === 161) { var name_10 = declaration.propertyName || declaration.name; - type = getTypeOfPropertyOfType(parentType, name_10.text) || - isNumericLiteralName(name_10.text) && getIndexTypeOfType(parentType, 1) || + if (isComputedNonLiteralName(name_10)) { + return anyType; + } + var text = getTextOfPropertyName(name_10); + type = getTypeOfPropertyOfType(parentType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1) || getIndexTypeOfType(parentType, 0); if (!type) { error(name_10, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_10)); @@ -12727,10 +12886,16 @@ var ts; } function getTypeFromObjectBindingPattern(pattern, includePatternInType) { var members = {}; + var hasComputedProperties = false; ts.forEach(pattern.elements, function (e) { - var flags = 4 | 67108864 | (e.initializer ? 536870912 : 0); var name = e.propertyName || e.name; - var symbol = createSymbol(flags, name.text); + if (isComputedNonLiteralName(name)) { + hasComputedProperties = true; + return; + } + var text = getTextOfPropertyName(name); + var flags = 4 | 67108864 | (e.initializer ? 536870912 : 0); + var symbol = createSymbol(flags, text); symbol.type = getTypeFromBindingElement(e, includePatternInType); symbol.bindingElement = e; members[symbol.name] = symbol; @@ -12739,6 +12904,9 @@ var ts; if (includePatternInType) { result.pattern = pattern; } + if (hasComputedProperties) { + result.flags |= 67108864; + } return result; } function getTypeFromArrayBindingPattern(pattern, includePatternInType) { @@ -12789,6 +12957,12 @@ var ts; if (declaration.kind === 227) { return links.type = checkExpression(declaration.expression); } + if (declaration.kind === 181) { + return links.type = checkExpression(declaration.right); + } + if (declaration.kind === 166) { + return checkExpressionCached(declaration.parent.right); + } if (!pushTypeResolution(symbol, 0)) { return unknownType; } @@ -13038,17 +13212,19 @@ var ts; } function resolveBaseTypesOfClass(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - var baseContructorType = getBaseConstructorTypeOfClass(type); - if (!(baseContructorType.flags & 80896)) { + var baseConstructorType = getBaseConstructorTypeOfClass(type); + if (!(baseConstructorType.flags & 80896)) { return; } var baseTypeNode = getBaseTypeNodeOfClass(type); var baseType; - if (baseContructorType.symbol && baseContructorType.symbol.flags & 32) { - baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseContructorType.symbol); + var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; + if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 && + areAllOuterTypeParametersApplied(originalBaseType)) { + baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); } else { - var constructors = getInstantiatedConstructorsForTypeArguments(baseContructorType, baseTypeNode.typeArguments); + var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); if (!constructors.length) { error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); return; @@ -13073,6 +13249,15 @@ var ts; type.resolvedBaseTypes.push(baseType); } } + function areAllOuterTypeParametersApplied(type) { + var outerTypeParameters = type.outerTypeParameters; + if (outerTypeParameters) { + var last = outerTypeParameters.length - 1; + var typeArguments = type.typeArguments; + return outerTypeParameters[last].symbol !== typeArguments[last].symbol; + } + return true; + } function resolveBaseTypesOfInterface(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { @@ -13144,7 +13329,7 @@ var ts; type.typeArguments = type.typeParameters; type.thisType = createType(512 | 33554432); type.thisType.symbol = symbol; - type.thisType.constraint = getTypeWithThisArgument(type); + type.thisType.constraint = type; } } return links.declaredType; @@ -13619,14 +13804,19 @@ var ts; type = getApparentType(type); return type.flags & 49152 ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); } + function getApparentTypeOfTypeParameter(type) { + if (!type.resolvedApparentType) { + var constraintType = getConstraintOfTypeParameter(type); + while (constraintType && constraintType.flags & 512) { + constraintType = getConstraintOfTypeParameter(constraintType); + } + type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); + } + return type.resolvedApparentType; + } function getApparentType(type) { if (type.flags & 512) { - do { - type = getConstraintOfTypeParameter(type); - } while (type && type.flags & 512); - if (!type) { - type = emptyObjectType; - } + type = getApparentTypeOfTypeParameter(type); } if (type.flags & 258) { type = globalStringType; @@ -13779,7 +13969,7 @@ var ts; if (node.initializer) { var signatureDeclaration = node.parent; var signature = getSignatureFromDeclaration(signatureDeclaration); - var parameterIndex = signatureDeclaration.parameters.indexOf(node); + var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); ts.Debug.assert(parameterIndex >= 0); return parameterIndex >= signature.minArgumentCount; } @@ -13874,6 +14064,16 @@ var ts; } return result; } + function resolveExternalModuleTypeByLiteral(name) { + var moduleSym = resolveExternalModuleName(name, name); + if (moduleSym) { + var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); + if (resolvedModuleSymbol) { + return getTypeOfSymbol(resolvedModuleSymbol); + } + } + return anyType; + } function getReturnTypeOfSignature(signature) { if (!signature.resolvedReturnType) { if (!pushTypeResolution(signature, 3)) { @@ -14335,11 +14535,12 @@ var ts; return links.resolvedType; } function getStringLiteralType(node) { - if (ts.hasProperty(stringLiteralTypes, node.text)) { - return stringLiteralTypes[node.text]; + var text = node.text; + if (ts.hasProperty(stringLiteralTypes, text)) { + return stringLiteralTypes[text]; } - var type = stringLiteralTypes[node.text] = createType(256); - type.text = ts.getTextOfNode(node); + var type = stringLiteralTypes[text] = createType(256); + type.text = text; return type; } function getTypeFromStringLiteral(node) { @@ -14808,7 +15009,7 @@ var ts; return false; } function hasExcessProperties(source, target, reportErrors) { - if (someConstituentTypeHasKind(target, 80896)) { + if (!(target.flags & 67108864) && someConstituentTypeHasKind(target, 80896)) { for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { var prop = _a[_i]; if (!isKnownProperty(target, prop.name)) { @@ -14898,9 +15099,6 @@ var ts; return result; } function typeParameterIdenticalTo(source, target) { - if (source.symbol.name !== target.symbol.name) { - return 0; - } if (source.constraint === target.constraint) { return -1; } @@ -15345,18 +15543,24 @@ var ts; } return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } + function isMatchingSignature(source, target, partialMatch) { + if (source.parameters.length === target.parameters.length && + source.minArgumentCount === target.minArgumentCount && + source.hasRestParameter === target.hasRestParameter) { + return true; + } + if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (source.hasRestParameter && !target.hasRestParameter || + source.hasRestParameter === target.hasRestParameter && source.parameters.length >= target.parameters.length)) { + return true; + } + return false; + } function compareSignatures(source, target, partialMatch, ignoreReturnTypes, compareTypes) { if (source === target) { return -1; } - if (source.parameters.length !== target.parameters.length || - source.minArgumentCount !== target.minArgumentCount || - source.hasRestParameter !== target.hasRestParameter) { - if (!partialMatch || - source.parameters.length < target.parameters.length && !source.hasRestParameter || - source.minArgumentCount > target.minArgumentCount) { - return 0; - } + if (!(isMatchingSignature(source, target, partialMatch))) { + return 0; } var result = -1; if (source.typeParameters && target.typeParameters) { @@ -15441,6 +15645,9 @@ var ts; function isTupleLikeType(type) { return !!getPropertyOfType(type, "0"); } + function isStringLiteralType(type) { + return type.flags & 256; + } function isTupleType(type) { return !!(type.flags & 8192); } @@ -16032,7 +16239,7 @@ var ts; } } function narrowTypeByInstanceof(type, expr, assumeTrue) { - if (isTypeAny(type) || !assumeTrue || expr.left.kind !== 69 || getResolvedSymbol(expr.left) !== symbol) { + if (isTypeAny(type) || expr.left.kind !== 69 || getResolvedSymbol(expr.left) !== symbol) { return type; } var rightType = checkExpression(expr.right); @@ -16060,6 +16267,12 @@ var ts; } } if (targetType) { + if (!assumeTrue) { + if (type.flags & 16384) { + return getUnionType(ts.filter(type.types, function (t) { return !isTypeSubtypeOf(t, targetType); })); + } + return type; + } return getNarrowedType(type, targetType); } return type; @@ -16471,6 +16684,9 @@ var ts; function getIndexTypeOfContextualType(type, kind) { return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); } + function contextualTypeIsStringLiteralType(type) { + return !!(type.flags & 16384 ? ts.forEach(type.types, isStringLiteralType) : isStringLiteralType(type)); + } function contextualTypeIsTupleLikeType(type) { return !!(type.flags & 16384 ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); } @@ -16486,7 +16702,7 @@ var ts; } function getContextualTypeForObjectLiteralElement(element) { var objectLiteral = element.parent; - var type = getContextualType(objectLiteral); + var type = getApparentTypeOfContextualType(objectLiteral); if (type) { if (!ts.hasDynamicName(element)) { var symbolName = getSymbolOfNode(element).name; @@ -16502,7 +16718,7 @@ var ts; } function getContextualTypeForElementExpression(node) { var arrayLiteral = node.parent; - var type = getContextualType(arrayLiteral); + var type = getApparentTypeOfContextualType(arrayLiteral); if (type) { var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) @@ -16531,11 +16747,11 @@ var ts; } return undefined; } - function getContextualType(node) { - var type = getContextualTypeWorker(node); + function getApparentTypeOfContextualType(node) { + var type = getContextualType(node); return type && getApparentType(type); } - function getContextualTypeWorker(node) { + function getContextualType(node) { if (isInsideWithStatementBody(node)) { return undefined; } @@ -16601,7 +16817,7 @@ var ts; ts.Debug.assert(node.kind !== 143 || ts.isObjectLiteralMethod(node)); var type = ts.isObjectLiteralMethod(node) ? getContextualTypeForObjectLiteralMethod(node) - : getContextualType(node); + : getApparentTypeOfContextualType(node); if (!type) { return undefined; } @@ -16684,7 +16900,7 @@ var ts; type.pattern = node; return type; } - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { var pattern = contextualType.pattern; if (pattern && (pattern.kind === 162 || pattern.kind === 164)) { @@ -16739,10 +16955,11 @@ var ts; checkGrammarObjectLiteralExpression(node, inDestructuringPattern); var propertiesTable = {}; var propertiesArray = []; - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); var contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === 161 || contextualType.pattern.kind === 165); var typeFlags = 0; + var patternWithComputedProperties = false; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var memberDecl = _a[_i]; var member = memberDecl.symbol; @@ -16768,8 +16985,11 @@ var ts; if (isOptional) { prop.flags |= 536870912; } + if (ts.hasDynamicName(memberDecl)) { + patternWithComputedProperties = true; + } } - else if (contextualTypeHasPattern) { + else if (contextualTypeHasPattern && !(contextualType.flags & 67108864)) { var impliedProp = getPropertyOfType(contextualType, member.name); if (impliedProp) { prop.flags |= impliedProp.flags & 536870912; @@ -16812,7 +17032,7 @@ var ts; var numberIndexType = getIndexType(1); var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexType, numberIndexType); var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 1048576; - result.flags |= 524288 | 4194304 | freshObjectLiteralFlag | (typeFlags & 14680064); + result.flags |= 524288 | 4194304 | freshObjectLiteralFlag | (typeFlags & 14680064) | (patternWithComputedProperties ? 67108864 : 0); if (inDestructuringPattern) { result.pattern = node; } @@ -18027,6 +18247,9 @@ var ts; return anyType; } } + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node)) { + return resolveExternalModuleTypeByLiteral(node.arguments[0]); + } return getReturnTypeOfSignature(signature); } function checkTaggedTemplateExpression(node) { @@ -18037,7 +18260,9 @@ var ts; var targetType = getTypeFromTypeNode(node.type); if (produceDiagnostics && targetType !== unknownType) { var widenedType = getWidenedType(exprType); - if (!(isTypeAssignableTo(targetType, widenedType))) { + var bothAreStringLike = someConstituentTypeHasKind(targetType, 258) && + someConstituentTypeHasKind(widenedType, 258); + if (!bothAreStringLike && !(isTypeAssignableTo(targetType, widenedType))) { checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } @@ -18476,17 +18701,24 @@ var ts; var p = properties_3[_i]; if (p.kind === 245 || p.kind === 246) { var name_13 = p.name; + if (name_13.kind === 136) { + checkComputedPropertyName(name_13); + } + if (isComputedNonLiteralName(name_13)) { + continue; + } + var text = getTextOfPropertyName(name_13); var type = isTypeAny(sourceType) ? sourceType - : getTypeOfPropertyOfType(sourceType, name_13.text) || - isNumericLiteralName(name_13.text) && getIndexTypeOfType(sourceType, 1) || + : getTypeOfPropertyOfType(sourceType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(sourceType, 1) || getIndexTypeOfType(sourceType, 0); if (type) { if (p.kind === 246) { checkDestructuringAssignment(p, type); } else { - checkDestructuringAssignment(p.initializer || name_13, type); + checkDestructuringAssignment(p.initializer, type); } } else { @@ -18664,6 +18896,9 @@ var ts; case 31: case 32: case 33: + if (someConstituentTypeHasKind(leftType, 258) && someConstituentTypeHasKind(rightType, 258)) { + return booleanType; + } if (!isTypeAssignableTo(leftType, rightType) && !isTypeAssignableTo(rightType, leftType)) { reportOperatorError(); } @@ -18771,6 +19006,13 @@ var ts; var type2 = checkExpression(node.whenFalse, contextualMapper); return getUnionType([type1, type2]); } + function checkStringLiteralExpression(node) { + var contextualType = getContextualType(node); + if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { + return getStringLiteralType(node); + } + return stringType; + } function checkTemplateExpression(node) { ts.forEach(node.templateSpans, function (templateSpan) { checkExpression(templateSpan.expression); @@ -18809,7 +19051,7 @@ var ts; if (isInferentialContext(contextualMapper)) { var signature = getSingleCallSignature(type); if (signature && signature.typeParameters) { - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType) { var contextualSignature = getSingleCallSignature(contextualType); if (contextualSignature && !contextualSignature.typeParameters) { @@ -18861,6 +19103,7 @@ var ts; case 183: return checkTemplateExpression(node); case 9: + return checkStringLiteralExpression(node); case 11: return stringType; case 10: @@ -19922,7 +20165,7 @@ var ts; return; } var parent = getDeclarationContainer(node); - if (parent.kind === 248 && ts.isExternalModule(parent)) { + if (parent.kind === 248 && ts.isExternalOrCommonJsModule(parent)) { error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); } } @@ -19993,6 +20236,11 @@ var ts; checkExpressionCached(node.initializer); } } + if (node.kind === 163) { + if (node.propertyName && node.propertyName.kind === 136) { + checkComputedPropertyName(node.propertyName); + } + } if (ts.isBindingPattern(node.name)) { ts.forEach(node.name.elements, checkSourceElement); } @@ -20351,6 +20599,7 @@ var ts; var firstDefaultClause; var hasDuplicateDefaultClause = false; var expressionType = checkExpression(node.expression); + var expressionTypeIsStringLike = someConstituentTypeHasKind(expressionType, 258); ts.forEach(node.caseBlock.clauses, function (clause) { if (clause.kind === 242 && !hasDuplicateDefaultClause) { if (firstDefaultClause === undefined) { @@ -20367,6 +20616,9 @@ var ts; if (produceDiagnostics && clause.kind === 241) { var caseClause = clause; var caseType = checkExpression(caseClause.expression); + if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, 258)) { + return; + } if (!isTypeAssignableTo(expressionType, caseType)) { checkTypeAssignableTo(caseType, expressionType, caseClause.expression, undefined); } @@ -20774,11 +21026,14 @@ var ts; var enumIsConst = ts.isConst(node); for (var _i = 0, _a = node.members; _i < _a.length; _i++) { var member = _a[_i]; - if (member.name.kind === 136) { + if (isComputedNonLiteralName(member.name)) { error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); } - else if (isNumericLiteralName(member.name.text)) { - error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + else { + var text = getTextOfPropertyName(member.name); + if (isNumericLiteralName(text)) { + error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + } } var previousEnumMemberIsNonConstant = autoValue === undefined; var initializer = member.initializer; @@ -21476,8 +21731,10 @@ var ts; function checkSourceFileWorker(node) { var links = getNodeLinks(node); if (!(links.flags & 1)) { - if (node.isDefaultLib && compilerOptions.skipDefaultLibCheck) { - return; + if (compilerOptions.skipDefaultLibCheck) { + if (node.hasNoDefaultLib) { + return; + } } checkGrammarSourceFile(node); emitExtends = false; @@ -21486,7 +21743,7 @@ var ts; potentialThisCollisions.length = 0; ts.forEach(node.statements, checkSourceElement); checkFunctionAndClassExpressionBodies(node); - if (ts.isExternalModule(node)) { + if (ts.isExternalOrCommonJsModule(node)) { checkExternalModuleExports(node); } if (potentialThisCollisions.length) { @@ -21564,7 +21821,7 @@ var ts; } switch (location.kind) { case 248: - if (!ts.isExternalModule(location)) { + if (!ts.isExternalOrCommonJsModule(location)) { break; } case 218: @@ -22123,15 +22380,24 @@ var ts; getReferencedValueDeclaration: getReferencedValueDeclaration, getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, isOptionalParameter: isOptionalParameter, - isArgumentsLocalBinding: isArgumentsLocalBinding + isArgumentsLocalBinding: isArgumentsLocalBinding, + getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration }; } + function getExternalModuleFileFromDeclaration(declaration) { + var specifier = ts.getExternalModuleName(declaration); + var moduleSymbol = getSymbolAtLocation(specifier); + if (!moduleSymbol) { + return undefined; + } + return ts.getDeclarationOfKind(moduleSymbol, 248); + } function initializeTypeChecker() { ts.forEach(host.getSourceFiles(), function (file) { ts.bindSourceFile(file, compilerOptions); }); ts.forEach(host.getSourceFiles(), function (file) { - if (!ts.isExternalModule(file)) { + if (!ts.isExternalOrCommonJsModule(file)) { mergeSymbolTable(globals, file.locals); } }); @@ -22808,7 +23074,7 @@ var ts; } } function checkGrammarForNonSymbolComputedProperty(node, message) { - if (node.kind === 136 && !ts.isWellKnownSymbolSyntactically(node.expression)) { + if (ts.isDynamicName(node)) { return grammarErrorOnNode(node, message); } } @@ -23122,11 +23388,15 @@ var ts; var writeTextOfNode; var writer = createAndSetNewTextWriterWithSymbolWriter(); var enclosingDeclaration; - var currentSourceFile; + var currentText; + var currentLineMap; + var currentIdentifiers; + var isCurrentFileExternalModule; var reportedDeclarationError = false; var errorNameNode; var emitJsDocComments = compilerOptions.removeComments ? function (declaration) { } : writeJsDocComments; var emit = compilerOptions.stripInternal ? stripInternal : emitNode; + var noDeclare = !root; var moduleElementDeclarationEmitInfo = []; var asynchronousSubModuleDeclarationEmitInfo; var referencePathsOutput = ""; @@ -23162,21 +23432,53 @@ var ts; } else { var emittedReferencedFiles = []; + var prevModuleElementDeclarationEmitInfo = []; ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + if (!ts.isDeclarationFile(sourceFile)) { if (!compilerOptions.noResolve) { ts.forEach(sourceFile.referencedFiles, function (fileReference) { var referencedFile = ts.tryResolveScriptReference(host, sourceFile, fileReference); - if (referencedFile && (ts.isExternalModuleOrDeclarationFile(referencedFile) && + if (referencedFile && (ts.isDeclarationFile(referencedFile) && !ts.contains(emittedReferencedFiles, referencedFile))) { writeReferencePath(referencedFile); emittedReferencedFiles.push(referencedFile); } }); } + } + if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + noDeclare = false; emitSourceFile(sourceFile); } + else if (ts.isExternalModule(sourceFile)) { + noDeclare = true; + write("declare module \"" + ts.getResolvedExternalModuleName(host, sourceFile) + "\" {"); + writeLine(); + increaseIndent(); + emitSourceFile(sourceFile); + decreaseIndent(); + write("}"); + writeLine(); + if (moduleElementDeclarationEmitInfo.length) { + var oldWriter = writer; + ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { + if (aliasEmitInfo.isVisible && !aliasEmitInfo.asynchronousOutput) { + ts.Debug.assert(aliasEmitInfo.node.kind === 222); + createAndSetNewTextWriterWithSymbolWriter(); + ts.Debug.assert(aliasEmitInfo.indent === 1); + increaseIndent(); + writeImportDeclaration(aliasEmitInfo.node); + aliasEmitInfo.asynchronousOutput = writer.getText(); + decreaseIndent(); + } + }); + setWriter(oldWriter); + } + prevModuleElementDeclarationEmitInfo = prevModuleElementDeclarationEmitInfo.concat(moduleElementDeclarationEmitInfo); + moduleElementDeclarationEmitInfo = []; + } }); + moduleElementDeclarationEmitInfo = moduleElementDeclarationEmitInfo.concat(prevModuleElementDeclarationEmitInfo); } return { reportedDeclarationError: reportedDeclarationError, @@ -23185,13 +23487,12 @@ var ts; referencePathsOutput: referencePathsOutput }; function hasInternalAnnotation(range) { - var text = currentSourceFile.text; - var comment = text.substring(range.pos, range.end); + var comment = currentText.substring(range.pos, range.end); return comment.indexOf("@internal") >= 0; } function stripInternal(node) { if (node) { - var leadingCommentRanges = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + var leadingCommentRanges = ts.getLeadingCommentRanges(currentText, node.pos); if (ts.forEach(leadingCommentRanges, hasInternalAnnotation)) { return; } @@ -23272,7 +23573,7 @@ var ts; var errorInfo = writer.getSymbolAccessibilityDiagnostic(symbolAccesibilityResult); if (errorInfo) { if (errorInfo.typeName) { - diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); + diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getTextOfNodeFromSourceText(currentText, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); } else { diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); @@ -23336,9 +23637,9 @@ var ts; } function writeJsDocComments(declaration) { if (declaration) { - var jsDocComments = ts.getJsDocComments(declaration, currentSourceFile); - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, declaration, jsDocComments); - ts.emitComments(currentSourceFile, writer, jsDocComments, true, newLine, ts.writeCommentRange); + var jsDocComments = ts.getJsDocCommentsFromText(declaration, currentText); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments); + ts.emitComments(currentText, currentLineMap, writer, jsDocComments, true, newLine, ts.writeCommentRange); } } function emitTypeWithNewGetSymbolAccessibilityDiagnostic(type, getSymbolAccessibilityDiagnostic) { @@ -23355,7 +23656,7 @@ var ts; case 103: case 97: case 9: - return writeTextOfNode(currentSourceFile, type); + return writeTextOfNode(currentText, type); case 188: return emitExpressionWithTypeArguments(type); case 151: @@ -23386,14 +23687,14 @@ var ts; } function writeEntityName(entityName) { if (entityName.kind === 69) { - writeTextOfNode(currentSourceFile, entityName); + writeTextOfNode(currentText, entityName); } else { var left = entityName.kind === 135 ? entityName.left : entityName.expression; var right = entityName.kind === 135 ? entityName.right : entityName.name; writeEntityName(left); write("."); - writeTextOfNode(currentSourceFile, right); + writeTextOfNode(currentText, right); } } function emitEntityName(entityName) { @@ -23421,7 +23722,7 @@ var ts; } } function emitTypePredicate(type) { - writeTextOfNode(currentSourceFile, type.parameterName); + writeTextOfNode(currentText, type.parameterName); write(" is "); emitType(type.type); } @@ -23461,20 +23762,23 @@ var ts; } } function emitSourceFile(node) { - currentSourceFile = node; + currentText = node.text; + currentLineMap = ts.getLineStarts(node); + currentIdentifiers = node.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(node); enclosingDeclaration = node; - ts.emitDetachedComments(currentSourceFile, writer, ts.writeCommentRange, node, newLine, true); + ts.emitDetachedComments(currentText, currentLineMap, writer, ts.writeCommentRange, node, newLine, true); emitLines(node.statements); } function getExportDefaultTempVariableName() { var baseName = "_default"; - if (!ts.hasProperty(currentSourceFile.identifiers, baseName)) { + if (!ts.hasProperty(currentIdentifiers, baseName)) { return baseName; } var count = 0; while (true) { var name_18 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_18)) { + if (!ts.hasProperty(currentIdentifiers, name_18)) { return name_18; } } @@ -23482,7 +23786,7 @@ var ts; function emitExportAssignment(node) { if (node.expression.kind === 69) { write(node.isExportEquals ? "export = " : "export default "); - writeTextOfNode(currentSourceFile, node.expression); + writeTextOfNode(currentText, node.expression); } else { var tempVarName = getExportDefaultTempVariableName(); @@ -23517,7 +23821,7 @@ var ts; writeModuleElement(node); } else if (node.kind === 221 || - (node.parent.kind === 248 && ts.isExternalModule(currentSourceFile))) { + (node.parent.kind === 248 && isCurrentFileExternalModule)) { var isVisible; if (asynchronousSubModuleDeclarationEmitInfo && node.parent.kind !== 248) { asynchronousSubModuleDeclarationEmitInfo.push({ @@ -23569,14 +23873,14 @@ var ts; } } function emitModuleElementDeclarationFlags(node) { - if (node.parent === currentSourceFile) { + if (node.parent.kind === 248) { if (node.flags & 2) { write("export "); } if (node.flags & 512) { write("default "); } - else if (node.kind !== 215) { + else if (node.kind !== 215 && !noDeclare) { write("declare "); } } @@ -23601,7 +23905,7 @@ var ts; write("export "); } write("import "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" = "); if (ts.isInternalModuleImportEqualsDeclaration(node)) { emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.moduleReference, getImportEntityNameVisibilityError); @@ -23609,7 +23913,7 @@ var ts; } else { write("require("); - writeTextOfNode(currentSourceFile, ts.getExternalModuleImportEqualsDeclarationExpression(node)); + writeTextOfNode(currentText, ts.getExternalModuleImportEqualsDeclarationExpression(node)); write(");"); } writer.writeLine(); @@ -23643,7 +23947,7 @@ var ts; if (node.importClause) { var currentWriterPos = writer.getTextPos(); if (node.importClause.name && resolver.isDeclarationVisible(node.importClause)) { - writeTextOfNode(currentSourceFile, node.importClause.name); + writeTextOfNode(currentText, node.importClause.name); } if (node.importClause.namedBindings && isVisibleNamedBinding(node.importClause.namedBindings)) { if (currentWriterPos !== writer.getTextPos()) { @@ -23651,7 +23955,7 @@ var ts; } if (node.importClause.namedBindings.kind === 224) { write("* as "); - writeTextOfNode(currentSourceFile, node.importClause.namedBindings.name); + writeTextOfNode(currentText, node.importClause.namedBindings.name); } else { write("{ "); @@ -23661,16 +23965,28 @@ var ts; } write(" from "); } - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); write(";"); writer.writeLine(); } + function emitExternalModuleSpecifier(moduleSpecifier) { + if (moduleSpecifier.kind === 9 && (!root) && (compilerOptions.out || compilerOptions.outFile)) { + var moduleName = ts.getExternalModuleNameFromDeclaration(host, resolver, moduleSpecifier.parent); + if (moduleName) { + write("\""); + write(moduleName); + write("\""); + return; + } + } + writeTextOfNode(currentText, moduleSpecifier); + } function emitImportOrExportSpecifier(node) { if (node.propertyName) { - writeTextOfNode(currentSourceFile, node.propertyName); + writeTextOfNode(currentText, node.propertyName); write(" as "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } function emitExportSpecifier(node) { emitImportOrExportSpecifier(node); @@ -23690,7 +24006,7 @@ var ts; } if (node.moduleSpecifier) { write(" from "); - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); } write(";"); writer.writeLine(); @@ -23704,11 +24020,11 @@ var ts; else { write("module "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); while (node.body.kind !== 219) { node = node.body; write("."); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; @@ -23727,7 +24043,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("type "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); emitTypeParameters(node.typeParameters); write(" = "); emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.type, getTypeAliasDeclarationVisibilityError); @@ -23749,7 +24065,7 @@ var ts; write("const "); } write("enum "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" {"); writeLine(); increaseIndent(); @@ -23760,7 +24076,7 @@ var ts; } function emitEnumMemberDeclaration(node) { emitJsDocComments(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var enumMemberValue = resolver.getConstantValue(node); if (enumMemberValue !== undefined) { write(" = "); @@ -23777,7 +24093,7 @@ var ts; increaseIndent(); emitJsDocComments(node); decreaseIndent(); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (node.constraint && !isPrivateMethodTypeParameter(node)) { write(" extends "); if (node.parent.kind === 152 || @@ -23887,7 +24203,7 @@ var ts; write("abstract "); } write("class "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -23910,7 +24226,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("interface "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -23940,7 +24256,7 @@ var ts; emitBindingPattern(node.name); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if ((node.kind === 141 || node.kind === 140) && ts.hasQuestionToken(node)) { write("?"); } @@ -24014,7 +24330,7 @@ var ts; emitBindingPattern(bindingElement.name); } else { - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); writeTypeOfDeclaration(bindingElement, undefined, getBindingElementTypeVisibilityError); } } @@ -24055,7 +24371,7 @@ var ts; emitJsDocComments(accessors.getAccessor); emitJsDocComments(accessors.setAccessor); emitClassMemberDeclarationFlags(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (!(node.flags & 16)) { accessorWithTypeAnnotation = node; var type = getTypeAnnotationFromAccessor(node); @@ -24136,13 +24452,13 @@ var ts; } if (node.kind === 213) { write("function "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } else if (node.kind === 144) { write("constructor"); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (ts.hasQuestionToken(node)) { write("?"); } @@ -24255,7 +24571,7 @@ var ts; emitBindingPattern(node.name); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } if (resolver.isOptionalParameter(node)) { write("?"); @@ -24354,7 +24670,7 @@ var ts; } else if (bindingElement.kind === 163) { if (bindingElement.propertyName) { - writeTextOfNode(currentSourceFile, bindingElement.propertyName); + writeTextOfNode(currentText, bindingElement.propertyName); write(": "); } if (bindingElement.name) { @@ -24366,7 +24682,7 @@ var ts; if (bindingElement.dotDotDotToken) { write("..."); } - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); } } } @@ -24449,6 +24765,18 @@ var ts; return ts.isExternalModule(sourceFile) || ts.isDeclarationFile(sourceFile); } ts.isExternalModuleOrDeclarationFile = isExternalModuleOrDeclarationFile; + function getResolvedExternalModuleName(host, file) { + return file.moduleName || ts.getExternalModuleNameFromPath(host, file.fileName); + } + ts.getResolvedExternalModuleName = getResolvedExternalModuleName; + function getExternalModuleNameFromDeclaration(host, resolver, declaration) { + var file = resolver.getExternalModuleFileFromDeclaration(declaration); + if (!file || ts.isDeclarationFile(file)) { + return undefined; + } + return getResolvedExternalModuleName(host, file); + } + ts.getExternalModuleNameFromDeclaration = getExternalModuleNameFromDeclaration; var entities = { "quot": 0x0022, "amp": 0x0026, @@ -24718,15 +25046,19 @@ var ts; var newLine = host.getNewLine(); var jsxDesugaring = host.getCompilerOptions().jsx !== 1; var shouldEmitJsx = function (s) { return (s.languageVariant === 1 && !jsxDesugaring); }; + var outFile = compilerOptions.outFile || compilerOptions.out; + var emitJavaScript = createFileEmitter(); if (targetSourceFile === undefined) { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { - var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); - emitFile(jsFilePath, sourceFile); - } - }); - if (compilerOptions.outFile || compilerOptions.out) { - emitFile(compilerOptions.outFile || compilerOptions.out); + if (outFile) { + emitFile(outFile); + } + else { + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { + var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); + emitFile(jsFilePath, sourceFile); + } + }); } } else { @@ -24734,8 +25066,8 @@ var ts; var jsFilePath = ts.getOwnEmitOutputFilePath(targetSourceFile, host, shouldEmitJsx(targetSourceFile) ? ".jsx" : ".js"); emitFile(jsFilePath, targetSourceFile); } - else if (!ts.isDeclarationFile(targetSourceFile) && (compilerOptions.outFile || compilerOptions.out)) { - emitFile(compilerOptions.outFile || compilerOptions.out); + else if (!ts.isDeclarationFile(targetSourceFile) && outFile) { + emitFile(outFile); } } diagnostics = ts.sortAndDeduplicateDiagnostics(diagnostics); @@ -24785,20 +25117,26 @@ var ts; } } } - function emitJavaScript(jsFilePath, root) { + function createFileEmitter() { var writer = ts.createTextWriter(newLine); var write = writer.write, writeTextOfNode = writer.writeTextOfNode, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; var currentSourceFile; + var currentText; + var currentLineMap; + var currentFileIdentifiers; + var renamedDependencies; + var isEs6Module; + var isCurrentFileExternalModule; var exportFunctionForFile; - var generatedNameSet = {}; - var nodeToGeneratedName = []; + var generatedNameSet; + var nodeToGeneratedName; var computedPropertyNamesToGeneratedNames; var convertedLoopState; - var extendsEmitted = false; - var decorateEmitted = false; - var paramEmitted = false; - var awaiterEmitted = false; - var tempFlags = 0; + var extendsEmitted; + var decorateEmitted; + var paramEmitted; + var awaiterEmitted; + var tempFlags; var tempVariables; var tempParameters; var externalImports; @@ -24815,6 +25153,7 @@ var ts; var scopeEmitStart = function (scopeDeclaration, scopeName) { }; var scopeEmitEnd = function () { }; var sourceMapData; + var root; var emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos) { } : emitLeadingCommentsOfPositionWorker; var moduleEmitDelegates = (_a = {}, _a[5] = emitES6Module, @@ -24824,30 +25163,75 @@ var ts; _a[1] = emitCommonJSModule, _a ); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - initializeEmitterWithSourceMaps(); - } - if (root) { - emitSourceFile(root); - } - else { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!isExternalModuleOrDeclarationFile(sourceFile)) { - emitSourceFile(sourceFile); + var bundleEmitDelegates = (_b = {}, + _b[5] = function () { }, + _b[2] = emitAMDModule, + _b[4] = emitSystemModule, + _b[3] = function () { }, + _b[1] = function () { }, + _b + ); + return doEmit; + function doEmit(jsFilePath, rootFile) { + writer.reset(); + currentSourceFile = undefined; + currentText = undefined; + currentLineMap = undefined; + exportFunctionForFile = undefined; + generatedNameSet = {}; + nodeToGeneratedName = []; + computedPropertyNamesToGeneratedNames = undefined; + convertedLoopState = undefined; + extendsEmitted = false; + decorateEmitted = false; + paramEmitted = false; + awaiterEmitted = false; + tempFlags = 0; + tempVariables = undefined; + tempParameters = undefined; + externalImports = undefined; + exportSpecifiers = undefined; + exportEquals = undefined; + hasExportStars = undefined; + detachedCommentsInfo = undefined; + sourceMapData = undefined; + isEs6Module = false; + renamedDependencies = undefined; + isCurrentFileExternalModule = false; + root = rootFile; + if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { + initializeEmitterWithSourceMaps(jsFilePath, root); + } + if (root) { + emitSourceFile(root); + } + else { + if (modulekind) { + ts.forEach(host.getSourceFiles(), emitEmitHelpers); } - }); + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if ((!isExternalModuleOrDeclarationFile(sourceFile)) || (modulekind && ts.isExternalModule(sourceFile))) { + emitSourceFile(sourceFile); + } + }); + } + writeLine(); + writeEmittedFiles(writer.getText(), jsFilePath, compilerOptions.emitBOM); } - writeLine(); - writeEmittedFiles(writer.getText(), compilerOptions.emitBOM); - return; function emitSourceFile(sourceFile) { currentSourceFile = sourceFile; + currentText = sourceFile.text; + currentLineMap = ts.getLineStarts(sourceFile); exportFunctionForFile = undefined; + isEs6Module = sourceFile.symbol && sourceFile.symbol.exports && !!sourceFile.symbol.exports["___esModule"]; + renamedDependencies = sourceFile.renamedDependencies; + currentFileIdentifiers = sourceFile.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(sourceFile); emit(sourceFile); } function isUniqueName(name) { return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentSourceFile.identifiers, name) && + !ts.hasProperty(currentFileIdentifiers, name) && !ts.hasProperty(generatedNameSet, name); } function makeTempVariableName(flags) { @@ -24920,7 +25304,7 @@ var ts; var id = ts.getNodeId(node); return nodeToGeneratedName[id] || (nodeToGeneratedName[id] = ts.unescapeIdentifier(generateNameForNode(node))); } - function initializeEmitterWithSourceMaps() { + function initializeEmitterWithSourceMaps(jsFilePath, root) { var sourceMapDir; var sourceMapSourceIndex = -1; var sourceMapNameIndexMap = {}; @@ -24989,7 +25373,7 @@ var ts; } } function recordSourceMapSpan(pos) { - var sourceLinePos = ts.getLineAndCharacterOfPosition(currentSourceFile, pos); + var sourceLinePos = ts.computeLineAndCharacterOfPosition(currentLineMap, pos); sourceLinePos.line++; sourceLinePos.character++; var emittedLine = writer.getLine(); @@ -25017,13 +25401,13 @@ var ts; } } function recordEmitNodeStartSpan(node) { - recordSourceMapSpan(ts.skipTrivia(currentSourceFile.text, node.pos)); + recordSourceMapSpan(ts.skipTrivia(currentText, node.pos)); } function recordEmitNodeEndSpan(node) { recordSourceMapSpan(node.end); } function writeTextWithSpanRecord(tokenKind, startPos, emitFn) { - var tokenStartPos = ts.skipTrivia(currentSourceFile.text, startPos); + var tokenStartPos = ts.skipTrivia(currentText, startPos); recordSourceMapSpan(tokenStartPos); var tokenEndPos = emitTokenText(tokenKind, tokenStartPos, emitFn); recordSourceMapSpan(tokenEndPos); @@ -25093,9 +25477,9 @@ var ts; sourceMapNameIndices.pop(); } ; - function writeCommentRangeWithMap(curentSourceFile, writer, comment, newLine) { + function writeCommentRangeWithMap(currentText, currentLineMap, writer, comment, newLine) { recordSourceMapSpan(comment.pos); - ts.writeCommentRange(currentSourceFile, writer, comment, newLine); + ts.writeCommentRange(currentText, currentLineMap, writer, comment, newLine); recordSourceMapSpan(comment.end); } function serializeSourceMapContents(version, file, sourceRoot, sources, names, mappings, sourcesContent) { @@ -25125,7 +25509,7 @@ var ts; return output; } } - function writeJavaScriptAndSourceMapFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptAndSourceMapFile(emitOutput, jsFilePath, writeByteOrderMark) { encodeLastRecordedSourceMapSpan(); var sourceMapText = serializeSourceMapContents(3, sourceMapData.sourceMapFile, sourceMapData.sourceMapSourceRoot, sourceMapData.sourceMapSources, sourceMapData.sourceMapNames, sourceMapData.sourceMapMappings, sourceMapData.sourceMapSourcesContent); sourceMapDataList.push(sourceMapData); @@ -25138,7 +25522,7 @@ var ts; ts.writeFile(host, diagnostics, sourceMapData.sourceMapFilePath, sourceMapText, false); sourceMapUrl = "//# sourceMappingURL=" + sourceMapData.jsSourceMappingURL; } - writeJavaScriptFile(emitOutput + sourceMapUrl, writeByteOrderMark); + writeJavaScriptFile(emitOutput + sourceMapUrl, jsFilePath, writeByteOrderMark); } var sourceMapJsFile = ts.getBaseFileName(ts.normalizeSlashes(jsFilePath)); sourceMapData = { @@ -25201,7 +25585,7 @@ var ts; scopeEmitEnd = recordScopeNameEnd; writeComment = writeCommentRangeWithMap; } - function writeJavaScriptFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptFile(emitOutput, jsFilePath, writeByteOrderMark) { ts.writeFile(host, diagnostics, jsFilePath, emitOutput, writeByteOrderMark); } function createTempVariable(flags) { @@ -25371,7 +25755,7 @@ var ts; return getQuotedEscapedLiteralText("\"", node.text, "\""); } if (node.parent) { - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + return ts.getTextOfNodeFromSourceText(currentText, node); } switch (node.kind) { case 9: @@ -25393,7 +25777,7 @@ var ts; return leftQuote + ts.escapeNonAsciiCharacters(ts.escapeString(text)) + rightQuote; } function emitDownlevelRawTemplateLiteral(node) { - var text = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + var text = ts.getTextOfNodeFromSourceText(currentText, node); var isLast = node.kind === 11 || node.kind === 14; text = text.substring(1, text.length - (isLast ? 1 : 2)); text = text.replace(/\r\n?/g, "\n"); @@ -25723,7 +26107,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } write("\""); } @@ -25819,7 +26203,7 @@ var ts; else if (declaration.kind === 226) { write(getGeneratedNameForNode(declaration.parent.parent.parent)); var name_23 = declaration.propertyName || declaration.name; - var identifier = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, name_23); + var identifier = ts.getTextOfNodeFromSourceText(currentText, name_23); if (languageVersion === 0 && identifier === "default") { write("[\"default\"]"); } @@ -25843,7 +26227,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function isNameOfNestedRedeclaration(node) { @@ -25880,7 +26264,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function emitThis(node) { @@ -26247,8 +26631,8 @@ var ts; return container && container.kind !== 248; } function emitShorthandPropertyAssignment(node) { - writeTextOfNode(currentSourceFile, node.name); - if (languageVersion < 2 || isNamespaceExportReference(node.name)) { + writeTextOfNode(currentText, node.name); + if (modulekind !== 5 || isNamespaceExportReference(node.name)) { write(": "); emit(node.name); } @@ -26298,10 +26682,10 @@ var ts; } emit(node.expression); var indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken); - var shouldEmitSpace; + var shouldEmitSpace = false; if (!indentedBeforeDot) { if (node.expression.kind === 8) { - var text = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node.expression); + var text = ts.getTextOfNodeFromSourceText(currentText, node.expression); shouldEmitSpace = text.indexOf(ts.tokenToString(21)) < 0; } else { @@ -27307,16 +27691,16 @@ var ts; emitToken(16, node.clauses.end); } function nodeStartPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node1.pos)) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node1.pos)) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function nodeEndPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, node2.end); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, node2.end); } function nodeEndIsOnSameLineAsNodeStart(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function emitCaseOrDefaultClause(node) { if (node.kind === 241) { @@ -27421,7 +27805,7 @@ var ts; if (node.parent.kind === 248) { ts.Debug.assert(!!(node.flags & 512) || node.kind === 227); if (modulekind === 1 || modulekind === 2 || modulekind === 3) { - if (!currentSourceFile.symbol.exports["___esModule"]) { + if (!isEs6Module) { if (languageVersion === 1) { write("Object.defineProperty(exports, \"__esModule\", { value: true });"); writeLine(); @@ -27584,12 +27968,18 @@ var ts; return node; } function createPropertyAccessForDestructuringProperty(object, propName) { - var syntheticName = ts.createSynthesizedNode(propName.kind); - syntheticName.text = propName.text; - if (syntheticName.kind !== 69) { - return createElementAccessExpression(object, syntheticName); + var index; + var nameIsComputed = propName.kind === 136; + if (nameIsComputed) { + index = ensureIdentifier(propName.expression, false); } - return createPropertyAccessExpression(object, syntheticName); + else { + index = ts.createSynthesizedNode(propName.kind); + index.text = propName.text; + } + return !nameIsComputed && index.kind === 69 + ? createPropertyAccessExpression(object, index) + : createElementAccessExpression(object, index); } function createSliceCall(value, sliceIndex) { var call = ts.createSynthesizedNode(168); @@ -28003,7 +28393,6 @@ var ts; var promiseConstructor = ts.getEntityNameFromTypeNode(node.type); var isArrowFunction = node.kind === 174; var hasLexicalArguments = (resolver.getNodeCheckFlags(node) & 4096) !== 0; - var args; if (!isArrowFunction) { write(" {"); increaseIndent(); @@ -29177,8 +29566,8 @@ var ts; } } function tryRenameExternalModule(moduleName) { - if (currentSourceFile.renamedDependencies && ts.hasProperty(currentSourceFile.renamedDependencies, moduleName.text)) { - return "\"" + currentSourceFile.renamedDependencies[moduleName.text] + "\""; + if (renamedDependencies && ts.hasProperty(renamedDependencies, moduleName.text)) { + return "\"" + renamedDependencies[moduleName.text] + "\""; } return undefined; } @@ -29318,7 +29707,7 @@ var ts; return; } if (resolver.isReferencedAliasDeclaration(node) || - (!ts.isExternalModule(currentSourceFile) && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { + (!isCurrentFileExternalModule && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { emitLeadingComments(node); emitStart(node); var variableDeclarationIsHoisted = shouldHoistVariable(node, true); @@ -29530,7 +29919,7 @@ var ts; function getLocalNameForExternalImport(node) { var namespaceDeclaration = getNamespaceDeclarationNode(node); if (namespaceDeclaration && !isDefaultImport(node)) { - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, namespaceDeclaration.name); + return ts.getTextOfNodeFromSourceText(currentText, namespaceDeclaration.name); } if (node.kind === 222 && node.importClause) { return getGeneratedNameForNode(node); @@ -29803,7 +30192,7 @@ var ts; ts.getEnclosingBlockScopeContainer(node).kind === 248; } function isCurrentFileSystemExternalModule() { - return modulekind === 4 && ts.isExternalModule(currentSourceFile); + return modulekind === 4 && isCurrentFileExternalModule; } function emitSystemModuleBody(node, dependencyGroups, startIndex) { emitVariableDeclarationsForImports(); @@ -29916,15 +30305,19 @@ var ts; writeLine(); write("}"); } - function emitSystemModule(node) { + function writeModuleName(node, emitRelativePathAsModuleName) { + var moduleName = node.moduleName; + if (moduleName || (emitRelativePathAsModuleName && (moduleName = getResolvedExternalModuleName(host, node)))) { + write("\"" + moduleName + "\", "); + } + } + function emitSystemModule(node, emitRelativePathAsModuleName) { collectExternalModuleInfo(node); ts.Debug.assert(!exportFunctionForFile); exportFunctionForFile = makeUniqueName("exports"); writeLine(); write("System.register("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } + writeModuleName(node, emitRelativePathAsModuleName); write("["); var groupIndices = {}; var dependencyGroups = []; @@ -29942,6 +30335,12 @@ var ts; if (i !== 0) { write(", "); } + if (emitRelativePathAsModuleName) { + var name_29 = getExternalModuleNameFromDeclaration(host, resolver, externalImports[i]); + if (name_29) { + text = "\"" + name_29 + "\""; + } + } write(text); } write("], function(" + exportFunctionForFile + ") {"); @@ -29955,7 +30354,7 @@ var ts; writeLine(); write("});"); } - function getAMDDependencyNames(node, includeNonAmdDependencies) { + function getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { var aliasedModuleNames = []; var unaliasedModuleNames = []; var importAliasNames = []; @@ -29972,6 +30371,12 @@ var ts; for (var _c = 0, externalImports_4 = externalImports; _c < externalImports_4.length; _c++) { var importNode = externalImports_4[_c]; var externalModuleName = getExternalModuleNameText(importNode); + if (emitRelativePathAsModuleName) { + var name_30 = getExternalModuleNameFromDeclaration(host, resolver, importNode); + if (name_30) { + externalModuleName = "\"" + name_30 + "\""; + } + } var importAliasName = getLocalNameForExternalImport(importNode); if (includeNonAmdDependencies && importAliasName) { aliasedModuleNames.push(externalModuleName); @@ -29983,8 +30388,8 @@ var ts; } return { aliasedModuleNames: aliasedModuleNames, unaliasedModuleNames: unaliasedModuleNames, importAliasNames: importAliasNames }; } - function emitAMDDependencies(node, includeNonAmdDependencies) { - var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies); + function emitAMDDependencies(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { + var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName); emitAMDDependencyList(dependencyNames); write(", "); emitAMDFactoryHeader(dependencyNames); @@ -30011,15 +30416,13 @@ var ts; } write(") {"); } - function emitAMDModule(node) { + function emitAMDModule(node, emitRelativePathAsModuleName) { emitEmitHelpers(node); collectExternalModuleInfo(node); writeLine(); write("define("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } - emitAMDDependencies(node, true); + writeModuleName(node, emitRelativePathAsModuleName); + emitAMDDependencies(node, true, emitRelativePathAsModuleName); increaseIndent(); var startIndex = emitDirectivePrologues(node.statements, true); emitExportStarHelper(); @@ -30225,8 +30628,13 @@ var ts; emitShebang(); emitDetachedCommentsAndUpdateCommentsInfo(node); if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { - var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1]; - emitModule(node); + if (root || (!ts.isExternalModule(node) && compilerOptions.isolatedModules)) { + var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1]; + emitModule(node); + } + else { + bundleEmitDelegates[modulekind](node, true); + } } else { var startIndex = emitDirectivePrologues(node.statements, false); @@ -30470,7 +30878,7 @@ var ts; return detachedCommentsInfo !== undefined && ts.lastOrUndefined(detachedCommentsInfo).nodePos === pos; } function getLeadingCommentsWithoutDetachedComments() { - var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); + var leadingComments = ts.getLeadingCommentRanges(currentText, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); if (detachedCommentsInfo.length - 1) { detachedCommentsInfo.pop(); } @@ -30480,10 +30888,10 @@ var ts; return leadingComments; } function isTripleSlashComment(comment) { - if (currentSourceFile.text.charCodeAt(comment.pos + 1) === 47 && + if (currentText.charCodeAt(comment.pos + 1) === 47 && comment.pos + 2 < comment.end && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 47) { - var textSubStr = currentSourceFile.text.substring(comment.pos, comment.end); + currentText.charCodeAt(comment.pos + 2) === 47) { + var textSubStr = currentText.substring(comment.pos, comment.end); return textSubStr.match(ts.fullTripleSlashReferencePathRegEx) || textSubStr.match(ts.fullTripleSlashAMDReferencePathRegEx) ? true : false; @@ -30497,7 +30905,7 @@ var ts; return getLeadingCommentsWithoutDetachedComments(); } else { - return ts.getLeadingCommentRangesOfNode(node, currentSourceFile); + return ts.getLeadingCommentRangesOfNodeFromText(node, currentText); } } } @@ -30505,7 +30913,7 @@ var ts; function getTrailingCommentsToEmit(node) { if (node.parent) { if (node.parent.kind === 248 || node.end !== node.parent.end) { - return ts.getTrailingCommentRanges(currentSourceFile.text, node.end); + return ts.getTrailingCommentRanges(currentText, node.end); } } } @@ -30528,22 +30936,22 @@ var ts; leadingComments = ts.filter(getLeadingCommentsToEmit(node), isTripleSlashComment); } } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); - ts.emitComments(currentSourceFile, writer, leadingComments, true, newLine, writeComment); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, node, leadingComments); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, true, newLine, writeComment); } function emitTrailingComments(node) { if (compilerOptions.removeComments) { return; } var trailingComments = getTrailingCommentsToEmit(node); - ts.emitComments(currentSourceFile, writer, trailingComments, false, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, false, newLine, writeComment); } function emitTrailingCommentsOfPosition(pos) { if (compilerOptions.removeComments) { return; } - var trailingComments = ts.getTrailingCommentRanges(currentSourceFile.text, pos); - ts.emitComments(currentSourceFile, writer, trailingComments, true, newLine, writeComment); + var trailingComments = ts.getTrailingCommentRanges(currentText, pos); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, true, newLine, writeComment); } function emitLeadingCommentsOfPositionWorker(pos) { if (compilerOptions.removeComments) { @@ -30554,13 +30962,13 @@ var ts; leadingComments = getLeadingCommentsWithoutDetachedComments(); } else { - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, pos); + leadingComments = ts.getLeadingCommentRanges(currentText, pos); } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, { pos: pos, end: pos }, leadingComments); - ts.emitComments(currentSourceFile, writer, leadingComments, true, newLine, writeComment); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, { pos: pos, end: pos }, leadingComments); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, true, newLine, writeComment); } function emitDetachedCommentsAndUpdateCommentsInfo(node) { - var currentDetachedCommentInfo = ts.emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, compilerOptions.removeComments); + var currentDetachedCommentInfo = ts.emitDetachedComments(currentText, currentLineMap, writer, writeComment, node, newLine, compilerOptions.removeComments); if (currentDetachedCommentInfo) { if (detachedCommentsInfo) { detachedCommentsInfo.push(currentDetachedCommentInfo); @@ -30571,12 +30979,12 @@ var ts; } } function emitShebang() { - var shebang = ts.getShebang(currentSourceFile.text); + var shebang = ts.getShebang(currentText); if (shebang) { write(shebang); } } - var _a; + var _a, _b; } function emitFile(jsFilePath, sourceFile) { emitJavaScript(jsFilePath, sourceFile); @@ -30632,11 +31040,11 @@ var ts; if (ts.getRootLength(moduleName) !== 0 || nameStartsWithDotSlashOrDotDotSlash(moduleName)) { var failedLookupLocations = []; var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var resolvedFileName = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var resolvedFileName = loadNodeModuleFromFile(ts.supportedJsExtensions, candidate, failedLookupLocations, host); if (resolvedFileName) { return { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations }; } - resolvedFileName = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + resolvedFileName = loadNodeModuleFromDirectory(ts.supportedJsExtensions, candidate, failedLookupLocations, host); return resolvedFileName ? { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations } : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; @@ -30646,8 +31054,8 @@ var ts; } } ts.nodeModuleNameResolver = nodeModuleNameResolver; - function loadNodeModuleFromFile(candidate, failedLookupLocation, host) { - return ts.forEach(ts.moduleFileExtensions, tryLoad); + function loadNodeModuleFromFile(extensions, candidate, failedLookupLocation, host) { + return ts.forEach(extensions, tryLoad); function tryLoad(ext) { var fileName = ts.fileExtensionIs(candidate, ext) ? candidate : candidate + ext; if (host.fileExists(fileName)) { @@ -30659,7 +31067,7 @@ var ts; } } } - function loadNodeModuleFromDirectory(candidate, failedLookupLocation, host) { + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, host) { var packageJsonPath = ts.combinePaths(candidate, "package.json"); if (host.fileExists(packageJsonPath)) { var jsonContent; @@ -30671,7 +31079,7 @@ var ts; jsonContent = { typings: undefined }; } if (jsonContent.typings) { - var result = loadNodeModuleFromFile(ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); + var result = loadNodeModuleFromFile(extensions, ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); if (result) { return result; } @@ -30680,7 +31088,7 @@ var ts; else { failedLookupLocation.push(packageJsonPath); } - return loadNodeModuleFromFile(ts.combinePaths(candidate, "index"), failedLookupLocation, host); + return loadNodeModuleFromFile(extensions, ts.combinePaths(candidate, "index"), failedLookupLocation, host); } function loadModuleFromNodeModules(moduleName, directory, host) { var failedLookupLocations = []; @@ -30690,11 +31098,11 @@ var ts; if (baseName !== "node_modules") { var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var result = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var result = loadNodeModuleFromFile(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } - result = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + result = loadNodeModuleFromDirectory(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } @@ -30719,9 +31127,10 @@ var ts; var searchName; var failedLookupLocations = []; var referencedSourceFile; + var extensions = compilerOptions.allowNonTsExtensions ? ts.supportedJsExtensions : ts.supportedExtensions; while (true) { searchName = ts.normalizePath(ts.combinePaths(searchPath, moduleName)); - referencedSourceFile = ts.forEach(ts.supportedExtensions, function (extension) { + referencedSourceFile = ts.forEach(extensions, function (extension) { if (extension === ".tsx" && !compilerOptions.jsx) { return undefined; } @@ -30749,10 +31158,8 @@ var ts; ts.classicNameResolver = classicNameResolver; ts.defaultInitCompilerOptions = { module: 1, - target: 0, + target: 1, noImplicitAny: false, - outDir: "built", - rootDir: ".", sourceMap: false }; function createCompilerHost(options, setParentNodes) { @@ -31110,35 +31517,47 @@ var ts; if (file.imports) { return; } + var isJavaScriptFile = ts.isSourceFileJavaScript(file); var imports; for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { var node = _a[_i]; - collect(node, true); + collect(node, true, false); } file.imports = imports || emptyArray; - function collect(node, allowRelativeModuleNames) { - switch (node.kind) { - case 222: - case 221: - case 228: - var moduleNameExpr = ts.getExternalModuleName(node); - if (!moduleNameExpr || moduleNameExpr.kind !== 9) { + return; + function collect(node, allowRelativeModuleNames, collectOnlyRequireCalls) { + if (!collectOnlyRequireCalls) { + switch (node.kind) { + case 222: + case 221: + case 228: + var moduleNameExpr = ts.getExternalModuleName(node); + if (!moduleNameExpr || moduleNameExpr.kind !== 9) { + break; + } + if (!moduleNameExpr.text) { + break; + } + if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { + (imports || (imports = [])).push(moduleNameExpr); + } break; - } - if (!moduleNameExpr.text) { + case 218: + if (node.name.kind === 9 && (node.flags & 4 || ts.isDeclarationFile(file))) { + ts.forEachChild(node.body, function (node) { + collect(node, false, collectOnlyRequireCalls); + }); + } break; - } - if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { - (imports || (imports = [])).push(moduleNameExpr); - } - break; - case 218: - if (node.name.kind === 9 && (node.flags & 4 || ts.isDeclarationFile(file))) { - ts.forEachChild(node.body, function (node) { - collect(node, false); - }); - } - break; + } + } + if (isJavaScriptFile) { + if (ts.isRequireCall(node)) { + (imports || (imports = [])).push(node.arguments[0]); + } + else { + ts.forEachChild(node, function (node) { return collect(node, allowRelativeModuleNames, true); }); + } } } } @@ -31225,7 +31644,6 @@ var ts; } processImportedModules(file, basePath); if (isDefaultLib) { - file.isDefaultLib = true; files.unshift(file); } else { @@ -31298,6 +31716,9 @@ var ts; commonPathComponents.length = sourcePathComponents.length; } }); + if (!commonPathComponents) { + return currentDirectory; + } return ts.getNormalizedPathFromPathComponents(commonPathComponents); } function checkSourceFilesBelongToPath(sourceFiles, rootDirectory) { @@ -31380,10 +31801,12 @@ var ts; if (options.module === 5 && languageVersion < 2) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_compile_modules_into_es2015_when_targeting_ES5_or_lower)); } + if (outFile && options.module && !(options.module === 2 || options.module === 4)) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile")); + } if (options.outDir || options.sourceRoot || - (options.mapRoot && - (!outFile || firstExternalModuleSourceFile !== undefined))) { + options.mapRoot) { if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { commonSourceDirectory = ts.getNormalizedAbsolutePath(options.rootDir, currentDirectory); } @@ -31862,20 +32285,20 @@ var ts; var exclude = json["exclude"] instanceof Array ? ts.map(json["exclude"], ts.normalizeSlashes) : undefined; var sysFiles = host.readDirectory(basePath, ".ts", exclude).concat(host.readDirectory(basePath, ".tsx", exclude)); for (var i = 0; i < sysFiles.length; i++) { - var name_29 = sysFiles[i]; - if (ts.fileExtensionIs(name_29, ".d.ts")) { - var baseName = name_29.substr(0, name_29.length - ".d.ts".length); + var name_31 = sysFiles[i]; + if (ts.fileExtensionIs(name_31, ".d.ts")) { + var baseName = name_31.substr(0, name_31.length - ".d.ts".length); if (!ts.contains(sysFiles, baseName + ".tsx") && !ts.contains(sysFiles, baseName + ".ts")) { - fileNames.push(name_29); + fileNames.push(name_31); } } - else if (ts.fileExtensionIs(name_29, ".ts")) { - if (!ts.contains(sysFiles, name_29 + "x")) { - fileNames.push(name_29); + else if (ts.fileExtensionIs(name_31, ".ts")) { + if (!ts.contains(sysFiles, name_31 + "x")) { + fileNames.push(name_31); } } else { - fileNames.push(name_29); + fileNames.push(name_31); } } } @@ -31994,14 +32417,15 @@ var ts; var diagnostic = ts.createCompilerDiagnostic.apply(undefined, arguments); return diagnostic.messageText; } + function getRelativeFileName(fileName, host) { + return host ? ts.convertToRelativePath(fileName, host.getCurrentDirectory(), function (fileName) { return host.getCanonicalFileName(fileName); }) : fileName; + } function reportDiagnosticSimply(diagnostic, host) { var output = ""; if (diagnostic.file) { var _a = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start), line = _a.line, character = _a.character; - var relativeFileName = host - ? ts.convertToRelativePath(diagnostic.file.fileName, host.getCurrentDirectory(), function (fileName) { return host.getCanonicalFileName(fileName); }) - : diagnostic.file.fileName; - output += diagnostic.file.fileName + "(" + (line + 1) + "," + (character + 1) + "): "; + var relativeFileName = getRelativeFileName(diagnostic.file.fileName, host); + output += relativeFileName + "(" + (line + 1) + "," + (character + 1) + "): "; } var category = ts.DiagnosticCategory[diagnostic.category].toLowerCase(); output += category + " TS" + diagnostic.code + ": " + ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine) + ts.sys.newLine; @@ -32030,6 +32454,7 @@ var ts; var _a = ts.getLineAndCharacterOfPosition(file, start), firstLine = _a.line, firstLineChar = _a.character; var _b = ts.getLineAndCharacterOfPosition(file, start + length_3), lastLine = _b.line, lastLineChar = _b.character; var lastLineInFile = ts.getLineAndCharacterOfPosition(file, file.text.length).line; + var relativeFileName = getRelativeFileName(file.fileName, host); var hasMoreThanFiveLines = (lastLine - firstLine) >= 4; var gutterWidth = (lastLine + 1 + "").length; if (hasMoreThanFiveLines) { @@ -32065,7 +32490,7 @@ var ts; output += ts.sys.newLine; } output += ts.sys.newLine; - output += file.fileName + "(" + (firstLine + 1) + "," + (firstLineChar + 1) + "): "; + output += relativeFileName + "(" + (firstLine + 1) + "," + (firstLineChar + 1) + "): "; } var categoryColor = categoryFormatMap[diagnostic.category]; var category = ts.DiagnosticCategory[diagnostic.category].toLowerCase(); @@ -32191,8 +32616,19 @@ var ts; return; } } + if (!cachedConfigFileText) { + var error = ts.createCompilerDiagnostic(ts.Diagnostics.File_0_not_found, configFileName); + reportDiagnostics([error], undefined); + ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped); + return; + } var result = ts.parseConfigFileTextToJson(configFileName, cachedConfigFileText); var configObject = result.config; + if (!configObject) { + reportDiagnostics([result.error], undefined); + ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped); + return; + } var configParseResult = ts.parseJsonConfigFileContent(configObject, ts.sys, ts.getDirectoryPath(configFileName)); if (configParseResult.errors.length > 0) { reportDiagnostics(configParseResult.errors, undefined); @@ -32455,10 +32891,10 @@ var ts; function serializeCompilerOptions(options) { var result = {}; var optionsNameMap = ts.getOptionNameMap().optionNameMap; - for (var name_30 in options) { - if (ts.hasProperty(options, name_30)) { - var value = options[name_30]; - switch (name_30) { + for (var name_32 in options) { + if (ts.hasProperty(options, name_32)) { + var value = options[name_32]; + switch (name_32) { case "init": case "watch": case "version": @@ -32466,17 +32902,17 @@ var ts; case "project": break; default: - var optionDefinition = optionsNameMap[name_30.toLowerCase()]; + var optionDefinition = optionsNameMap[name_32.toLowerCase()]; if (optionDefinition) { if (typeof optionDefinition.type === "string") { - result[name_30] = value; + result[name_32] = value; } else { var typeMap = optionDefinition.type; for (var key in typeMap) { if (ts.hasProperty(typeMap, key)) { if (typeMap[key] === value) - result[name_30] = key; + result[name_32] = key; } } } diff --git a/lib/tsserver.js b/lib/tsserver.js index 2893dab82ef..9f0c1674080 100644 --- a/lib/tsserver.js +++ b/lib/tsserver.js @@ -665,7 +665,7 @@ var ts; } ts.fileExtensionIs = fileExtensionIs; ts.supportedExtensions = [".ts", ".tsx", ".d.ts"]; - ts.moduleFileExtensions = ts.supportedExtensions; + ts.supportedJsExtensions = ts.supportedExtensions.concat(".js", ".jsx"); function isSupportedSourceFileName(fileName) { if (!fileName) { return false; @@ -716,17 +716,16 @@ var ts; } function Signature(checker) { } + function Node(kind, pos, end) { + this.kind = kind; + this.pos = pos; + this.end = end; + this.flags = 0; + this.parent = undefined; + } ts.objectAllocator = { - getNodeConstructor: function (kind) { - function Node(pos, end) { - this.pos = pos; - this.end = end; - this.flags = 0; - this.parent = undefined; - } - Node.prototype = { kind: kind }; - return Node; - }, + getNodeConstructor: function () { return Node; }, + getSourceFileConstructor: function () { return Node; }, getSymbolConstructor: function () { return Symbol; }, getTypeConstructor: function () { return Type; }, getSignatureConstructor: function () { return Signature; } @@ -1003,7 +1002,16 @@ var ts; if (writeByteOrderMark) { data = "\uFEFF" + data; } - _fs.writeFileSync(fileName, data, "utf8"); + var fd; + try { + fd = _fs.openSync(fileName, "w"); + _fs.writeSync(fd, data, undefined, "utf8"); + } + finally { + if (fd !== undefined) { + _fs.closeSync(fd); + } + } } function getCanonicalPath(path) { return useCaseSensitiveFileNames ? path.toLowerCase() : path; @@ -1688,6 +1696,7 @@ var ts; Disallow_inconsistently_cased_references_to_the_same_file: { code: 6078, category: ts.DiagnosticCategory.Message, key: "Disallow_inconsistently_cased_references_to_the_same_file_6078", message: "Disallow inconsistently-cased references to the same file." }, Specify_JSX_code_generation_Colon_preserve_or_react: { code: 6080, category: ts.DiagnosticCategory.Message, key: "Specify_JSX_code_generation_Colon_preserve_or_react_6080", message: "Specify JSX code generation: 'preserve' or 'react'" }, Argument_for_jsx_must_be_preserve_or_react: { code: 6081, category: ts.DiagnosticCategory.Message, key: "Argument_for_jsx_must_be_preserve_or_react_6081", message: "Argument for '--jsx' must be 'preserve' or 'react'." }, + Only_amd_and_system_modules_are_supported_alongside_0: { code: 6082, category: ts.DiagnosticCategory.Error, key: "Only_amd_and_system_modules_are_supported_alongside_0_6082", message: "Only 'amd' and 'system' modules are supported alongside --{0}." }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -2148,7 +2157,7 @@ var ts; function getCommentRanges(text, pos, trailing) { var result; var collecting = trailing || pos === 0; - while (true) { + while (pos < text.length) { var ch = text.charCodeAt(pos); switch (ch) { case 13: @@ -2217,6 +2226,7 @@ var ts; } return result; } + return result; } function getLeadingCommentRanges(text, pos) { return getCommentRanges(text, pos, false); @@ -2312,7 +2322,7 @@ var ts; error(ts.Diagnostics.Digit_expected); } } - return +(text.substring(start, end)); + return "" + +(text.substring(start, end)); } function scanOctalDigits() { var start = pos; @@ -2704,7 +2714,7 @@ var ts; return pos++, token = 36; case 46: if (isDigit(text.charCodeAt(pos + 1))) { - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8; } if (text.charCodeAt(pos + 1) === 46 && text.charCodeAt(pos + 2) === 46) { @@ -2801,7 +2811,7 @@ var ts; case 55: case 56: case 57: - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8; case 58: return pos++, token = 54; @@ -3879,6 +3889,10 @@ var ts; return file.externalModuleIndicator !== undefined; } ts.isExternalModule = isExternalModule; + function isExternalOrCommonJsModule(file) { + return (file.externalModuleIndicator || file.commonJsModuleIndicator) !== undefined; + } + ts.isExternalOrCommonJsModule = isExternalOrCommonJsModule; function isDeclarationFile(file) { return (file.flags & 4096) !== 0; } @@ -3925,18 +3939,26 @@ var ts; return ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos); } ts.getLeadingCommentRangesOfNode = getLeadingCommentRangesOfNode; + function getLeadingCommentRangesOfNodeFromText(node, text) { + return ts.getLeadingCommentRanges(text, node.pos); + } + ts.getLeadingCommentRangesOfNodeFromText = getLeadingCommentRangesOfNodeFromText; function getJsDocComments(node, sourceFileOfNode) { - var commentRanges = (node.kind === 138 || node.kind === 137) ? - ts.concatenate(ts.getTrailingCommentRanges(sourceFileOfNode.text, node.pos), ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos)) : - getLeadingCommentRangesOfNode(node, sourceFileOfNode); - return ts.filter(commentRanges, isJsDocComment); - function isJsDocComment(comment) { - return sourceFileOfNode.text.charCodeAt(comment.pos + 1) === 42 && - sourceFileOfNode.text.charCodeAt(comment.pos + 2) === 42 && - sourceFileOfNode.text.charCodeAt(comment.pos + 3) !== 47; - } + return getJsDocCommentsFromText(node, sourceFileOfNode.text); } ts.getJsDocComments = getJsDocComments; + function getJsDocCommentsFromText(node, text) { + var commentRanges = (node.kind === 138 || node.kind === 137) ? + ts.concatenate(ts.getTrailingCommentRanges(text, node.pos), ts.getLeadingCommentRanges(text, node.pos)) : + getLeadingCommentRangesOfNodeFromText(node, text); + return ts.filter(commentRanges, isJsDocComment); + function isJsDocComment(comment) { + return text.charCodeAt(comment.pos + 1) === 42 && + text.charCodeAt(comment.pos + 2) === 42 && + text.charCodeAt(comment.pos + 3) !== 47; + } + } + ts.getJsDocCommentsFromText = getJsDocCommentsFromText; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; ts.fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*/; function isTypeNode(node) { @@ -4465,6 +4487,41 @@ var ts; return node.kind === 221 && node.moduleReference.kind !== 232; } ts.isInternalModuleImportEqualsDeclaration = isInternalModuleImportEqualsDeclaration; + function isSourceFileJavaScript(file) { + return isInJavaScriptFile(file); + } + ts.isSourceFileJavaScript = isSourceFileJavaScript; + function isInJavaScriptFile(node) { + return node && !!(node.parserContextFlags & 32); + } + ts.isInJavaScriptFile = isInJavaScriptFile; + function isRequireCall(expression) { + return expression.kind === 168 && + expression.expression.kind === 69 && + expression.expression.text === "require" && + expression.arguments.length === 1 && + expression.arguments[0].kind === 9; + } + ts.isRequireCall = isRequireCall; + function isExportsPropertyAssignment(expression) { + return isInJavaScriptFile(expression) && + (expression.kind === 181) && + (expression.operatorToken.kind === 56) && + (expression.left.kind === 166) && + (expression.left.expression.kind === 69) && + ((expression.left.expression).text === "exports"); + } + ts.isExportsPropertyAssignment = isExportsPropertyAssignment; + function isModuleExportsAssignment(expression) { + return isInJavaScriptFile(expression) && + (expression.kind === 181) && + (expression.operatorToken.kind === 56) && + (expression.left.kind === 166) && + (expression.left.expression.kind === 69) && + ((expression.left.expression).text === "module") && + (expression.left.name.text === "exports"); + } + ts.isModuleExportsAssignment = isModuleExportsAssignment; function getExternalModuleName(node) { if (node.kind === 222) { return node.moduleSpecifier; @@ -4776,8 +4833,8 @@ var ts; function getFileReferenceFromReferencePath(comment, commentRange) { var simpleReferenceRegEx = /^\/\/\/\s*/gim; - if (simpleReferenceRegEx.exec(comment)) { - if (isNoDefaultLibRegEx.exec(comment)) { + if (simpleReferenceRegEx.test(comment)) { + if (isNoDefaultLibRegEx.test(comment)) { return { isNoDefaultLib: true }; @@ -4819,12 +4876,20 @@ var ts; return isFunctionLike(node) && (node.flags & 256) !== 0 && !isAccessor(node); } ts.isAsyncFunctionLike = isAsyncFunctionLike; + function isStringOrNumericLiteral(kind) { + return kind === 9 || kind === 8; + } + ts.isStringOrNumericLiteral = isStringOrNumericLiteral; function hasDynamicName(declaration) { - return declaration.name && - declaration.name.kind === 136 && - !isWellKnownSymbolSyntactically(declaration.name.expression); + return declaration.name && isDynamicName(declaration.name); } ts.hasDynamicName = hasDynamicName; + function isDynamicName(name) { + return name.kind === 136 && + !isStringOrNumericLiteral(name.expression.kind) && + !isWellKnownSymbolSyntactically(name.expression); + } + ts.isDynamicName = isDynamicName; function isWellKnownSymbolSyntactically(node) { return isPropertyAccessExpression(node) && isESSymbolIdentifier(node.expression); } @@ -5045,11 +5110,11 @@ var ts; } ts.getIndentSize = getIndentSize; function createTextWriter(newLine) { - var output = ""; - var indent = 0; - var lineStart = true; - var lineCount = 0; - var linePos = 0; + var output; + var indent; + var lineStart; + var lineCount; + var linePos; function write(s) { if (s && s.length) { if (lineStart) { @@ -5059,6 +5124,13 @@ var ts; output += s; } } + function reset() { + output = ""; + indent = 0; + lineStart = true; + lineCount = 0; + linePos = 0; + } function rawWrite(s) { if (s !== undefined) { if (lineStart) { @@ -5085,9 +5157,10 @@ var ts; lineStart = true; } } - function writeTextOfNode(sourceFile, node) { - write(getSourceTextOfNodeFromSourceFile(sourceFile, node)); + function writeTextOfNode(text, node) { + write(getTextOfNodeFromSourceText(text, node)); } + reset(); return { write: write, rawWrite: rawWrite, @@ -5100,10 +5173,17 @@ var ts; getTextPos: function () { return output.length; }, getLine: function () { return lineCount + 1; }, getColumn: function () { return lineStart ? indent * getIndentSize() + 1 : output.length - linePos + 1; }, - getText: function () { return output; } + getText: function () { return output; }, + reset: reset }; } ts.createTextWriter = createTextWriter; + function getExternalModuleNameFromPath(host, fileName) { + var dir = host.getCurrentDirectory(); + var relativePath = ts.getRelativePathToDirectoryOrUrl(dir, fileName, dir, function (f) { return host.getCanonicalFileName(f); }, false); + return ts.removeFileExtension(relativePath); + } + ts.getExternalModuleNameFromPath = getExternalModuleNameFromPath; function getOwnEmitOutputFilePath(sourceFile, host, extension) { var compilerOptions = host.getCompilerOptions(); var emitOutputFilePathWithoutExtension; @@ -5132,6 +5212,10 @@ var ts; return ts.getLineAndCharacterOfPosition(currentSourceFile, pos).line; } ts.getLineOfLocalPosition = getLineOfLocalPosition; + function getLineOfLocalPositionFromLineMap(lineMap, pos) { + return ts.computeLineAndCharacterOfPosition(lineMap, pos).line; + } + ts.getLineOfLocalPositionFromLineMap = getLineOfLocalPositionFromLineMap; function getFirstConstructorWithBody(node) { return ts.forEach(node.members, function (member) { if (member.kind === 144 && nodeIsPresent(member.body)) { @@ -5202,21 +5286,21 @@ var ts; }; } ts.getAllAccessorDeclarations = getAllAccessorDeclarations; - function emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments) { + function emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments) { if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos && - getLineOfLocalPosition(currentSourceFile, node.pos) !== getLineOfLocalPosition(currentSourceFile, leadingComments[0].pos)) { + getLineOfLocalPositionFromLineMap(lineMap, node.pos) !== getLineOfLocalPositionFromLineMap(lineMap, leadingComments[0].pos)) { writer.writeLine(); } } ts.emitNewLineBeforeLeadingComments = emitNewLineBeforeLeadingComments; - function emitComments(currentSourceFile, writer, comments, trailingSeparator, newLine, writeComment) { + function emitComments(text, lineMap, writer, comments, trailingSeparator, newLine, writeComment) { var emitLeadingSpace = !trailingSeparator; ts.forEach(comments, function (comment) { if (emitLeadingSpace) { writer.write(" "); emitLeadingSpace = false; } - writeComment(currentSourceFile, writer, comment, newLine); + writeComment(text, lineMap, writer, comment, newLine); if (comment.hasTrailingNewLine) { writer.writeLine(); } @@ -5229,16 +5313,16 @@ var ts; }); } ts.emitComments = emitComments; - function emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, removeComments) { + function emitDetachedComments(text, lineMap, writer, writeComment, node, newLine, removeComments) { var leadingComments; var currentDetachedCommentInfo; if (removeComments) { if (node.pos === 0) { - leadingComments = ts.filter(ts.getLeadingCommentRanges(currentSourceFile.text, node.pos), isPinnedComment); + leadingComments = ts.filter(ts.getLeadingCommentRanges(text, node.pos), isPinnedComment); } } else { - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + leadingComments = ts.getLeadingCommentRanges(text, node.pos); } if (leadingComments) { var detachedComments = []; @@ -5246,8 +5330,8 @@ var ts; for (var _i = 0, leadingComments_1 = leadingComments; _i < leadingComments_1.length; _i++) { var comment = leadingComments_1[_i]; if (lastComment) { - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastComment.end); - var commentLine = getLineOfLocalPosition(currentSourceFile, comment.pos); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, lastComment.end); + var commentLine = getLineOfLocalPositionFromLineMap(lineMap, comment.pos); if (commentLine >= lastCommentLine + 2) { break; } @@ -5256,37 +5340,37 @@ var ts; lastComment = comment; } if (detachedComments.length) { - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, ts.lastOrUndefined(detachedComments).end); - var nodeLine = getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node.pos)); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, ts.lastOrUndefined(detachedComments).end); + var nodeLine = getLineOfLocalPositionFromLineMap(lineMap, ts.skipTrivia(text, node.pos)); if (nodeLine >= lastCommentLine + 2) { - emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); - emitComments(currentSourceFile, writer, detachedComments, true, newLine, writeComment); + emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments); + emitComments(text, lineMap, writer, detachedComments, true, newLine, writeComment); currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: ts.lastOrUndefined(detachedComments).end }; } } } return currentDetachedCommentInfo; function isPinnedComment(comment) { - return currentSourceFile.text.charCodeAt(comment.pos + 1) === 42 && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 33; + return text.charCodeAt(comment.pos + 1) === 42 && + text.charCodeAt(comment.pos + 2) === 33; } } ts.emitDetachedComments = emitDetachedComments; - function writeCommentRange(currentSourceFile, writer, comment, newLine) { - if (currentSourceFile.text.charCodeAt(comment.pos + 1) === 42) { - var firstCommentLineAndCharacter = ts.getLineAndCharacterOfPosition(currentSourceFile, comment.pos); - var lineCount = ts.getLineStarts(currentSourceFile).length; + function writeCommentRange(text, lineMap, writer, comment, newLine) { + if (text.charCodeAt(comment.pos + 1) === 42) { + var firstCommentLineAndCharacter = ts.computeLineAndCharacterOfPosition(lineMap, comment.pos); + var lineCount = lineMap.length; var firstCommentLineIndent; for (var pos = comment.pos, currentLine = firstCommentLineAndCharacter.line; pos < comment.end; currentLine++) { var nextLineStart = (currentLine + 1) === lineCount - ? currentSourceFile.text.length + 1 - : getStartPositionOfLine(currentLine + 1, currentSourceFile); + ? text.length + 1 + : lineMap[currentLine + 1]; if (pos !== comment.pos) { if (firstCommentLineIndent === undefined) { - firstCommentLineIndent = calculateIndent(getStartPositionOfLine(firstCommentLineAndCharacter.line, currentSourceFile), comment.pos); + firstCommentLineIndent = calculateIndent(text, lineMap[firstCommentLineAndCharacter.line], comment.pos); } var currentWriterIndentSpacing = writer.getIndent() * getIndentSize(); - var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(pos, nextLineStart); + var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(text, pos, nextLineStart); if (spacesToEmit > 0) { var numberOfSingleSpacesToEmit = spacesToEmit % getIndentSize(); var indentSizeSpaceString = getIndentString((spacesToEmit - numberOfSingleSpacesToEmit) / getIndentSize()); @@ -5300,40 +5384,40 @@ var ts; writer.rawWrite(""); } } - writeTrimmedCurrentLine(pos, nextLineStart); + writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart); pos = nextLineStart; } } else { - writer.write(currentSourceFile.text.substring(comment.pos, comment.end)); - } - function writeTrimmedCurrentLine(pos, nextLineStart) { - var end = Math.min(comment.end, nextLineStart - 1); - var currentLineText = currentSourceFile.text.substring(pos, end).replace(/^\s+|\s+$/g, ""); - if (currentLineText) { - writer.write(currentLineText); - if (end !== comment.end) { - writer.writeLine(); - } - } - else { - writer.writeLiteral(newLine); - } - } - function calculateIndent(pos, end) { - var currentLineIndent = 0; - for (; pos < end && ts.isWhiteSpace(currentSourceFile.text.charCodeAt(pos)); pos++) { - if (currentSourceFile.text.charCodeAt(pos) === 9) { - currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); - } - else { - currentLineIndent++; - } - } - return currentLineIndent; + writer.write(text.substring(comment.pos, comment.end)); } } ts.writeCommentRange = writeCommentRange; + function writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart) { + var end = Math.min(comment.end, nextLineStart - 1); + var currentLineText = text.substring(pos, end).replace(/^\s+|\s+$/g, ""); + if (currentLineText) { + writer.write(currentLineText); + if (end !== comment.end) { + writer.writeLine(); + } + } + else { + writer.writeLiteral(newLine); + } + } + function calculateIndent(text, pos, end) { + var currentLineIndent = 0; + for (; pos < end && ts.isWhiteSpace(text.charCodeAt(pos)); pos++) { + if (text.charCodeAt(pos) === 9) { + currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); + } + else { + currentLineIndent++; + } + } + return currentLineIndent; + } function modifierToFlag(token) { switch (token) { case 113: return 64; @@ -5427,14 +5511,14 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 512) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function isJavaScript(fileName) { - return ts.fileExtensionIs(fileName, ".js"); + function hasJavaScriptFileExtension(fileName) { + return ts.fileExtensionIs(fileName, ".js") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isJavaScript = isJavaScript; - function isTsx(fileName) { - return ts.fileExtensionIs(fileName, ".tsx"); + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function allowsJsxExpressions(fileName) { + return ts.fileExtensionIs(fileName, ".tsx") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isTsx = isTsx; + ts.allowsJsxExpressions = allowsJsxExpressions; function getExpandedCharCodes(input) { var output = []; var length = input.length; @@ -5644,14 +5728,16 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - var nodeConstructors = new Array(272); ts.parseTime = 0; - function getNodeConstructor(kind) { - return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); - } - ts.getNodeConstructor = getNodeConstructor; + var NodeConstructor; + var SourceFileConstructor; function createNode(kind, pos, end) { - return new (getNodeConstructor(kind))(pos, end); + if (kind === 248) { + return new (SourceFileConstructor || (SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor()))(kind, pos, end); + } + else { + return new (NodeConstructor || (NodeConstructor = ts.objectAllocator.getNodeConstructor()))(kind, pos, end); + } } ts.createNode = createNode; function visitNode(cbNode, node) { @@ -6050,6 +6136,8 @@ var ts; (function (Parser) { var scanner = ts.createScanner(2, true); var disallowInAndDecoratorContext = 1 | 4; + var NodeConstructor; + var SourceFileConstructor; var sourceFile; var parseDiagnostics; var syntaxCursor; @@ -6062,13 +6150,16 @@ var ts; var contextFlags; var parseErrorBeforeNextFinishedNode = false; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { - initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var isJavaScriptFile = ts.hasJavaScriptFileExtension(fileName) || _sourceText.lastIndexOf("// @language=javascript", 0) === 0; + initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor); var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); clearState(); return result; } Parser.parseSourceFile = parseSourceFile; - function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { + function initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor) { + NodeConstructor = ts.objectAllocator.getNodeConstructor(); + SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor(); sourceText = _sourceText; syntaxCursor = _syntaxCursor; parseDiagnostics = []; @@ -6076,12 +6167,12 @@ var ts; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = ts.isJavaScript(fileName) ? 32 : 0; + contextFlags = isJavaScriptFile ? 32 : 0; parseErrorBeforeNextFinishedNode = false; scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); - scanner.setLanguageVariant(ts.isTsx(fileName) ? 1 : 0); + scanner.setLanguageVariant(ts.allowsJsxExpressions(fileName) ? 1 : 0); } function clearState() { scanner.setText(""); @@ -6094,6 +6185,9 @@ var ts; } function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { sourceFile = createSourceFile(fileName, languageVersion); + if (contextFlags & 32) { + sourceFile.parserContextFlags = 32; + } token = nextToken(); processReferenceComments(sourceFile); sourceFile.statements = parseList(0, parseStatement); @@ -6107,7 +6201,7 @@ var ts; if (setParentNodes) { fixupParentReferences(sourceFile); } - if (ts.isJavaScript(fileName)) { + if (ts.isSourceFileJavaScript(sourceFile)) { addJSDocComments(); } return sourceFile; @@ -6153,15 +6247,14 @@ var ts; } Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - var sourceFile = createNode(248, 0); - sourceFile.pos = 0; - sourceFile.end = sourceText.length; + var sourceFile = new SourceFileConstructor(248, 0, sourceText.length); + nodeCount++; sourceFile.text = sourceText; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 4096 : 0; - sourceFile.languageVariant = ts.isTsx(sourceFile.fileName) ? 1 : 0; + sourceFile.languageVariant = ts.allowsJsxExpressions(sourceFile.fileName) ? 1 : 0; return sourceFile; } function setContextFlag(val, flag) { @@ -6383,7 +6476,7 @@ var ts; if (!(pos >= 0)) { pos = scanner.getStartPos(); } - return new (nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)))(pos, pos); + return new NodeConstructor(kind, pos, pos); } function finishNode(node, end) { node.end = end === undefined ? scanner.getStartPos() : end; @@ -6521,7 +6614,7 @@ var ts; case 12: return token === 19 || token === 37 || isLiteralPropertyName(); case 9: - return isLiteralPropertyName(); + return token === 19 || isLiteralPropertyName(); case 7: if (token === 15) { return lookAhead(isValidHeritageClauseObjectLiteral); @@ -7042,9 +7135,7 @@ var ts; } function parseParameterType() { if (parseOptional(54)) { - return token === 9 - ? parseLiteralNode(true) - : parseType(); + return parseType(); } return undefined; } @@ -7301,6 +7392,8 @@ var ts; case 131: var node = tryParse(parseKeywordAndNoDot); return node || parseTypeReferenceOrTypePredicate(); + case 9: + return parseLiteralNode(true); case 103: case 97: return parseTokenNode(); @@ -7330,6 +7423,7 @@ var ts; case 19: case 25: case 92: + case 9: return true; case 17: return lookAhead(isStartOfParenthesizedOrFunctionType); @@ -7843,7 +7937,6 @@ var ts; var unaryOperator = token; var simpleUnaryExpression = parseSimpleUnaryExpression(); if (token === 38) { - var diagnostic; var start = ts.skipTrivia(sourceText, simpleUnaryExpression.pos); if (simpleUnaryExpression.kind === 171) { parseErrorAtPosition(start, simpleUnaryExpression.end - start, ts.Diagnostics.A_type_assertion_expression_is_not_allowed_in_the_left_hand_side_of_an_exponentiation_expression_Consider_enclosing_the_expression_in_parentheses); @@ -9507,7 +9600,7 @@ var ts; } JSDocParser.isJSDocType = isJSDocType; function parseJSDocTypeExpressionForTests(content, start, length) { - initializeState("file.js", content, 2, undefined); + initializeState("file.js", content, 2, true, undefined); var jsDocTypeExpression = parseJSDocTypeExpression(start, length); var diagnostics = parseDiagnostics; clearState(); @@ -9758,7 +9851,7 @@ var ts; } } function parseIsolatedJSDocComment(content, start, length) { - initializeState("file.js", content, 2, undefined); + initializeState("file.js", content, 2, true, undefined); var jsDocComment = parseJSDocComment(undefined, start, length); var diagnostics = parseDiagnostics; clearState(); @@ -10394,6 +10487,9 @@ var ts; } if (node.name.kind === 136) { var nameExpression = node.name.expression; + if (ts.isStringOrNumericLiteral(nameExpression.kind)) { + return nameExpression.text; + } ts.Debug.assert(ts.isWellKnownSymbolSyntactically(nameExpression)); return ts.getPropertyNameForKnownSymbolName(nameExpression.name.text); } @@ -10414,6 +10510,8 @@ var ts; return "__export"; case 227: return node.isExportEquals ? "export=" : "default"; + case 181: + return "export="; case 213: case 214: return node.flags & 512 ? "default" : undefined; @@ -11033,6 +11131,14 @@ var ts; case 69: return checkStrictModeIdentifier(node); case 181: + if (ts.isInJavaScriptFile(node)) { + if (ts.isExportsPropertyAssignment(node)) { + bindExportsPropertyAssignment(node); + } + else if (ts.isModuleExportsAssignment(node)) { + bindModuleExportsAssignment(node); + } + } return checkStrictModeBinaryExpression(node); case 244: return checkStrictModeCatchClause(node); @@ -11092,6 +11198,11 @@ var ts; checkStrictModeFunctionName(node); var bindingName = node.name ? node.name.text : "__function"; return bindAnonymousDeclaration(node, 16, bindingName); + case 168: + if (ts.isInJavaScriptFile(node)) { + bindCallExpression(node); + } + break; case 186: case 214: return bindClassLikeDeclaration(node); @@ -11121,14 +11232,18 @@ var ts; function bindSourceFileIfExternalModule() { setExportContextFlag(file); if (ts.isExternalModule(file)) { - bindAnonymousDeclaration(file, 512, "\"" + ts.removeFileExtension(file.fileName) + "\""); + bindSourceFileAsExternalModule(); } } + function bindSourceFileAsExternalModule() { + bindAnonymousDeclaration(file, 512, "\"" + ts.removeFileExtension(file.fileName) + "\""); + } function bindExportAssignment(node) { + var boundExpression = node.kind === 227 ? node.expression : node.right; if (!container.symbol || !container.symbol.exports) { bindAnonymousDeclaration(node, 8388608, getDeclarationName(node)); } - else if (node.expression.kind === 69) { + else if (boundExpression.kind === 69) { declareSymbol(container.symbol.exports, container.symbol, node, 8388608, 107455 | 8388608); } else { @@ -11148,6 +11263,25 @@ var ts; declareSymbolAndAddToSymbolTable(node, 8388608, 8388608); } } + function setCommonJsModuleIndicator(node) { + if (!file.commonJsModuleIndicator) { + file.commonJsModuleIndicator = node; + bindSourceFileAsExternalModule(); + } + } + function bindExportsPropertyAssignment(node) { + setCommonJsModuleIndicator(node); + declareSymbol(file.symbol.exports, file.symbol, node.left, 4 | 7340032, 0); + } + function bindModuleExportsAssignment(node) { + setCommonJsModuleIndicator(node); + bindExportAssignment(node); + } + function bindCallExpression(node) { + if (!file.commonJsModuleIndicator && ts.isRequireCall(node)) { + setCommonJsModuleIndicator(node); + } + } function bindClassLikeDeclaration(node) { if (node.kind === 214) { bindBlockScopedDeclaration(node, 32, 899519); @@ -11268,7 +11402,7 @@ var ts; function checkUnreachable(node) { switch (currentReachabilityState) { case 4: - var reportError = ts.isStatement(node) || + var reportError = (ts.isStatement(node) && node.kind !== 194) || node.kind === 214 || (node.kind === 218 && shouldReportErrorOnModuleDeclaration(node)) || (node.kind === 217 && (!ts.isConstEnumDeclaration(node) || options.preserveConstEnums)); @@ -11364,7 +11498,7 @@ var ts; symbolToString: symbolToString, getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, getRootSymbols: getRootSymbols, - getContextualType: getContextualType, + getContextualType: getApparentTypeOfContextualType, getFullyQualifiedName: getFullyQualifiedName, getResolvedSignature: getResolvedSignature, getConstantValue: getConstantValue, @@ -11620,7 +11754,7 @@ var ts; return ts.getAncestor(node, 248); } function isGlobalSourceFile(node) { - return node.kind === 248 && !ts.isExternalModule(node); + return node.kind === 248 && !ts.isExternalOrCommonJsModule(node); } function getSymbol(symbols, name, meaning) { if (meaning && ts.hasProperty(symbols, name)) { @@ -11706,23 +11840,24 @@ var ts; } switch (location.kind) { case 248: - if (!ts.isExternalModule(location)) + if (!ts.isExternalOrCommonJsModule(location)) break; case 218: var moduleExports = getSymbolOfNode(location).exports; if (location.kind === 248 || (location.kind === 218 && location.name.kind === 9)) { + if (result = moduleExports["default"]) { + var localSymbol = ts.getLocalSymbolForExportDefault(result); + if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { + break loop; + } + result = undefined; + } if (ts.hasProperty(moduleExports, name) && moduleExports[name].flags === 8388608 && ts.getDeclarationOfKind(moduleExports[name], 230)) { break; } - result = moduleExports["default"]; - var localSymbol = ts.getLocalSymbolForExportDefault(result); - if (result && localSymbol && (result.flags & meaning) && localSymbol.name === name) { - break loop; - } - result = undefined; } if (result = getSymbol(moduleExports, name, meaning & 8914931)) { break loop; @@ -12080,6 +12215,9 @@ var ts; if (moduleName === undefined) { return; } + if (moduleName.indexOf("!") >= 0) { + moduleName = moduleName.substr(0, moduleName.indexOf("!")); + } var isRelative = ts.isExternalModuleNameRelative(moduleName); if (!isRelative) { var symbol = getSymbol(globals, "\"" + moduleName + "\"", 512); @@ -12250,7 +12388,7 @@ var ts; } switch (location_1.kind) { case 248: - if (!ts.isExternalModule(location_1)) { + if (!ts.isExternalOrCommonJsModule(location_1)) { break; } case 218: @@ -12377,7 +12515,7 @@ var ts; } function hasExternalModuleSymbol(declaration) { return (declaration.kind === 218 && declaration.name.kind === 9) || - (declaration.kind === 248 && ts.isExternalModule(declaration)); + (declaration.kind === 248 && ts.isExternalOrCommonJsModule(declaration)); } function hasVisibleDeclarations(symbol) { var aliasesToMakeVisible; @@ -12575,7 +12713,7 @@ var ts; writeAnonymousType(type, flags); } else if (type.flags & 256) { - writer.writeStringLiteral(type.text); + writer.writeStringLiteral("\"" + ts.escapeString(type.text) + "\""); } else { writePunctuation(writer, 15); @@ -12939,7 +13077,7 @@ var ts; } } else if (node.kind === 248) { - return ts.isExternalModule(node) ? node : undefined; + return ts.isExternalOrCommonJsModule(node) ? node : undefined; } } ts.Debug.fail("getContainingModule cant reach here"); @@ -13144,6 +13282,23 @@ var ts; var symbol = getSymbolOfNode(node); return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node); } + function getTextOfPropertyName(name) { + switch (name.kind) { + case 69: + return name.text; + case 9: + case 8: + return name.text; + case 136: + if (ts.isStringOrNumericLiteral(name.expression.kind)) { + return name.expression.text; + } + } + return undefined; + } + function isComputedNonLiteralName(name) { + return name.kind === 136 && !ts.isStringOrNumericLiteral(name.expression.kind); + } function getTypeForBindingElement(declaration) { var pattern = declaration.parent; var parentType = getTypeForBindingElementParent(pattern.parent); @@ -13159,8 +13314,12 @@ var ts; var type; if (pattern.kind === 161) { var name_11 = declaration.propertyName || declaration.name; - type = getTypeOfPropertyOfType(parentType, name_11.text) || - isNumericLiteralName(name_11.text) && getIndexTypeOfType(parentType, 1) || + if (isComputedNonLiteralName(name_11)) { + return anyType; + } + var text = getTextOfPropertyName(name_11); + type = getTypeOfPropertyOfType(parentType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1) || getIndexTypeOfType(parentType, 0); if (!type) { error(name_11, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_11)); @@ -13238,10 +13397,16 @@ var ts; } function getTypeFromObjectBindingPattern(pattern, includePatternInType) { var members = {}; + var hasComputedProperties = false; ts.forEach(pattern.elements, function (e) { - var flags = 4 | 67108864 | (e.initializer ? 536870912 : 0); var name = e.propertyName || e.name; - var symbol = createSymbol(flags, name.text); + if (isComputedNonLiteralName(name)) { + hasComputedProperties = true; + return; + } + var text = getTextOfPropertyName(name); + var flags = 4 | 67108864 | (e.initializer ? 536870912 : 0); + var symbol = createSymbol(flags, text); symbol.type = getTypeFromBindingElement(e, includePatternInType); symbol.bindingElement = e; members[symbol.name] = symbol; @@ -13250,6 +13415,9 @@ var ts; if (includePatternInType) { result.pattern = pattern; } + if (hasComputedProperties) { + result.flags |= 67108864; + } return result; } function getTypeFromArrayBindingPattern(pattern, includePatternInType) { @@ -13300,6 +13468,12 @@ var ts; if (declaration.kind === 227) { return links.type = checkExpression(declaration.expression); } + if (declaration.kind === 181) { + return links.type = checkExpression(declaration.right); + } + if (declaration.kind === 166) { + return checkExpressionCached(declaration.parent.right); + } if (!pushTypeResolution(symbol, 0)) { return unknownType; } @@ -13549,17 +13723,19 @@ var ts; } function resolveBaseTypesOfClass(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - var baseContructorType = getBaseConstructorTypeOfClass(type); - if (!(baseContructorType.flags & 80896)) { + var baseConstructorType = getBaseConstructorTypeOfClass(type); + if (!(baseConstructorType.flags & 80896)) { return; } var baseTypeNode = getBaseTypeNodeOfClass(type); var baseType; - if (baseContructorType.symbol && baseContructorType.symbol.flags & 32) { - baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseContructorType.symbol); + var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; + if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 && + areAllOuterTypeParametersApplied(originalBaseType)) { + baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); } else { - var constructors = getInstantiatedConstructorsForTypeArguments(baseContructorType, baseTypeNode.typeArguments); + var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); if (!constructors.length) { error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); return; @@ -13584,6 +13760,15 @@ var ts; type.resolvedBaseTypes.push(baseType); } } + function areAllOuterTypeParametersApplied(type) { + var outerTypeParameters = type.outerTypeParameters; + if (outerTypeParameters) { + var last = outerTypeParameters.length - 1; + var typeArguments = type.typeArguments; + return outerTypeParameters[last].symbol !== typeArguments[last].symbol; + } + return true; + } function resolveBaseTypesOfInterface(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { @@ -13655,7 +13840,7 @@ var ts; type.typeArguments = type.typeParameters; type.thisType = createType(512 | 33554432); type.thisType.symbol = symbol; - type.thisType.constraint = getTypeWithThisArgument(type); + type.thisType.constraint = type; } } return links.declaredType; @@ -14130,14 +14315,19 @@ var ts; type = getApparentType(type); return type.flags & 49152 ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); } + function getApparentTypeOfTypeParameter(type) { + if (!type.resolvedApparentType) { + var constraintType = getConstraintOfTypeParameter(type); + while (constraintType && constraintType.flags & 512) { + constraintType = getConstraintOfTypeParameter(constraintType); + } + type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); + } + return type.resolvedApparentType; + } function getApparentType(type) { if (type.flags & 512) { - do { - type = getConstraintOfTypeParameter(type); - } while (type && type.flags & 512); - if (!type) { - type = emptyObjectType; - } + type = getApparentTypeOfTypeParameter(type); } if (type.flags & 258) { type = globalStringType; @@ -14290,7 +14480,7 @@ var ts; if (node.initializer) { var signatureDeclaration = node.parent; var signature = getSignatureFromDeclaration(signatureDeclaration); - var parameterIndex = signatureDeclaration.parameters.indexOf(node); + var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); ts.Debug.assert(parameterIndex >= 0); return parameterIndex >= signature.minArgumentCount; } @@ -14385,6 +14575,16 @@ var ts; } return result; } + function resolveExternalModuleTypeByLiteral(name) { + var moduleSym = resolveExternalModuleName(name, name); + if (moduleSym) { + var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); + if (resolvedModuleSymbol) { + return getTypeOfSymbol(resolvedModuleSymbol); + } + } + return anyType; + } function getReturnTypeOfSignature(signature) { if (!signature.resolvedReturnType) { if (!pushTypeResolution(signature, 3)) { @@ -14846,11 +15046,12 @@ var ts; return links.resolvedType; } function getStringLiteralType(node) { - if (ts.hasProperty(stringLiteralTypes, node.text)) { - return stringLiteralTypes[node.text]; + var text = node.text; + if (ts.hasProperty(stringLiteralTypes, text)) { + return stringLiteralTypes[text]; } - var type = stringLiteralTypes[node.text] = createType(256); - type.text = ts.getTextOfNode(node); + var type = stringLiteralTypes[text] = createType(256); + type.text = text; return type; } function getTypeFromStringLiteral(node) { @@ -15319,7 +15520,7 @@ var ts; return false; } function hasExcessProperties(source, target, reportErrors) { - if (someConstituentTypeHasKind(target, 80896)) { + if (!(target.flags & 67108864) && someConstituentTypeHasKind(target, 80896)) { for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { var prop = _a[_i]; if (!isKnownProperty(target, prop.name)) { @@ -15409,9 +15610,6 @@ var ts; return result; } function typeParameterIdenticalTo(source, target) { - if (source.symbol.name !== target.symbol.name) { - return 0; - } if (source.constraint === target.constraint) { return -1; } @@ -15856,18 +16054,24 @@ var ts; } return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } + function isMatchingSignature(source, target, partialMatch) { + if (source.parameters.length === target.parameters.length && + source.minArgumentCount === target.minArgumentCount && + source.hasRestParameter === target.hasRestParameter) { + return true; + } + if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (source.hasRestParameter && !target.hasRestParameter || + source.hasRestParameter === target.hasRestParameter && source.parameters.length >= target.parameters.length)) { + return true; + } + return false; + } function compareSignatures(source, target, partialMatch, ignoreReturnTypes, compareTypes) { if (source === target) { return -1; } - if (source.parameters.length !== target.parameters.length || - source.minArgumentCount !== target.minArgumentCount || - source.hasRestParameter !== target.hasRestParameter) { - if (!partialMatch || - source.parameters.length < target.parameters.length && !source.hasRestParameter || - source.minArgumentCount > target.minArgumentCount) { - return 0; - } + if (!(isMatchingSignature(source, target, partialMatch))) { + return 0; } var result = -1; if (source.typeParameters && target.typeParameters) { @@ -15952,6 +16156,9 @@ var ts; function isTupleLikeType(type) { return !!getPropertyOfType(type, "0"); } + function isStringLiteralType(type) { + return type.flags & 256; + } function isTupleType(type) { return !!(type.flags & 8192); } @@ -16543,7 +16750,7 @@ var ts; } } function narrowTypeByInstanceof(type, expr, assumeTrue) { - if (isTypeAny(type) || !assumeTrue || expr.left.kind !== 69 || getResolvedSymbol(expr.left) !== symbol) { + if (isTypeAny(type) || expr.left.kind !== 69 || getResolvedSymbol(expr.left) !== symbol) { return type; } var rightType = checkExpression(expr.right); @@ -16571,6 +16778,12 @@ var ts; } } if (targetType) { + if (!assumeTrue) { + if (type.flags & 16384) { + return getUnionType(ts.filter(type.types, function (t) { return !isTypeSubtypeOf(t, targetType); })); + } + return type; + } return getNarrowedType(type, targetType); } return type; @@ -16982,6 +17195,9 @@ var ts; function getIndexTypeOfContextualType(type, kind) { return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); } + function contextualTypeIsStringLiteralType(type) { + return !!(type.flags & 16384 ? ts.forEach(type.types, isStringLiteralType) : isStringLiteralType(type)); + } function contextualTypeIsTupleLikeType(type) { return !!(type.flags & 16384 ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); } @@ -16997,7 +17213,7 @@ var ts; } function getContextualTypeForObjectLiteralElement(element) { var objectLiteral = element.parent; - var type = getContextualType(objectLiteral); + var type = getApparentTypeOfContextualType(objectLiteral); if (type) { if (!ts.hasDynamicName(element)) { var symbolName = getSymbolOfNode(element).name; @@ -17013,7 +17229,7 @@ var ts; } function getContextualTypeForElementExpression(node) { var arrayLiteral = node.parent; - var type = getContextualType(arrayLiteral); + var type = getApparentTypeOfContextualType(arrayLiteral); if (type) { var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) @@ -17042,11 +17258,11 @@ var ts; } return undefined; } - function getContextualType(node) { - var type = getContextualTypeWorker(node); + function getApparentTypeOfContextualType(node) { + var type = getContextualType(node); return type && getApparentType(type); } - function getContextualTypeWorker(node) { + function getContextualType(node) { if (isInsideWithStatementBody(node)) { return undefined; } @@ -17112,7 +17328,7 @@ var ts; ts.Debug.assert(node.kind !== 143 || ts.isObjectLiteralMethod(node)); var type = ts.isObjectLiteralMethod(node) ? getContextualTypeForObjectLiteralMethod(node) - : getContextualType(node); + : getApparentTypeOfContextualType(node); if (!type) { return undefined; } @@ -17195,7 +17411,7 @@ var ts; type.pattern = node; return type; } - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { var pattern = contextualType.pattern; if (pattern && (pattern.kind === 162 || pattern.kind === 164)) { @@ -17250,10 +17466,11 @@ var ts; checkGrammarObjectLiteralExpression(node, inDestructuringPattern); var propertiesTable = {}; var propertiesArray = []; - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); var contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === 161 || contextualType.pattern.kind === 165); var typeFlags = 0; + var patternWithComputedProperties = false; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var memberDecl = _a[_i]; var member = memberDecl.symbol; @@ -17279,8 +17496,11 @@ var ts; if (isOptional) { prop.flags |= 536870912; } + if (ts.hasDynamicName(memberDecl)) { + patternWithComputedProperties = true; + } } - else if (contextualTypeHasPattern) { + else if (contextualTypeHasPattern && !(contextualType.flags & 67108864)) { var impliedProp = getPropertyOfType(contextualType, member.name); if (impliedProp) { prop.flags |= impliedProp.flags & 536870912; @@ -17323,7 +17543,7 @@ var ts; var numberIndexType = getIndexType(1); var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexType, numberIndexType); var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 1048576; - result.flags |= 524288 | 4194304 | freshObjectLiteralFlag | (typeFlags & 14680064); + result.flags |= 524288 | 4194304 | freshObjectLiteralFlag | (typeFlags & 14680064) | (patternWithComputedProperties ? 67108864 : 0); if (inDestructuringPattern) { result.pattern = node; } @@ -18538,6 +18758,9 @@ var ts; return anyType; } } + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node)) { + return resolveExternalModuleTypeByLiteral(node.arguments[0]); + } return getReturnTypeOfSignature(signature); } function checkTaggedTemplateExpression(node) { @@ -18548,7 +18771,9 @@ var ts; var targetType = getTypeFromTypeNode(node.type); if (produceDiagnostics && targetType !== unknownType) { var widenedType = getWidenedType(exprType); - if (!(isTypeAssignableTo(targetType, widenedType))) { + var bothAreStringLike = someConstituentTypeHasKind(targetType, 258) && + someConstituentTypeHasKind(widenedType, 258); + if (!bothAreStringLike && !(isTypeAssignableTo(targetType, widenedType))) { checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } @@ -18987,17 +19212,24 @@ var ts; var p = properties_3[_i]; if (p.kind === 245 || p.kind === 246) { var name_14 = p.name; + if (name_14.kind === 136) { + checkComputedPropertyName(name_14); + } + if (isComputedNonLiteralName(name_14)) { + continue; + } + var text = getTextOfPropertyName(name_14); var type = isTypeAny(sourceType) ? sourceType - : getTypeOfPropertyOfType(sourceType, name_14.text) || - isNumericLiteralName(name_14.text) && getIndexTypeOfType(sourceType, 1) || + : getTypeOfPropertyOfType(sourceType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(sourceType, 1) || getIndexTypeOfType(sourceType, 0); if (type) { if (p.kind === 246) { checkDestructuringAssignment(p, type); } else { - checkDestructuringAssignment(p.initializer || name_14, type); + checkDestructuringAssignment(p.initializer, type); } } else { @@ -19175,6 +19407,9 @@ var ts; case 31: case 32: case 33: + if (someConstituentTypeHasKind(leftType, 258) && someConstituentTypeHasKind(rightType, 258)) { + return booleanType; + } if (!isTypeAssignableTo(leftType, rightType) && !isTypeAssignableTo(rightType, leftType)) { reportOperatorError(); } @@ -19282,6 +19517,13 @@ var ts; var type2 = checkExpression(node.whenFalse, contextualMapper); return getUnionType([type1, type2]); } + function checkStringLiteralExpression(node) { + var contextualType = getContextualType(node); + if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { + return getStringLiteralType(node); + } + return stringType; + } function checkTemplateExpression(node) { ts.forEach(node.templateSpans, function (templateSpan) { checkExpression(templateSpan.expression); @@ -19320,7 +19562,7 @@ var ts; if (isInferentialContext(contextualMapper)) { var signature = getSingleCallSignature(type); if (signature && signature.typeParameters) { - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType) { var contextualSignature = getSingleCallSignature(contextualType); if (contextualSignature && !contextualSignature.typeParameters) { @@ -19372,6 +19614,7 @@ var ts; case 183: return checkTemplateExpression(node); case 9: + return checkStringLiteralExpression(node); case 11: return stringType; case 10: @@ -20433,7 +20676,7 @@ var ts; return; } var parent = getDeclarationContainer(node); - if (parent.kind === 248 && ts.isExternalModule(parent)) { + if (parent.kind === 248 && ts.isExternalOrCommonJsModule(parent)) { error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); } } @@ -20504,6 +20747,11 @@ var ts; checkExpressionCached(node.initializer); } } + if (node.kind === 163) { + if (node.propertyName && node.propertyName.kind === 136) { + checkComputedPropertyName(node.propertyName); + } + } if (ts.isBindingPattern(node.name)) { ts.forEach(node.name.elements, checkSourceElement); } @@ -20862,6 +21110,7 @@ var ts; var firstDefaultClause; var hasDuplicateDefaultClause = false; var expressionType = checkExpression(node.expression); + var expressionTypeIsStringLike = someConstituentTypeHasKind(expressionType, 258); ts.forEach(node.caseBlock.clauses, function (clause) { if (clause.kind === 242 && !hasDuplicateDefaultClause) { if (firstDefaultClause === undefined) { @@ -20878,6 +21127,9 @@ var ts; if (produceDiagnostics && clause.kind === 241) { var caseClause = clause; var caseType = checkExpression(caseClause.expression); + if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, 258)) { + return; + } if (!isTypeAssignableTo(expressionType, caseType)) { checkTypeAssignableTo(caseType, expressionType, caseClause.expression, undefined); } @@ -21285,11 +21537,14 @@ var ts; var enumIsConst = ts.isConst(node); for (var _i = 0, _a = node.members; _i < _a.length; _i++) { var member = _a[_i]; - if (member.name.kind === 136) { + if (isComputedNonLiteralName(member.name)) { error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); } - else if (isNumericLiteralName(member.name.text)) { - error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + else { + var text = getTextOfPropertyName(member.name); + if (isNumericLiteralName(text)) { + error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + } } var previousEnumMemberIsNonConstant = autoValue === undefined; var initializer = member.initializer; @@ -21987,8 +22242,10 @@ var ts; function checkSourceFileWorker(node) { var links = getNodeLinks(node); if (!(links.flags & 1)) { - if (node.isDefaultLib && compilerOptions.skipDefaultLibCheck) { - return; + if (compilerOptions.skipDefaultLibCheck) { + if (node.hasNoDefaultLib) { + return; + } } checkGrammarSourceFile(node); emitExtends = false; @@ -21997,7 +22254,7 @@ var ts; potentialThisCollisions.length = 0; ts.forEach(node.statements, checkSourceElement); checkFunctionAndClassExpressionBodies(node); - if (ts.isExternalModule(node)) { + if (ts.isExternalOrCommonJsModule(node)) { checkExternalModuleExports(node); } if (potentialThisCollisions.length) { @@ -22075,7 +22332,7 @@ var ts; } switch (location.kind) { case 248: - if (!ts.isExternalModule(location)) { + if (!ts.isExternalOrCommonJsModule(location)) { break; } case 218: @@ -22634,15 +22891,24 @@ var ts; getReferencedValueDeclaration: getReferencedValueDeclaration, getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, isOptionalParameter: isOptionalParameter, - isArgumentsLocalBinding: isArgumentsLocalBinding + isArgumentsLocalBinding: isArgumentsLocalBinding, + getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration }; } + function getExternalModuleFileFromDeclaration(declaration) { + var specifier = ts.getExternalModuleName(declaration); + var moduleSymbol = getSymbolAtLocation(specifier); + if (!moduleSymbol) { + return undefined; + } + return ts.getDeclarationOfKind(moduleSymbol, 248); + } function initializeTypeChecker() { ts.forEach(host.getSourceFiles(), function (file) { ts.bindSourceFile(file, compilerOptions); }); ts.forEach(host.getSourceFiles(), function (file) { - if (!ts.isExternalModule(file)) { + if (!ts.isExternalOrCommonJsModule(file)) { mergeSymbolTable(globals, file.locals); } }); @@ -23319,7 +23585,7 @@ var ts; } } function checkGrammarForNonSymbolComputedProperty(node, message) { - if (node.kind === 136 && !ts.isWellKnownSymbolSyntactically(node.expression)) { + if (ts.isDynamicName(node)) { return grammarErrorOnNode(node, message); } } @@ -23633,11 +23899,15 @@ var ts; var writeTextOfNode; var writer = createAndSetNewTextWriterWithSymbolWriter(); var enclosingDeclaration; - var currentSourceFile; + var currentText; + var currentLineMap; + var currentIdentifiers; + var isCurrentFileExternalModule; var reportedDeclarationError = false; var errorNameNode; var emitJsDocComments = compilerOptions.removeComments ? function (declaration) { } : writeJsDocComments; var emit = compilerOptions.stripInternal ? stripInternal : emitNode; + var noDeclare = !root; var moduleElementDeclarationEmitInfo = []; var asynchronousSubModuleDeclarationEmitInfo; var referencePathsOutput = ""; @@ -23673,21 +23943,53 @@ var ts; } else { var emittedReferencedFiles = []; + var prevModuleElementDeclarationEmitInfo = []; ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + if (!ts.isDeclarationFile(sourceFile)) { if (!compilerOptions.noResolve) { ts.forEach(sourceFile.referencedFiles, function (fileReference) { var referencedFile = ts.tryResolveScriptReference(host, sourceFile, fileReference); - if (referencedFile && (ts.isExternalModuleOrDeclarationFile(referencedFile) && + if (referencedFile && (ts.isDeclarationFile(referencedFile) && !ts.contains(emittedReferencedFiles, referencedFile))) { writeReferencePath(referencedFile); emittedReferencedFiles.push(referencedFile); } }); } + } + if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + noDeclare = false; emitSourceFile(sourceFile); } + else if (ts.isExternalModule(sourceFile)) { + noDeclare = true; + write("declare module \"" + ts.getResolvedExternalModuleName(host, sourceFile) + "\" {"); + writeLine(); + increaseIndent(); + emitSourceFile(sourceFile); + decreaseIndent(); + write("}"); + writeLine(); + if (moduleElementDeclarationEmitInfo.length) { + var oldWriter = writer; + ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { + if (aliasEmitInfo.isVisible && !aliasEmitInfo.asynchronousOutput) { + ts.Debug.assert(aliasEmitInfo.node.kind === 222); + createAndSetNewTextWriterWithSymbolWriter(); + ts.Debug.assert(aliasEmitInfo.indent === 1); + increaseIndent(); + writeImportDeclaration(aliasEmitInfo.node); + aliasEmitInfo.asynchronousOutput = writer.getText(); + decreaseIndent(); + } + }); + setWriter(oldWriter); + } + prevModuleElementDeclarationEmitInfo = prevModuleElementDeclarationEmitInfo.concat(moduleElementDeclarationEmitInfo); + moduleElementDeclarationEmitInfo = []; + } }); + moduleElementDeclarationEmitInfo = moduleElementDeclarationEmitInfo.concat(prevModuleElementDeclarationEmitInfo); } return { reportedDeclarationError: reportedDeclarationError, @@ -23696,13 +23998,12 @@ var ts; referencePathsOutput: referencePathsOutput }; function hasInternalAnnotation(range) { - var text = currentSourceFile.text; - var comment = text.substring(range.pos, range.end); + var comment = currentText.substring(range.pos, range.end); return comment.indexOf("@internal") >= 0; } function stripInternal(node) { if (node) { - var leadingCommentRanges = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + var leadingCommentRanges = ts.getLeadingCommentRanges(currentText, node.pos); if (ts.forEach(leadingCommentRanges, hasInternalAnnotation)) { return; } @@ -23783,7 +24084,7 @@ var ts; var errorInfo = writer.getSymbolAccessibilityDiagnostic(symbolAccesibilityResult); if (errorInfo) { if (errorInfo.typeName) { - diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); + diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getTextOfNodeFromSourceText(currentText, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); } else { diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); @@ -23847,9 +24148,9 @@ var ts; } function writeJsDocComments(declaration) { if (declaration) { - var jsDocComments = ts.getJsDocComments(declaration, currentSourceFile); - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, declaration, jsDocComments); - ts.emitComments(currentSourceFile, writer, jsDocComments, true, newLine, ts.writeCommentRange); + var jsDocComments = ts.getJsDocCommentsFromText(declaration, currentText); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments); + ts.emitComments(currentText, currentLineMap, writer, jsDocComments, true, newLine, ts.writeCommentRange); } } function emitTypeWithNewGetSymbolAccessibilityDiagnostic(type, getSymbolAccessibilityDiagnostic) { @@ -23866,7 +24167,7 @@ var ts; case 103: case 97: case 9: - return writeTextOfNode(currentSourceFile, type); + return writeTextOfNode(currentText, type); case 188: return emitExpressionWithTypeArguments(type); case 151: @@ -23897,14 +24198,14 @@ var ts; } function writeEntityName(entityName) { if (entityName.kind === 69) { - writeTextOfNode(currentSourceFile, entityName); + writeTextOfNode(currentText, entityName); } else { var left = entityName.kind === 135 ? entityName.left : entityName.expression; var right = entityName.kind === 135 ? entityName.right : entityName.name; writeEntityName(left); write("."); - writeTextOfNode(currentSourceFile, right); + writeTextOfNode(currentText, right); } } function emitEntityName(entityName) { @@ -23932,7 +24233,7 @@ var ts; } } function emitTypePredicate(type) { - writeTextOfNode(currentSourceFile, type.parameterName); + writeTextOfNode(currentText, type.parameterName); write(" is "); emitType(type.type); } @@ -23972,20 +24273,23 @@ var ts; } } function emitSourceFile(node) { - currentSourceFile = node; + currentText = node.text; + currentLineMap = ts.getLineStarts(node); + currentIdentifiers = node.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(node); enclosingDeclaration = node; - ts.emitDetachedComments(currentSourceFile, writer, ts.writeCommentRange, node, newLine, true); + ts.emitDetachedComments(currentText, currentLineMap, writer, ts.writeCommentRange, node, newLine, true); emitLines(node.statements); } function getExportDefaultTempVariableName() { var baseName = "_default"; - if (!ts.hasProperty(currentSourceFile.identifiers, baseName)) { + if (!ts.hasProperty(currentIdentifiers, baseName)) { return baseName; } var count = 0; while (true) { var name_19 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_19)) { + if (!ts.hasProperty(currentIdentifiers, name_19)) { return name_19; } } @@ -23993,7 +24297,7 @@ var ts; function emitExportAssignment(node) { if (node.expression.kind === 69) { write(node.isExportEquals ? "export = " : "export default "); - writeTextOfNode(currentSourceFile, node.expression); + writeTextOfNode(currentText, node.expression); } else { var tempVarName = getExportDefaultTempVariableName(); @@ -24028,7 +24332,7 @@ var ts; writeModuleElement(node); } else if (node.kind === 221 || - (node.parent.kind === 248 && ts.isExternalModule(currentSourceFile))) { + (node.parent.kind === 248 && isCurrentFileExternalModule)) { var isVisible; if (asynchronousSubModuleDeclarationEmitInfo && node.parent.kind !== 248) { asynchronousSubModuleDeclarationEmitInfo.push({ @@ -24080,14 +24384,14 @@ var ts; } } function emitModuleElementDeclarationFlags(node) { - if (node.parent === currentSourceFile) { + if (node.parent.kind === 248) { if (node.flags & 2) { write("export "); } if (node.flags & 512) { write("default "); } - else if (node.kind !== 215) { + else if (node.kind !== 215 && !noDeclare) { write("declare "); } } @@ -24112,7 +24416,7 @@ var ts; write("export "); } write("import "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" = "); if (ts.isInternalModuleImportEqualsDeclaration(node)) { emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.moduleReference, getImportEntityNameVisibilityError); @@ -24120,7 +24424,7 @@ var ts; } else { write("require("); - writeTextOfNode(currentSourceFile, ts.getExternalModuleImportEqualsDeclarationExpression(node)); + writeTextOfNode(currentText, ts.getExternalModuleImportEqualsDeclarationExpression(node)); write(");"); } writer.writeLine(); @@ -24154,7 +24458,7 @@ var ts; if (node.importClause) { var currentWriterPos = writer.getTextPos(); if (node.importClause.name && resolver.isDeclarationVisible(node.importClause)) { - writeTextOfNode(currentSourceFile, node.importClause.name); + writeTextOfNode(currentText, node.importClause.name); } if (node.importClause.namedBindings && isVisibleNamedBinding(node.importClause.namedBindings)) { if (currentWriterPos !== writer.getTextPos()) { @@ -24162,7 +24466,7 @@ var ts; } if (node.importClause.namedBindings.kind === 224) { write("* as "); - writeTextOfNode(currentSourceFile, node.importClause.namedBindings.name); + writeTextOfNode(currentText, node.importClause.namedBindings.name); } else { write("{ "); @@ -24172,16 +24476,28 @@ var ts; } write(" from "); } - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); write(";"); writer.writeLine(); } + function emitExternalModuleSpecifier(moduleSpecifier) { + if (moduleSpecifier.kind === 9 && (!root) && (compilerOptions.out || compilerOptions.outFile)) { + var moduleName = ts.getExternalModuleNameFromDeclaration(host, resolver, moduleSpecifier.parent); + if (moduleName) { + write("\""); + write(moduleName); + write("\""); + return; + } + } + writeTextOfNode(currentText, moduleSpecifier); + } function emitImportOrExportSpecifier(node) { if (node.propertyName) { - writeTextOfNode(currentSourceFile, node.propertyName); + writeTextOfNode(currentText, node.propertyName); write(" as "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } function emitExportSpecifier(node) { emitImportOrExportSpecifier(node); @@ -24201,7 +24517,7 @@ var ts; } if (node.moduleSpecifier) { write(" from "); - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); } write(";"); writer.writeLine(); @@ -24215,11 +24531,11 @@ var ts; else { write("module "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); while (node.body.kind !== 219) { node = node.body; write("."); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; @@ -24238,7 +24554,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("type "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); emitTypeParameters(node.typeParameters); write(" = "); emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.type, getTypeAliasDeclarationVisibilityError); @@ -24260,7 +24576,7 @@ var ts; write("const "); } write("enum "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" {"); writeLine(); increaseIndent(); @@ -24271,7 +24587,7 @@ var ts; } function emitEnumMemberDeclaration(node) { emitJsDocComments(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var enumMemberValue = resolver.getConstantValue(node); if (enumMemberValue !== undefined) { write(" = "); @@ -24288,7 +24604,7 @@ var ts; increaseIndent(); emitJsDocComments(node); decreaseIndent(); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (node.constraint && !isPrivateMethodTypeParameter(node)) { write(" extends "); if (node.parent.kind === 152 || @@ -24398,7 +24714,7 @@ var ts; write("abstract "); } write("class "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -24421,7 +24737,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("interface "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -24451,7 +24767,7 @@ var ts; emitBindingPattern(node.name); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if ((node.kind === 141 || node.kind === 140) && ts.hasQuestionToken(node)) { write("?"); } @@ -24525,7 +24841,7 @@ var ts; emitBindingPattern(bindingElement.name); } else { - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); writeTypeOfDeclaration(bindingElement, undefined, getBindingElementTypeVisibilityError); } } @@ -24566,7 +24882,7 @@ var ts; emitJsDocComments(accessors.getAccessor); emitJsDocComments(accessors.setAccessor); emitClassMemberDeclarationFlags(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (!(node.flags & 16)) { accessorWithTypeAnnotation = node; var type = getTypeAnnotationFromAccessor(node); @@ -24647,13 +24963,13 @@ var ts; } if (node.kind === 213) { write("function "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } else if (node.kind === 144) { write("constructor"); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (ts.hasQuestionToken(node)) { write("?"); } @@ -24766,7 +25082,7 @@ var ts; emitBindingPattern(node.name); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } if (resolver.isOptionalParameter(node)) { write("?"); @@ -24865,7 +25181,7 @@ var ts; } else if (bindingElement.kind === 163) { if (bindingElement.propertyName) { - writeTextOfNode(currentSourceFile, bindingElement.propertyName); + writeTextOfNode(currentText, bindingElement.propertyName); write(": "); } if (bindingElement.name) { @@ -24877,7 +25193,7 @@ var ts; if (bindingElement.dotDotDotToken) { write("..."); } - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); } } } @@ -24960,6 +25276,18 @@ var ts; return ts.isExternalModule(sourceFile) || ts.isDeclarationFile(sourceFile); } ts.isExternalModuleOrDeclarationFile = isExternalModuleOrDeclarationFile; + function getResolvedExternalModuleName(host, file) { + return file.moduleName || ts.getExternalModuleNameFromPath(host, file.fileName); + } + ts.getResolvedExternalModuleName = getResolvedExternalModuleName; + function getExternalModuleNameFromDeclaration(host, resolver, declaration) { + var file = resolver.getExternalModuleFileFromDeclaration(declaration); + if (!file || ts.isDeclarationFile(file)) { + return undefined; + } + return getResolvedExternalModuleName(host, file); + } + ts.getExternalModuleNameFromDeclaration = getExternalModuleNameFromDeclaration; var entities = { "quot": 0x0022, "amp": 0x0026, @@ -25229,15 +25557,19 @@ var ts; var newLine = host.getNewLine(); var jsxDesugaring = host.getCompilerOptions().jsx !== 1; var shouldEmitJsx = function (s) { return (s.languageVariant === 1 && !jsxDesugaring); }; + var outFile = compilerOptions.outFile || compilerOptions.out; + var emitJavaScript = createFileEmitter(); if (targetSourceFile === undefined) { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { - var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); - emitFile(jsFilePath, sourceFile); - } - }); - if (compilerOptions.outFile || compilerOptions.out) { - emitFile(compilerOptions.outFile || compilerOptions.out); + if (outFile) { + emitFile(outFile); + } + else { + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { + var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); + emitFile(jsFilePath, sourceFile); + } + }); } } else { @@ -25245,8 +25577,8 @@ var ts; var jsFilePath = ts.getOwnEmitOutputFilePath(targetSourceFile, host, shouldEmitJsx(targetSourceFile) ? ".jsx" : ".js"); emitFile(jsFilePath, targetSourceFile); } - else if (!ts.isDeclarationFile(targetSourceFile) && (compilerOptions.outFile || compilerOptions.out)) { - emitFile(compilerOptions.outFile || compilerOptions.out); + else if (!ts.isDeclarationFile(targetSourceFile) && outFile) { + emitFile(outFile); } } diagnostics = ts.sortAndDeduplicateDiagnostics(diagnostics); @@ -25296,20 +25628,26 @@ var ts; } } } - function emitJavaScript(jsFilePath, root) { + function createFileEmitter() { var writer = ts.createTextWriter(newLine); var write = writer.write, writeTextOfNode = writer.writeTextOfNode, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; var currentSourceFile; + var currentText; + var currentLineMap; + var currentFileIdentifiers; + var renamedDependencies; + var isEs6Module; + var isCurrentFileExternalModule; var exportFunctionForFile; - var generatedNameSet = {}; - var nodeToGeneratedName = []; + var generatedNameSet; + var nodeToGeneratedName; var computedPropertyNamesToGeneratedNames; var convertedLoopState; - var extendsEmitted = false; - var decorateEmitted = false; - var paramEmitted = false; - var awaiterEmitted = false; - var tempFlags = 0; + var extendsEmitted; + var decorateEmitted; + var paramEmitted; + var awaiterEmitted; + var tempFlags; var tempVariables; var tempParameters; var externalImports; @@ -25326,6 +25664,7 @@ var ts; var scopeEmitStart = function (scopeDeclaration, scopeName) { }; var scopeEmitEnd = function () { }; var sourceMapData; + var root; var emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos) { } : emitLeadingCommentsOfPositionWorker; var moduleEmitDelegates = (_a = {}, _a[5] = emitES6Module, @@ -25335,30 +25674,75 @@ var ts; _a[1] = emitCommonJSModule, _a ); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - initializeEmitterWithSourceMaps(); - } - if (root) { - emitSourceFile(root); - } - else { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!isExternalModuleOrDeclarationFile(sourceFile)) { - emitSourceFile(sourceFile); + var bundleEmitDelegates = (_b = {}, + _b[5] = function () { }, + _b[2] = emitAMDModule, + _b[4] = emitSystemModule, + _b[3] = function () { }, + _b[1] = function () { }, + _b + ); + return doEmit; + function doEmit(jsFilePath, rootFile) { + writer.reset(); + currentSourceFile = undefined; + currentText = undefined; + currentLineMap = undefined; + exportFunctionForFile = undefined; + generatedNameSet = {}; + nodeToGeneratedName = []; + computedPropertyNamesToGeneratedNames = undefined; + convertedLoopState = undefined; + extendsEmitted = false; + decorateEmitted = false; + paramEmitted = false; + awaiterEmitted = false; + tempFlags = 0; + tempVariables = undefined; + tempParameters = undefined; + externalImports = undefined; + exportSpecifiers = undefined; + exportEquals = undefined; + hasExportStars = undefined; + detachedCommentsInfo = undefined; + sourceMapData = undefined; + isEs6Module = false; + renamedDependencies = undefined; + isCurrentFileExternalModule = false; + root = rootFile; + if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { + initializeEmitterWithSourceMaps(jsFilePath, root); + } + if (root) { + emitSourceFile(root); + } + else { + if (modulekind) { + ts.forEach(host.getSourceFiles(), emitEmitHelpers); } - }); + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if ((!isExternalModuleOrDeclarationFile(sourceFile)) || (modulekind && ts.isExternalModule(sourceFile))) { + emitSourceFile(sourceFile); + } + }); + } + writeLine(); + writeEmittedFiles(writer.getText(), jsFilePath, compilerOptions.emitBOM); } - writeLine(); - writeEmittedFiles(writer.getText(), compilerOptions.emitBOM); - return; function emitSourceFile(sourceFile) { currentSourceFile = sourceFile; + currentText = sourceFile.text; + currentLineMap = ts.getLineStarts(sourceFile); exportFunctionForFile = undefined; + isEs6Module = sourceFile.symbol && sourceFile.symbol.exports && !!sourceFile.symbol.exports["___esModule"]; + renamedDependencies = sourceFile.renamedDependencies; + currentFileIdentifiers = sourceFile.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(sourceFile); emit(sourceFile); } function isUniqueName(name) { return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentSourceFile.identifiers, name) && + !ts.hasProperty(currentFileIdentifiers, name) && !ts.hasProperty(generatedNameSet, name); } function makeTempVariableName(flags) { @@ -25431,7 +25815,7 @@ var ts; var id = ts.getNodeId(node); return nodeToGeneratedName[id] || (nodeToGeneratedName[id] = ts.unescapeIdentifier(generateNameForNode(node))); } - function initializeEmitterWithSourceMaps() { + function initializeEmitterWithSourceMaps(jsFilePath, root) { var sourceMapDir; var sourceMapSourceIndex = -1; var sourceMapNameIndexMap = {}; @@ -25500,7 +25884,7 @@ var ts; } } function recordSourceMapSpan(pos) { - var sourceLinePos = ts.getLineAndCharacterOfPosition(currentSourceFile, pos); + var sourceLinePos = ts.computeLineAndCharacterOfPosition(currentLineMap, pos); sourceLinePos.line++; sourceLinePos.character++; var emittedLine = writer.getLine(); @@ -25528,13 +25912,13 @@ var ts; } } function recordEmitNodeStartSpan(node) { - recordSourceMapSpan(ts.skipTrivia(currentSourceFile.text, node.pos)); + recordSourceMapSpan(ts.skipTrivia(currentText, node.pos)); } function recordEmitNodeEndSpan(node) { recordSourceMapSpan(node.end); } function writeTextWithSpanRecord(tokenKind, startPos, emitFn) { - var tokenStartPos = ts.skipTrivia(currentSourceFile.text, startPos); + var tokenStartPos = ts.skipTrivia(currentText, startPos); recordSourceMapSpan(tokenStartPos); var tokenEndPos = emitTokenText(tokenKind, tokenStartPos, emitFn); recordSourceMapSpan(tokenEndPos); @@ -25604,9 +25988,9 @@ var ts; sourceMapNameIndices.pop(); } ; - function writeCommentRangeWithMap(curentSourceFile, writer, comment, newLine) { + function writeCommentRangeWithMap(currentText, currentLineMap, writer, comment, newLine) { recordSourceMapSpan(comment.pos); - ts.writeCommentRange(currentSourceFile, writer, comment, newLine); + ts.writeCommentRange(currentText, currentLineMap, writer, comment, newLine); recordSourceMapSpan(comment.end); } function serializeSourceMapContents(version, file, sourceRoot, sources, names, mappings, sourcesContent) { @@ -25636,7 +26020,7 @@ var ts; return output; } } - function writeJavaScriptAndSourceMapFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptAndSourceMapFile(emitOutput, jsFilePath, writeByteOrderMark) { encodeLastRecordedSourceMapSpan(); var sourceMapText = serializeSourceMapContents(3, sourceMapData.sourceMapFile, sourceMapData.sourceMapSourceRoot, sourceMapData.sourceMapSources, sourceMapData.sourceMapNames, sourceMapData.sourceMapMappings, sourceMapData.sourceMapSourcesContent); sourceMapDataList.push(sourceMapData); @@ -25649,7 +26033,7 @@ var ts; ts.writeFile(host, diagnostics, sourceMapData.sourceMapFilePath, sourceMapText, false); sourceMapUrl = "//# sourceMappingURL=" + sourceMapData.jsSourceMappingURL; } - writeJavaScriptFile(emitOutput + sourceMapUrl, writeByteOrderMark); + writeJavaScriptFile(emitOutput + sourceMapUrl, jsFilePath, writeByteOrderMark); } var sourceMapJsFile = ts.getBaseFileName(ts.normalizeSlashes(jsFilePath)); sourceMapData = { @@ -25712,7 +26096,7 @@ var ts; scopeEmitEnd = recordScopeNameEnd; writeComment = writeCommentRangeWithMap; } - function writeJavaScriptFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptFile(emitOutput, jsFilePath, writeByteOrderMark) { ts.writeFile(host, diagnostics, jsFilePath, emitOutput, writeByteOrderMark); } function createTempVariable(flags) { @@ -25882,7 +26266,7 @@ var ts; return getQuotedEscapedLiteralText("\"", node.text, "\""); } if (node.parent) { - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + return ts.getTextOfNodeFromSourceText(currentText, node); } switch (node.kind) { case 9: @@ -25904,7 +26288,7 @@ var ts; return leftQuote + ts.escapeNonAsciiCharacters(ts.escapeString(text)) + rightQuote; } function emitDownlevelRawTemplateLiteral(node) { - var text = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + var text = ts.getTextOfNodeFromSourceText(currentText, node); var isLast = node.kind === 11 || node.kind === 14; text = text.substring(1, text.length - (isLast ? 1 : 2)); text = text.replace(/\r\n?/g, "\n"); @@ -26234,7 +26618,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } write("\""); } @@ -26330,7 +26714,7 @@ var ts; else if (declaration.kind === 226) { write(getGeneratedNameForNode(declaration.parent.parent.parent)); var name_24 = declaration.propertyName || declaration.name; - var identifier = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, name_24); + var identifier = ts.getTextOfNodeFromSourceText(currentText, name_24); if (languageVersion === 0 && identifier === "default") { write("[\"default\"]"); } @@ -26354,7 +26738,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function isNameOfNestedRedeclaration(node) { @@ -26391,7 +26775,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function emitThis(node) { @@ -26758,8 +27142,8 @@ var ts; return container && container.kind !== 248; } function emitShorthandPropertyAssignment(node) { - writeTextOfNode(currentSourceFile, node.name); - if (languageVersion < 2 || isNamespaceExportReference(node.name)) { + writeTextOfNode(currentText, node.name); + if (modulekind !== 5 || isNamespaceExportReference(node.name)) { write(": "); emit(node.name); } @@ -26809,10 +27193,10 @@ var ts; } emit(node.expression); var indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.dotToken); - var shouldEmitSpace; + var shouldEmitSpace = false; if (!indentedBeforeDot) { if (node.expression.kind === 8) { - var text = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node.expression); + var text = ts.getTextOfNodeFromSourceText(currentText, node.expression); shouldEmitSpace = text.indexOf(ts.tokenToString(21)) < 0; } else { @@ -27818,16 +28202,16 @@ var ts; emitToken(16, node.clauses.end); } function nodeStartPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node1.pos)) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node1.pos)) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function nodeEndPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, node2.end); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, node2.end); } function nodeEndIsOnSameLineAsNodeStart(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function emitCaseOrDefaultClause(node) { if (node.kind === 241) { @@ -27932,7 +28316,7 @@ var ts; if (node.parent.kind === 248) { ts.Debug.assert(!!(node.flags & 512) || node.kind === 227); if (modulekind === 1 || modulekind === 2 || modulekind === 3) { - if (!currentSourceFile.symbol.exports["___esModule"]) { + if (!isEs6Module) { if (languageVersion === 1) { write("Object.defineProperty(exports, \"__esModule\", { value: true });"); writeLine(); @@ -28095,12 +28479,18 @@ var ts; return node; } function createPropertyAccessForDestructuringProperty(object, propName) { - var syntheticName = ts.createSynthesizedNode(propName.kind); - syntheticName.text = propName.text; - if (syntheticName.kind !== 69) { - return createElementAccessExpression(object, syntheticName); + var index; + var nameIsComputed = propName.kind === 136; + if (nameIsComputed) { + index = ensureIdentifier(propName.expression, false); } - return createPropertyAccessExpression(object, syntheticName); + else { + index = ts.createSynthesizedNode(propName.kind); + index.text = propName.text; + } + return !nameIsComputed && index.kind === 69 + ? createPropertyAccessExpression(object, index) + : createElementAccessExpression(object, index); } function createSliceCall(value, sliceIndex) { var call = ts.createSynthesizedNode(168); @@ -28514,7 +28904,6 @@ var ts; var promiseConstructor = ts.getEntityNameFromTypeNode(node.type); var isArrowFunction = node.kind === 174; var hasLexicalArguments = (resolver.getNodeCheckFlags(node) & 4096) !== 0; - var args; if (!isArrowFunction) { write(" {"); increaseIndent(); @@ -29688,8 +30077,8 @@ var ts; } } function tryRenameExternalModule(moduleName) { - if (currentSourceFile.renamedDependencies && ts.hasProperty(currentSourceFile.renamedDependencies, moduleName.text)) { - return "\"" + currentSourceFile.renamedDependencies[moduleName.text] + "\""; + if (renamedDependencies && ts.hasProperty(renamedDependencies, moduleName.text)) { + return "\"" + renamedDependencies[moduleName.text] + "\""; } return undefined; } @@ -29829,7 +30218,7 @@ var ts; return; } if (resolver.isReferencedAliasDeclaration(node) || - (!ts.isExternalModule(currentSourceFile) && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { + (!isCurrentFileExternalModule && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { emitLeadingComments(node); emitStart(node); var variableDeclarationIsHoisted = shouldHoistVariable(node, true); @@ -30041,7 +30430,7 @@ var ts; function getLocalNameForExternalImport(node) { var namespaceDeclaration = getNamespaceDeclarationNode(node); if (namespaceDeclaration && !isDefaultImport(node)) { - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, namespaceDeclaration.name); + return ts.getTextOfNodeFromSourceText(currentText, namespaceDeclaration.name); } if (node.kind === 222 && node.importClause) { return getGeneratedNameForNode(node); @@ -30314,7 +30703,7 @@ var ts; ts.getEnclosingBlockScopeContainer(node).kind === 248; } function isCurrentFileSystemExternalModule() { - return modulekind === 4 && ts.isExternalModule(currentSourceFile); + return modulekind === 4 && isCurrentFileExternalModule; } function emitSystemModuleBody(node, dependencyGroups, startIndex) { emitVariableDeclarationsForImports(); @@ -30427,15 +30816,19 @@ var ts; writeLine(); write("}"); } - function emitSystemModule(node) { + function writeModuleName(node, emitRelativePathAsModuleName) { + var moduleName = node.moduleName; + if (moduleName || (emitRelativePathAsModuleName && (moduleName = getResolvedExternalModuleName(host, node)))) { + write("\"" + moduleName + "\", "); + } + } + function emitSystemModule(node, emitRelativePathAsModuleName) { collectExternalModuleInfo(node); ts.Debug.assert(!exportFunctionForFile); exportFunctionForFile = makeUniqueName("exports"); writeLine(); write("System.register("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } + writeModuleName(node, emitRelativePathAsModuleName); write("["); var groupIndices = {}; var dependencyGroups = []; @@ -30453,6 +30846,12 @@ var ts; if (i !== 0) { write(", "); } + if (emitRelativePathAsModuleName) { + var name_30 = getExternalModuleNameFromDeclaration(host, resolver, externalImports[i]); + if (name_30) { + text = "\"" + name_30 + "\""; + } + } write(text); } write("], function(" + exportFunctionForFile + ") {"); @@ -30466,7 +30865,7 @@ var ts; writeLine(); write("});"); } - function getAMDDependencyNames(node, includeNonAmdDependencies) { + function getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { var aliasedModuleNames = []; var unaliasedModuleNames = []; var importAliasNames = []; @@ -30483,6 +30882,12 @@ var ts; for (var _c = 0, externalImports_4 = externalImports; _c < externalImports_4.length; _c++) { var importNode = externalImports_4[_c]; var externalModuleName = getExternalModuleNameText(importNode); + if (emitRelativePathAsModuleName) { + var name_31 = getExternalModuleNameFromDeclaration(host, resolver, importNode); + if (name_31) { + externalModuleName = "\"" + name_31 + "\""; + } + } var importAliasName = getLocalNameForExternalImport(importNode); if (includeNonAmdDependencies && importAliasName) { aliasedModuleNames.push(externalModuleName); @@ -30494,8 +30899,8 @@ var ts; } return { aliasedModuleNames: aliasedModuleNames, unaliasedModuleNames: unaliasedModuleNames, importAliasNames: importAliasNames }; } - function emitAMDDependencies(node, includeNonAmdDependencies) { - var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies); + function emitAMDDependencies(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { + var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName); emitAMDDependencyList(dependencyNames); write(", "); emitAMDFactoryHeader(dependencyNames); @@ -30522,15 +30927,13 @@ var ts; } write(") {"); } - function emitAMDModule(node) { + function emitAMDModule(node, emitRelativePathAsModuleName) { emitEmitHelpers(node); collectExternalModuleInfo(node); writeLine(); write("define("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } - emitAMDDependencies(node, true); + writeModuleName(node, emitRelativePathAsModuleName); + emitAMDDependencies(node, true, emitRelativePathAsModuleName); increaseIndent(); var startIndex = emitDirectivePrologues(node.statements, true); emitExportStarHelper(); @@ -30736,8 +31139,13 @@ var ts; emitShebang(); emitDetachedCommentsAndUpdateCommentsInfo(node); if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { - var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1]; - emitModule(node); + if (root || (!ts.isExternalModule(node) && compilerOptions.isolatedModules)) { + var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1]; + emitModule(node); + } + else { + bundleEmitDelegates[modulekind](node, true); + } } else { var startIndex = emitDirectivePrologues(node.statements, false); @@ -30981,7 +31389,7 @@ var ts; return detachedCommentsInfo !== undefined && ts.lastOrUndefined(detachedCommentsInfo).nodePos === pos; } function getLeadingCommentsWithoutDetachedComments() { - var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); + var leadingComments = ts.getLeadingCommentRanges(currentText, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); if (detachedCommentsInfo.length - 1) { detachedCommentsInfo.pop(); } @@ -30991,10 +31399,10 @@ var ts; return leadingComments; } function isTripleSlashComment(comment) { - if (currentSourceFile.text.charCodeAt(comment.pos + 1) === 47 && + if (currentText.charCodeAt(comment.pos + 1) === 47 && comment.pos + 2 < comment.end && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 47) { - var textSubStr = currentSourceFile.text.substring(comment.pos, comment.end); + currentText.charCodeAt(comment.pos + 2) === 47) { + var textSubStr = currentText.substring(comment.pos, comment.end); return textSubStr.match(ts.fullTripleSlashReferencePathRegEx) || textSubStr.match(ts.fullTripleSlashAMDReferencePathRegEx) ? true : false; @@ -31008,7 +31416,7 @@ var ts; return getLeadingCommentsWithoutDetachedComments(); } else { - return ts.getLeadingCommentRangesOfNode(node, currentSourceFile); + return ts.getLeadingCommentRangesOfNodeFromText(node, currentText); } } } @@ -31016,7 +31424,7 @@ var ts; function getTrailingCommentsToEmit(node) { if (node.parent) { if (node.parent.kind === 248 || node.end !== node.parent.end) { - return ts.getTrailingCommentRanges(currentSourceFile.text, node.end); + return ts.getTrailingCommentRanges(currentText, node.end); } } } @@ -31039,22 +31447,22 @@ var ts; leadingComments = ts.filter(getLeadingCommentsToEmit(node), isTripleSlashComment); } } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); - ts.emitComments(currentSourceFile, writer, leadingComments, true, newLine, writeComment); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, node, leadingComments); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, true, newLine, writeComment); } function emitTrailingComments(node) { if (compilerOptions.removeComments) { return; } var trailingComments = getTrailingCommentsToEmit(node); - ts.emitComments(currentSourceFile, writer, trailingComments, false, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, false, newLine, writeComment); } function emitTrailingCommentsOfPosition(pos) { if (compilerOptions.removeComments) { return; } - var trailingComments = ts.getTrailingCommentRanges(currentSourceFile.text, pos); - ts.emitComments(currentSourceFile, writer, trailingComments, true, newLine, writeComment); + var trailingComments = ts.getTrailingCommentRanges(currentText, pos); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, true, newLine, writeComment); } function emitLeadingCommentsOfPositionWorker(pos) { if (compilerOptions.removeComments) { @@ -31065,13 +31473,13 @@ var ts; leadingComments = getLeadingCommentsWithoutDetachedComments(); } else { - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, pos); + leadingComments = ts.getLeadingCommentRanges(currentText, pos); } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, { pos: pos, end: pos }, leadingComments); - ts.emitComments(currentSourceFile, writer, leadingComments, true, newLine, writeComment); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, { pos: pos, end: pos }, leadingComments); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, true, newLine, writeComment); } function emitDetachedCommentsAndUpdateCommentsInfo(node) { - var currentDetachedCommentInfo = ts.emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, compilerOptions.removeComments); + var currentDetachedCommentInfo = ts.emitDetachedComments(currentText, currentLineMap, writer, writeComment, node, newLine, compilerOptions.removeComments); if (currentDetachedCommentInfo) { if (detachedCommentsInfo) { detachedCommentsInfo.push(currentDetachedCommentInfo); @@ -31082,12 +31490,12 @@ var ts; } } function emitShebang() { - var shebang = ts.getShebang(currentSourceFile.text); + var shebang = ts.getShebang(currentText); if (shebang) { write(shebang); } } - var _a; + var _a, _b; } function emitFile(jsFilePath, sourceFile) { emitJavaScript(jsFilePath, sourceFile); @@ -31143,11 +31551,11 @@ var ts; if (ts.getRootLength(moduleName) !== 0 || nameStartsWithDotSlashOrDotDotSlash(moduleName)) { var failedLookupLocations = []; var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var resolvedFileName = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var resolvedFileName = loadNodeModuleFromFile(ts.supportedJsExtensions, candidate, failedLookupLocations, host); if (resolvedFileName) { return { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations }; } - resolvedFileName = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + resolvedFileName = loadNodeModuleFromDirectory(ts.supportedJsExtensions, candidate, failedLookupLocations, host); return resolvedFileName ? { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations } : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; @@ -31157,8 +31565,8 @@ var ts; } } ts.nodeModuleNameResolver = nodeModuleNameResolver; - function loadNodeModuleFromFile(candidate, failedLookupLocation, host) { - return ts.forEach(ts.moduleFileExtensions, tryLoad); + function loadNodeModuleFromFile(extensions, candidate, failedLookupLocation, host) { + return ts.forEach(extensions, tryLoad); function tryLoad(ext) { var fileName = ts.fileExtensionIs(candidate, ext) ? candidate : candidate + ext; if (host.fileExists(fileName)) { @@ -31170,7 +31578,7 @@ var ts; } } } - function loadNodeModuleFromDirectory(candidate, failedLookupLocation, host) { + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, host) { var packageJsonPath = ts.combinePaths(candidate, "package.json"); if (host.fileExists(packageJsonPath)) { var jsonContent; @@ -31182,7 +31590,7 @@ var ts; jsonContent = { typings: undefined }; } if (jsonContent.typings) { - var result = loadNodeModuleFromFile(ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); + var result = loadNodeModuleFromFile(extensions, ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); if (result) { return result; } @@ -31191,7 +31599,7 @@ var ts; else { failedLookupLocation.push(packageJsonPath); } - return loadNodeModuleFromFile(ts.combinePaths(candidate, "index"), failedLookupLocation, host); + return loadNodeModuleFromFile(extensions, ts.combinePaths(candidate, "index"), failedLookupLocation, host); } function loadModuleFromNodeModules(moduleName, directory, host) { var failedLookupLocations = []; @@ -31201,11 +31609,11 @@ var ts; if (baseName !== "node_modules") { var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var result = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var result = loadNodeModuleFromFile(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } - result = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + result = loadNodeModuleFromDirectory(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } @@ -31230,9 +31638,10 @@ var ts; var searchName; var failedLookupLocations = []; var referencedSourceFile; + var extensions = compilerOptions.allowNonTsExtensions ? ts.supportedJsExtensions : ts.supportedExtensions; while (true) { searchName = ts.normalizePath(ts.combinePaths(searchPath, moduleName)); - referencedSourceFile = ts.forEach(ts.supportedExtensions, function (extension) { + referencedSourceFile = ts.forEach(extensions, function (extension) { if (extension === ".tsx" && !compilerOptions.jsx) { return undefined; } @@ -31260,10 +31669,8 @@ var ts; ts.classicNameResolver = classicNameResolver; ts.defaultInitCompilerOptions = { module: 1, - target: 0, + target: 1, noImplicitAny: false, - outDir: "built", - rootDir: ".", sourceMap: false }; function createCompilerHost(options, setParentNodes) { @@ -31621,35 +32028,47 @@ var ts; if (file.imports) { return; } + var isJavaScriptFile = ts.isSourceFileJavaScript(file); var imports; for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { var node = _a[_i]; - collect(node, true); + collect(node, true, false); } file.imports = imports || emptyArray; - function collect(node, allowRelativeModuleNames) { - switch (node.kind) { - case 222: - case 221: - case 228: - var moduleNameExpr = ts.getExternalModuleName(node); - if (!moduleNameExpr || moduleNameExpr.kind !== 9) { + return; + function collect(node, allowRelativeModuleNames, collectOnlyRequireCalls) { + if (!collectOnlyRequireCalls) { + switch (node.kind) { + case 222: + case 221: + case 228: + var moduleNameExpr = ts.getExternalModuleName(node); + if (!moduleNameExpr || moduleNameExpr.kind !== 9) { + break; + } + if (!moduleNameExpr.text) { + break; + } + if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { + (imports || (imports = [])).push(moduleNameExpr); + } break; - } - if (!moduleNameExpr.text) { + case 218: + if (node.name.kind === 9 && (node.flags & 4 || ts.isDeclarationFile(file))) { + ts.forEachChild(node.body, function (node) { + collect(node, false, collectOnlyRequireCalls); + }); + } break; - } - if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { - (imports || (imports = [])).push(moduleNameExpr); - } - break; - case 218: - if (node.name.kind === 9 && (node.flags & 4 || ts.isDeclarationFile(file))) { - ts.forEachChild(node.body, function (node) { - collect(node, false); - }); - } - break; + } + } + if (isJavaScriptFile) { + if (ts.isRequireCall(node)) { + (imports || (imports = [])).push(node.arguments[0]); + } + else { + ts.forEachChild(node, function (node) { return collect(node, allowRelativeModuleNames, true); }); + } } } } @@ -31736,7 +32155,6 @@ var ts; } processImportedModules(file, basePath); if (isDefaultLib) { - file.isDefaultLib = true; files.unshift(file); } else { @@ -31809,6 +32227,9 @@ var ts; commonPathComponents.length = sourcePathComponents.length; } }); + if (!commonPathComponents) { + return currentDirectory; + } return ts.getNormalizedPathFromPathComponents(commonPathComponents); } function checkSourceFilesBelongToPath(sourceFiles, rootDirectory) { @@ -31891,10 +32312,12 @@ var ts; if (options.module === 5 && languageVersion < 2) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_compile_modules_into_es2015_when_targeting_ES5_or_lower)); } + if (outFile && options.module && !(options.module === 2 || options.module === 4)) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile")); + } if (options.outDir || options.sourceRoot || - (options.mapRoot && - (!outFile || firstExternalModuleSourceFile !== undefined))) { + options.mapRoot) { if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { commonSourceDirectory = ts.getNormalizedAbsolutePath(options.rootDir, currentDirectory); } @@ -32448,10 +32871,10 @@ var ts; ts.forEach(program.getSourceFiles(), function (sourceFile) { cancellationToken.throwIfCancellationRequested(); var nameToDeclarations = sourceFile.getNamedDeclarations(); - for (var name_30 in nameToDeclarations) { - var declarations = ts.getProperty(nameToDeclarations, name_30); + for (var name_32 in nameToDeclarations) { + var declarations = ts.getProperty(nameToDeclarations, name_32); if (declarations) { - var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_30); + var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_32); if (!matches) { continue; } @@ -32462,14 +32885,14 @@ var ts; if (!containers) { return undefined; } - matches = patternMatcher.getMatches(containers, name_30); + matches = patternMatcher.getMatches(containers, name_32); if (!matches) { continue; } } var fileName = sourceFile.fileName; var matchKind = bestMatchKind(matches); - rawItems.push({ name: name_30, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); + rawItems.push({ name: name_32, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); } } } @@ -32797,9 +33220,9 @@ var ts; case 211: case 163: var variableDeclarationNode; - var name_31; + var name_33; if (node.kind === 163) { - name_31 = node.name; + name_33 = node.name; variableDeclarationNode = node; while (variableDeclarationNode && variableDeclarationNode.kind !== 211) { variableDeclarationNode = variableDeclarationNode.parent; @@ -32809,16 +33232,16 @@ var ts; else { ts.Debug.assert(!ts.isBindingPattern(node.name)); variableDeclarationNode = node; - name_31 = node.name; + name_33 = node.name; } if (ts.isConst(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.constElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.constElement); } else if (ts.isLet(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.letElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.letElement); } else { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.variableElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.variableElement); } case 144: return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); @@ -33425,7 +33848,7 @@ var ts; var resolvedSignature = typeChecker.getResolvedSignature(call, candidates); cancellationToken.throwIfCancellationRequested(); if (!candidates.length) { - if (ts.isJavaScript(sourceFile.fileName)) { + if (ts.isSourceFileJavaScript(sourceFile)) { return createJavaScriptSignatureHelpItems(argumentInfo); } return undefined; @@ -34941,9 +35364,9 @@ var ts; } Rules.prototype.getRuleName = function (rule) { var o = this; - for (var name_32 in o) { - if (o[name_32] === rule) { - return name_32; + for (var name_34 in o) { + if (o[name_34] === rule) { + return name_34; } } throw new Error("Unknown rule"); @@ -35318,7 +35741,7 @@ var ts; function TokenRangeAccess(from, to, except) { this.tokens = []; for (var token = from; token <= to; token++) { - if (except.indexOf(token) < 0) { + if (ts.indexOf(except, token) < 0) { this.tokens.push(token); } } @@ -36706,13 +37129,18 @@ var ts; ]; var jsDocCompletionEntries; function createNode(kind, pos, end, flags, parent) { - var node = new (ts.getNodeConstructor(kind))(pos, end); + var node = new NodeObject(kind, pos, end); node.flags = flags; node.parent = parent; return node; } var NodeObject = (function () { - function NodeObject() { + function NodeObject(kind, pos, end) { + this.kind = kind; + this.pos = pos; + this.end = end; + this.flags = 0; + this.parent = undefined; } NodeObject.prototype.getSourceFile = function () { return ts.getSourceFileOfNode(this); @@ -37151,8 +37579,8 @@ var ts; })(); var SourceFileObject = (function (_super) { __extends(SourceFileObject, _super); - function SourceFileObject() { - _super.apply(this, arguments); + function SourceFileObject(kind, pos, end) { + _super.call(this, kind, pos, end); } SourceFileObject.prototype.update = function (newText, textChangeRange) { return ts.updateSourceFile(this, newText, textChangeRange); @@ -37421,6 +37849,9 @@ var ts; ClassificationTypeNames.typeAliasName = "type alias name"; ClassificationTypeNames.parameterName = "parameter name"; ClassificationTypeNames.docCommentTagName = "doc comment tag name"; + ClassificationTypeNames.jsxOpenTagName = "jsx open tag name"; + ClassificationTypeNames.jsxCloseTagName = "jsx close tag name"; + ClassificationTypeNames.jsxSelfClosingTagName = "jsx self closing tag name"; return ClassificationTypeNames; })(); ts.ClassificationTypeNames = ClassificationTypeNames; @@ -37740,8 +38171,9 @@ var ts; }; } ts.createDocumentRegistry = createDocumentRegistry; - function preProcessFile(sourceText, readImportFiles) { + function preProcessFile(sourceText, readImportFiles, detectJavaScriptImports) { if (readImportFiles === void 0) { readImportFiles = true; } + if (detectJavaScriptImports === void 0) { detectJavaScriptImports = false; } var referencedFiles = []; var importedFiles = []; var ambientExternalModules; @@ -37775,93 +38207,53 @@ var ts; end: pos + importPath.length }); } - function processImport() { - scanner.setText(sourceText); - var token = scanner.scan(); - while (token !== 1) { - if (token === 122) { - token = scanner.scan(); - if (token === 125) { - token = scanner.scan(); - if (token === 9) { - recordAmbientExternalModule(); - continue; - } - } - } - else if (token === 89) { + function tryConsumeDeclare() { + var token = scanner.getToken(); + if (token === 122) { + token = scanner.scan(); + if (token === 125) { token = scanner.scan(); if (token === 9) { - recordModuleName(); - continue; - } - else { - if (token === 69 || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 133) { - token = scanner.scan(); - if (token === 9) { - recordModuleName(); - continue; - } - } - else if (token === 56) { - token = scanner.scan(); - if (token === 127) { - token = scanner.scan(); - if (token === 17) { - token = scanner.scan(); - if (token === 9) { - recordModuleName(); - continue; - } - } - } - } - else if (token === 24) { - token = scanner.scan(); - } - else { - continue; - } - } - if (token === 15) { - token = scanner.scan(); - while (token !== 16) { - token = scanner.scan(); - } - if (token === 16) { - token = scanner.scan(); - if (token === 133) { - token = scanner.scan(); - if (token === 9) { - recordModuleName(); - } - } - } - } - else if (token === 37) { - token = scanner.scan(); - if (token === 116) { - token = scanner.scan(); - if (token === 69 || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 133) { - token = scanner.scan(); - if (token === 9) { - recordModuleName(); - } - } - } - } - } + recordAmbientExternalModule(); } } - else if (token === 82) { - token = scanner.scan(); + return true; + } + return false; + } + function tryConsumeImport() { + var token = scanner.getToken(); + if (token === 89) { + token = scanner.scan(); + if (token === 9) { + recordModuleName(); + return true; + } + else { + if (token === 69 || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 133) { + token = scanner.scan(); + if (token === 9) { + recordModuleName(); + return true; + } + } + else if (token === 56) { + if (tryConsumeRequireCall(true)) { + return true; + } + } + else if (token === 24) { + token = scanner.scan(); + } + else { + return true; + } + } if (token === 15) { token = scanner.scan(); - while (token !== 16) { + while (token !== 16 && token !== 1) { token = scanner.scan(); } if (token === 16) { @@ -37875,6 +38267,35 @@ var ts; } } else if (token === 37) { + token = scanner.scan(); + if (token === 116) { + token = scanner.scan(); + if (token === 69 || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 133) { + token = scanner.scan(); + if (token === 9) { + recordModuleName(); + } + } + } + } + } + } + return true; + } + return false; + } + function tryConsumeExport() { + var token = scanner.getToken(); + if (token === 82) { + token = scanner.scan(); + if (token === 15) { + token = scanner.scan(); + while (token !== 16 && token !== 1) { + token = scanner.scan(); + } + if (token === 16) { token = scanner.scan(); if (token === 133) { token = scanner.scan(); @@ -37883,31 +38304,99 @@ var ts; } } } - else if (token === 89) { + } + else if (token === 37) { + token = scanner.scan(); + if (token === 133) { token = scanner.scan(); - if (token === 69 || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 56) { - token = scanner.scan(); - if (token === 127) { - token = scanner.scan(); - if (token === 17) { - token = scanner.scan(); - if (token === 9) { - recordModuleName(); - } - } - } + if (token === 9) { + recordModuleName(); + } + } + } + else if (token === 89) { + token = scanner.scan(); + if (token === 69 || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 56) { + if (tryConsumeRequireCall(true)) { + return true; } } } } + return true; + } + return false; + } + function tryConsumeRequireCall(skipCurrentToken) { + var token = skipCurrentToken ? scanner.scan() : scanner.getToken(); + if (token === 127) { token = scanner.scan(); + if (token === 17) { + token = scanner.scan(); + if (token === 9) { + recordModuleName(); + } + } + return true; + } + return false; + } + function tryConsumeDefine() { + var token = scanner.getToken(); + if (token === 69 && scanner.getTokenValue() === "define") { + token = scanner.scan(); + if (token !== 17) { + return true; + } + token = scanner.scan(); + if (token === 9) { + token = scanner.scan(); + if (token === 24) { + token = scanner.scan(); + } + else { + return true; + } + } + if (token !== 19) { + return true; + } + token = scanner.scan(); + var i = 0; + while (token !== 20 && token !== 1) { + if (token === 9) { + recordModuleName(); + i++; + } + token = scanner.scan(); + } + return true; + } + return false; + } + function processImports() { + scanner.setText(sourceText); + scanner.scan(); + while (true) { + if (scanner.getToken() === 1) { + break; + } + if (tryConsumeDeclare() || + tryConsumeImport() || + tryConsumeExport() || + (detectJavaScriptImports && (tryConsumeRequireCall(false) || tryConsumeDefine()))) { + continue; + } + else { + scanner.scan(); + } } scanner.setText(undefined); } if (readImportFiles) { - processImport(); + processImports(); } processTripleSlashDirectives(); return { referencedFiles: referencedFiles, importedFiles: importedFiles, isLibFile: isNoDefaultLib, ambientExternalModules: ambientExternalModules }; @@ -38250,7 +38739,7 @@ var ts; function getSemanticDiagnostics(fileName) { synchronizeHostData(); var targetSourceFile = getValidSourceFile(fileName); - if (ts.isJavaScript(fileName)) { + if (ts.isSourceFileJavaScript(targetSourceFile)) { return getJavaScriptSemanticDiagnostics(targetSourceFile); } var semanticDiagnostics = program.getSemanticDiagnostics(targetSourceFile, cancellationToken); @@ -38442,7 +38931,7 @@ var ts; var typeChecker = program.getTypeChecker(); var syntacticStart = new Date().getTime(); var sourceFile = getValidSourceFile(fileName); - var isJavaScriptFile = ts.isJavaScript(fileName); + var isJavaScriptFile = ts.isSourceFileJavaScript(sourceFile); var isJsDocTagName = false; var start = new Date().getTime(); var currentToken = ts.getTokenAtPosition(sourceFile, position); @@ -38953,8 +39442,8 @@ var ts; if (element.getStart() <= position && position <= element.getEnd()) { continue; } - var name_33 = element.propertyName || element.name; - exisingImportsOrExports[name_33.text] = true; + var name_35 = element.propertyName || element.name; + exisingImportsOrExports[name_35.text] = true; } if (ts.isEmpty(exisingImportsOrExports)) { return exportsOfModule; @@ -38978,7 +39467,9 @@ var ts; } var existingName = void 0; if (m.kind === 163 && m.propertyName) { - existingName = m.propertyName.text; + if (m.propertyName.kind === 69) { + existingName = m.propertyName.text; + } } else { existingName = m.name.text; @@ -39008,44 +39499,41 @@ var ts; return undefined; } var symbols = completionData.symbols, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isRightOfDot = completionData.isRightOfDot, isJsDocTagName = completionData.isJsDocTagName; - var entries; if (isJsDocTagName) { return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: getAllJsDocCompletionEntries() }; } - if (isRightOfDot && ts.isJavaScript(fileName)) { - entries = getCompletionEntriesFromSymbols(symbols); - ts.addRange(entries, getJavaScriptCompletionEntries()); + var sourceFile = getValidSourceFile(fileName); + var entries = []; + if (isRightOfDot && ts.isSourceFileJavaScript(sourceFile)) { + var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries); + ts.addRange(entries, getJavaScriptCompletionEntries(sourceFile, uniqueNames)); } else { if (!symbols || symbols.length === 0) { return undefined; } - entries = getCompletionEntriesFromSymbols(symbols); + getCompletionEntriesFromSymbols(symbols, entries); } if (!isMemberCompletion && !isJsDocTagName) { ts.addRange(entries, keywordCompletions); } return { isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries: entries }; - function getJavaScriptCompletionEntries() { + function getJavaScriptCompletionEntries(sourceFile, uniqueNames) { var entries = []; - var allNames = {}; var target = program.getCompilerOptions().target; - for (var _i = 0, _a = program.getSourceFiles(); _i < _a.length; _i++) { - var sourceFile = _a[_i]; - var nameTable = getNameTable(sourceFile); - for (var name_34 in nameTable) { - if (!allNames[name_34]) { - allNames[name_34] = name_34; - var displayName = getCompletionEntryDisplayName(name_34, target, true); - if (displayName) { - var entry = { - name: displayName, - kind: ScriptElementKind.warning, - kindModifiers: "", - sortText: "1" - }; - entries.push(entry); - } + var nameTable = getNameTable(sourceFile); + for (var name_36 in nameTable) { + if (!uniqueNames[name_36]) { + uniqueNames[name_36] = name_36; + var displayName = getCompletionEntryDisplayName(name_36, target, true); + if (displayName) { + var entry = { + name: displayName, + kind: ScriptElementKind.warning, + kindModifiers: "", + sortText: "1" + }; + entries.push(entry); } } } @@ -39073,25 +39561,24 @@ var ts; sortText: "0" }; } - function getCompletionEntriesFromSymbols(symbols) { + function getCompletionEntriesFromSymbols(symbols, entries) { var start = new Date().getTime(); - var entries = []; + var uniqueNames = {}; if (symbols) { - var nameToSymbol = {}; for (var _i = 0, symbols_3 = symbols; _i < symbols_3.length; _i++) { var symbol = symbols_3[_i]; var entry = createCompletionEntry(symbol, location); if (entry) { var id = ts.escapeIdentifier(entry.name); - if (!ts.lookUp(nameToSymbol, id)) { + if (!ts.lookUp(uniqueNames, id)) { entries.push(entry); - nameToSymbol[id] = symbol; + uniqueNames[id] = id; } } } } log("getCompletionsAtPosition: getCompletionEntriesFromSymbols: " + (new Date().getTime() - start)); - return entries; + return uniqueNames; } } function getCompletionEntryDetails(fileName, position, entryName) { @@ -39272,16 +39759,16 @@ var ts; case ScriptElementKind.letElement: case ScriptElementKind.parameterElement: case ScriptElementKind.localVariableElement: - displayParts.push(ts.punctuationPart(54)); + displayParts.push(ts.punctuationPart(ts.SyntaxKind.ColonToken)); displayParts.push(ts.spacePart()); if (useConstructSignatures) { - displayParts.push(ts.keywordPart(92)); + displayParts.push(ts.keywordPart(ts.SyntaxKind.NewKeyword)); displayParts.push(ts.spacePart()); } - if (!(type.flags & 65536)) { - ts.addRange(displayParts, ts.symbolToDisplayParts(typeChecker, type.symbol, enclosingDeclaration, undefined, 1)); + if (!(type.flags & ts.TypeFlags.Anonymous)) { + ts.addRange(displayParts, ts.symbolToDisplayParts(typeChecker, type.symbol, enclosingDeclaration, undefined, ts.SymbolFormatFlags.WriteTypeParametersOrArguments)); } - addSignatureDisplayParts(signature, allSignatures, 8); + addSignatureDisplayParts(signature, allSignatures, ts.TypeFormatFlags.WriteArrowStyleSignature); break; default: addSignatureDisplayParts(signature, allSignatures); @@ -40728,17 +41215,17 @@ var ts; if (isNameOfPropertyAssignment(node)) { var objectLiteral = node.parent.parent; var contextualType = typeChecker.getContextualType(objectLiteral); - var name_35 = node.text; + var name_37 = node.text; if (contextualType) { if (contextualType.flags & 16384) { - var unionProperty = contextualType.getProperty(name_35); + var unionProperty = contextualType.getProperty(name_37); if (unionProperty) { return [unionProperty]; } else { var result_4 = []; ts.forEach(contextualType.types, function (t) { - var symbol = t.getProperty(name_35); + var symbol = t.getProperty(name_37); if (symbol) { result_4.push(symbol); } @@ -40747,7 +41234,7 @@ var ts; } } else { - var symbol_1 = contextualType.getProperty(name_35); + var symbol_1 = contextualType.getProperty(name_37); if (symbol_1) { return [symbol_1]; } @@ -41105,6 +41592,9 @@ var ts; case 16: return ClassificationTypeNames.typeAliasName; case 17: return ClassificationTypeNames.parameterName; case 18: return ClassificationTypeNames.docCommentTagName; + case 19: return ClassificationTypeNames.jsxOpenTagName; + case 20: return ClassificationTypeNames.jsxCloseTagName; + case 21: return ClassificationTypeNames.jsxSelfClosingTagName; } } function convertClassifications(classifications) { @@ -41344,6 +41834,21 @@ var ts; return 17; } return; + case 235: + if (token.parent.tagName === token) { + return 19; + } + return; + case 237: + if (token.parent.tagName === token) { + return 20; + } + return; + case 234: + if (token.parent.tagName === token) { + return 21; + } + return; } } return 2; @@ -42053,18 +42558,8 @@ var ts; ts.getDefaultLibFilePath = getDefaultLibFilePath; function initializeServices() { ts.objectAllocator = { - getNodeConstructor: function (kind) { - function Node(pos, end) { - this.pos = pos; - this.end = end; - this.flags = 0; - this.parent = undefined; - } - var proto = kind === 248 ? new SourceFileObject() : new NodeObject(); - proto.kind = kind; - Node.prototype = proto; - return Node; - }, + getNodeConstructor: function () { return NodeObject; }, + getSourceFileConstructor: function () { return SourceFileObject; }, getSymbolConstructor: function () { return SymbolObject; }, getTypeConstructor: function () { return TypeObject; }, getSignatureConstructor: function () { return SignatureObject; } @@ -42959,8 +43454,8 @@ var ts; }; Session.prototype.getDiagnosticsForProject = function (delay, fileName) { var _this = this; - var _a = this.getProjectInfo(fileName, true), configFileName = _a.configFileName, fileNamesInProject = _a.fileNames; - fileNamesInProject = fileNamesInProject.filter(function (value, index, array) { return value.indexOf("lib.d.ts") < 0; }); + var _a = this.getProjectInfo(fileName, true), configFileName = _a.configFileName, fileNames = _a.fileNames; + var fileNamesInProject = fileNames.filter(function (value, index, array) { return value.indexOf("lib.d.ts") < 0; }); var highPriorityFiles = []; var mediumPriorityFiles = []; var lowPriorityFiles = []; @@ -43329,6 +43824,9 @@ var ts; this.filenameToSourceFile = {}; this.updateGraphSeq = 0; this.openRefCount = 0; + if (projectOptions && projectOptions.files) { + projectOptions.compilerOptions.allowNonTsExtensions = true; + } this.compilerService = new CompilerService(this, projectOptions && projectOptions.compilerOptions); } Project.prototype.addOpenRef = function () { @@ -43399,6 +43897,7 @@ var ts; Project.prototype.setProjectOptions = function (projectOptions) { this.projectOptions = projectOptions; if (projectOptions.compilerOptions) { + projectOptions.compilerOptions.allowNonTsExtensions = true; this.compilerService.setCompilerOptions(projectOptions.compilerOptions); } }; @@ -43824,7 +44323,6 @@ var ts; } } if (content !== undefined) { - var indentSize; info = new ScriptInfo(this.host, fileName, content, openedByClient); info.setFormatOptions(this.getFormatCodeOptions()); this.filenameToScriptInfo[fileName] = info; @@ -44081,7 +44579,9 @@ var ts; this.setCompilerOptions(opt); } else { - this.setCompilerOptions(ts.getDefaultCompilerOptions()); + var defaultOpts = ts.getDefaultCompilerOptions(); + defaultOpts.allowNonTsExtensions = true; + this.setCompilerOptions(defaultOpts); } this.languageService = ts.createLanguageService(this.host, this.documentRegistry); this.classifier = ts.createClassifier(); @@ -45635,7 +46135,7 @@ var ts; }; CoreServicesShimObject.prototype.getPreProcessedFileInfo = function (fileName, sourceTextSnapshot) { return this.forwardJSONCall("getPreProcessedFileInfo('" + fileName + "')", function () { - var result = ts.preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength())); + var result = ts.preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), true, true); var convertResult = { referencedFiles: [], importedFiles: [], @@ -45696,7 +46196,7 @@ var ts; TypeScriptServicesFactory.prototype.createLanguageServiceShim = function (host) { try { if (this.documentRegistry === undefined) { - this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames()); + this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(), host.getCurrentDirectory()); } var hostAdapter = new LanguageServiceShimHostAdapter(host); var languageService = ts.createLanguageService(hostAdapter, this.documentRegistry); @@ -45728,7 +46228,7 @@ var ts; }; TypeScriptServicesFactory.prototype.close = function () { this._shims = []; - this.documentRegistry = ts.createDocumentRegistry(); + this.documentRegistry = undefined; }; TypeScriptServicesFactory.prototype.registerShim = function (shim) { this._shims.push(shim); diff --git a/lib/typescript.d.ts b/lib/typescript.d.ts index 972ebfa472d..32a6dee4623 100644 --- a/lib/typescript.d.ts +++ b/lib/typescript.d.ts @@ -387,6 +387,7 @@ declare namespace ts { right: Identifier; } type EntityName = Identifier | QualifiedName; + type PropertyName = Identifier | LiteralExpression | ComputedPropertyName; type DeclarationName = Identifier | LiteralExpression | ComputedPropertyName | BindingPattern; interface Declaration extends Node { _declarationBrand: any; @@ -425,7 +426,7 @@ declare namespace ts { initializer?: Expression; } interface BindingElement extends Declaration { - propertyName?: Identifier; + propertyName?: PropertyName; dotDotDotToken?: Node; name: Identifier | BindingPattern; initializer?: Expression; @@ -452,7 +453,7 @@ declare namespace ts { objectAssignmentInitializer?: Expression; } interface VariableLikeDeclaration extends Declaration { - propertyName?: Identifier; + propertyName?: PropertyName; dotDotDotToken?: Node; name: DeclarationName; questionToken?: Node; @@ -581,7 +582,7 @@ declare namespace ts { asteriskToken?: Node; expression?: Expression; } - interface BinaryExpression extends Expression { + interface BinaryExpression extends Expression, Declaration { left: Expression; operatorToken: Node; right: Expression; @@ -625,7 +626,7 @@ declare namespace ts { interface ObjectLiteralExpression extends PrimaryExpression, Declaration { properties: NodeArray; } - interface PropertyAccessExpression extends MemberExpression { + interface PropertyAccessExpression extends MemberExpression, Declaration { expression: LeftHandSideExpression; dotToken: Node; name: Identifier; @@ -1220,6 +1221,7 @@ declare namespace ts { ObjectLiteral = 524288, ESSymbol = 16777216, ThisType = 33554432, + ObjectLiteralPatternWithComputedProperties = 67108864, StringLike = 258, NumberLike = 132, ObjectType = 80896, @@ -1537,7 +1539,6 @@ declare namespace ts { function getTypeParameterOwner(d: Declaration): Declaration; } declare namespace ts { - function getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node; function createNode(kind: SyntaxKind, pos?: number, end?: number): Node; function forEachChild(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T; function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile; @@ -2126,6 +2127,9 @@ declare namespace ts { static typeAliasName: string; static parameterName: string; static docCommentTagName: string; + static jsxOpenTagName: string; + static jsxCloseTagName: string; + static jsxSelfClosingTagName: string; } enum ClassificationType { comment = 1, @@ -2146,6 +2150,9 @@ declare namespace ts { typeAliasName = 16, parameterName = 17, docCommentTagName = 18, + jsxOpenTagName = 19, + jsxCloseTagName = 20, + jsxSelfClosingTagName = 21, } interface DisplayPartsSymbolWriter extends SymbolWriter { displayParts(): SymbolDisplayPart[]; @@ -2171,7 +2178,7 @@ declare namespace ts { function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; function createGetCanonicalFileName(useCaseSensitivefileNames: boolean): (fileName: string) => string; function createDocumentRegistry(useCaseSensitiveFileNames?: boolean, currentDirectory?: string): DocumentRegistry; - function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo; + function preProcessFile(sourceText: string, readImportFiles?: boolean, detectJavaScriptImports?: boolean): PreProcessedFileInfo; function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService; function createClassifier(): Classifier; /** diff --git a/lib/typescript.js b/lib/typescript.js index 8b0ef04f96e..498ddc37860 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -622,6 +622,7 @@ var ts; TypeFlags[TypeFlags["ContainsAnyFunctionType"] = 8388608] = "ContainsAnyFunctionType"; TypeFlags[TypeFlags["ESSymbol"] = 16777216] = "ESSymbol"; TypeFlags[TypeFlags["ThisType"] = 33554432] = "ThisType"; + TypeFlags[TypeFlags["ObjectLiteralPatternWithComputedProperties"] = 67108864] = "ObjectLiteralPatternWithComputedProperties"; /* @internal */ TypeFlags[TypeFlags["Intrinsic"] = 16777343] = "Intrinsic"; /* @internal */ @@ -1530,12 +1531,7 @@ var ts; * List of supported extensions in order of file resolution precedence. */ ts.supportedExtensions = [".ts", ".tsx", ".d.ts"]; - /** - * List of extensions that will be used to look for external modules. - * This list is kept separate from supportedExtensions to for cases when we'll allow to include .js files in compilation, - * but still would like to load only TypeScript files as modules - */ - ts.moduleFileExtensions = ts.supportedExtensions; + ts.supportedJsExtensions = ts.supportedExtensions.concat(".js", ".jsx"); function isSupportedSourceFileName(fileName) { if (!fileName) { return false; @@ -1586,17 +1582,16 @@ var ts; } function Signature(checker) { } + function Node(kind, pos, end) { + this.kind = kind; + this.pos = pos; + this.end = end; + this.flags = 0 /* None */; + this.parent = undefined; + } ts.objectAllocator = { - getNodeConstructor: function (kind) { - function Node(pos, end) { - this.pos = pos; - this.end = end; - this.flags = 0 /* None */; - this.parent = undefined; - } - Node.prototype = { kind: kind }; - return Node; - }, + getNodeConstructor: function () { return Node; }, + getSourceFileConstructor: function () { return Node; }, getSymbolConstructor: function () { return Symbol; }, getTypeConstructor: function () { return Type; }, getSignatureConstructor: function () { return Signature; } @@ -1913,7 +1908,16 @@ var ts; if (writeByteOrderMark) { data = "\uFEFF" + data; } - _fs.writeFileSync(fileName, data, "utf8"); + var fd; + try { + fd = _fs.openSync(fileName, "w"); + _fs.writeSync(fd, data, undefined, "utf8"); + } + finally { + if (fd !== undefined) { + _fs.closeSync(fd); + } + } } function getCanonicalPath(path) { return useCaseSensitiveFileNames ? path.toLowerCase() : path; @@ -2614,6 +2618,7 @@ var ts; Disallow_inconsistently_cased_references_to_the_same_file: { code: 6078, category: ts.DiagnosticCategory.Message, key: "Disallow_inconsistently_cased_references_to_the_same_file_6078", message: "Disallow inconsistently-cased references to the same file." }, Specify_JSX_code_generation_Colon_preserve_or_react: { code: 6080, category: ts.DiagnosticCategory.Message, key: "Specify_JSX_code_generation_Colon_preserve_or_react_6080", message: "Specify JSX code generation: 'preserve' or 'react'" }, Argument_for_jsx_must_be_preserve_or_react: { code: 6081, category: ts.DiagnosticCategory.Message, key: "Argument_for_jsx_must_be_preserve_or_react_6081", message: "Argument for '--jsx' must be 'preserve' or 'react'." }, + Only_amd_and_system_modules_are_supported_alongside_0: { code: 6082, category: ts.DiagnosticCategory.Error, key: "Only_amd_and_system_modules_are_supported_alongside_0_6082", message: "Only 'amd' and 'system' modules are supported alongside --{0}." }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -3173,7 +3178,7 @@ var ts; function getCommentRanges(text, pos, trailing) { var result; var collecting = trailing || pos === 0; - while (true) { + while (pos < text.length) { var ch = text.charCodeAt(pos); switch (ch) { case 13 /* carriageReturn */: @@ -3242,6 +3247,7 @@ var ts; } return result; } + return result; } function getLeadingCommentRanges(text, pos) { return getCommentRanges(text, pos, /*trailing*/ false); @@ -3343,7 +3349,7 @@ var ts; error(ts.Diagnostics.Digit_expected); } } - return +(text.substring(start, end)); + return "" + +(text.substring(start, end)); } function scanOctalDigits() { var start = pos; @@ -3770,7 +3776,7 @@ var ts; return pos++, token = 36 /* MinusToken */; case 46 /* dot */: if (isDigit(text.charCodeAt(pos + 1))) { - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8 /* NumericLiteral */; } if (text.charCodeAt(pos + 1) === 46 /* dot */ && text.charCodeAt(pos + 2) === 46 /* dot */) { @@ -3873,7 +3879,7 @@ var ts; case 55 /* _7 */: case 56 /* _8 */: case 57 /* _9 */: - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8 /* NumericLiteral */; case 58 /* colon */: return pos++, token = 54 /* ColonToken */; @@ -4177,1321 +4183,6 @@ var ts; } ts.createScanner = createScanner; })(ts || (ts = {})); -/// -/* @internal */ -var ts; -(function (ts) { - ts.bindTime = 0; - (function (ModuleInstanceState) { - ModuleInstanceState[ModuleInstanceState["NonInstantiated"] = 0] = "NonInstantiated"; - ModuleInstanceState[ModuleInstanceState["Instantiated"] = 1] = "Instantiated"; - ModuleInstanceState[ModuleInstanceState["ConstEnumOnly"] = 2] = "ConstEnumOnly"; - })(ts.ModuleInstanceState || (ts.ModuleInstanceState = {})); - var ModuleInstanceState = ts.ModuleInstanceState; - var Reachability; - (function (Reachability) { - Reachability[Reachability["Unintialized"] = 1] = "Unintialized"; - Reachability[Reachability["Reachable"] = 2] = "Reachable"; - Reachability[Reachability["Unreachable"] = 4] = "Unreachable"; - Reachability[Reachability["ReportedUnreachable"] = 8] = "ReportedUnreachable"; - })(Reachability || (Reachability = {})); - function or(state1, state2) { - return (state1 | state2) & 2 /* Reachable */ - ? 2 /* Reachable */ - : (state1 & state2) & 8 /* ReportedUnreachable */ - ? 8 /* ReportedUnreachable */ - : 4 /* Unreachable */; - } - function getModuleInstanceState(node) { - // A module is uninstantiated if it contains only - // 1. interface declarations, type alias declarations - if (node.kind === 215 /* InterfaceDeclaration */ || node.kind === 216 /* TypeAliasDeclaration */) { - return 0 /* NonInstantiated */; - } - else if (ts.isConstEnumDeclaration(node)) { - return 2 /* ConstEnumOnly */; - } - else if ((node.kind === 222 /* ImportDeclaration */ || node.kind === 221 /* ImportEqualsDeclaration */) && !(node.flags & 2 /* Export */)) { - return 0 /* NonInstantiated */; - } - else if (node.kind === 219 /* ModuleBlock */) { - var state = 0 /* NonInstantiated */; - ts.forEachChild(node, function (n) { - switch (getModuleInstanceState(n)) { - case 0 /* NonInstantiated */: - // child is non-instantiated - continue searching - return false; - case 2 /* ConstEnumOnly */: - // child is const enum only - record state and continue searching - state = 2 /* ConstEnumOnly */; - return false; - case 1 /* Instantiated */: - // child is instantiated - record state and stop - state = 1 /* Instantiated */; - return true; - } - }); - return state; - } - else if (node.kind === 218 /* ModuleDeclaration */) { - return getModuleInstanceState(node.body); - } - else { - return 1 /* Instantiated */; - } - } - ts.getModuleInstanceState = getModuleInstanceState; - var ContainerFlags; - (function (ContainerFlags) { - // The current node is not a container, and no container manipulation should happen before - // recursing into it. - ContainerFlags[ContainerFlags["None"] = 0] = "None"; - // The current node is a container. It should be set as the current container (and block- - // container) before recursing into it. The current node does not have locals. Examples: - // - // Classes, ObjectLiterals, TypeLiterals, Interfaces... - ContainerFlags[ContainerFlags["IsContainer"] = 1] = "IsContainer"; - // The current node is a block-scoped-container. It should be set as the current block- - // container before recursing into it. Examples: - // - // Blocks (when not parented by functions), Catch clauses, For/For-in/For-of statements... - ContainerFlags[ContainerFlags["IsBlockScopedContainer"] = 2] = "IsBlockScopedContainer"; - ContainerFlags[ContainerFlags["HasLocals"] = 4] = "HasLocals"; - // If the current node is a container that also container that also contains locals. Examples: - // - // Functions, Methods, Modules, Source-files. - ContainerFlags[ContainerFlags["IsContainerWithLocals"] = 5] = "IsContainerWithLocals"; - })(ContainerFlags || (ContainerFlags = {})); - var binder = createBinder(); - function bindSourceFile(file, options) { - var start = new Date().getTime(); - binder(file, options); - ts.bindTime += new Date().getTime() - start; - } - ts.bindSourceFile = bindSourceFile; - function createBinder() { - var file; - var options; - var parent; - var container; - var blockScopeContainer; - var lastContainer; - var seenThisKeyword; - // state used by reachability checks - var hasExplicitReturn; - var currentReachabilityState; - var labelStack; - var labelIndexMap; - var implicitLabels; - // If this file is an external module, then it is automatically in strict-mode according to - // ES6. If it is not an external module, then we'll determine if it is in strict mode or - // not depending on if we see "use strict" in certain places (or if we hit a class/namespace). - var inStrictMode; - var symbolCount = 0; - var Symbol; - var classifiableNames; - function bindSourceFile(f, opts) { - file = f; - options = opts; - inStrictMode = !!file.externalModuleIndicator; - classifiableNames = {}; - Symbol = ts.objectAllocator.getSymbolConstructor(); - if (!file.locals) { - bind(file); - file.symbolCount = symbolCount; - file.classifiableNames = classifiableNames; - } - parent = undefined; - container = undefined; - blockScopeContainer = undefined; - lastContainer = undefined; - seenThisKeyword = false; - hasExplicitReturn = false; - labelStack = undefined; - labelIndexMap = undefined; - implicitLabels = undefined; - } - return bindSourceFile; - function createSymbol(flags, name) { - symbolCount++; - return new Symbol(flags, name); - } - function addDeclarationToSymbol(symbol, node, symbolFlags) { - symbol.flags |= symbolFlags; - node.symbol = symbol; - if (!symbol.declarations) { - symbol.declarations = []; - } - symbol.declarations.push(node); - if (symbolFlags & 1952 /* HasExports */ && !symbol.exports) { - symbol.exports = {}; - } - if (symbolFlags & 6240 /* HasMembers */ && !symbol.members) { - symbol.members = {}; - } - if (symbolFlags & 107455 /* Value */ && !symbol.valueDeclaration) { - symbol.valueDeclaration = node; - } - } - // Should not be called on a declaration with a computed property name, - // unless it is a well known Symbol. - function getDeclarationName(node) { - if (node.name) { - if (node.kind === 218 /* ModuleDeclaration */ && node.name.kind === 9 /* StringLiteral */) { - return "\"" + node.name.text + "\""; - } - if (node.name.kind === 136 /* ComputedPropertyName */) { - var nameExpression = node.name.expression; - ts.Debug.assert(ts.isWellKnownSymbolSyntactically(nameExpression)); - return ts.getPropertyNameForKnownSymbolName(nameExpression.name.text); - } - return node.name.text; - } - switch (node.kind) { - case 144 /* Constructor */: - return "__constructor"; - case 152 /* FunctionType */: - case 147 /* CallSignature */: - return "__call"; - case 153 /* ConstructorType */: - case 148 /* ConstructSignature */: - return "__new"; - case 149 /* IndexSignature */: - return "__index"; - case 228 /* ExportDeclaration */: - return "__export"; - case 227 /* ExportAssignment */: - return node.isExportEquals ? "export=" : "default"; - case 213 /* FunctionDeclaration */: - case 214 /* ClassDeclaration */: - return node.flags & 512 /* Default */ ? "default" : undefined; - } - } - function getDisplayName(node) { - return node.name ? ts.declarationNameToString(node.name) : getDeclarationName(node); - } - /** - * Declares a Symbol for the node and adds it to symbols. Reports errors for conflicting identifier names. - * @param symbolTable - The symbol table which node will be added to. - * @param parent - node's parent declaration. - * @param node - The declaration to be added to the symbol table - * @param includes - The SymbolFlags that node has in addition to its declaration type (eg: export, ambient, etc.) - * @param excludes - The flags which node cannot be declared alongside in a symbol table. Used to report forbidden declarations. - */ - function declareSymbol(symbolTable, parent, node, includes, excludes) { - ts.Debug.assert(!ts.hasDynamicName(node)); - var isDefaultExport = node.flags & 512 /* Default */; - // The exported symbol for an export default function/class node is always named "default" - var name = isDefaultExport && parent ? "default" : getDeclarationName(node); - var symbol; - if (name !== undefined) { - // Check and see if the symbol table already has a symbol with this name. If not, - // create a new symbol with this name and add it to the table. Note that we don't - // give the new symbol any flags *yet*. This ensures that it will not conflict - // with the 'excludes' flags we pass in. - // - // If we do get an existing symbol, see if it conflicts with the new symbol we're - // creating. For example, a 'var' symbol and a 'class' symbol will conflict within - // the same symbol table. If we have a conflict, report the issue on each - // declaration we have for this symbol, and then create a new symbol for this - // declaration. - // - // If we created a new symbol, either because we didn't have a symbol with this name - // in the symbol table, or we conflicted with an existing symbol, then just add this - // node as the sole declaration of the new symbol. - // - // Otherwise, we'll be merging into a compatible existing symbol (for example when - // you have multiple 'vars' with the same name in the same container). In this case - // just add this node into the declarations list of the symbol. - symbol = ts.hasProperty(symbolTable, name) - ? symbolTable[name] - : (symbolTable[name] = createSymbol(0 /* None */, name)); - if (name && (includes & 788448 /* Classifiable */)) { - classifiableNames[name] = name; - } - if (symbol.flags & excludes) { - if (node.name) { - node.name.parent = node; - } - // Report errors every position with duplicate declaration - // Report errors on previous encountered declarations - var message = symbol.flags & 2 /* BlockScopedVariable */ - ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 - : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (declaration.flags & 512 /* Default */) { - message = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; - } - }); - ts.forEach(symbol.declarations, function (declaration) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message, getDisplayName(declaration))); - }); - file.bindDiagnostics.push(ts.createDiagnosticForNode(node.name || node, message, getDisplayName(node))); - symbol = createSymbol(0 /* None */, name); - } - } - else { - symbol = createSymbol(0 /* None */, "__missing"); - } - addDeclarationToSymbol(symbol, node, includes); - symbol.parent = parent; - return symbol; - } - function declareModuleMember(node, symbolFlags, symbolExcludes) { - var hasExportModifier = ts.getCombinedNodeFlags(node) & 2 /* Export */; - if (symbolFlags & 8388608 /* Alias */) { - if (node.kind === 230 /* ExportSpecifier */ || (node.kind === 221 /* ImportEqualsDeclaration */ && hasExportModifier)) { - return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - } - else { - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - else { - // Exported module members are given 2 symbols: A local symbol that is classified with an ExportValue, - // ExportType, or ExportContainer flag, and an associated export symbol with all the correct flags set - // on it. There are 2 main reasons: - // - // 1. We treat locals and exports of the same name as mutually exclusive within a container. - // That means the binder will issue a Duplicate Identifier error if you mix locals and exports - // with the same name in the same container. - // TODO: Make this a more specific error and decouple it from the exclusion logic. - // 2. When we checkIdentifier in the checker, we set its resolved symbol to the local symbol, - // but return the export symbol (by calling getExportSymbolOfValueSymbolIfExported). That way - // when the emitter comes back to it, it knows not to qualify the name if it was found in a containing scope. - if (hasExportModifier || container.flags & 131072 /* ExportContext */) { - var exportKind = (symbolFlags & 107455 /* Value */ ? 1048576 /* ExportValue */ : 0) | - (symbolFlags & 793056 /* Type */ ? 2097152 /* ExportType */ : 0) | - (symbolFlags & 1536 /* Namespace */ ? 4194304 /* ExportNamespace */ : 0); - var local = declareSymbol(container.locals, undefined, node, exportKind, symbolExcludes); - local.exportSymbol = declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - node.localSymbol = local; - return local; - } - else { - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - } - // All container nodes are kept on a linked list in declaration order. This list is used by - // the getLocalNameOfContainer function in the type checker to validate that the local name - // used for a container is unique. - function bindChildren(node) { - // Before we recurse into a node's chilren, we first save the existing parent, container - // and block-container. Then after we pop out of processing the children, we restore - // these saved values. - var saveParent = parent; - var saveContainer = container; - var savedBlockScopeContainer = blockScopeContainer; - // This node will now be set as the parent of all of its children as we recurse into them. - parent = node; - // Depending on what kind of node this is, we may have to adjust the current container - // and block-container. If the current node is a container, then it is automatically - // considered the current block-container as well. Also, for containers that we know - // may contain locals, we proactively initialize the .locals field. We do this because - // it's highly likely that the .locals will be needed to place some child in (for example, - // a parameter, or variable declaration). - // - // However, we do not proactively create the .locals for block-containers because it's - // totally normal and common for block-containers to never actually have a block-scoped - // variable in them. We don't want to end up allocating an object for every 'block' we - // run into when most of them won't be necessary. - // - // Finally, if this is a block-container, then we clear out any existing .locals object - // it may contain within it. This happens in incremental scenarios. Because we can be - // reusing a node from a previous compilation, that node may have had 'locals' created - // for it. We must clear this so we don't accidently move any stale data forward from - // a previous compilation. - var containerFlags = getContainerFlags(node); - if (containerFlags & 1 /* IsContainer */) { - container = blockScopeContainer = node; - if (containerFlags & 4 /* HasLocals */) { - container.locals = {}; - } - addToContainerChain(container); - } - else if (containerFlags & 2 /* IsBlockScopedContainer */) { - blockScopeContainer = node; - blockScopeContainer.locals = undefined; - } - var savedReachabilityState; - var savedLabelStack; - var savedLabels; - var savedImplicitLabels; - var savedHasExplicitReturn; - var kind = node.kind; - var flags = node.flags; - // reset all reachability check related flags on node (for incremental scenarios) - flags &= ~1572864 /* ReachabilityCheckFlags */; - if (kind === 215 /* InterfaceDeclaration */) { - seenThisKeyword = false; - } - var saveState = kind === 248 /* SourceFile */ || kind === 219 /* ModuleBlock */ || ts.isFunctionLikeKind(kind); - if (saveState) { - savedReachabilityState = currentReachabilityState; - savedLabelStack = labelStack; - savedLabels = labelIndexMap; - savedImplicitLabels = implicitLabels; - savedHasExplicitReturn = hasExplicitReturn; - currentReachabilityState = 2 /* Reachable */; - hasExplicitReturn = false; - labelStack = labelIndexMap = implicitLabels = undefined; - } - bindReachableStatement(node); - if (currentReachabilityState === 2 /* Reachable */ && ts.isFunctionLikeKind(kind) && ts.nodeIsPresent(node.body)) { - flags |= 524288 /* HasImplicitReturn */; - if (hasExplicitReturn) { - flags |= 1048576 /* HasExplicitReturn */; - } - } - if (kind === 215 /* InterfaceDeclaration */) { - flags = seenThisKeyword ? flags | 262144 /* ContainsThis */ : flags & ~262144 /* ContainsThis */; - } - node.flags = flags; - if (saveState) { - hasExplicitReturn = savedHasExplicitReturn; - currentReachabilityState = savedReachabilityState; - labelStack = savedLabelStack; - labelIndexMap = savedLabels; - implicitLabels = savedImplicitLabels; - } - container = saveContainer; - parent = saveParent; - blockScopeContainer = savedBlockScopeContainer; - } - /** - * Returns true if node and its subnodes were successfully traversed. - * Returning false means that node was not examined and caller needs to dive into the node himself. - */ - function bindReachableStatement(node) { - if (checkUnreachable(node)) { - ts.forEachChild(node, bind); - return; - } - switch (node.kind) { - case 198 /* WhileStatement */: - bindWhileStatement(node); - break; - case 197 /* DoStatement */: - bindDoStatement(node); - break; - case 199 /* ForStatement */: - bindForStatement(node); - break; - case 200 /* ForInStatement */: - case 201 /* ForOfStatement */: - bindForInOrForOfStatement(node); - break; - case 196 /* IfStatement */: - bindIfStatement(node); - break; - case 204 /* ReturnStatement */: - case 208 /* ThrowStatement */: - bindReturnOrThrow(node); - break; - case 203 /* BreakStatement */: - case 202 /* ContinueStatement */: - bindBreakOrContinueStatement(node); - break; - case 209 /* TryStatement */: - bindTryStatement(node); - break; - case 206 /* SwitchStatement */: - bindSwitchStatement(node); - break; - case 220 /* CaseBlock */: - bindCaseBlock(node); - break; - case 207 /* LabeledStatement */: - bindLabeledStatement(node); - break; - default: - ts.forEachChild(node, bind); - break; - } - } - function bindWhileStatement(n) { - var preWhileState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - var postWhileState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - // bind expressions (don't affect reachability) - bind(n.expression); - currentReachabilityState = preWhileState; - var postWhileLabel = pushImplicitLabel(); - bind(n.statement); - popImplicitLabel(postWhileLabel, postWhileState); - } - function bindDoStatement(n) { - var preDoState = currentReachabilityState; - var postDoLabel = pushImplicitLabel(); - bind(n.statement); - var postDoState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : preDoState; - popImplicitLabel(postDoLabel, postDoState); - // bind expressions (don't affect reachability) - bind(n.expression); - } - function bindForStatement(n) { - var preForState = currentReachabilityState; - var postForLabel = pushImplicitLabel(); - // bind expressions (don't affect reachability) - bind(n.initializer); - bind(n.condition); - bind(n.incrementor); - bind(n.statement); - // for statement is considered infinite when it condition is either omitted or is true keyword - // - for(..;;..) - // - for(..;true;..) - var isInfiniteLoop = (!n.condition || n.condition.kind === 99 /* TrueKeyword */); - var postForState = isInfiniteLoop ? 4 /* Unreachable */ : preForState; - popImplicitLabel(postForLabel, postForState); - } - function bindForInOrForOfStatement(n) { - var preStatementState = currentReachabilityState; - var postStatementLabel = pushImplicitLabel(); - // bind expressions (don't affect reachability) - bind(n.initializer); - bind(n.expression); - bind(n.statement); - popImplicitLabel(postStatementLabel, preStatementState); - } - function bindIfStatement(n) { - // denotes reachability state when entering 'thenStatement' part of the if statement: - // i.e. if condition is false then thenStatement is unreachable - var ifTrueState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - // denotes reachability state when entering 'elseStatement': - // i.e. if condition is true then elseStatement is unreachable - var ifFalseState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - currentReachabilityState = ifTrueState; - // bind expression (don't affect reachability) - bind(n.expression); - bind(n.thenStatement); - if (n.elseStatement) { - var preElseState = currentReachabilityState; - currentReachabilityState = ifFalseState; - bind(n.elseStatement); - currentReachabilityState = or(currentReachabilityState, preElseState); - } - else { - currentReachabilityState = or(currentReachabilityState, ifFalseState); - } - } - function bindReturnOrThrow(n) { - // bind expression (don't affect reachability) - bind(n.expression); - if (n.kind === 204 /* ReturnStatement */) { - hasExplicitReturn = true; - } - currentReachabilityState = 4 /* Unreachable */; - } - function bindBreakOrContinueStatement(n) { - // call bind on label (don't affect reachability) - bind(n.label); - // for continue case touch label so it will be marked a used - var isValidJump = jumpToLabel(n.label, n.kind === 203 /* BreakStatement */ ? currentReachabilityState : 4 /* Unreachable */); - if (isValidJump) { - currentReachabilityState = 4 /* Unreachable */; - } - } - function bindTryStatement(n) { - // catch\finally blocks has the same reachability as try block - var preTryState = currentReachabilityState; - bind(n.tryBlock); - var postTryState = currentReachabilityState; - currentReachabilityState = preTryState; - bind(n.catchClause); - var postCatchState = currentReachabilityState; - currentReachabilityState = preTryState; - bind(n.finallyBlock); - // post catch/finally state is reachable if - // - post try state is reachable - control flow can fall out of try block - // - post catch state is reachable - control flow can fall out of catch block - currentReachabilityState = or(postTryState, postCatchState); - } - function bindSwitchStatement(n) { - var preSwitchState = currentReachabilityState; - var postSwitchLabel = pushImplicitLabel(); - // bind expression (don't affect reachability) - bind(n.expression); - bind(n.caseBlock); - var hasDefault = ts.forEach(n.caseBlock.clauses, function (c) { return c.kind === 242 /* DefaultClause */; }); - // post switch state is unreachable if switch is exaustive (has a default case ) and does not have fallthrough from the last case - var postSwitchState = hasDefault && currentReachabilityState !== 2 /* Reachable */ ? 4 /* Unreachable */ : preSwitchState; - popImplicitLabel(postSwitchLabel, postSwitchState); - } - function bindCaseBlock(n) { - var startState = currentReachabilityState; - for (var _i = 0, _a = n.clauses; _i < _a.length; _i++) { - var clause = _a[_i]; - currentReachabilityState = startState; - bind(clause); - if (clause.statements.length && currentReachabilityState === 2 /* Reachable */ && options.noFallthroughCasesInSwitch) { - errorOnFirstToken(clause, ts.Diagnostics.Fallthrough_case_in_switch); - } - } - } - function bindLabeledStatement(n) { - // call bind on label (don't affect reachability) - bind(n.label); - var ok = pushNamedLabel(n.label); - bind(n.statement); - if (ok) { - popNamedLabel(n.label, currentReachabilityState); - } - } - function getContainerFlags(node) { - switch (node.kind) { - case 186 /* ClassExpression */: - case 214 /* ClassDeclaration */: - case 215 /* InterfaceDeclaration */: - case 217 /* EnumDeclaration */: - case 155 /* TypeLiteral */: - case 165 /* ObjectLiteralExpression */: - return 1 /* IsContainer */; - case 147 /* CallSignature */: - case 148 /* ConstructSignature */: - case 149 /* IndexSignature */: - case 143 /* MethodDeclaration */: - case 142 /* MethodSignature */: - case 213 /* FunctionDeclaration */: - case 144 /* Constructor */: - case 145 /* GetAccessor */: - case 146 /* SetAccessor */: - case 152 /* FunctionType */: - case 153 /* ConstructorType */: - case 173 /* FunctionExpression */: - case 174 /* ArrowFunction */: - case 218 /* ModuleDeclaration */: - case 248 /* SourceFile */: - case 216 /* TypeAliasDeclaration */: - return 5 /* IsContainerWithLocals */; - case 244 /* CatchClause */: - case 199 /* ForStatement */: - case 200 /* ForInStatement */: - case 201 /* ForOfStatement */: - case 220 /* CaseBlock */: - return 2 /* IsBlockScopedContainer */; - case 192 /* Block */: - // do not treat blocks directly inside a function as a block-scoped-container. - // Locals that reside in this block should go to the function locals. Othewise 'x' - // would not appear to be a redeclaration of a block scoped local in the following - // example: - // - // function foo() { - // var x; - // let x; - // } - // - // If we placed 'var x' into the function locals and 'let x' into the locals of - // the block, then there would be no collision. - // - // By not creating a new block-scoped-container here, we ensure that both 'var x' - // and 'let x' go into the Function-container's locals, and we do get a collision - // conflict. - return ts.isFunctionLike(node.parent) ? 0 /* None */ : 2 /* IsBlockScopedContainer */; - } - return 0 /* None */; - } - function addToContainerChain(next) { - if (lastContainer) { - lastContainer.nextContainer = next; - } - lastContainer = next; - } - function declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes) { - // Just call this directly so that the return type of this function stays "void". - declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes); - } - function declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes) { - switch (container.kind) { - // Modules, source files, and classes need specialized handling for how their - // members are declared (for example, a member of a class will go into a specific - // symbol table depending on if it is static or not). We defer to specialized - // handlers to take care of declaring these child members. - case 218 /* ModuleDeclaration */: - return declareModuleMember(node, symbolFlags, symbolExcludes); - case 248 /* SourceFile */: - return declareSourceFileMember(node, symbolFlags, symbolExcludes); - case 186 /* ClassExpression */: - case 214 /* ClassDeclaration */: - return declareClassMember(node, symbolFlags, symbolExcludes); - case 217 /* EnumDeclaration */: - return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - case 155 /* TypeLiteral */: - case 165 /* ObjectLiteralExpression */: - case 215 /* InterfaceDeclaration */: - // Interface/Object-types always have their children added to the 'members' of - // their container. They are only accessible through an instance of their - // container, and are never in scope otherwise (even inside the body of the - // object / type / interface declaring them). An exception is type parameters, - // which are in scope without qualification (similar to 'locals'). - return declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); - case 152 /* FunctionType */: - case 153 /* ConstructorType */: - case 147 /* CallSignature */: - case 148 /* ConstructSignature */: - case 149 /* IndexSignature */: - case 143 /* MethodDeclaration */: - case 142 /* MethodSignature */: - case 144 /* Constructor */: - case 145 /* GetAccessor */: - case 146 /* SetAccessor */: - case 213 /* FunctionDeclaration */: - case 173 /* FunctionExpression */: - case 174 /* ArrowFunction */: - case 216 /* TypeAliasDeclaration */: - // All the children of these container types are never visible through another - // symbol (i.e. through another symbol's 'exports' or 'members'). Instead, - // they're only accessed 'lexically' (i.e. from code that exists underneath - // their container in the tree. To accomplish this, we simply add their declared - // symbol to the 'locals' of the container. These symbols can then be found as - // the type checker walks up the containers, checking them for matching names. - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - function declareClassMember(node, symbolFlags, symbolExcludes) { - return node.flags & 64 /* Static */ - ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) - : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); - } - function declareSourceFileMember(node, symbolFlags, symbolExcludes) { - return ts.isExternalModule(file) - ? declareModuleMember(node, symbolFlags, symbolExcludes) - : declareSymbol(file.locals, undefined, node, symbolFlags, symbolExcludes); - } - function hasExportDeclarations(node) { - var body = node.kind === 248 /* SourceFile */ ? node : node.body; - if (body.kind === 248 /* SourceFile */ || body.kind === 219 /* ModuleBlock */) { - for (var _i = 0, _a = body.statements; _i < _a.length; _i++) { - var stat = _a[_i]; - if (stat.kind === 228 /* ExportDeclaration */ || stat.kind === 227 /* ExportAssignment */) { - return true; - } - } - } - return false; - } - function setExportContextFlag(node) { - // A declaration source file or ambient module declaration that contains no export declarations (but possibly regular - // declarations with export modifiers) is an export context in which declarations are implicitly exported. - if (ts.isInAmbientContext(node) && !hasExportDeclarations(node)) { - node.flags |= 131072 /* ExportContext */; - } - else { - node.flags &= ~131072 /* ExportContext */; - } - } - function bindModuleDeclaration(node) { - setExportContextFlag(node); - if (node.name.kind === 9 /* StringLiteral */) { - declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); - } - else { - var state = getModuleInstanceState(node); - if (state === 0 /* NonInstantiated */) { - declareSymbolAndAddToSymbolTable(node, 1024 /* NamespaceModule */, 0 /* NamespaceModuleExcludes */); - } - else { - declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); - if (node.symbol.flags & (16 /* Function */ | 32 /* Class */ | 256 /* RegularEnum */)) { - // if module was already merged with some function, class or non-const enum - // treat is a non-const-enum-only - node.symbol.constEnumOnlyModule = false; - } - else { - var currentModuleIsConstEnumOnly = state === 2 /* ConstEnumOnly */; - if (node.symbol.constEnumOnlyModule === undefined) { - // non-merged case - use the current state - node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly; - } - else { - // merged case: module is const enum only if all its pieces are non-instantiated or const enum - node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly; - } - } - } - } - } - function bindFunctionOrConstructorType(node) { - // For a given function symbol "<...>(...) => T" we want to generate a symbol identical - // to the one we would get for: { <...>(...): T } - // - // We do that by making an anonymous type literal symbol, and then setting the function - // symbol as its sole member. To the rest of the system, this symbol will be indistinguishable - // from an actual type literal symbol you would have gotten had you used the long form. - var symbol = createSymbol(131072 /* Signature */, getDeclarationName(node)); - addDeclarationToSymbol(symbol, node, 131072 /* Signature */); - var typeLiteralSymbol = createSymbol(2048 /* TypeLiteral */, "__type"); - addDeclarationToSymbol(typeLiteralSymbol, node, 2048 /* TypeLiteral */); - typeLiteralSymbol.members = (_a = {}, _a[symbol.name] = symbol, _a); - var _a; - } - function bindObjectLiteralExpression(node) { - var ElementKind; - (function (ElementKind) { - ElementKind[ElementKind["Property"] = 1] = "Property"; - ElementKind[ElementKind["Accessor"] = 2] = "Accessor"; - })(ElementKind || (ElementKind = {})); - if (inStrictMode) { - var seen = {}; - for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { - var prop = _a[_i]; - if (prop.name.kind !== 69 /* Identifier */) { - continue; - } - var identifier = prop.name; - // ECMA-262 11.1.5 Object Initialiser - // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true - // a.This production is contained in strict code and IsDataDescriptor(previous) is true and - // IsDataDescriptor(propId.descriptor) is true. - // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. - // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. - // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true - // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields - var currentKind = prop.kind === 245 /* PropertyAssignment */ || prop.kind === 246 /* ShorthandPropertyAssignment */ || prop.kind === 143 /* MethodDeclaration */ - ? 1 /* Property */ - : 2 /* Accessor */; - var existingKind = seen[identifier.text]; - if (!existingKind) { - seen[identifier.text] = currentKind; - continue; - } - if (currentKind === 1 /* Property */ && existingKind === 1 /* Property */) { - var span = ts.getErrorSpanForNode(file, identifier); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode)); - } - } - } - return bindAnonymousDeclaration(node, 4096 /* ObjectLiteral */, "__object"); - } - function bindAnonymousDeclaration(node, symbolFlags, name) { - var symbol = createSymbol(symbolFlags, name); - addDeclarationToSymbol(symbol, node, symbolFlags); - } - function bindBlockScopedDeclaration(node, symbolFlags, symbolExcludes) { - switch (blockScopeContainer.kind) { - case 218 /* ModuleDeclaration */: - declareModuleMember(node, symbolFlags, symbolExcludes); - break; - case 248 /* SourceFile */: - if (ts.isExternalModule(container)) { - declareModuleMember(node, symbolFlags, symbolExcludes); - break; - } - // fall through. - default: - if (!blockScopeContainer.locals) { - blockScopeContainer.locals = {}; - addToContainerChain(blockScopeContainer); - } - declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - function bindBlockScopedVariableDeclaration(node) { - bindBlockScopedDeclaration(node, 2 /* BlockScopedVariable */, 107455 /* BlockScopedVariableExcludes */); - } - // The binder visits every node in the syntax tree so it is a convenient place to perform a single localized - // check for reserved words used as identifiers in strict mode code. - function checkStrictModeIdentifier(node) { - if (inStrictMode && - node.originalKeywordKind >= 106 /* FirstFutureReservedWord */ && - node.originalKeywordKind <= 114 /* LastFutureReservedWord */ && - !ts.isIdentifierName(node)) { - // Report error only if there are no parse errors in file - if (!file.parseDiagnostics.length) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(node, getStrictModeIdentifierMessage(node), ts.declarationNameToString(node))); - } - } - } - function getStrictModeIdentifierMessage(node) { - // Provide specialized messages to help the user understand why we think they're in - // strict mode. - if (ts.getContainingClass(node)) { - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; - } - if (file.externalModuleIndicator) { - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; - } - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode; - } - function checkStrictModeBinaryExpression(node) { - if (inStrictMode && ts.isLeftHandSideExpression(node.left) && ts.isAssignmentOperator(node.operatorToken.kind)) { - // ECMA 262 (Annex C) The identifier eval or arguments may not appear as the LeftHandSideExpression of an - // Assignment operator(11.13) or of a PostfixExpression(11.3) - checkStrictModeEvalOrArguments(node, node.left); - } - } - function checkStrictModeCatchClause(node) { - // It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the - // Catch production is eval or arguments - if (inStrictMode && node.variableDeclaration) { - checkStrictModeEvalOrArguments(node, node.variableDeclaration.name); - } - } - function checkStrictModeDeleteExpression(node) { - // Grammar checking - if (inStrictMode && node.expression.kind === 69 /* Identifier */) { - // When a delete operator occurs within strict mode code, a SyntaxError is thrown if its - // UnaryExpression is a direct reference to a variable, function argument, or function name - var span = ts.getErrorSpanForNode(file, node.expression); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.delete_cannot_be_called_on_an_identifier_in_strict_mode)); - } - } - function isEvalOrArgumentsIdentifier(node) { - return node.kind === 69 /* Identifier */ && - (node.text === "eval" || node.text === "arguments"); - } - function checkStrictModeEvalOrArguments(contextNode, name) { - if (name && name.kind === 69 /* Identifier */) { - var identifier = name; - if (isEvalOrArgumentsIdentifier(identifier)) { - // We check first if the name is inside class declaration or class expression; if so give explicit message - // otherwise report generic error message. - var span = ts.getErrorSpanForNode(file, name); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, getStrictModeEvalOrArgumentsMessage(contextNode), identifier.text)); - } - } - } - function getStrictModeEvalOrArgumentsMessage(node) { - // Provide specialized messages to help the user understand why we think they're in - // strict mode. - if (ts.getContainingClass(node)) { - return ts.Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; - } - if (file.externalModuleIndicator) { - return ts.Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; - } - return ts.Diagnostics.Invalid_use_of_0_in_strict_mode; - } - function checkStrictModeFunctionName(node) { - if (inStrictMode) { - // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression (13.1)) - checkStrictModeEvalOrArguments(node, node.name); - } - } - function checkStrictModeNumericLiteral(node) { - if (inStrictMode && node.flags & 32768 /* OctalLiteral */) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Octal_literals_are_not_allowed_in_strict_mode)); - } - } - function checkStrictModePostfixUnaryExpression(node) { - // Grammar checking - // The identifier eval or arguments may not appear as the LeftHandSideExpression of an - // Assignment operator(11.13) or of a PostfixExpression(11.3) or as the UnaryExpression - // operated upon by a Prefix Increment(11.4.4) or a Prefix Decrement(11.4.5) operator. - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.operand); - } - } - function checkStrictModePrefixUnaryExpression(node) { - // Grammar checking - if (inStrictMode) { - if (node.operator === 41 /* PlusPlusToken */ || node.operator === 42 /* MinusMinusToken */) { - checkStrictModeEvalOrArguments(node, node.operand); - } - } - } - function checkStrictModeWithStatement(node) { - // Grammar checking for withStatement - if (inStrictMode) { - errorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_strict_mode); - } - } - function errorOnFirstToken(node, message, arg0, arg1, arg2) { - var span = ts.getSpanOfTokenAtPosition(file, node.pos); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2)); - } - function getDestructuringParameterName(node) { - return "__" + ts.indexOf(node.parent.parameters, node); - } - function bind(node) { - if (!node) { - return; - } - node.parent = parent; - var savedInStrictMode = inStrictMode; - if (!savedInStrictMode) { - updateStrictMode(node); - } - // First we bind declaration nodes to a symbol if possible. We'll both create a symbol - // and then potentially add the symbol to an appropriate symbol table. Possible - // destination symbol tables are: - // - // 1) The 'exports' table of the current container's symbol. - // 2) The 'members' table of the current container's symbol. - // 3) The 'locals' table of the current container. - // - // However, not all symbols will end up in any of these tables. 'Anonymous' symbols - // (like TypeLiterals for example) will not be put in any table. - bindWorker(node); - // Then we recurse into the children of the node to bind them as well. For certain - // symbols we do specialized work when we recurse. For example, we'll keep track of - // the current 'container' node when it changes. This helps us know which symbol table - // a local should go into for example. - bindChildren(node); - inStrictMode = savedInStrictMode; - } - function updateStrictMode(node) { - switch (node.kind) { - case 248 /* SourceFile */: - case 219 /* ModuleBlock */: - updateStrictModeStatementList(node.statements); - return; - case 192 /* Block */: - if (ts.isFunctionLike(node.parent)) { - updateStrictModeStatementList(node.statements); - } - return; - case 214 /* ClassDeclaration */: - case 186 /* ClassExpression */: - // All classes are automatically in strict mode in ES6. - inStrictMode = true; - return; - } - } - function updateStrictModeStatementList(statements) { - for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) { - var statement = statements_1[_i]; - if (!ts.isPrologueDirective(statement)) { - return; - } - if (isUseStrictPrologueDirective(statement)) { - inStrictMode = true; - return; - } - } - } - /// Should be called only on prologue directives (isPrologueDirective(node) should be true) - function isUseStrictPrologueDirective(node) { - var nodeText = ts.getTextOfNodeFromSourceText(file.text, node.expression); - // Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the - // string to contain unicode escapes (as per ES5). - return nodeText === "\"use strict\"" || nodeText === "'use strict'"; - } - function bindWorker(node) { - switch (node.kind) { - case 69 /* Identifier */: - return checkStrictModeIdentifier(node); - case 181 /* BinaryExpression */: - return checkStrictModeBinaryExpression(node); - case 244 /* CatchClause */: - return checkStrictModeCatchClause(node); - case 175 /* DeleteExpression */: - return checkStrictModeDeleteExpression(node); - case 8 /* NumericLiteral */: - return checkStrictModeNumericLiteral(node); - case 180 /* PostfixUnaryExpression */: - return checkStrictModePostfixUnaryExpression(node); - case 179 /* PrefixUnaryExpression */: - return checkStrictModePrefixUnaryExpression(node); - case 205 /* WithStatement */: - return checkStrictModeWithStatement(node); - case 97 /* ThisKeyword */: - seenThisKeyword = true; - return; - case 137 /* TypeParameter */: - return declareSymbolAndAddToSymbolTable(node, 262144 /* TypeParameter */, 530912 /* TypeParameterExcludes */); - case 138 /* Parameter */: - return bindParameter(node); - case 211 /* VariableDeclaration */: - case 163 /* BindingElement */: - return bindVariableDeclarationOrBindingElement(node); - case 141 /* PropertyDeclaration */: - case 140 /* PropertySignature */: - return bindPropertyOrMethodOrAccessor(node, 4 /* Property */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), 107455 /* PropertyExcludes */); - case 245 /* PropertyAssignment */: - case 246 /* ShorthandPropertyAssignment */: - return bindPropertyOrMethodOrAccessor(node, 4 /* Property */, 107455 /* PropertyExcludes */); - case 247 /* EnumMember */: - return bindPropertyOrMethodOrAccessor(node, 8 /* EnumMember */, 107455 /* EnumMemberExcludes */); - case 147 /* CallSignature */: - case 148 /* ConstructSignature */: - case 149 /* IndexSignature */: - return declareSymbolAndAddToSymbolTable(node, 131072 /* Signature */, 0 /* None */); - case 143 /* MethodDeclaration */: - case 142 /* MethodSignature */: - // If this is an ObjectLiteralExpression method, then it sits in the same space - // as other properties in the object literal. So we use SymbolFlags.PropertyExcludes - // so that it will conflict with any other object literal members with the same - // name. - return bindPropertyOrMethodOrAccessor(node, 8192 /* Method */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), ts.isObjectLiteralMethod(node) ? 107455 /* PropertyExcludes */ : 99263 /* MethodExcludes */); - case 213 /* FunctionDeclaration */: - checkStrictModeFunctionName(node); - return declareSymbolAndAddToSymbolTable(node, 16 /* Function */, 106927 /* FunctionExcludes */); - case 144 /* Constructor */: - return declareSymbolAndAddToSymbolTable(node, 16384 /* Constructor */, /*symbolExcludes:*/ 0 /* None */); - case 145 /* GetAccessor */: - return bindPropertyOrMethodOrAccessor(node, 32768 /* GetAccessor */, 41919 /* GetAccessorExcludes */); - case 146 /* SetAccessor */: - return bindPropertyOrMethodOrAccessor(node, 65536 /* SetAccessor */, 74687 /* SetAccessorExcludes */); - case 152 /* FunctionType */: - case 153 /* ConstructorType */: - return bindFunctionOrConstructorType(node); - case 155 /* TypeLiteral */: - return bindAnonymousDeclaration(node, 2048 /* TypeLiteral */, "__type"); - case 165 /* ObjectLiteralExpression */: - return bindObjectLiteralExpression(node); - case 173 /* FunctionExpression */: - case 174 /* ArrowFunction */: - checkStrictModeFunctionName(node); - var bindingName = node.name ? node.name.text : "__function"; - return bindAnonymousDeclaration(node, 16 /* Function */, bindingName); - case 186 /* ClassExpression */: - case 214 /* ClassDeclaration */: - return bindClassLikeDeclaration(node); - case 215 /* InterfaceDeclaration */: - return bindBlockScopedDeclaration(node, 64 /* Interface */, 792960 /* InterfaceExcludes */); - case 216 /* TypeAliasDeclaration */: - return bindBlockScopedDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */); - case 217 /* EnumDeclaration */: - return bindEnumDeclaration(node); - case 218 /* ModuleDeclaration */: - return bindModuleDeclaration(node); - case 221 /* ImportEqualsDeclaration */: - case 224 /* NamespaceImport */: - case 226 /* ImportSpecifier */: - case 230 /* ExportSpecifier */: - return declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); - case 223 /* ImportClause */: - return bindImportClause(node); - case 228 /* ExportDeclaration */: - return bindExportDeclaration(node); - case 227 /* ExportAssignment */: - return bindExportAssignment(node); - case 248 /* SourceFile */: - return bindSourceFileIfExternalModule(); - } - } - function bindSourceFileIfExternalModule() { - setExportContextFlag(file); - if (ts.isExternalModule(file)) { - bindAnonymousDeclaration(file, 512 /* ValueModule */, "\"" + ts.removeFileExtension(file.fileName) + "\""); - } - } - function bindExportAssignment(node) { - if (!container.symbol || !container.symbol.exports) { - // Export assignment in some sort of block construct - bindAnonymousDeclaration(node, 8388608 /* Alias */, getDeclarationName(node)); - } - else if (node.expression.kind === 69 /* Identifier */) { - // An export default clause with an identifier exports all meanings of that identifier - declareSymbol(container.symbol.exports, container.symbol, node, 8388608 /* Alias */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); - } - else { - // An export default clause with an expression exports a value - declareSymbol(container.symbol.exports, container.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); - } - } - function bindExportDeclaration(node) { - if (!container.symbol || !container.symbol.exports) { - // Export * in some sort of block construct - bindAnonymousDeclaration(node, 1073741824 /* ExportStar */, getDeclarationName(node)); - } - else if (!node.exportClause) { - // All export * declarations are collected in an __export symbol - declareSymbol(container.symbol.exports, container.symbol, node, 1073741824 /* ExportStar */, 0 /* None */); - } - } - function bindImportClause(node) { - if (node.name) { - declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); - } - } - function bindClassLikeDeclaration(node) { - if (node.kind === 214 /* ClassDeclaration */) { - bindBlockScopedDeclaration(node, 32 /* Class */, 899519 /* ClassExcludes */); - } - else { - var bindingName = node.name ? node.name.text : "__class"; - bindAnonymousDeclaration(node, 32 /* Class */, bindingName); - // Add name of class expression into the map for semantic classifier - if (node.name) { - classifiableNames[node.name.text] = node.name.text; - } - } - var symbol = node.symbol; - // TypeScript 1.0 spec (April 2014): 8.4 - // Every class automatically contains a static property member named 'prototype', the - // type of which is an instantiation of the class type with type Any supplied as a type - // argument for each type parameter. It is an error to explicitly declare a static - // property member with the name 'prototype'. - // - // Note: we check for this here because this class may be merging into a module. The - // module might have an exported variable called 'prototype'. We can't allow that as - // that would clash with the built-in 'prototype' for the class. - var prototypeSymbol = createSymbol(4 /* Property */ | 134217728 /* Prototype */, "prototype"); - if (ts.hasProperty(symbol.exports, prototypeSymbol.name)) { - if (node.name) { - node.name.parent = node; - } - file.bindDiagnostics.push(ts.createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0], ts.Diagnostics.Duplicate_identifier_0, prototypeSymbol.name)); - } - symbol.exports[prototypeSymbol.name] = prototypeSymbol; - prototypeSymbol.parent = symbol; - } - function bindEnumDeclaration(node) { - return ts.isConst(node) - ? bindBlockScopedDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */) - : bindBlockScopedDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */); - } - function bindVariableDeclarationOrBindingElement(node) { - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.name); - } - if (!ts.isBindingPattern(node.name)) { - if (ts.isBlockOrCatchScoped(node)) { - bindBlockScopedVariableDeclaration(node); - } - else if (ts.isParameterDeclaration(node)) { - // It is safe to walk up parent chain to find whether the node is a destructing parameter declaration - // because its parent chain has already been set up, since parents are set before descending into children. - // - // If node is a binding element in parameter declaration, we need to use ParameterExcludes. - // Using ParameterExcludes flag allows the compiler to report an error on duplicate identifiers in Parameter Declaration - // For example: - // function foo([a,a]) {} // Duplicate Identifier error - // function bar(a,a) {} // Duplicate Identifier error, parameter declaration in this case is handled in bindParameter - // // which correctly set excluded symbols - declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); - } - else { - declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107454 /* FunctionScopedVariableExcludes */); - } - } - } - function bindParameter(node) { - if (inStrictMode) { - // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a - // strict mode FunctionLikeDeclaration or FunctionExpression(13.1) - checkStrictModeEvalOrArguments(node, node.name); - } - if (ts.isBindingPattern(node.name)) { - bindAnonymousDeclaration(node, 1 /* FunctionScopedVariable */, getDestructuringParameterName(node)); - } - else { - declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); - } - // If this is a property-parameter, then also declare the property symbol into the - // containing class. - if (node.flags & 56 /* AccessibilityModifier */ && - node.parent.kind === 144 /* Constructor */ && - ts.isClassLike(node.parent.parent)) { - var classDeclaration = node.parent.parent; - declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */); - } - } - function bindPropertyOrMethodOrAccessor(node, symbolFlags, symbolExcludes) { - return ts.hasDynamicName(node) - ? bindAnonymousDeclaration(node, symbolFlags, "__computed") - : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); - } - // reachability checks - function pushNamedLabel(name) { - initializeReachabilityStateIfNecessary(); - if (ts.hasProperty(labelIndexMap, name.text)) { - return false; - } - labelIndexMap[name.text] = labelStack.push(1 /* Unintialized */) - 1; - return true; - } - function pushImplicitLabel() { - initializeReachabilityStateIfNecessary(); - var index = labelStack.push(1 /* Unintialized */) - 1; - implicitLabels.push(index); - return index; - } - function popNamedLabel(label, outerState) { - var index = labelIndexMap[label.text]; - ts.Debug.assert(index !== undefined); - ts.Debug.assert(labelStack.length == index + 1); - labelIndexMap[label.text] = undefined; - setCurrentStateAtLabel(labelStack.pop(), outerState, label); - } - function popImplicitLabel(implicitLabelIndex, outerState) { - if (labelStack.length !== implicitLabelIndex + 1) { - ts.Debug.assert(false, "Label stack: " + labelStack.length + ", index:" + implicitLabelIndex); - } - var i = implicitLabels.pop(); - if (implicitLabelIndex !== i) { - ts.Debug.assert(false, "i: " + i + ", index: " + implicitLabelIndex); - } - setCurrentStateAtLabel(labelStack.pop(), outerState, /*name*/ undefined); - } - function setCurrentStateAtLabel(innerMergedState, outerState, label) { - if (innerMergedState === 1 /* Unintialized */) { - if (label && !options.allowUnusedLabels) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(label, ts.Diagnostics.Unused_label)); - } - currentReachabilityState = outerState; - } - else { - currentReachabilityState = or(innerMergedState, outerState); - } - } - function jumpToLabel(label, outerState) { - initializeReachabilityStateIfNecessary(); - var index = label ? labelIndexMap[label.text] : ts.lastOrUndefined(implicitLabels); - if (index === undefined) { - // reference to unknown label or - // break/continue used outside of loops - return false; - } - var stateAtLabel = labelStack[index]; - labelStack[index] = stateAtLabel === 1 /* Unintialized */ ? outerState : or(stateAtLabel, outerState); - return true; - } - function checkUnreachable(node) { - switch (currentReachabilityState) { - case 4 /* Unreachable */: - var reportError = - // report error on all statements - ts.isStatement(node) || - // report error on class declarations - node.kind === 214 /* ClassDeclaration */ || - // report error on instantiated modules or const-enums only modules if preserveConstEnums is set - (node.kind === 218 /* ModuleDeclaration */ && shouldReportErrorOnModuleDeclaration(node)) || - // report error on regular enums and const enums if preserveConstEnums is set - (node.kind === 217 /* EnumDeclaration */ && (!ts.isConstEnumDeclaration(node) || options.preserveConstEnums)); - if (reportError) { - currentReachabilityState = 8 /* ReportedUnreachable */; - // unreachable code is reported if - // - user has explicitly asked about it AND - // - statement is in not ambient context (statements in ambient context is already an error - // so we should not report extras) AND - // - node is not variable statement OR - // - node is block scoped variable statement OR - // - node is not block scoped variable statement and at least one variable declaration has initializer - // Rationale: we don't want to report errors on non-initialized var's since they are hoisted - // On the other side we do want to report errors on non-initialized 'lets' because of TDZ - var reportUnreachableCode = !options.allowUnreachableCode && - !ts.isInAmbientContext(node) && - (node.kind !== 193 /* VariableStatement */ || - ts.getCombinedNodeFlags(node.declarationList) & 24576 /* BlockScoped */ || - ts.forEach(node.declarationList.declarations, function (d) { return d.initializer; })); - if (reportUnreachableCode) { - errorOnFirstToken(node, ts.Diagnostics.Unreachable_code_detected); - } - } - case 8 /* ReportedUnreachable */: - return true; - default: - return false; - } - function shouldReportErrorOnModuleDeclaration(node) { - var instanceState = getModuleInstanceState(node); - return instanceState === 1 /* Instantiated */ || (instanceState === 2 /* ConstEnumOnly */ && options.preserveConstEnums); - } - } - function initializeReachabilityStateIfNecessary() { - if (labelIndexMap) { - return; - } - currentReachabilityState = 2 /* Reachable */; - labelIndexMap = {}; - labelStack = []; - implicitLabels = []; - } - } -})(ts || (ts = {})); -/// /// /* @internal */ var ts; @@ -5812,6 +4503,10 @@ var ts; return file.externalModuleIndicator !== undefined; } ts.isExternalModule = isExternalModule; + function isExternalOrCommonJsModule(file) { + return (file.externalModuleIndicator || file.commonJsModuleIndicator) !== undefined; + } + ts.isExternalOrCommonJsModule = isExternalOrCommonJsModule; function isDeclarationFile(file) { return (file.flags & 4096 /* DeclarationFile */) !== 0; } @@ -5865,19 +4560,27 @@ var ts; return ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos); } ts.getLeadingCommentRangesOfNode = getLeadingCommentRangesOfNode; + function getLeadingCommentRangesOfNodeFromText(node, text) { + return ts.getLeadingCommentRanges(text, node.pos); + } + ts.getLeadingCommentRangesOfNodeFromText = getLeadingCommentRangesOfNodeFromText; function getJsDocComments(node, sourceFileOfNode) { + return getJsDocCommentsFromText(node, sourceFileOfNode.text); + } + ts.getJsDocComments = getJsDocComments; + function getJsDocCommentsFromText(node, text) { var commentRanges = (node.kind === 138 /* Parameter */ || node.kind === 137 /* TypeParameter */) ? - ts.concatenate(ts.getTrailingCommentRanges(sourceFileOfNode.text, node.pos), ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos)) : - getLeadingCommentRangesOfNode(node, sourceFileOfNode); + ts.concatenate(ts.getTrailingCommentRanges(text, node.pos), ts.getLeadingCommentRanges(text, node.pos)) : + getLeadingCommentRangesOfNodeFromText(node, text); return ts.filter(commentRanges, isJsDocComment); function isJsDocComment(comment) { // True if the comment starts with '/**' but not if it is '/**/' - return sourceFileOfNode.text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && - sourceFileOfNode.text.charCodeAt(comment.pos + 2) === 42 /* asterisk */ && - sourceFileOfNode.text.charCodeAt(comment.pos + 3) !== 47 /* slash */; + return text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && + text.charCodeAt(comment.pos + 2) === 42 /* asterisk */ && + text.charCodeAt(comment.pos + 3) !== 47 /* slash */; } } - ts.getJsDocComments = getJsDocComments; + ts.getJsDocCommentsFromText = getJsDocCommentsFromText; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; ts.fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*/; function isTypeNode(node) { @@ -6464,6 +5167,57 @@ var ts; return node.kind === 221 /* ImportEqualsDeclaration */ && node.moduleReference.kind !== 232 /* ExternalModuleReference */; } ts.isInternalModuleImportEqualsDeclaration = isInternalModuleImportEqualsDeclaration; + function isSourceFileJavaScript(file) { + return isInJavaScriptFile(file); + } + ts.isSourceFileJavaScript = isSourceFileJavaScript; + function isInJavaScriptFile(node) { + return node && !!(node.parserContextFlags & 32 /* JavaScriptFile */); + } + ts.isInJavaScriptFile = isInJavaScriptFile; + /** + * Returns true if the node is a CallExpression to the identifier 'require' with + * exactly one string literal argument. + * This function does not test if the node is in a JavaScript file or not. + */ + function isRequireCall(expression) { + // of the form 'require("name")' + return expression.kind === 168 /* CallExpression */ && + expression.expression.kind === 69 /* Identifier */ && + expression.expression.text === "require" && + expression.arguments.length === 1 && + expression.arguments[0].kind === 9 /* StringLiteral */; + } + ts.isRequireCall = isRequireCall; + /** + * Returns true if the node is an assignment to a property on the identifier 'exports'. + * This function does not test if the node is in a JavaScript file or not. + */ + function isExportsPropertyAssignment(expression) { + // of the form 'exports.name = expr' where 'name' and 'expr' are arbitrary + return isInJavaScriptFile(expression) && + (expression.kind === 181 /* BinaryExpression */) && + (expression.operatorToken.kind === 56 /* EqualsToken */) && + (expression.left.kind === 166 /* PropertyAccessExpression */) && + (expression.left.expression.kind === 69 /* Identifier */) && + ((expression.left.expression).text === "exports"); + } + ts.isExportsPropertyAssignment = isExportsPropertyAssignment; + /** + * Returns true if the node is an assignment to the property access expression 'module.exports'. + * This function does not test if the node is in a JavaScript file or not. + */ + function isModuleExportsAssignment(expression) { + // of the form 'module.exports = expr' where 'expr' is arbitrary + return isInJavaScriptFile(expression) && + (expression.kind === 181 /* BinaryExpression */) && + (expression.operatorToken.kind === 56 /* EqualsToken */) && + (expression.left.kind === 166 /* PropertyAccessExpression */) && + (expression.left.expression.kind === 69 /* Identifier */) && + ((expression.left.expression).text === "module") && + (expression.left.name.text === "exports"); + } + ts.isModuleExportsAssignment = isModuleExportsAssignment; function getExternalModuleName(node) { if (node.kind === 222 /* ImportDeclaration */) { return node.moduleSpecifier; @@ -6791,8 +5545,8 @@ var ts; function getFileReferenceFromReferencePath(comment, commentRange) { var simpleReferenceRegEx = /^\/\/\/\s*/gim; - if (simpleReferenceRegEx.exec(comment)) { - if (isNoDefaultLibRegEx.exec(comment)) { + if (simpleReferenceRegEx.test(comment)) { + if (isNoDefaultLibRegEx.test(comment)) { return { isNoDefaultLib: true }; @@ -6834,6 +5588,10 @@ var ts; return isFunctionLike(node) && (node.flags & 256 /* Async */) !== 0 && !isAccessor(node); } ts.isAsyncFunctionLike = isAsyncFunctionLike; + function isStringOrNumericLiteral(kind) { + return kind === 9 /* StringLiteral */ || kind === 8 /* NumericLiteral */; + } + ts.isStringOrNumericLiteral = isStringOrNumericLiteral; /** * A declaration has a dynamic name if both of the following are true: * 1. The declaration has a computed property name @@ -6842,11 +5600,15 @@ var ts; * Symbol. */ function hasDynamicName(declaration) { - return declaration.name && - declaration.name.kind === 136 /* ComputedPropertyName */ && - !isWellKnownSymbolSyntactically(declaration.name.expression); + return declaration.name && isDynamicName(declaration.name); } ts.hasDynamicName = hasDynamicName; + function isDynamicName(name) { + return name.kind === 136 /* ComputedPropertyName */ && + !isStringOrNumericLiteral(name.expression.kind) && + !isWellKnownSymbolSyntactically(name.expression); + } + ts.isDynamicName = isDynamicName; /** * Checks if the expression is of the form: * Symbol.name @@ -7087,11 +5849,11 @@ var ts; } ts.getIndentSize = getIndentSize; function createTextWriter(newLine) { - var output = ""; - var indent = 0; - var lineStart = true; - var lineCount = 0; - var linePos = 0; + var output; + var indent; + var lineStart; + var lineCount; + var linePos; function write(s) { if (s && s.length) { if (lineStart) { @@ -7101,6 +5863,13 @@ var ts; output += s; } } + function reset() { + output = ""; + indent = 0; + lineStart = true; + lineCount = 0; + linePos = 0; + } function rawWrite(s) { if (s !== undefined) { if (lineStart) { @@ -7127,9 +5896,10 @@ var ts; lineStart = true; } } - function writeTextOfNode(sourceFile, node) { - write(getSourceTextOfNodeFromSourceFile(sourceFile, node)); + function writeTextOfNode(text, node) { + write(getTextOfNodeFromSourceText(text, node)); } + reset(); return { write: write, rawWrite: rawWrite, @@ -7142,10 +5912,20 @@ var ts; getTextPos: function () { return output.length; }, getLine: function () { return lineCount + 1; }, getColumn: function () { return lineStart ? indent * getIndentSize() + 1 : output.length - linePos + 1; }, - getText: function () { return output; } + getText: function () { return output; }, + reset: reset }; } ts.createTextWriter = createTextWriter; + /** + * Resolves a local path to a path which is absolute to the base of the emit + */ + function getExternalModuleNameFromPath(host, fileName) { + var dir = host.getCurrentDirectory(); + var relativePath = ts.getRelativePathToDirectoryOrUrl(dir, fileName, dir, function (f) { return host.getCanonicalFileName(f); }, /*isAbsolutePathAnUrl*/ false); + return ts.removeFileExtension(relativePath); + } + ts.getExternalModuleNameFromPath = getExternalModuleNameFromPath; function getOwnEmitOutputFilePath(sourceFile, host, extension) { var compilerOptions = host.getCompilerOptions(); var emitOutputFilePathWithoutExtension; @@ -7174,6 +5954,10 @@ var ts; return ts.getLineAndCharacterOfPosition(currentSourceFile, pos).line; } ts.getLineOfLocalPosition = getLineOfLocalPosition; + function getLineOfLocalPositionFromLineMap(lineMap, pos) { + return ts.computeLineAndCharacterOfPosition(lineMap, pos).line; + } + ts.getLineOfLocalPositionFromLineMap = getLineOfLocalPositionFromLineMap; function getFirstConstructorWithBody(node) { return ts.forEach(node.members, function (member) { if (member.kind === 144 /* Constructor */ && nodeIsPresent(member.body)) { @@ -7246,22 +6030,22 @@ var ts; }; } ts.getAllAccessorDeclarations = getAllAccessorDeclarations; - function emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments) { + function emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments) { // If the leading comments start on different line than the start of node, write new line if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos && - getLineOfLocalPosition(currentSourceFile, node.pos) !== getLineOfLocalPosition(currentSourceFile, leadingComments[0].pos)) { + getLineOfLocalPositionFromLineMap(lineMap, node.pos) !== getLineOfLocalPositionFromLineMap(lineMap, leadingComments[0].pos)) { writer.writeLine(); } } ts.emitNewLineBeforeLeadingComments = emitNewLineBeforeLeadingComments; - function emitComments(currentSourceFile, writer, comments, trailingSeparator, newLine, writeComment) { + function emitComments(text, lineMap, writer, comments, trailingSeparator, newLine, writeComment) { var emitLeadingSpace = !trailingSeparator; ts.forEach(comments, function (comment) { if (emitLeadingSpace) { writer.write(" "); emitLeadingSpace = false; } - writeComment(currentSourceFile, writer, comment, newLine); + writeComment(text, lineMap, writer, comment, newLine); if (comment.hasTrailingNewLine) { writer.writeLine(); } @@ -7279,7 +6063,7 @@ var ts; * Detached comment is a comment at the top of file or function body that is separated from * the next statement by space. */ - function emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, removeComments) { + function emitDetachedComments(text, lineMap, writer, writeComment, node, newLine, removeComments) { var leadingComments; var currentDetachedCommentInfo; if (removeComments) { @@ -7289,12 +6073,12 @@ var ts; // // var x = 10; if (node.pos === 0) { - leadingComments = ts.filter(ts.getLeadingCommentRanges(currentSourceFile.text, node.pos), isPinnedComment); + leadingComments = ts.filter(ts.getLeadingCommentRanges(text, node.pos), isPinnedComment); } } else { // removeComments is false, just get detached as normal and bypass the process to filter comment - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + leadingComments = ts.getLeadingCommentRanges(text, node.pos); } if (leadingComments) { var detachedComments = []; @@ -7302,8 +6086,8 @@ var ts; for (var _i = 0, leadingComments_1 = leadingComments; _i < leadingComments_1.length; _i++) { var comment = leadingComments_1[_i]; if (lastComment) { - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastComment.end); - var commentLine = getLineOfLocalPosition(currentSourceFile, comment.pos); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, lastComment.end); + var commentLine = getLineOfLocalPositionFromLineMap(lineMap, comment.pos); if (commentLine >= lastCommentLine + 2) { // There was a blank line between the last comment and this comment. This // comment is not part of the copyright comments. Return what we have so @@ -7318,36 +6102,36 @@ var ts; // All comments look like they could have been part of the copyright header. Make // sure there is at least one blank line between it and the node. If not, it's not // a copyright header. - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, ts.lastOrUndefined(detachedComments).end); - var nodeLine = getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node.pos)); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, ts.lastOrUndefined(detachedComments).end); + var nodeLine = getLineOfLocalPositionFromLineMap(lineMap, ts.skipTrivia(text, node.pos)); if (nodeLine >= lastCommentLine + 2) { // Valid detachedComments - emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); - emitComments(currentSourceFile, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment); + emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments); + emitComments(text, lineMap, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment); currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: ts.lastOrUndefined(detachedComments).end }; } } } return currentDetachedCommentInfo; function isPinnedComment(comment) { - return currentSourceFile.text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 33 /* exclamation */; + return text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && + text.charCodeAt(comment.pos + 2) === 33 /* exclamation */; } } ts.emitDetachedComments = emitDetachedComments; - function writeCommentRange(currentSourceFile, writer, comment, newLine) { - if (currentSourceFile.text.charCodeAt(comment.pos + 1) === 42 /* asterisk */) { - var firstCommentLineAndCharacter = ts.getLineAndCharacterOfPosition(currentSourceFile, comment.pos); - var lineCount = ts.getLineStarts(currentSourceFile).length; + function writeCommentRange(text, lineMap, writer, comment, newLine) { + if (text.charCodeAt(comment.pos + 1) === 42 /* asterisk */) { + var firstCommentLineAndCharacter = ts.computeLineAndCharacterOfPosition(lineMap, comment.pos); + var lineCount = lineMap.length; var firstCommentLineIndent; for (var pos = comment.pos, currentLine = firstCommentLineAndCharacter.line; pos < comment.end; currentLine++) { var nextLineStart = (currentLine + 1) === lineCount - ? currentSourceFile.text.length + 1 - : getStartPositionOfLine(currentLine + 1, currentSourceFile); + ? text.length + 1 + : lineMap[currentLine + 1]; if (pos !== comment.pos) { // If we are not emitting first line, we need to write the spaces to adjust the alignment if (firstCommentLineIndent === undefined) { - firstCommentLineIndent = calculateIndent(getStartPositionOfLine(firstCommentLineAndCharacter.line, currentSourceFile), comment.pos); + firstCommentLineIndent = calculateIndent(text, lineMap[firstCommentLineAndCharacter.line], comment.pos); } // These are number of spaces writer is going to write at current indent var currentWriterIndentSpacing = writer.getIndent() * getIndentSize(); @@ -7365,7 +6149,7 @@ var ts; // More right indented comment */ --4 = 8 - 4 + 11 // class c { } // } - var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(pos, nextLineStart); + var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(text, pos, nextLineStart); if (spacesToEmit > 0) { var numberOfSingleSpacesToEmit = spacesToEmit % getIndentSize(); var indentSizeSpaceString = getIndentString((spacesToEmit - numberOfSingleSpacesToEmit) / getIndentSize()); @@ -7383,45 +6167,45 @@ var ts; } } // Write the comment line text - writeTrimmedCurrentLine(pos, nextLineStart); + writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart); pos = nextLineStart; } } else { // Single line comment of style //.... - writer.write(currentSourceFile.text.substring(comment.pos, comment.end)); - } - function writeTrimmedCurrentLine(pos, nextLineStart) { - var end = Math.min(comment.end, nextLineStart - 1); - var currentLineText = currentSourceFile.text.substring(pos, end).replace(/^\s+|\s+$/g, ""); - if (currentLineText) { - // trimmed forward and ending spaces text - writer.write(currentLineText); - if (end !== comment.end) { - writer.writeLine(); - } - } - else { - // Empty string - make sure we write empty line - writer.writeLiteral(newLine); - } - } - function calculateIndent(pos, end) { - var currentLineIndent = 0; - for (; pos < end && ts.isWhiteSpace(currentSourceFile.text.charCodeAt(pos)); pos++) { - if (currentSourceFile.text.charCodeAt(pos) === 9 /* tab */) { - // Tabs = TabSize = indent size and go to next tabStop - currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); - } - else { - // Single space - currentLineIndent++; - } - } - return currentLineIndent; + writer.write(text.substring(comment.pos, comment.end)); } } ts.writeCommentRange = writeCommentRange; + function writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart) { + var end = Math.min(comment.end, nextLineStart - 1); + var currentLineText = text.substring(pos, end).replace(/^\s+|\s+$/g, ""); + if (currentLineText) { + // trimmed forward and ending spaces text + writer.write(currentLineText); + if (end !== comment.end) { + writer.writeLine(); + } + } + else { + // Empty string - make sure we write empty line + writer.writeLiteral(newLine); + } + } + function calculateIndent(text, pos, end) { + var currentLineIndent = 0; + for (; pos < end && ts.isWhiteSpace(text.charCodeAt(pos)); pos++) { + if (text.charCodeAt(pos) === 9 /* tab */) { + // Tabs = TabSize = indent size and go to next tabStop + currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); + } + else { + // Single space + currentLineIndent++; + } + } + return currentLineIndent; + } function modifierToFlag(token) { switch (token) { case 113 /* StaticKeyword */: return 64 /* Static */; @@ -7517,14 +6301,14 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 512 /* Default */) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function isJavaScript(fileName) { - return ts.fileExtensionIs(fileName, ".js"); + function hasJavaScriptFileExtension(fileName) { + return ts.fileExtensionIs(fileName, ".js") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isJavaScript = isJavaScript; - function isTsx(fileName) { - return ts.fileExtensionIs(fileName, ".tsx"); + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function allowsJsxExpressions(fileName) { + return ts.fileExtensionIs(fileName, ".tsx") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isTsx = isTsx; + ts.allowsJsxExpressions = allowsJsxExpressions; /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences * representing the UTF-8 encoding of the character, and return the expanded char code list. @@ -7835,18 +6619,20 @@ var ts; } ts.getTypeParameterOwner = getTypeParameterOwner; })(ts || (ts = {})); -/// /// +/// var ts; (function (ts) { - var nodeConstructors = new Array(272 /* Count */); /* @internal */ ts.parseTime = 0; - function getNodeConstructor(kind) { - return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); - } - ts.getNodeConstructor = getNodeConstructor; + var NodeConstructor; + var SourceFileConstructor; function createNode(kind, pos, end) { - return new (getNodeConstructor(kind))(pos, end); + if (kind === 248 /* SourceFile */) { + return new (SourceFileConstructor || (SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor()))(kind, pos, end); + } + else { + return new (NodeConstructor || (NodeConstructor = ts.objectAllocator.getNodeConstructor()))(kind, pos, end); + } } ts.createNode = createNode; function visitNode(cbNode, node) { @@ -8269,6 +7055,9 @@ var ts; // up by avoiding the cost of creating/compiling scanners over and over again. var scanner = ts.createScanner(2 /* Latest */, /*skipTrivia*/ true); var disallowInAndDecoratorContext = 1 /* DisallowIn */ | 4 /* Decorator */; + // capture constructors in 'initializeState' to avoid null checks + var NodeConstructor; + var SourceFileConstructor; var sourceFile; var parseDiagnostics; var syntaxCursor; @@ -8354,13 +7143,16 @@ var ts; // attached to the EOF token. var parseErrorBeforeNextFinishedNode = false; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { - initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var isJavaScriptFile = ts.hasJavaScriptFileExtension(fileName) || _sourceText.lastIndexOf("// @language=javascript", 0) === 0; + initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor); var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); clearState(); return result; } Parser.parseSourceFile = parseSourceFile; - function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { + function initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor) { + NodeConstructor = ts.objectAllocator.getNodeConstructor(); + SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor(); sourceText = _sourceText; syntaxCursor = _syntaxCursor; parseDiagnostics = []; @@ -8368,13 +7160,13 @@ var ts; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = ts.isJavaScript(fileName) ? 32 /* JavaScriptFile */ : 0 /* None */; + contextFlags = isJavaScriptFile ? 32 /* JavaScriptFile */ : 0 /* None */; parseErrorBeforeNextFinishedNode = false; // Initialize and prime the scanner before parsing the source elements. scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); - scanner.setLanguageVariant(ts.isTsx(fileName) ? 1 /* JSX */ : 0 /* Standard */); + scanner.setLanguageVariant(ts.allowsJsxExpressions(fileName) ? 1 /* JSX */ : 0 /* Standard */); } function clearState() { // Clear out the text the scanner is pointing at, so it doesn't keep anything alive unnecessarily. @@ -8389,6 +7181,9 @@ var ts; } function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { sourceFile = createSourceFile(fileName, languageVersion); + if (contextFlags & 32 /* JavaScriptFile */) { + sourceFile.parserContextFlags = 32 /* JavaScriptFile */; + } // Prime the scanner. token = nextToken(); processReferenceComments(sourceFile); @@ -8406,7 +7201,7 @@ var ts; // If this is a javascript file, proactively see if we can get JSDoc comments for // relevant nodes in the file. We'll use these to provide typing informaion if they're // available. - if (ts.isJavaScript(fileName)) { + if (ts.isSourceFileJavaScript(sourceFile)) { addJSDocComments(); } return sourceFile; @@ -8461,15 +7256,16 @@ var ts; } Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - var sourceFile = createNode(248 /* SourceFile */, /*pos*/ 0); - sourceFile.pos = 0; - sourceFile.end = sourceText.length; + // code from createNode is inlined here so createNode won't have to deal with special case of creating source files + // this is quite rare comparing to other nodes and createNode should be as fast as possible + var sourceFile = new SourceFileConstructor(248 /* SourceFile */, /*pos*/ 0, /* end */ sourceText.length); + nodeCount++; sourceFile.text = sourceText; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 4096 /* DeclarationFile */ : 0; - sourceFile.languageVariant = ts.isTsx(sourceFile.fileName) ? 1 /* JSX */ : 0 /* Standard */; + sourceFile.languageVariant = ts.allowsJsxExpressions(sourceFile.fileName) ? 1 /* JSX */ : 0 /* Standard */; return sourceFile; } function setContextFlag(val, flag) { @@ -8734,12 +7530,13 @@ var ts; return parseExpected(23 /* SemicolonToken */); } } + // note: this function creates only node function createNode(kind, pos) { nodeCount++; if (!(pos >= 0)) { pos = scanner.getStartPos(); } - return new (nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)))(pos, pos); + return new NodeConstructor(kind, pos, pos); } function finishNode(node, end) { node.end = end === undefined ? scanner.getStartPos() : end; @@ -8904,7 +7701,7 @@ var ts; case 12 /* ObjectLiteralMembers */: return token === 19 /* OpenBracketToken */ || token === 37 /* AsteriskToken */ || isLiteralPropertyName(); case 9 /* ObjectBindingElements */: - return isLiteralPropertyName(); + return token === 19 /* OpenBracketToken */ || isLiteralPropertyName(); case 7 /* HeritageClauseElement */: // If we see { } then only consume it as an expression if it is followed by , or { // That way we won't consume the body of a class in its heritage clause. @@ -9584,9 +8381,7 @@ var ts; } function parseParameterType() { if (parseOptional(54 /* ColonToken */)) { - return token === 9 /* StringLiteral */ - ? parseLiteralNode(/*internName*/ true) - : parseType(); + return parseType(); } return undefined; } @@ -9917,6 +8712,8 @@ var ts; // If these are followed by a dot, then parse these out as a dotted type reference instead. var node = tryParse(parseKeywordAndNoDot); return node || parseTypeReferenceOrTypePredicate(); + case 9 /* StringLiteral */: + return parseLiteralNode(/*internName*/ true); case 103 /* VoidKeyword */: case 97 /* ThisKeyword */: return parseTokenNode(); @@ -9946,6 +8743,7 @@ var ts; case 19 /* OpenBracketToken */: case 25 /* LessThanToken */: case 92 /* NewKeyword */: + case 9 /* StringLiteral */: return true; case 17 /* OpenParenToken */: // Only consider '(' the start of a type if followed by ')', '...', an identifier, a modifier, @@ -10362,7 +9160,7 @@ var ts; return 1 /* True */; } // This *could* be a parenthesized arrow function. - // Return Unknown to let the caller know. + // Return Unknown to const the caller know. return 2 /* Unknown */; } else { @@ -10448,7 +9246,7 @@ var ts; // user meant to supply a block. For example, if the user wrote: // // a => - // let v = 0; + // const v = 0; // } // // they may be missing an open brace. Check to see if that's the case so we can @@ -10664,7 +9462,6 @@ var ts; var unaryOperator = token; var simpleUnaryExpression = parseSimpleUnaryExpression(); if (token === 38 /* AsteriskAsteriskToken */) { - var diagnostic; var start = ts.skipTrivia(sourceText, simpleUnaryExpression.pos); if (simpleUnaryExpression.kind === 171 /* TypeAssertionExpression */) { parseErrorAtPosition(start, simpleUnaryExpression.end - start, ts.Diagnostics.A_type_assertion_expression_is_not_allowed_in_the_left_hand_side_of_an_exponentiation_expression_Consider_enclosing_the_expression_in_parentheses); @@ -11868,7 +10665,6 @@ var ts; } function parseObjectBindingElement() { var node = createNode(163 /* BindingElement */); - // TODO(andersh): Handle computed properties var tokenIsIdentifier = isIdentifier(); var propertyName = parsePropertyName(); if (tokenIsIdentifier && token !== 54 /* ColonToken */) { @@ -12691,7 +11487,7 @@ var ts; } JSDocParser.isJSDocType = isJSDocType; function parseJSDocTypeExpressionForTests(content, start, length) { - initializeState("file.js", content, 2 /* Latest */, /*_syntaxCursor:*/ undefined); + initializeState("file.js", content, 2 /* Latest */, /*isJavaScriptFile*/ true, /*_syntaxCursor:*/ undefined); var jsDocTypeExpression = parseJSDocTypeExpression(start, length); var diagnostics = parseDiagnostics; clearState(); @@ -12786,6 +11582,7 @@ var ts; case 103 /* VoidKeyword */: return parseTokenNode(); } + // TODO (drosen): Parse string literal types in JSDoc as well. return parseJSDocTypeReference(); } function parseJSDocThisType() { @@ -12957,7 +11754,7 @@ var ts; } } function parseIsolatedJSDocComment(content, start, length) { - initializeState("file.js", content, 2 /* Latest */, /*_syntaxCursor:*/ undefined); + initializeState("file.js", content, 2 /* Latest */, /*isJavaScriptFile*/ true, /*_syntaxCursor:*/ undefined); var jsDocComment = parseJSDocComment(/*parent:*/ undefined, start, length); var diagnostics = parseDiagnostics; clearState(); @@ -13682,6 +12479,1372 @@ var ts; })(InvalidPosition || (InvalidPosition = {})); })(IncrementalParser || (IncrementalParser = {})); })(ts || (ts = {})); +/// +/// +/* @internal */ +var ts; +(function (ts) { + ts.bindTime = 0; + (function (ModuleInstanceState) { + ModuleInstanceState[ModuleInstanceState["NonInstantiated"] = 0] = "NonInstantiated"; + ModuleInstanceState[ModuleInstanceState["Instantiated"] = 1] = "Instantiated"; + ModuleInstanceState[ModuleInstanceState["ConstEnumOnly"] = 2] = "ConstEnumOnly"; + })(ts.ModuleInstanceState || (ts.ModuleInstanceState = {})); + var ModuleInstanceState = ts.ModuleInstanceState; + var Reachability; + (function (Reachability) { + Reachability[Reachability["Unintialized"] = 1] = "Unintialized"; + Reachability[Reachability["Reachable"] = 2] = "Reachable"; + Reachability[Reachability["Unreachable"] = 4] = "Unreachable"; + Reachability[Reachability["ReportedUnreachable"] = 8] = "ReportedUnreachable"; + })(Reachability || (Reachability = {})); + function or(state1, state2) { + return (state1 | state2) & 2 /* Reachable */ + ? 2 /* Reachable */ + : (state1 & state2) & 8 /* ReportedUnreachable */ + ? 8 /* ReportedUnreachable */ + : 4 /* Unreachable */; + } + function getModuleInstanceState(node) { + // A module is uninstantiated if it contains only + // 1. interface declarations, type alias declarations + if (node.kind === 215 /* InterfaceDeclaration */ || node.kind === 216 /* TypeAliasDeclaration */) { + return 0 /* NonInstantiated */; + } + else if (ts.isConstEnumDeclaration(node)) { + return 2 /* ConstEnumOnly */; + } + else if ((node.kind === 222 /* ImportDeclaration */ || node.kind === 221 /* ImportEqualsDeclaration */) && !(node.flags & 2 /* Export */)) { + return 0 /* NonInstantiated */; + } + else if (node.kind === 219 /* ModuleBlock */) { + var state = 0 /* NonInstantiated */; + ts.forEachChild(node, function (n) { + switch (getModuleInstanceState(n)) { + case 0 /* NonInstantiated */: + // child is non-instantiated - continue searching + return false; + case 2 /* ConstEnumOnly */: + // child is const enum only - record state and continue searching + state = 2 /* ConstEnumOnly */; + return false; + case 1 /* Instantiated */: + // child is instantiated - record state and stop + state = 1 /* Instantiated */; + return true; + } + }); + return state; + } + else if (node.kind === 218 /* ModuleDeclaration */) { + return getModuleInstanceState(node.body); + } + else { + return 1 /* Instantiated */; + } + } + ts.getModuleInstanceState = getModuleInstanceState; + var ContainerFlags; + (function (ContainerFlags) { + // The current node is not a container, and no container manipulation should happen before + // recursing into it. + ContainerFlags[ContainerFlags["None"] = 0] = "None"; + // The current node is a container. It should be set as the current container (and block- + // container) before recursing into it. The current node does not have locals. Examples: + // + // Classes, ObjectLiterals, TypeLiterals, Interfaces... + ContainerFlags[ContainerFlags["IsContainer"] = 1] = "IsContainer"; + // The current node is a block-scoped-container. It should be set as the current block- + // container before recursing into it. Examples: + // + // Blocks (when not parented by functions), Catch clauses, For/For-in/For-of statements... + ContainerFlags[ContainerFlags["IsBlockScopedContainer"] = 2] = "IsBlockScopedContainer"; + ContainerFlags[ContainerFlags["HasLocals"] = 4] = "HasLocals"; + // If the current node is a container that also container that also contains locals. Examples: + // + // Functions, Methods, Modules, Source-files. + ContainerFlags[ContainerFlags["IsContainerWithLocals"] = 5] = "IsContainerWithLocals"; + })(ContainerFlags || (ContainerFlags = {})); + var binder = createBinder(); + function bindSourceFile(file, options) { + var start = new Date().getTime(); + binder(file, options); + ts.bindTime += new Date().getTime() - start; + } + ts.bindSourceFile = bindSourceFile; + function createBinder() { + var file; + var options; + var parent; + var container; + var blockScopeContainer; + var lastContainer; + var seenThisKeyword; + // state used by reachability checks + var hasExplicitReturn; + var currentReachabilityState; + var labelStack; + var labelIndexMap; + var implicitLabels; + // If this file is an external module, then it is automatically in strict-mode according to + // ES6. If it is not an external module, then we'll determine if it is in strict mode or + // not depending on if we see "use strict" in certain places (or if we hit a class/namespace). + var inStrictMode; + var symbolCount = 0; + var Symbol; + var classifiableNames; + function bindSourceFile(f, opts) { + file = f; + options = opts; + inStrictMode = !!file.externalModuleIndicator; + classifiableNames = {}; + Symbol = ts.objectAllocator.getSymbolConstructor(); + if (!file.locals) { + bind(file); + file.symbolCount = symbolCount; + file.classifiableNames = classifiableNames; + } + parent = undefined; + container = undefined; + blockScopeContainer = undefined; + lastContainer = undefined; + seenThisKeyword = false; + hasExplicitReturn = false; + labelStack = undefined; + labelIndexMap = undefined; + implicitLabels = undefined; + } + return bindSourceFile; + function createSymbol(flags, name) { + symbolCount++; + return new Symbol(flags, name); + } + function addDeclarationToSymbol(symbol, node, symbolFlags) { + symbol.flags |= symbolFlags; + node.symbol = symbol; + if (!symbol.declarations) { + symbol.declarations = []; + } + symbol.declarations.push(node); + if (symbolFlags & 1952 /* HasExports */ && !symbol.exports) { + symbol.exports = {}; + } + if (symbolFlags & 6240 /* HasMembers */ && !symbol.members) { + symbol.members = {}; + } + if (symbolFlags & 107455 /* Value */ && !symbol.valueDeclaration) { + symbol.valueDeclaration = node; + } + } + // Should not be called on a declaration with a computed property name, + // unless it is a well known Symbol. + function getDeclarationName(node) { + if (node.name) { + if (node.kind === 218 /* ModuleDeclaration */ && node.name.kind === 9 /* StringLiteral */) { + return "\"" + node.name.text + "\""; + } + if (node.name.kind === 136 /* ComputedPropertyName */) { + var nameExpression = node.name.expression; + // treat computed property names where expression is string/numeric literal as just string/numeric literal + if (ts.isStringOrNumericLiteral(nameExpression.kind)) { + return nameExpression.text; + } + ts.Debug.assert(ts.isWellKnownSymbolSyntactically(nameExpression)); + return ts.getPropertyNameForKnownSymbolName(nameExpression.name.text); + } + return node.name.text; + } + switch (node.kind) { + case 144 /* Constructor */: + return "__constructor"; + case 152 /* FunctionType */: + case 147 /* CallSignature */: + return "__call"; + case 153 /* ConstructorType */: + case 148 /* ConstructSignature */: + return "__new"; + case 149 /* IndexSignature */: + return "__index"; + case 228 /* ExportDeclaration */: + return "__export"; + case 227 /* ExportAssignment */: + return node.isExportEquals ? "export=" : "default"; + case 181 /* BinaryExpression */: + // Binary expression case is for JS module 'module.exports = expr' + return "export="; + case 213 /* FunctionDeclaration */: + case 214 /* ClassDeclaration */: + return node.flags & 512 /* Default */ ? "default" : undefined; + } + } + function getDisplayName(node) { + return node.name ? ts.declarationNameToString(node.name) : getDeclarationName(node); + } + /** + * Declares a Symbol for the node and adds it to symbols. Reports errors for conflicting identifier names. + * @param symbolTable - The symbol table which node will be added to. + * @param parent - node's parent declaration. + * @param node - The declaration to be added to the symbol table + * @param includes - The SymbolFlags that node has in addition to its declaration type (eg: export, ambient, etc.) + * @param excludes - The flags which node cannot be declared alongside in a symbol table. Used to report forbidden declarations. + */ + function declareSymbol(symbolTable, parent, node, includes, excludes) { + ts.Debug.assert(!ts.hasDynamicName(node)); + var isDefaultExport = node.flags & 512 /* Default */; + // The exported symbol for an export default function/class node is always named "default" + var name = isDefaultExport && parent ? "default" : getDeclarationName(node); + var symbol; + if (name !== undefined) { + // Check and see if the symbol table already has a symbol with this name. If not, + // create a new symbol with this name and add it to the table. Note that we don't + // give the new symbol any flags *yet*. This ensures that it will not conflict + // with the 'excludes' flags we pass in. + // + // If we do get an existing symbol, see if it conflicts with the new symbol we're + // creating. For example, a 'var' symbol and a 'class' symbol will conflict within + // the same symbol table. If we have a conflict, report the issue on each + // declaration we have for this symbol, and then create a new symbol for this + // declaration. + // + // If we created a new symbol, either because we didn't have a symbol with this name + // in the symbol table, or we conflicted with an existing symbol, then just add this + // node as the sole declaration of the new symbol. + // + // Otherwise, we'll be merging into a compatible existing symbol (for example when + // you have multiple 'vars' with the same name in the same container). In this case + // just add this node into the declarations list of the symbol. + symbol = ts.hasProperty(symbolTable, name) + ? symbolTable[name] + : (symbolTable[name] = createSymbol(0 /* None */, name)); + if (name && (includes & 788448 /* Classifiable */)) { + classifiableNames[name] = name; + } + if (symbol.flags & excludes) { + if (node.name) { + node.name.parent = node; + } + // Report errors every position with duplicate declaration + // Report errors on previous encountered declarations + var message = symbol.flags & 2 /* BlockScopedVariable */ + ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 + : ts.Diagnostics.Duplicate_identifier_0; + ts.forEach(symbol.declarations, function (declaration) { + if (declaration.flags & 512 /* Default */) { + message = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + }); + ts.forEach(symbol.declarations, function (declaration) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message, getDisplayName(declaration))); + }); + file.bindDiagnostics.push(ts.createDiagnosticForNode(node.name || node, message, getDisplayName(node))); + symbol = createSymbol(0 /* None */, name); + } + } + else { + symbol = createSymbol(0 /* None */, "__missing"); + } + addDeclarationToSymbol(symbol, node, includes); + symbol.parent = parent; + return symbol; + } + function declareModuleMember(node, symbolFlags, symbolExcludes) { + var hasExportModifier = ts.getCombinedNodeFlags(node) & 2 /* Export */; + if (symbolFlags & 8388608 /* Alias */) { + if (node.kind === 230 /* ExportSpecifier */ || (node.kind === 221 /* ImportEqualsDeclaration */ && hasExportModifier)) { + return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + } + else { + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + else { + // Exported module members are given 2 symbols: A local symbol that is classified with an ExportValue, + // ExportType, or ExportContainer flag, and an associated export symbol with all the correct flags set + // on it. There are 2 main reasons: + // + // 1. We treat locals and exports of the same name as mutually exclusive within a container. + // That means the binder will issue a Duplicate Identifier error if you mix locals and exports + // with the same name in the same container. + // TODO: Make this a more specific error and decouple it from the exclusion logic. + // 2. When we checkIdentifier in the checker, we set its resolved symbol to the local symbol, + // but return the export symbol (by calling getExportSymbolOfValueSymbolIfExported). That way + // when the emitter comes back to it, it knows not to qualify the name if it was found in a containing scope. + if (hasExportModifier || container.flags & 131072 /* ExportContext */) { + var exportKind = (symbolFlags & 107455 /* Value */ ? 1048576 /* ExportValue */ : 0) | + (symbolFlags & 793056 /* Type */ ? 2097152 /* ExportType */ : 0) | + (symbolFlags & 1536 /* Namespace */ ? 4194304 /* ExportNamespace */ : 0); + var local = declareSymbol(container.locals, undefined, node, exportKind, symbolExcludes); + local.exportSymbol = declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + node.localSymbol = local; + return local; + } + else { + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + } + // All container nodes are kept on a linked list in declaration order. This list is used by + // the getLocalNameOfContainer function in the type checker to validate that the local name + // used for a container is unique. + function bindChildren(node) { + // Before we recurse into a node's chilren, we first save the existing parent, container + // and block-container. Then after we pop out of processing the children, we restore + // these saved values. + var saveParent = parent; + var saveContainer = container; + var savedBlockScopeContainer = blockScopeContainer; + // This node will now be set as the parent of all of its children as we recurse into them. + parent = node; + // Depending on what kind of node this is, we may have to adjust the current container + // and block-container. If the current node is a container, then it is automatically + // considered the current block-container as well. Also, for containers that we know + // may contain locals, we proactively initialize the .locals field. We do this because + // it's highly likely that the .locals will be needed to place some child in (for example, + // a parameter, or variable declaration). + // + // However, we do not proactively create the .locals for block-containers because it's + // totally normal and common for block-containers to never actually have a block-scoped + // variable in them. We don't want to end up allocating an object for every 'block' we + // run into when most of them won't be necessary. + // + // Finally, if this is a block-container, then we clear out any existing .locals object + // it may contain within it. This happens in incremental scenarios. Because we can be + // reusing a node from a previous compilation, that node may have had 'locals' created + // for it. We must clear this so we don't accidently move any stale data forward from + // a previous compilation. + var containerFlags = getContainerFlags(node); + if (containerFlags & 1 /* IsContainer */) { + container = blockScopeContainer = node; + if (containerFlags & 4 /* HasLocals */) { + container.locals = {}; + } + addToContainerChain(container); + } + else if (containerFlags & 2 /* IsBlockScopedContainer */) { + blockScopeContainer = node; + blockScopeContainer.locals = undefined; + } + var savedReachabilityState; + var savedLabelStack; + var savedLabels; + var savedImplicitLabels; + var savedHasExplicitReturn; + var kind = node.kind; + var flags = node.flags; + // reset all reachability check related flags on node (for incremental scenarios) + flags &= ~1572864 /* ReachabilityCheckFlags */; + if (kind === 215 /* InterfaceDeclaration */) { + seenThisKeyword = false; + } + var saveState = kind === 248 /* SourceFile */ || kind === 219 /* ModuleBlock */ || ts.isFunctionLikeKind(kind); + if (saveState) { + savedReachabilityState = currentReachabilityState; + savedLabelStack = labelStack; + savedLabels = labelIndexMap; + savedImplicitLabels = implicitLabels; + savedHasExplicitReturn = hasExplicitReturn; + currentReachabilityState = 2 /* Reachable */; + hasExplicitReturn = false; + labelStack = labelIndexMap = implicitLabels = undefined; + } + bindReachableStatement(node); + if (currentReachabilityState === 2 /* Reachable */ && ts.isFunctionLikeKind(kind) && ts.nodeIsPresent(node.body)) { + flags |= 524288 /* HasImplicitReturn */; + if (hasExplicitReturn) { + flags |= 1048576 /* HasExplicitReturn */; + } + } + if (kind === 215 /* InterfaceDeclaration */) { + flags = seenThisKeyword ? flags | 262144 /* ContainsThis */ : flags & ~262144 /* ContainsThis */; + } + node.flags = flags; + if (saveState) { + hasExplicitReturn = savedHasExplicitReturn; + currentReachabilityState = savedReachabilityState; + labelStack = savedLabelStack; + labelIndexMap = savedLabels; + implicitLabels = savedImplicitLabels; + } + container = saveContainer; + parent = saveParent; + blockScopeContainer = savedBlockScopeContainer; + } + /** + * Returns true if node and its subnodes were successfully traversed. + * Returning false means that node was not examined and caller needs to dive into the node himself. + */ + function bindReachableStatement(node) { + if (checkUnreachable(node)) { + ts.forEachChild(node, bind); + return; + } + switch (node.kind) { + case 198 /* WhileStatement */: + bindWhileStatement(node); + break; + case 197 /* DoStatement */: + bindDoStatement(node); + break; + case 199 /* ForStatement */: + bindForStatement(node); + break; + case 200 /* ForInStatement */: + case 201 /* ForOfStatement */: + bindForInOrForOfStatement(node); + break; + case 196 /* IfStatement */: + bindIfStatement(node); + break; + case 204 /* ReturnStatement */: + case 208 /* ThrowStatement */: + bindReturnOrThrow(node); + break; + case 203 /* BreakStatement */: + case 202 /* ContinueStatement */: + bindBreakOrContinueStatement(node); + break; + case 209 /* TryStatement */: + bindTryStatement(node); + break; + case 206 /* SwitchStatement */: + bindSwitchStatement(node); + break; + case 220 /* CaseBlock */: + bindCaseBlock(node); + break; + case 207 /* LabeledStatement */: + bindLabeledStatement(node); + break; + default: + ts.forEachChild(node, bind); + break; + } + } + function bindWhileStatement(n) { + var preWhileState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + var postWhileState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + // bind expressions (don't affect reachability) + bind(n.expression); + currentReachabilityState = preWhileState; + var postWhileLabel = pushImplicitLabel(); + bind(n.statement); + popImplicitLabel(postWhileLabel, postWhileState); + } + function bindDoStatement(n) { + var preDoState = currentReachabilityState; + var postDoLabel = pushImplicitLabel(); + bind(n.statement); + var postDoState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : preDoState; + popImplicitLabel(postDoLabel, postDoState); + // bind expressions (don't affect reachability) + bind(n.expression); + } + function bindForStatement(n) { + var preForState = currentReachabilityState; + var postForLabel = pushImplicitLabel(); + // bind expressions (don't affect reachability) + bind(n.initializer); + bind(n.condition); + bind(n.incrementor); + bind(n.statement); + // for statement is considered infinite when it condition is either omitted or is true keyword + // - for(..;;..) + // - for(..;true;..) + var isInfiniteLoop = (!n.condition || n.condition.kind === 99 /* TrueKeyword */); + var postForState = isInfiniteLoop ? 4 /* Unreachable */ : preForState; + popImplicitLabel(postForLabel, postForState); + } + function bindForInOrForOfStatement(n) { + var preStatementState = currentReachabilityState; + var postStatementLabel = pushImplicitLabel(); + // bind expressions (don't affect reachability) + bind(n.initializer); + bind(n.expression); + bind(n.statement); + popImplicitLabel(postStatementLabel, preStatementState); + } + function bindIfStatement(n) { + // denotes reachability state when entering 'thenStatement' part of the if statement: + // i.e. if condition is false then thenStatement is unreachable + var ifTrueState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + // denotes reachability state when entering 'elseStatement': + // i.e. if condition is true then elseStatement is unreachable + var ifFalseState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + currentReachabilityState = ifTrueState; + // bind expression (don't affect reachability) + bind(n.expression); + bind(n.thenStatement); + if (n.elseStatement) { + var preElseState = currentReachabilityState; + currentReachabilityState = ifFalseState; + bind(n.elseStatement); + currentReachabilityState = or(currentReachabilityState, preElseState); + } + else { + currentReachabilityState = or(currentReachabilityState, ifFalseState); + } + } + function bindReturnOrThrow(n) { + // bind expression (don't affect reachability) + bind(n.expression); + if (n.kind === 204 /* ReturnStatement */) { + hasExplicitReturn = true; + } + currentReachabilityState = 4 /* Unreachable */; + } + function bindBreakOrContinueStatement(n) { + // call bind on label (don't affect reachability) + bind(n.label); + // for continue case touch label so it will be marked a used + var isValidJump = jumpToLabel(n.label, n.kind === 203 /* BreakStatement */ ? currentReachabilityState : 4 /* Unreachable */); + if (isValidJump) { + currentReachabilityState = 4 /* Unreachable */; + } + } + function bindTryStatement(n) { + // catch\finally blocks has the same reachability as try block + var preTryState = currentReachabilityState; + bind(n.tryBlock); + var postTryState = currentReachabilityState; + currentReachabilityState = preTryState; + bind(n.catchClause); + var postCatchState = currentReachabilityState; + currentReachabilityState = preTryState; + bind(n.finallyBlock); + // post catch/finally state is reachable if + // - post try state is reachable - control flow can fall out of try block + // - post catch state is reachable - control flow can fall out of catch block + currentReachabilityState = or(postTryState, postCatchState); + } + function bindSwitchStatement(n) { + var preSwitchState = currentReachabilityState; + var postSwitchLabel = pushImplicitLabel(); + // bind expression (don't affect reachability) + bind(n.expression); + bind(n.caseBlock); + var hasDefault = ts.forEach(n.caseBlock.clauses, function (c) { return c.kind === 242 /* DefaultClause */; }); + // post switch state is unreachable if switch is exaustive (has a default case ) and does not have fallthrough from the last case + var postSwitchState = hasDefault && currentReachabilityState !== 2 /* Reachable */ ? 4 /* Unreachable */ : preSwitchState; + popImplicitLabel(postSwitchLabel, postSwitchState); + } + function bindCaseBlock(n) { + var startState = currentReachabilityState; + for (var _i = 0, _a = n.clauses; _i < _a.length; _i++) { + var clause = _a[_i]; + currentReachabilityState = startState; + bind(clause); + if (clause.statements.length && currentReachabilityState === 2 /* Reachable */ && options.noFallthroughCasesInSwitch) { + errorOnFirstToken(clause, ts.Diagnostics.Fallthrough_case_in_switch); + } + } + } + function bindLabeledStatement(n) { + // call bind on label (don't affect reachability) + bind(n.label); + var ok = pushNamedLabel(n.label); + bind(n.statement); + if (ok) { + popNamedLabel(n.label, currentReachabilityState); + } + } + function getContainerFlags(node) { + switch (node.kind) { + case 186 /* ClassExpression */: + case 214 /* ClassDeclaration */: + case 215 /* InterfaceDeclaration */: + case 217 /* EnumDeclaration */: + case 155 /* TypeLiteral */: + case 165 /* ObjectLiteralExpression */: + return 1 /* IsContainer */; + case 147 /* CallSignature */: + case 148 /* ConstructSignature */: + case 149 /* IndexSignature */: + case 143 /* MethodDeclaration */: + case 142 /* MethodSignature */: + case 213 /* FunctionDeclaration */: + case 144 /* Constructor */: + case 145 /* GetAccessor */: + case 146 /* SetAccessor */: + case 152 /* FunctionType */: + case 153 /* ConstructorType */: + case 173 /* FunctionExpression */: + case 174 /* ArrowFunction */: + case 218 /* ModuleDeclaration */: + case 248 /* SourceFile */: + case 216 /* TypeAliasDeclaration */: + return 5 /* IsContainerWithLocals */; + case 244 /* CatchClause */: + case 199 /* ForStatement */: + case 200 /* ForInStatement */: + case 201 /* ForOfStatement */: + case 220 /* CaseBlock */: + return 2 /* IsBlockScopedContainer */; + case 192 /* Block */: + // do not treat blocks directly inside a function as a block-scoped-container. + // Locals that reside in this block should go to the function locals. Othewise 'x' + // would not appear to be a redeclaration of a block scoped local in the following + // example: + // + // function foo() { + // var x; + // let x; + // } + // + // If we placed 'var x' into the function locals and 'let x' into the locals of + // the block, then there would be no collision. + // + // By not creating a new block-scoped-container here, we ensure that both 'var x' + // and 'let x' go into the Function-container's locals, and we do get a collision + // conflict. + return ts.isFunctionLike(node.parent) ? 0 /* None */ : 2 /* IsBlockScopedContainer */; + } + return 0 /* None */; + } + function addToContainerChain(next) { + if (lastContainer) { + lastContainer.nextContainer = next; + } + lastContainer = next; + } + function declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes) { + // Just call this directly so that the return type of this function stays "void". + declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes); + } + function declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes) { + switch (container.kind) { + // Modules, source files, and classes need specialized handling for how their + // members are declared (for example, a member of a class will go into a specific + // symbol table depending on if it is static or not). We defer to specialized + // handlers to take care of declaring these child members. + case 218 /* ModuleDeclaration */: + return declareModuleMember(node, symbolFlags, symbolExcludes); + case 248 /* SourceFile */: + return declareSourceFileMember(node, symbolFlags, symbolExcludes); + case 186 /* ClassExpression */: + case 214 /* ClassDeclaration */: + return declareClassMember(node, symbolFlags, symbolExcludes); + case 217 /* EnumDeclaration */: + return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + case 155 /* TypeLiteral */: + case 165 /* ObjectLiteralExpression */: + case 215 /* InterfaceDeclaration */: + // Interface/Object-types always have their children added to the 'members' of + // their container. They are only accessible through an instance of their + // container, and are never in scope otherwise (even inside the body of the + // object / type / interface declaring them). An exception is type parameters, + // which are in scope without qualification (similar to 'locals'). + return declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); + case 152 /* FunctionType */: + case 153 /* ConstructorType */: + case 147 /* CallSignature */: + case 148 /* ConstructSignature */: + case 149 /* IndexSignature */: + case 143 /* MethodDeclaration */: + case 142 /* MethodSignature */: + case 144 /* Constructor */: + case 145 /* GetAccessor */: + case 146 /* SetAccessor */: + case 213 /* FunctionDeclaration */: + case 173 /* FunctionExpression */: + case 174 /* ArrowFunction */: + case 216 /* TypeAliasDeclaration */: + // All the children of these container types are never visible through another + // symbol (i.e. through another symbol's 'exports' or 'members'). Instead, + // they're only accessed 'lexically' (i.e. from code that exists underneath + // their container in the tree. To accomplish this, we simply add their declared + // symbol to the 'locals' of the container. These symbols can then be found as + // the type checker walks up the containers, checking them for matching names. + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + function declareClassMember(node, symbolFlags, symbolExcludes) { + return node.flags & 64 /* Static */ + ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) + : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); + } + function declareSourceFileMember(node, symbolFlags, symbolExcludes) { + return ts.isExternalModule(file) + ? declareModuleMember(node, symbolFlags, symbolExcludes) + : declareSymbol(file.locals, undefined, node, symbolFlags, symbolExcludes); + } + function hasExportDeclarations(node) { + var body = node.kind === 248 /* SourceFile */ ? node : node.body; + if (body.kind === 248 /* SourceFile */ || body.kind === 219 /* ModuleBlock */) { + for (var _i = 0, _a = body.statements; _i < _a.length; _i++) { + var stat = _a[_i]; + if (stat.kind === 228 /* ExportDeclaration */ || stat.kind === 227 /* ExportAssignment */) { + return true; + } + } + } + return false; + } + function setExportContextFlag(node) { + // A declaration source file or ambient module declaration that contains no export declarations (but possibly regular + // declarations with export modifiers) is an export context in which declarations are implicitly exported. + if (ts.isInAmbientContext(node) && !hasExportDeclarations(node)) { + node.flags |= 131072 /* ExportContext */; + } + else { + node.flags &= ~131072 /* ExportContext */; + } + } + function bindModuleDeclaration(node) { + setExportContextFlag(node); + if (node.name.kind === 9 /* StringLiteral */) { + declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); + } + else { + var state = getModuleInstanceState(node); + if (state === 0 /* NonInstantiated */) { + declareSymbolAndAddToSymbolTable(node, 1024 /* NamespaceModule */, 0 /* NamespaceModuleExcludes */); + } + else { + declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); + if (node.symbol.flags & (16 /* Function */ | 32 /* Class */ | 256 /* RegularEnum */)) { + // if module was already merged with some function, class or non-const enum + // treat is a non-const-enum-only + node.symbol.constEnumOnlyModule = false; + } + else { + var currentModuleIsConstEnumOnly = state === 2 /* ConstEnumOnly */; + if (node.symbol.constEnumOnlyModule === undefined) { + // non-merged case - use the current state + node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly; + } + else { + // merged case: module is const enum only if all its pieces are non-instantiated or const enum + node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly; + } + } + } + } + } + function bindFunctionOrConstructorType(node) { + // For a given function symbol "<...>(...) => T" we want to generate a symbol identical + // to the one we would get for: { <...>(...): T } + // + // We do that by making an anonymous type literal symbol, and then setting the function + // symbol as its sole member. To the rest of the system, this symbol will be indistinguishable + // from an actual type literal symbol you would have gotten had you used the long form. + var symbol = createSymbol(131072 /* Signature */, getDeclarationName(node)); + addDeclarationToSymbol(symbol, node, 131072 /* Signature */); + var typeLiteralSymbol = createSymbol(2048 /* TypeLiteral */, "__type"); + addDeclarationToSymbol(typeLiteralSymbol, node, 2048 /* TypeLiteral */); + typeLiteralSymbol.members = (_a = {}, _a[symbol.name] = symbol, _a); + var _a; + } + function bindObjectLiteralExpression(node) { + var ElementKind; + (function (ElementKind) { + ElementKind[ElementKind["Property"] = 1] = "Property"; + ElementKind[ElementKind["Accessor"] = 2] = "Accessor"; + })(ElementKind || (ElementKind = {})); + if (inStrictMode) { + var seen = {}; + for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { + var prop = _a[_i]; + if (prop.name.kind !== 69 /* Identifier */) { + continue; + } + var identifier = prop.name; + // ECMA-262 11.1.5 Object Initialiser + // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true + // a.This production is contained in strict code and IsDataDescriptor(previous) is true and + // IsDataDescriptor(propId.descriptor) is true. + // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. + // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. + // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true + // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields + var currentKind = prop.kind === 245 /* PropertyAssignment */ || prop.kind === 246 /* ShorthandPropertyAssignment */ || prop.kind === 143 /* MethodDeclaration */ + ? 1 /* Property */ + : 2 /* Accessor */; + var existingKind = seen[identifier.text]; + if (!existingKind) { + seen[identifier.text] = currentKind; + continue; + } + if (currentKind === 1 /* Property */ && existingKind === 1 /* Property */) { + var span = ts.getErrorSpanForNode(file, identifier); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode)); + } + } + } + return bindAnonymousDeclaration(node, 4096 /* ObjectLiteral */, "__object"); + } + function bindAnonymousDeclaration(node, symbolFlags, name) { + var symbol = createSymbol(symbolFlags, name); + addDeclarationToSymbol(symbol, node, symbolFlags); + } + function bindBlockScopedDeclaration(node, symbolFlags, symbolExcludes) { + switch (blockScopeContainer.kind) { + case 218 /* ModuleDeclaration */: + declareModuleMember(node, symbolFlags, symbolExcludes); + break; + case 248 /* SourceFile */: + if (ts.isExternalModule(container)) { + declareModuleMember(node, symbolFlags, symbolExcludes); + break; + } + // fall through. + default: + if (!blockScopeContainer.locals) { + blockScopeContainer.locals = {}; + addToContainerChain(blockScopeContainer); + } + declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + function bindBlockScopedVariableDeclaration(node) { + bindBlockScopedDeclaration(node, 2 /* BlockScopedVariable */, 107455 /* BlockScopedVariableExcludes */); + } + // The binder visits every node in the syntax tree so it is a convenient place to perform a single localized + // check for reserved words used as identifiers in strict mode code. + function checkStrictModeIdentifier(node) { + if (inStrictMode && + node.originalKeywordKind >= 106 /* FirstFutureReservedWord */ && + node.originalKeywordKind <= 114 /* LastFutureReservedWord */ && + !ts.isIdentifierName(node)) { + // Report error only if there are no parse errors in file + if (!file.parseDiagnostics.length) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(node, getStrictModeIdentifierMessage(node), ts.declarationNameToString(node))); + } + } + } + function getStrictModeIdentifierMessage(node) { + // Provide specialized messages to help the user understand why we think they're in + // strict mode. + if (ts.getContainingClass(node)) { + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; + } + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode; + } + function checkStrictModeBinaryExpression(node) { + if (inStrictMode && ts.isLeftHandSideExpression(node.left) && ts.isAssignmentOperator(node.operatorToken.kind)) { + // ECMA 262 (Annex C) The identifier eval or arguments may not appear as the LeftHandSideExpression of an + // Assignment operator(11.13) or of a PostfixExpression(11.3) + checkStrictModeEvalOrArguments(node, node.left); + } + } + function checkStrictModeCatchClause(node) { + // It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the + // Catch production is eval or arguments + if (inStrictMode && node.variableDeclaration) { + checkStrictModeEvalOrArguments(node, node.variableDeclaration.name); + } + } + function checkStrictModeDeleteExpression(node) { + // Grammar checking + if (inStrictMode && node.expression.kind === 69 /* Identifier */) { + // When a delete operator occurs within strict mode code, a SyntaxError is thrown if its + // UnaryExpression is a direct reference to a variable, function argument, or function name + var span = ts.getErrorSpanForNode(file, node.expression); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.delete_cannot_be_called_on_an_identifier_in_strict_mode)); + } + } + function isEvalOrArgumentsIdentifier(node) { + return node.kind === 69 /* Identifier */ && + (node.text === "eval" || node.text === "arguments"); + } + function checkStrictModeEvalOrArguments(contextNode, name) { + if (name && name.kind === 69 /* Identifier */) { + var identifier = name; + if (isEvalOrArgumentsIdentifier(identifier)) { + // We check first if the name is inside class declaration or class expression; if so give explicit message + // otherwise report generic error message. + var span = ts.getErrorSpanForNode(file, name); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, getStrictModeEvalOrArgumentsMessage(contextNode), identifier.text)); + } + } + } + function getStrictModeEvalOrArgumentsMessage(node) { + // Provide specialized messages to help the user understand why we think they're in + // strict mode. + if (ts.getContainingClass(node)) { + return ts.Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { + return ts.Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; + } + return ts.Diagnostics.Invalid_use_of_0_in_strict_mode; + } + function checkStrictModeFunctionName(node) { + if (inStrictMode) { + // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression (13.1)) + checkStrictModeEvalOrArguments(node, node.name); + } + } + function checkStrictModeNumericLiteral(node) { + if (inStrictMode && node.flags & 32768 /* OctalLiteral */) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Octal_literals_are_not_allowed_in_strict_mode)); + } + } + function checkStrictModePostfixUnaryExpression(node) { + // Grammar checking + // The identifier eval or arguments may not appear as the LeftHandSideExpression of an + // Assignment operator(11.13) or of a PostfixExpression(11.3) or as the UnaryExpression + // operated upon by a Prefix Increment(11.4.4) or a Prefix Decrement(11.4.5) operator. + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.operand); + } + } + function checkStrictModePrefixUnaryExpression(node) { + // Grammar checking + if (inStrictMode) { + if (node.operator === 41 /* PlusPlusToken */ || node.operator === 42 /* MinusMinusToken */) { + checkStrictModeEvalOrArguments(node, node.operand); + } + } + } + function checkStrictModeWithStatement(node) { + // Grammar checking for withStatement + if (inStrictMode) { + errorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_strict_mode); + } + } + function errorOnFirstToken(node, message, arg0, arg1, arg2) { + var span = ts.getSpanOfTokenAtPosition(file, node.pos); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2)); + } + function getDestructuringParameterName(node) { + return "__" + ts.indexOf(node.parent.parameters, node); + } + function bind(node) { + if (!node) { + return; + } + node.parent = parent; + var savedInStrictMode = inStrictMode; + if (!savedInStrictMode) { + updateStrictMode(node); + } + // First we bind declaration nodes to a symbol if possible. We'll both create a symbol + // and then potentially add the symbol to an appropriate symbol table. Possible + // destination symbol tables are: + // + // 1) The 'exports' table of the current container's symbol. + // 2) The 'members' table of the current container's symbol. + // 3) The 'locals' table of the current container. + // + // However, not all symbols will end up in any of these tables. 'Anonymous' symbols + // (like TypeLiterals for example) will not be put in any table. + bindWorker(node); + // Then we recurse into the children of the node to bind them as well. For certain + // symbols we do specialized work when we recurse. For example, we'll keep track of + // the current 'container' node when it changes. This helps us know which symbol table + // a local should go into for example. + bindChildren(node); + inStrictMode = savedInStrictMode; + } + function updateStrictMode(node) { + switch (node.kind) { + case 248 /* SourceFile */: + case 219 /* ModuleBlock */: + updateStrictModeStatementList(node.statements); + return; + case 192 /* Block */: + if (ts.isFunctionLike(node.parent)) { + updateStrictModeStatementList(node.statements); + } + return; + case 214 /* ClassDeclaration */: + case 186 /* ClassExpression */: + // All classes are automatically in strict mode in ES6. + inStrictMode = true; + return; + } + } + function updateStrictModeStatementList(statements) { + for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) { + var statement = statements_1[_i]; + if (!ts.isPrologueDirective(statement)) { + return; + } + if (isUseStrictPrologueDirective(statement)) { + inStrictMode = true; + return; + } + } + } + /// Should be called only on prologue directives (isPrologueDirective(node) should be true) + function isUseStrictPrologueDirective(node) { + var nodeText = ts.getTextOfNodeFromSourceText(file.text, node.expression); + // Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the + // string to contain unicode escapes (as per ES5). + return nodeText === "\"use strict\"" || nodeText === "'use strict'"; + } + function bindWorker(node) { + switch (node.kind) { + /* Strict mode checks */ + case 69 /* Identifier */: + return checkStrictModeIdentifier(node); + case 181 /* BinaryExpression */: + if (ts.isInJavaScriptFile(node)) { + if (ts.isExportsPropertyAssignment(node)) { + bindExportsPropertyAssignment(node); + } + else if (ts.isModuleExportsAssignment(node)) { + bindModuleExportsAssignment(node); + } + } + return checkStrictModeBinaryExpression(node); + case 244 /* CatchClause */: + return checkStrictModeCatchClause(node); + case 175 /* DeleteExpression */: + return checkStrictModeDeleteExpression(node); + case 8 /* NumericLiteral */: + return checkStrictModeNumericLiteral(node); + case 180 /* PostfixUnaryExpression */: + return checkStrictModePostfixUnaryExpression(node); + case 179 /* PrefixUnaryExpression */: + return checkStrictModePrefixUnaryExpression(node); + case 205 /* WithStatement */: + return checkStrictModeWithStatement(node); + case 97 /* ThisKeyword */: + seenThisKeyword = true; + return; + case 137 /* TypeParameter */: + return declareSymbolAndAddToSymbolTable(node, 262144 /* TypeParameter */, 530912 /* TypeParameterExcludes */); + case 138 /* Parameter */: + return bindParameter(node); + case 211 /* VariableDeclaration */: + case 163 /* BindingElement */: + return bindVariableDeclarationOrBindingElement(node); + case 141 /* PropertyDeclaration */: + case 140 /* PropertySignature */: + return bindPropertyOrMethodOrAccessor(node, 4 /* Property */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), 107455 /* PropertyExcludes */); + case 245 /* PropertyAssignment */: + case 246 /* ShorthandPropertyAssignment */: + return bindPropertyOrMethodOrAccessor(node, 4 /* Property */, 107455 /* PropertyExcludes */); + case 247 /* EnumMember */: + return bindPropertyOrMethodOrAccessor(node, 8 /* EnumMember */, 107455 /* EnumMemberExcludes */); + case 147 /* CallSignature */: + case 148 /* ConstructSignature */: + case 149 /* IndexSignature */: + return declareSymbolAndAddToSymbolTable(node, 131072 /* Signature */, 0 /* None */); + case 143 /* MethodDeclaration */: + case 142 /* MethodSignature */: + // If this is an ObjectLiteralExpression method, then it sits in the same space + // as other properties in the object literal. So we use SymbolFlags.PropertyExcludes + // so that it will conflict with any other object literal members with the same + // name. + return bindPropertyOrMethodOrAccessor(node, 8192 /* Method */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), ts.isObjectLiteralMethod(node) ? 107455 /* PropertyExcludes */ : 99263 /* MethodExcludes */); + case 213 /* FunctionDeclaration */: + checkStrictModeFunctionName(node); + return declareSymbolAndAddToSymbolTable(node, 16 /* Function */, 106927 /* FunctionExcludes */); + case 144 /* Constructor */: + return declareSymbolAndAddToSymbolTable(node, 16384 /* Constructor */, /*symbolExcludes:*/ 0 /* None */); + case 145 /* GetAccessor */: + return bindPropertyOrMethodOrAccessor(node, 32768 /* GetAccessor */, 41919 /* GetAccessorExcludes */); + case 146 /* SetAccessor */: + return bindPropertyOrMethodOrAccessor(node, 65536 /* SetAccessor */, 74687 /* SetAccessorExcludes */); + case 152 /* FunctionType */: + case 153 /* ConstructorType */: + return bindFunctionOrConstructorType(node); + case 155 /* TypeLiteral */: + return bindAnonymousDeclaration(node, 2048 /* TypeLiteral */, "__type"); + case 165 /* ObjectLiteralExpression */: + return bindObjectLiteralExpression(node); + case 173 /* FunctionExpression */: + case 174 /* ArrowFunction */: + checkStrictModeFunctionName(node); + var bindingName = node.name ? node.name.text : "__function"; + return bindAnonymousDeclaration(node, 16 /* Function */, bindingName); + case 168 /* CallExpression */: + if (ts.isInJavaScriptFile(node)) { + bindCallExpression(node); + } + break; + // Members of classes, interfaces, and modules + case 186 /* ClassExpression */: + case 214 /* ClassDeclaration */: + return bindClassLikeDeclaration(node); + case 215 /* InterfaceDeclaration */: + return bindBlockScopedDeclaration(node, 64 /* Interface */, 792960 /* InterfaceExcludes */); + case 216 /* TypeAliasDeclaration */: + return bindBlockScopedDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */); + case 217 /* EnumDeclaration */: + return bindEnumDeclaration(node); + case 218 /* ModuleDeclaration */: + return bindModuleDeclaration(node); + // Imports and exports + case 221 /* ImportEqualsDeclaration */: + case 224 /* NamespaceImport */: + case 226 /* ImportSpecifier */: + case 230 /* ExportSpecifier */: + return declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); + case 223 /* ImportClause */: + return bindImportClause(node); + case 228 /* ExportDeclaration */: + return bindExportDeclaration(node); + case 227 /* ExportAssignment */: + return bindExportAssignment(node); + case 248 /* SourceFile */: + return bindSourceFileIfExternalModule(); + } + } + function bindSourceFileIfExternalModule() { + setExportContextFlag(file); + if (ts.isExternalModule(file)) { + bindSourceFileAsExternalModule(); + } + } + function bindSourceFileAsExternalModule() { + bindAnonymousDeclaration(file, 512 /* ValueModule */, "\"" + ts.removeFileExtension(file.fileName) + "\""); + } + function bindExportAssignment(node) { + var boundExpression = node.kind === 227 /* ExportAssignment */ ? node.expression : node.right; + if (!container.symbol || !container.symbol.exports) { + // Export assignment in some sort of block construct + bindAnonymousDeclaration(node, 8388608 /* Alias */, getDeclarationName(node)); + } + else if (boundExpression.kind === 69 /* Identifier */) { + // An export default clause with an identifier exports all meanings of that identifier + declareSymbol(container.symbol.exports, container.symbol, node, 8388608 /* Alias */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); + } + else { + // An export default clause with an expression exports a value + declareSymbol(container.symbol.exports, container.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); + } + } + function bindExportDeclaration(node) { + if (!container.symbol || !container.symbol.exports) { + // Export * in some sort of block construct + bindAnonymousDeclaration(node, 1073741824 /* ExportStar */, getDeclarationName(node)); + } + else if (!node.exportClause) { + // All export * declarations are collected in an __export symbol + declareSymbol(container.symbol.exports, container.symbol, node, 1073741824 /* ExportStar */, 0 /* None */); + } + } + function bindImportClause(node) { + if (node.name) { + declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); + } + } + function setCommonJsModuleIndicator(node) { + if (!file.commonJsModuleIndicator) { + file.commonJsModuleIndicator = node; + bindSourceFileAsExternalModule(); + } + } + function bindExportsPropertyAssignment(node) { + // When we create a property via 'exports.foo = bar', the 'exports.foo' property access + // expression is the declaration + setCommonJsModuleIndicator(node); + declareSymbol(file.symbol.exports, file.symbol, node.left, 4 /* Property */ | 7340032 /* Export */, 0 /* None */); + } + function bindModuleExportsAssignment(node) { + // 'module.exports = expr' assignment + setCommonJsModuleIndicator(node); + bindExportAssignment(node); + } + function bindCallExpression(node) { + // We're only inspecting call expressions to detect CommonJS modules, so we can skip + // this check if we've already seen the module indicator + if (!file.commonJsModuleIndicator && ts.isRequireCall(node)) { + setCommonJsModuleIndicator(node); + } + } + function bindClassLikeDeclaration(node) { + if (node.kind === 214 /* ClassDeclaration */) { + bindBlockScopedDeclaration(node, 32 /* Class */, 899519 /* ClassExcludes */); + } + else { + var bindingName = node.name ? node.name.text : "__class"; + bindAnonymousDeclaration(node, 32 /* Class */, bindingName); + // Add name of class expression into the map for semantic classifier + if (node.name) { + classifiableNames[node.name.text] = node.name.text; + } + } + var symbol = node.symbol; + // TypeScript 1.0 spec (April 2014): 8.4 + // Every class automatically contains a static property member named 'prototype', the + // type of which is an instantiation of the class type with type Any supplied as a type + // argument for each type parameter. It is an error to explicitly declare a static + // property member with the name 'prototype'. + // + // Note: we check for this here because this class may be merging into a module. The + // module might have an exported variable called 'prototype'. We can't allow that as + // that would clash with the built-in 'prototype' for the class. + var prototypeSymbol = createSymbol(4 /* Property */ | 134217728 /* Prototype */, "prototype"); + if (ts.hasProperty(symbol.exports, prototypeSymbol.name)) { + if (node.name) { + node.name.parent = node; + } + file.bindDiagnostics.push(ts.createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0], ts.Diagnostics.Duplicate_identifier_0, prototypeSymbol.name)); + } + symbol.exports[prototypeSymbol.name] = prototypeSymbol; + prototypeSymbol.parent = symbol; + } + function bindEnumDeclaration(node) { + return ts.isConst(node) + ? bindBlockScopedDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */) + : bindBlockScopedDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */); + } + function bindVariableDeclarationOrBindingElement(node) { + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.name); + } + if (!ts.isBindingPattern(node.name)) { + if (ts.isBlockOrCatchScoped(node)) { + bindBlockScopedVariableDeclaration(node); + } + else if (ts.isParameterDeclaration(node)) { + // It is safe to walk up parent chain to find whether the node is a destructing parameter declaration + // because its parent chain has already been set up, since parents are set before descending into children. + // + // If node is a binding element in parameter declaration, we need to use ParameterExcludes. + // Using ParameterExcludes flag allows the compiler to report an error on duplicate identifiers in Parameter Declaration + // For example: + // function foo([a,a]) {} // Duplicate Identifier error + // function bar(a,a) {} // Duplicate Identifier error, parameter declaration in this case is handled in bindParameter + // // which correctly set excluded symbols + declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); + } + else { + declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107454 /* FunctionScopedVariableExcludes */); + } + } + } + function bindParameter(node) { + if (inStrictMode) { + // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a + // strict mode FunctionLikeDeclaration or FunctionExpression(13.1) + checkStrictModeEvalOrArguments(node, node.name); + } + if (ts.isBindingPattern(node.name)) { + bindAnonymousDeclaration(node, 1 /* FunctionScopedVariable */, getDestructuringParameterName(node)); + } + else { + declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); + } + // If this is a property-parameter, then also declare the property symbol into the + // containing class. + if (node.flags & 56 /* AccessibilityModifier */ && + node.parent.kind === 144 /* Constructor */ && + ts.isClassLike(node.parent.parent)) { + var classDeclaration = node.parent.parent; + declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */); + } + } + function bindPropertyOrMethodOrAccessor(node, symbolFlags, symbolExcludes) { + return ts.hasDynamicName(node) + ? bindAnonymousDeclaration(node, symbolFlags, "__computed") + : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); + } + // reachability checks + function pushNamedLabel(name) { + initializeReachabilityStateIfNecessary(); + if (ts.hasProperty(labelIndexMap, name.text)) { + return false; + } + labelIndexMap[name.text] = labelStack.push(1 /* Unintialized */) - 1; + return true; + } + function pushImplicitLabel() { + initializeReachabilityStateIfNecessary(); + var index = labelStack.push(1 /* Unintialized */) - 1; + implicitLabels.push(index); + return index; + } + function popNamedLabel(label, outerState) { + var index = labelIndexMap[label.text]; + ts.Debug.assert(index !== undefined); + ts.Debug.assert(labelStack.length == index + 1); + labelIndexMap[label.text] = undefined; + setCurrentStateAtLabel(labelStack.pop(), outerState, label); + } + function popImplicitLabel(implicitLabelIndex, outerState) { + if (labelStack.length !== implicitLabelIndex + 1) { + ts.Debug.assert(false, "Label stack: " + labelStack.length + ", index:" + implicitLabelIndex); + } + var i = implicitLabels.pop(); + if (implicitLabelIndex !== i) { + ts.Debug.assert(false, "i: " + i + ", index: " + implicitLabelIndex); + } + setCurrentStateAtLabel(labelStack.pop(), outerState, /*name*/ undefined); + } + function setCurrentStateAtLabel(innerMergedState, outerState, label) { + if (innerMergedState === 1 /* Unintialized */) { + if (label && !options.allowUnusedLabels) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(label, ts.Diagnostics.Unused_label)); + } + currentReachabilityState = outerState; + } + else { + currentReachabilityState = or(innerMergedState, outerState); + } + } + function jumpToLabel(label, outerState) { + initializeReachabilityStateIfNecessary(); + var index = label ? labelIndexMap[label.text] : ts.lastOrUndefined(implicitLabels); + if (index === undefined) { + // reference to unknown label or + // break/continue used outside of loops + return false; + } + var stateAtLabel = labelStack[index]; + labelStack[index] = stateAtLabel === 1 /* Unintialized */ ? outerState : or(stateAtLabel, outerState); + return true; + } + function checkUnreachable(node) { + switch (currentReachabilityState) { + case 4 /* Unreachable */: + var reportError = + // report error on all statements except empty ones + (ts.isStatement(node) && node.kind !== 194 /* EmptyStatement */) || + // report error on class declarations + node.kind === 214 /* ClassDeclaration */ || + // report error on instantiated modules or const-enums only modules if preserveConstEnums is set + (node.kind === 218 /* ModuleDeclaration */ && shouldReportErrorOnModuleDeclaration(node)) || + // report error on regular enums and const enums if preserveConstEnums is set + (node.kind === 217 /* EnumDeclaration */ && (!ts.isConstEnumDeclaration(node) || options.preserveConstEnums)); + if (reportError) { + currentReachabilityState = 8 /* ReportedUnreachable */; + // unreachable code is reported if + // - user has explicitly asked about it AND + // - statement is in not ambient context (statements in ambient context is already an error + // so we should not report extras) AND + // - node is not variable statement OR + // - node is block scoped variable statement OR + // - node is not block scoped variable statement and at least one variable declaration has initializer + // Rationale: we don't want to report errors on non-initialized var's since they are hoisted + // On the other side we do want to report errors on non-initialized 'lets' because of TDZ + var reportUnreachableCode = !options.allowUnreachableCode && + !ts.isInAmbientContext(node) && + (node.kind !== 193 /* VariableStatement */ || + ts.getCombinedNodeFlags(node.declarationList) & 24576 /* BlockScoped */ || + ts.forEach(node.declarationList.declarations, function (d) { return d.initializer; })); + if (reportUnreachableCode) { + errorOnFirstToken(node, ts.Diagnostics.Unreachable_code_detected); + } + } + case 8 /* ReportedUnreachable */: + return true; + default: + return false; + } + function shouldReportErrorOnModuleDeclaration(node) { + var instanceState = getModuleInstanceState(node); + return instanceState === 1 /* Instantiated */ || (instanceState === 2 /* ConstEnumOnly */ && options.preserveConstEnums); + } + } + function initializeReachabilityStateIfNecessary() { + if (labelIndexMap) { + return; + } + currentReachabilityState = 2 /* Reachable */; + labelIndexMap = {}; + labelStack = []; + implicitLabels = []; + } + } +})(ts || (ts = {})); /// /* @internal */ var ts; @@ -13755,7 +13918,7 @@ var ts; symbolToString: symbolToString, getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, getRootSymbols: getRootSymbols, - getContextualType: getContextualType, + getContextualType: getApparentTypeOfContextualType, getFullyQualifiedName: getFullyQualifiedName, getResolvedSignature: getResolvedSignature, getConstantValue: getConstantValue, @@ -14025,7 +14188,7 @@ var ts; return ts.getAncestor(node, 248 /* SourceFile */); } function isGlobalSourceFile(node) { - return node.kind === 248 /* SourceFile */ && !ts.isExternalModule(node); + return node.kind === 248 /* SourceFile */ && !ts.isExternalOrCommonJsModule(node); } function getSymbol(symbols, name, meaning) { if (meaning && ts.hasProperty(symbols, name)) { @@ -14127,15 +14290,24 @@ var ts; } switch (location.kind) { case 248 /* SourceFile */: - if (!ts.isExternalModule(location)) + if (!ts.isExternalOrCommonJsModule(location)) break; case 218 /* ModuleDeclaration */: var moduleExports = getSymbolOfNode(location).exports; if (location.kind === 248 /* SourceFile */ || (location.kind === 218 /* ModuleDeclaration */ && location.name.kind === 9 /* StringLiteral */)) { - // It's an external module. Because of module/namespace merging, a module's exports are in scope, - // yet we never want to treat an export specifier as putting a member in scope. Therefore, - // if the name we find is purely an export specifier, it is not actually considered in scope. + // It's an external module. First see if the module has an export default and if the local + // name of that export default matches. + if (result = moduleExports["default"]) { + var localSymbol = ts.getLocalSymbolForExportDefault(result); + if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { + break loop; + } + result = undefined; + } + // Because of module/namespace merging, a module's exports are in scope, + // yet we never want to treat an export specifier as putting a member in scope. + // Therefore, if the name we find is purely an export specifier, it is not actually considered in scope. // Two things to note about this: // 1. We have to check this without calling getSymbol. The problem with calling getSymbol // on an export specifier is that it might find the export specifier itself, and try to @@ -14149,12 +14321,6 @@ var ts; ts.getDeclarationOfKind(moduleExports[name], 230 /* ExportSpecifier */)) { break; } - result = moduleExports["default"]; - var localSymbol = ts.getLocalSymbolForExportDefault(result); - if (result && localSymbol && (result.flags & meaning) && localSymbol.name === name) { - break loop; - } - result = undefined; } if (result = getSymbol(moduleExports, name, meaning & 8914931 /* ModuleMember */)) { break loop; @@ -14297,7 +14463,7 @@ var ts; // declare module foo { // interface bar {} // } - // let foo/*1*/: foo/*2*/.bar; + // const foo/*1*/: foo/*2*/.bar; // The foo at /*1*/ and /*2*/ will share same symbol with two meaning // block - scope variable and namespace module. However, only when we // try to resolve name in /*1*/ which is used in variable position, @@ -14601,6 +14767,9 @@ var ts; if (moduleName === undefined) { return; } + if (moduleName.indexOf("!") >= 0) { + moduleName = moduleName.substr(0, moduleName.indexOf("!")); + } var isRelative = ts.isExternalModuleNameRelative(moduleName); if (!isRelative) { var symbol = getSymbol(globals, "\"" + moduleName + "\"", 512 /* ValueModule */); @@ -14788,7 +14957,7 @@ var ts; } switch (location_1.kind) { case 248 /* SourceFile */: - if (!ts.isExternalModule(location_1)) { + if (!ts.isExternalOrCommonJsModule(location_1)) { break; } case 218 /* ModuleDeclaration */: @@ -14910,7 +15079,7 @@ var ts; // export class c { // } // } - // let x: typeof m.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 @@ -14949,7 +15118,7 @@ var ts; } function hasExternalModuleSymbol(declaration) { return (declaration.kind === 218 /* ModuleDeclaration */ && declaration.name.kind === 9 /* StringLiteral */) || - (declaration.kind === 248 /* SourceFile */ && ts.isExternalModule(declaration)); + (declaration.kind === 248 /* SourceFile */ && ts.isExternalOrCommonJsModule(declaration)); } function hasVisibleDeclarations(symbol) { var aliasesToMakeVisible; @@ -15100,7 +15269,7 @@ var ts; parentSymbol = symbol; appendSymbolNameOnly(symbol, writer); } - // Let the writer know we just wrote out a symbol. The declaration emitter writer uses + // 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. // @@ -15181,7 +15350,7 @@ var ts; writeAnonymousType(type, flags); } else if (type.flags & 256 /* StringLiteral */) { - writer.writeStringLiteral(type.text); + writer.writeStringLiteral("\"" + ts.escapeString(type.text) + "\""); } else { // Should never get here @@ -15568,7 +15737,7 @@ var ts; } } else if (node.kind === 248 /* SourceFile */) { - return ts.isExternalModule(node) ? node : undefined; + return ts.isExternalOrCommonJsModule(node) ? node : undefined; } } ts.Debug.fail("getContainingModule cant reach here"); @@ -15650,7 +15819,7 @@ var ts; // Private/protected properties/methods are not visible return false; } - // Public properties/methods are visible if its parents are visible, so let it fall into next case statement + // Public properties/methods are visible if its parents are visible, so const it fall into next case statement case 144 /* Constructor */: case 148 /* ConstructSignature */: case 147 /* CallSignature */: @@ -15678,7 +15847,7 @@ var ts; // Source file is always visible case 248 /* SourceFile */: return true; - // Export assignements do not create name bindings outside the module + // Export assignments do not create name bindings outside the module case 227 /* ExportAssignment */: return false; default: @@ -15814,6 +15983,23 @@ var ts; var symbol = getSymbolOfNode(node); return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node); } + function getTextOfPropertyName(name) { + switch (name.kind) { + case 69 /* Identifier */: + return name.text; + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + return name.text; + case 136 /* ComputedPropertyName */: + if (ts.isStringOrNumericLiteral(name.expression.kind)) { + return name.expression.text; + } + } + return undefined; + } + function isComputedNonLiteralName(name) { + return name.kind === 136 /* ComputedPropertyName */ && !ts.isStringOrNumericLiteral(name.expression.kind); + } // Return the inferred type for a binding element function getTypeForBindingElement(declaration) { var pattern = declaration.parent; @@ -15835,10 +16021,15 @@ var ts; if (pattern.kind === 161 /* ObjectBindingPattern */) { // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) var name_10 = declaration.propertyName || declaration.name; + if (isComputedNonLiteralName(name_10)) { + // computed properties with non-literal names are treated as 'any' + return anyType; + } // 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. - type = getTypeOfPropertyOfType(parentType, name_10.text) || - isNumericLiteralName(name_10.text) && getIndexTypeOfType(parentType, 1 /* Number */) || + var text = getTextOfPropertyName(name_10); + type = getTypeOfPropertyOfType(parentType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1 /* Number */) || getIndexTypeOfType(parentType, 0 /* String */); if (!type) { error(name_10, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_10)); @@ -15938,10 +16129,17 @@ var ts; // Return the type implied by an object binding pattern function getTypeFromObjectBindingPattern(pattern, includePatternInType) { var members = {}; + var hasComputedProperties = false; ts.forEach(pattern.elements, function (e) { - var flags = 4 /* Property */ | 67108864 /* Transient */ | (e.initializer ? 536870912 /* Optional */ : 0); var name = e.propertyName || e.name; - var symbol = createSymbol(flags, name.text); + 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); symbol.type = getTypeFromBindingElement(e, includePatternInType); symbol.bindingElement = e; members[symbol.name] = symbol; @@ -15950,6 +16148,9 @@ var ts; if (includePatternInType) { result.pattern = pattern; } + if (hasComputedProperties) { + result.flags |= 67108864 /* ObjectLiteralPatternWithComputedProperties */; + } return result; } // Return the type implied by an array binding pattern @@ -16026,6 +16227,14 @@ var ts; if (declaration.kind === 227 /* ExportAssignment */) { return links.type = checkExpression(declaration.expression); } + // Handle module.exports = expr + if (declaration.kind === 181 /* BinaryExpression */) { + return links.type = checkExpression(declaration.right); + } + // Handle exports.p = expr + if (declaration.kind === 166 /* PropertyAccessExpression */) { + return checkExpressionCached(declaration.parent.right); + } // Handle variable, parameter or property if (!pushTypeResolution(symbol, 0 /* Type */)) { return unknownType; @@ -16304,23 +16513,25 @@ var ts; } function resolveBaseTypesOfClass(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - var baseContructorType = getBaseConstructorTypeOfClass(type); - if (!(baseContructorType.flags & 80896 /* ObjectType */)) { + var baseConstructorType = getBaseConstructorTypeOfClass(type); + if (!(baseConstructorType.flags & 80896 /* ObjectType */)) { return; } var baseTypeNode = getBaseTypeNodeOfClass(type); var baseType; - if (baseContructorType.symbol && baseContructorType.symbol.flags & 32 /* Class */) { - // When base constructor type is a class we know that the constructors all have the same type parameters as the + var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; + if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 /* Class */ && + areAllOuterTypeParametersApplied(originalBaseType)) { + // When base constructor type is a class with no captured type arguments we know that the constructors all have the same type parameters as the // class and all return the instance type of the class. There is no need for further checks and we can apply the // type arguments in the same manner as a type reference to get the same error reporting experience. - baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseContructorType.symbol); + baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); } else { // The class derives from a "class-like" constructor function, check that we have at least one construct signature // with a matching number of type parameters and use the return type of the first instantiated signature. Elsewhere // we check that all instantiated signatures return the same type. - var constructors = getInstantiatedConstructorsForTypeArguments(baseContructorType, baseTypeNode.typeArguments); + var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); if (!constructors.length) { error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); return; @@ -16345,6 +16556,17 @@ var ts; type.resolvedBaseTypes.push(baseType); } } + function areAllOuterTypeParametersApplied(type) { + // An unapplied type parameter has its symbol still the same as the matching argument symbol. + // Since parameters are applied outer-to-inner, only the last outer parameter needs to be checked. + var outerTypeParameters = type.outerTypeParameters; + if (outerTypeParameters) { + var last = outerTypeParameters.length - 1; + var typeArguments = type.typeArguments; + return outerTypeParameters[last].symbol !== typeArguments[last].symbol; + } + return true; + } function resolveBaseTypesOfInterface(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { @@ -16424,7 +16646,7 @@ var ts; type.typeArguments = type.typeParameters; type.thisType = createType(512 /* TypeParameter */ | 33554432 /* ThisType */); type.thisType.symbol = symbol; - type.thisType.constraint = getTypeWithThisArgument(type); + type.thisType.constraint = type; } } return links.declaredType; @@ -16939,6 +17161,20 @@ var ts; type = getApparentType(type); return type.flags & 49152 /* UnionOrIntersection */ ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); } + /** + * The apparent type of a type parameter is the base constraint instantiated with the type parameter + * as the type argument for the 'this' type. + */ + function getApparentTypeOfTypeParameter(type) { + if (!type.resolvedApparentType) { + var constraintType = getConstraintOfTypeParameter(type); + while (constraintType && constraintType.flags & 512 /* TypeParameter */) { + constraintType = getConstraintOfTypeParameter(constraintType); + } + type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); + } + return type.resolvedApparentType; + } /** * For a type parameter, return the base constraint of the type parameter. For the string, number, * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the @@ -16946,12 +17182,7 @@ var ts; */ function getApparentType(type) { if (type.flags & 512 /* TypeParameter */) { - do { - type = getConstraintOfTypeParameter(type); - } while (type && type.flags & 512 /* TypeParameter */); - if (!type) { - type = emptyObjectType; - } + type = getApparentTypeOfTypeParameter(type); } if (type.flags & 258 /* StringLike */) { type = globalStringType; @@ -17116,7 +17347,7 @@ var ts; if (node.initializer) { var signatureDeclaration = node.parent; var signature = getSignatureFromDeclaration(signatureDeclaration); - var parameterIndex = signatureDeclaration.parameters.indexOf(node); + var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); ts.Debug.assert(parameterIndex >= 0); return parameterIndex >= signature.minArgumentCount; } @@ -17217,6 +17448,16 @@ var ts; } return result; } + function resolveExternalModuleTypeByLiteral(name) { + var moduleSym = resolveExternalModuleName(name, name); + if (moduleSym) { + var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); + if (resolvedModuleSymbol) { + return getTypeOfSymbol(resolvedModuleSymbol); + } + } + return anyType; + } function getReturnTypeOfSignature(signature) { if (!signature.resolvedReturnType) { if (!pushTypeResolution(signature, 3 /* ResolvedReturnType */)) { @@ -17740,11 +17981,12 @@ var ts; return links.resolvedType; } function getStringLiteralType(node) { - if (ts.hasProperty(stringLiteralTypes, node.text)) { - return stringLiteralTypes[node.text]; + var text = node.text; + if (ts.hasProperty(stringLiteralTypes, text)) { + return stringLiteralTypes[text]; } - var type = stringLiteralTypes[node.text] = createType(256 /* StringLiteral */); - type.text = ts.getTextOfNode(node); + var type = stringLiteralTypes[text] = createType(256 /* StringLiteral */); + type.text = text; return type; } function getTypeFromStringLiteral(node) { @@ -18265,7 +18507,7 @@ var ts; return false; } function hasExcessProperties(source, target, reportErrors) { - if (someConstituentTypeHasKind(target, 80896 /* ObjectType */)) { + if (!(target.flags & 67108864 /* ObjectLiteralPatternWithComputedProperties */) && someConstituentTypeHasKind(target, 80896 /* ObjectType */)) { for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { var prop = _a[_i]; if (!isKnownProperty(target, prop.name)) { @@ -18358,9 +18600,6 @@ var ts; return result; } function typeParameterIdenticalTo(source, target) { - if (source.symbol.name !== target.symbol.name) { - return 0 /* False */; - } // covers case when both type parameters does not have constraint (both equal to noConstraintType) if (source.constraint === target.constraint) { return -1 /* True */; @@ -18852,18 +19091,29 @@ var ts; } return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } + function isMatchingSignature(source, target, partialMatch) { + // A source signature matches a target signature if the two signatures have the same number of required, + // optional, and rest parameters. + if (source.parameters.length === target.parameters.length && + source.minArgumentCount === target.minArgumentCount && + source.hasRestParameter === target.hasRestParameter) { + return true; + } + // A source signature partially matches a target signature if the target signature has no fewer required + // parameters and no more overall parameters than the source signature (where a signature with a rest + // parameter is always considered to have more overall parameters than one without). + if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (source.hasRestParameter && !target.hasRestParameter || + source.hasRestParameter === target.hasRestParameter && source.parameters.length >= target.parameters.length)) { + return true; + } + return false; + } function compareSignatures(source, target, partialMatch, ignoreReturnTypes, compareTypes) { if (source === target) { return -1 /* True */; } - if (source.parameters.length !== target.parameters.length || - source.minArgumentCount !== target.minArgumentCount || - source.hasRestParameter !== target.hasRestParameter) { - if (!partialMatch || - source.parameters.length < target.parameters.length && !source.hasRestParameter || - source.minArgumentCount > target.minArgumentCount) { - return 0 /* False */; - } + if (!(isMatchingSignature(source, target, partialMatch))) { + return 0 /* False */; } var result = -1 /* True */; if (source.typeParameters && target.typeParameters) { @@ -18957,6 +19207,9 @@ var ts; function isTupleLikeType(type) { return !!getPropertyOfType(type, "0"); } + function isStringLiteralType(type) { + return type.flags & 256 /* StringLiteral */; + } /** * Check if a Type was written as a tuple type literal. * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. @@ -19629,7 +19882,7 @@ var ts; } function narrowTypeByInstanceof(type, expr, assumeTrue) { // Check that type is not any, assumed result is true, and we have variable symbol on the left - if (isTypeAny(type) || !assumeTrue || expr.left.kind !== 69 /* Identifier */ || getResolvedSymbol(expr.left) !== symbol) { + if (isTypeAny(type) || expr.left.kind !== 69 /* Identifier */ || getResolvedSymbol(expr.left) !== symbol) { return type; } // Check that right operand is a function type with a prototype property @@ -19660,6 +19913,12 @@ var ts; } } if (targetType) { + if (!assumeTrue) { + if (type.flags & 16384 /* Union */) { + return getUnionType(ts.filter(type.types, function (t) { return !isTypeSubtypeOf(t, targetType); })); + } + return type; + } return getNarrowedType(type, targetType); } return type; @@ -20129,6 +20388,9 @@ var ts; function getIndexTypeOfContextualType(type, kind) { return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); } + function contextualTypeIsStringLiteralType(type) { + return !!(type.flags & 16384 /* Union */ ? ts.forEach(type.types, isStringLiteralType) : isStringLiteralType(type)); + } // Return true if the given contextual type is a tuple-like type function contextualTypeIsTupleLikeType(type) { return !!(type.flags & 16384 /* Union */ ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); @@ -20150,7 +20412,7 @@ var ts; } function getContextualTypeForObjectLiteralElement(element) { var objectLiteral = element.parent; - var type = getContextualType(objectLiteral); + var type = getApparentTypeOfContextualType(objectLiteral); if (type) { if (!ts.hasDynamicName(element)) { // For a (non-symbol) computed property, there is no reason to look up the name @@ -20173,7 +20435,7 @@ var ts; // type of T. function getContextualTypeForElementExpression(node) { var arrayLiteral = node.parent; - var type = getContextualType(arrayLiteral); + var type = getApparentTypeOfContextualType(arrayLiteral); if (type) { var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) @@ -20206,11 +20468,28 @@ var ts; } // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily // be "pushed" onto a node using the contextualType property. - function getContextualType(node) { - var type = getContextualTypeWorker(node); + function getApparentTypeOfContextualType(node) { + var type = getContextualType(node); return type && getApparentType(type); } - function getContextualTypeWorker(node) { + /** + * Woah! Do you really want to use this function? + * + * Unless you're trying to get the *non-apparent* type for a + * value-literal type or you're authoring relevant portions of this algorithm, + * you probably meant to use 'getApparentTypeOfContextualType'. + * Otherwise this may not be very useful. + * + * In cases where you *are* working on this function, you should understand + * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContetxualType'. + * + * - Use 'getContextualType' when you are simply going to propagate the result to the expression. + * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type. + * + * @param node the expression whose contextual type will be returned. + * @returns the contextual type of an expression. + */ + function getContextualType(node) { if (isInsideWithStatementBody(node)) { // We cannot answer semantic questions within a with block, do not proceed any further return undefined; @@ -20285,7 +20564,7 @@ var ts; ts.Debug.assert(node.kind !== 143 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); var type = ts.isObjectLiteralMethod(node) ? getContextualTypeForObjectLiteralMethod(node) - : getContextualType(node); + : getApparentTypeOfContextualType(node); if (!type) { return undefined; } @@ -20411,7 +20690,7 @@ var ts; type.pattern = node; return type; } - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { var pattern = contextualType.pattern; // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting @@ -20494,10 +20773,11 @@ var ts; checkGrammarObjectLiteralExpression(node, inDestructuringPattern); var propertiesTable = {}; var propertiesArray = []; - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); var contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === 161 /* ObjectBindingPattern */ || contextualType.pattern.kind === 165 /* ObjectLiteralExpression */); var typeFlags = 0; + var patternWithComputedProperties = false; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var memberDecl = _a[_i]; var member = memberDecl.symbol; @@ -20525,8 +20805,11 @@ var ts; if (isOptional) { prop.flags |= 536870912 /* Optional */; } + if (ts.hasDynamicName(memberDecl)) { + patternWithComputedProperties = true; + } } - else if (contextualTypeHasPattern) { + else if (contextualTypeHasPattern && !(contextualType.flags & 67108864 /* ObjectLiteralPatternWithComputedProperties */)) { // If object literal is contextually typed by the implied type of a binding pattern, and if the // binding pattern specifies a default value for the property, make the property optional. var impliedProp = getPropertyOfType(contextualType, member.name); @@ -20578,7 +20861,7 @@ var ts; var numberIndexType = getIndexType(1 /* Number */); var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexType, numberIndexType); var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 1048576 /* FreshObjectLiteral */; - result.flags |= 524288 /* ObjectLiteral */ | 4194304 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 14680064 /* PropagatingFlags */); + result.flags |= 524288 /* ObjectLiteral */ | 4194304 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 14680064 /* PropagatingFlags */) | (patternWithComputedProperties ? 67108864 /* ObjectLiteralPatternWithComputedProperties */ : 0); if (inDestructuringPattern) { result.pattern = node; } @@ -21289,7 +21572,7 @@ var ts; // so order how inherited signatures are processed is still preserved. // interface A { (x: string): void } // interface B extends A { (x: 'foo'): string } - // let b: B; + // const b: B; // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void] function reorderCandidates(signatures, result) { var lastParent; @@ -22247,6 +22530,10 @@ var ts; return anyType; } } + // In JavaScript files, calls to any identifier 'require' are treated as external module imports + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node)) { + return resolveExternalModuleTypeByLiteral(node.arguments[0]); + } return getReturnTypeOfSignature(signature); } function checkTaggedTemplateExpression(node) { @@ -22257,7 +22544,10 @@ var ts; var targetType = getTypeFromTypeNode(node.type); if (produceDiagnostics && targetType !== unknownType) { var widenedType = getWidenedType(exprType); - if (!(isTypeAssignableTo(targetType, widenedType))) { + // Permit 'number[] | "foo"' to be asserted to 'string'. + var bothAreStringLike = someConstituentTypeHasKind(targetType, 258 /* StringLike */) && + someConstituentTypeHasKind(widenedType, 258 /* StringLike */); + if (!bothAreStringLike && !(isTypeAssignableTo(targetType, widenedType))) { checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } @@ -22801,19 +23091,26 @@ var ts; for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) { var p = properties_3[_i]; if (p.kind === 245 /* PropertyAssignment */ || p.kind === 246 /* ShorthandPropertyAssignment */) { - // TODO(andersh): Computed property support var name_13 = p.name; + if (name_13.kind === 136 /* ComputedPropertyName */) { + checkComputedPropertyName(name_13); + } + if (isComputedNonLiteralName(name_13)) { + continue; + } + var text = getTextOfPropertyName(name_13); var type = isTypeAny(sourceType) ? sourceType - : getTypeOfPropertyOfType(sourceType, name_13.text) || - isNumericLiteralName(name_13.text) && getIndexTypeOfType(sourceType, 1 /* Number */) || + : getTypeOfPropertyOfType(sourceType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(sourceType, 1 /* Number */) || getIndexTypeOfType(sourceType, 0 /* String */); if (type) { if (p.kind === 246 /* ShorthandPropertyAssignment */) { checkDestructuringAssignment(p, type); } else { - checkDestructuringAssignment(p.initializer || name_13, type); + // non-shorthand property assignments should always have initializers + checkDestructuringAssignment(p.initializer, type); } } else { @@ -23014,6 +23311,10 @@ var ts; case 31 /* ExclamationEqualsToken */: case 32 /* EqualsEqualsEqualsToken */: case 33 /* ExclamationEqualsEqualsToken */: + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (someConstituentTypeHasKind(leftType, 258 /* StringLike */) && someConstituentTypeHasKind(rightType, 258 /* StringLike */)) { + return booleanType; + } if (!isTypeAssignableTo(leftType, rightType) && !isTypeAssignableTo(rightType, leftType)) { reportOperatorError(); } @@ -23137,6 +23438,13 @@ var ts; var type2 = checkExpression(node.whenFalse, contextualMapper); return getUnionType([type1, type2]); } + function checkStringLiteralExpression(node) { + var contextualType = getContextualType(node); + if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { + return getStringLiteralType(node); + } + return stringType; + } function checkTemplateExpression(node) { // We just want to check each expressions, but we are unconcerned with // the type of each expression, as any value may be coerced into a string. @@ -23187,7 +23495,7 @@ var ts; if (isInferentialContext(contextualMapper)) { var signature = getSingleCallSignature(type); if (signature && signature.typeParameters) { - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType) { var contextualSignature = getSingleCallSignature(contextualType); if (contextualSignature && !contextualSignature.typeParameters) { @@ -23251,6 +23559,7 @@ var ts; case 183 /* TemplateExpression */: return checkTemplateExpression(node); case 9 /* StringLiteral */: + return checkStringLiteralExpression(node); case 11 /* NoSubstitutionTemplateLiteral */: return stringType; case 10 /* RegularExpressionLiteral */: @@ -24580,7 +24889,7 @@ var ts; } // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent var parent = getDeclarationContainer(node); - if (parent.kind === 248 /* SourceFile */ && ts.isExternalModule(parent)) { + if (parent.kind === 248 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent)) { // If the declaration happens to be in external module, report error that require and exports are reserved keywords error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); } @@ -24598,15 +24907,15 @@ var ts; // A non-initialized declaration is a no-op as the block declaration will resolve before the var // declaration. the problem is if the declaration has an initializer. this will act as a write to the // block declared value. this is fine for let, but not const. - // Only consider declarations with initializers, uninitialized let declarations will not + // Only consider declarations with initializers, uninitialized const declarations will not // step on a let/const variable. - // Do not consider let and const declarations, as duplicate block-scoped declarations + // Do not consider const and const declarations, as duplicate block-scoped declarations // are handled by the binder. - // We are only looking for let declarations that step on let\const declarations from a + // We are only looking for const declarations that step on let\const declarations from a // different scope. e.g.: // { // const x = 0; // localDeclarationSymbol obtained after name resolution will correspond to this declaration - // let x = 0; // symbol for this declaration will be 'symbol' + // const x = 0; // symbol for this declaration will be 'symbol' // } // skip block-scoped variables and parameters if ((ts.getCombinedNodeFlags(node) & 24576 /* BlockScoped */) !== 0 || ts.isParameterDeclaration(node)) { @@ -24693,6 +25002,12 @@ var ts; checkExpressionCached(node.initializer); } } + if (node.kind === 163 /* BindingElement */) { + // check computed properties inside property names of binding elements + if (node.propertyName && node.propertyName.kind === 136 /* ComputedPropertyName */) { + checkComputedPropertyName(node.propertyName); + } + } // For a binding pattern, check contained binding elements if (ts.isBindingPattern(node.name)) { ts.forEach(node.name.elements, checkSourceElement); @@ -25176,6 +25491,7 @@ var ts; var firstDefaultClause; var hasDuplicateDefaultClause = false; var expressionType = checkExpression(node.expression); + var expressionTypeIsStringLike = someConstituentTypeHasKind(expressionType, 258 /* StringLike */); ts.forEach(node.caseBlock.clauses, function (clause) { // Grammar check for duplicate default clauses, skip if we already report duplicate default clause if (clause.kind === 242 /* DefaultClause */ && !hasDuplicateDefaultClause) { @@ -25195,6 +25511,10 @@ var ts; // TypeScript 1.0 spec (April 2014):5.9 // In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression. var caseType = checkExpression(caseClause.expression); + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, 258 /* StringLike */)) { + return; + } if (!isTypeAssignableTo(expressionType, caseType)) { // check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); @@ -25659,11 +25979,14 @@ var ts; var enumIsConst = ts.isConst(node); for (var _i = 0, _a = node.members; _i < _a.length; _i++) { var member = _a[_i]; - if (member.name.kind === 136 /* ComputedPropertyName */) { + if (isComputedNonLiteralName(member.name)) { error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); } - else if (isNumericLiteralName(member.name.text)) { - error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + else { + var text = getTextOfPropertyName(member.name); + if (isNumericLiteralName(text)) { + error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + } } var previousEnumMemberIsNonConstant = autoValue === undefined; var initializer = member.initializer; @@ -26305,8 +26628,8 @@ var ts; } // Function and class expression bodies are checked after all statements in the enclosing body. This is // to ensure constructs like the following are permitted: - // let foo = function () { - // let s = foo(); + // const foo = function () { + // const s = foo(); // return "hello"; // } // Here, performing a full type check of the body of the function expression whilst in the process of @@ -26421,8 +26744,12 @@ var ts; if (!(links.flags & 1 /* TypeChecked */)) { // Check whether the file has declared it is the default lib, // and whether the user has specifically chosen to avoid checking it. - if (node.isDefaultLib && compilerOptions.skipDefaultLibCheck) { - return; + if (compilerOptions.skipDefaultLibCheck) { + // If the user specified '--noLib' and a file has a '/// ', + // then we should treat that file as a default lib. + if (node.hasNoDefaultLib) { + return; + } } // Grammar checking checkGrammarSourceFile(node); @@ -26432,7 +26759,7 @@ var ts; potentialThisCollisions.length = 0; ts.forEach(node.statements, checkSourceElement); checkFunctionAndClassExpressionBodies(node); - if (ts.isExternalModule(node)) { + if (ts.isExternalOrCommonJsModule(node)) { checkExternalModuleExports(node); } if (potentialThisCollisions.length) { @@ -26515,7 +26842,7 @@ var ts; } switch (location.kind) { case 248 /* SourceFile */: - if (!ts.isExternalModule(location)) { + if (!ts.isExternalOrCommonJsModule(location)) { break; } case 218 /* ModuleDeclaration */: @@ -27153,9 +27480,18 @@ var ts; getReferencedValueDeclaration: getReferencedValueDeclaration, getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, isOptionalParameter: isOptionalParameter, - isArgumentsLocalBinding: isArgumentsLocalBinding + isArgumentsLocalBinding: isArgumentsLocalBinding, + getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration }; } + function getExternalModuleFileFromDeclaration(declaration) { + var specifier = ts.getExternalModuleName(declaration); + var moduleSymbol = getSymbolAtLocation(specifier); + if (!moduleSymbol) { + return undefined; + } + return ts.getDeclarationOfKind(moduleSymbol, 248 /* SourceFile */); + } function initializeTypeChecker() { // Bind all source files and propagate errors ts.forEach(host.getSourceFiles(), function (file) { @@ -27163,11 +27499,10 @@ var ts; }); // Initialize global symbol table ts.forEach(host.getSourceFiles(), function (file) { - if (!ts.isExternalModule(file)) { + if (!ts.isExternalOrCommonJsModule(file)) { mergeSymbolTable(globals, file.locals); } }); - // Initialize special symbols getSymbolLinks(undefinedSymbol).type = undefinedType; getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); getSymbolLinks(unknownSymbol).type = unknownType; @@ -27866,7 +28201,7 @@ var ts; } } function checkGrammarForNonSymbolComputedProperty(node, message) { - if (node.kind === 136 /* ComputedPropertyName */ && !ts.isWellKnownSymbolSyntactically(node.expression)) { + if (ts.isDynamicName(node)) { return grammarErrorOnNode(node, message); } } @@ -28225,11 +28560,15 @@ var ts; var writeTextOfNode; var writer = createAndSetNewTextWriterWithSymbolWriter(); var enclosingDeclaration; - var currentSourceFile; + var currentText; + var currentLineMap; + var currentIdentifiers; + var isCurrentFileExternalModule; var reportedDeclarationError = false; var errorNameNode; var emitJsDocComments = compilerOptions.removeComments ? function (declaration) { } : writeJsDocComments; var emit = compilerOptions.stripInternal ? stripInternal : emitNode; + var noDeclare = !root; var moduleElementDeclarationEmitInfo = []; var asynchronousSubModuleDeclarationEmitInfo; // Contains the reference paths that needs to go in the declaration file. @@ -28272,23 +28611,56 @@ var ts; else { // Emit references corresponding to this file var emittedReferencedFiles = []; + var prevModuleElementDeclarationEmitInfo = []; ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + if (!ts.isDeclarationFile(sourceFile)) { // Check what references need to be added if (!compilerOptions.noResolve) { ts.forEach(sourceFile.referencedFiles, function (fileReference) { var referencedFile = ts.tryResolveScriptReference(host, sourceFile, fileReference); - // If the reference file is a declaration file or an external module, emit that reference - if (referencedFile && (ts.isExternalModuleOrDeclarationFile(referencedFile) && + // If the reference file is a declaration file, emit that reference + if (referencedFile && (ts.isDeclarationFile(referencedFile) && !ts.contains(emittedReferencedFiles, referencedFile))) { writeReferencePath(referencedFile); emittedReferencedFiles.push(referencedFile); } }); } + } + if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + noDeclare = false; emitSourceFile(sourceFile); } + else if (ts.isExternalModule(sourceFile)) { + noDeclare = true; + write("declare module \"" + ts.getResolvedExternalModuleName(host, sourceFile) + "\" {"); + writeLine(); + increaseIndent(); + emitSourceFile(sourceFile); + decreaseIndent(); + write("}"); + writeLine(); + // create asynchronous output for the importDeclarations + if (moduleElementDeclarationEmitInfo.length) { + var oldWriter = writer; + ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { + if (aliasEmitInfo.isVisible && !aliasEmitInfo.asynchronousOutput) { + ts.Debug.assert(aliasEmitInfo.node.kind === 222 /* ImportDeclaration */); + createAndSetNewTextWriterWithSymbolWriter(); + ts.Debug.assert(aliasEmitInfo.indent === 1); + increaseIndent(); + writeImportDeclaration(aliasEmitInfo.node); + aliasEmitInfo.asynchronousOutput = writer.getText(); + decreaseIndent(); + } + }); + setWriter(oldWriter); + } + prevModuleElementDeclarationEmitInfo = prevModuleElementDeclarationEmitInfo.concat(moduleElementDeclarationEmitInfo); + moduleElementDeclarationEmitInfo = []; + } }); + moduleElementDeclarationEmitInfo = moduleElementDeclarationEmitInfo.concat(prevModuleElementDeclarationEmitInfo); } return { reportedDeclarationError: reportedDeclarationError, @@ -28297,13 +28669,12 @@ var ts; referencePathsOutput: referencePathsOutput }; function hasInternalAnnotation(range) { - var text = currentSourceFile.text; - var comment = text.substring(range.pos, range.end); + var comment = currentText.substring(range.pos, range.end); return comment.indexOf("@internal") >= 0; } function stripInternal(node) { if (node) { - var leadingCommentRanges = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + var leadingCommentRanges = ts.getLeadingCommentRanges(currentText, node.pos); if (ts.forEach(leadingCommentRanges, hasInternalAnnotation)) { return; } @@ -28395,7 +28766,7 @@ var ts; var errorInfo = writer.getSymbolAccessibilityDiagnostic(symbolAccesibilityResult); if (errorInfo) { if (errorInfo.typeName) { - diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); + diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getTextOfNodeFromSourceText(currentText, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); } else { diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); @@ -28461,10 +28832,10 @@ var ts; } function writeJsDocComments(declaration) { if (declaration) { - var jsDocComments = ts.getJsDocComments(declaration, currentSourceFile); - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, declaration, jsDocComments); + var jsDocComments = ts.getJsDocCommentsFromText(declaration, currentText); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments); // jsDoc comments are emitted at /*leading comment1 */space/*leading comment*/space - ts.emitComments(currentSourceFile, writer, jsDocComments, /*trailingSeparator*/ true, newLine, ts.writeCommentRange); + ts.emitComments(currentText, currentLineMap, writer, jsDocComments, /*trailingSeparator*/ true, newLine, ts.writeCommentRange); } } function emitTypeWithNewGetSymbolAccessibilityDiagnostic(type, getSymbolAccessibilityDiagnostic) { @@ -28481,7 +28852,7 @@ var ts; case 103 /* VoidKeyword */: case 97 /* ThisKeyword */: case 9 /* StringLiteral */: - return writeTextOfNode(currentSourceFile, type); + return writeTextOfNode(currentText, type); case 188 /* ExpressionWithTypeArguments */: return emitExpressionWithTypeArguments(type); case 151 /* TypeReference */: @@ -28512,14 +28883,14 @@ var ts; } function writeEntityName(entityName) { if (entityName.kind === 69 /* Identifier */) { - writeTextOfNode(currentSourceFile, entityName); + writeTextOfNode(currentText, entityName); } else { var left = entityName.kind === 135 /* QualifiedName */ ? entityName.left : entityName.expression; var right = entityName.kind === 135 /* QualifiedName */ ? entityName.right : entityName.name; writeEntityName(left); write("."); - writeTextOfNode(currentSourceFile, right); + writeTextOfNode(currentText, right); } } function emitEntityName(entityName) { @@ -28549,7 +28920,7 @@ var ts; } } function emitTypePredicate(type) { - writeTextOfNode(currentSourceFile, type.parameterName); + writeTextOfNode(currentText, type.parameterName); write(" is "); emitType(type.type); } @@ -28590,9 +28961,12 @@ var ts; } } function emitSourceFile(node) { - currentSourceFile = node; + currentText = node.text; + currentLineMap = ts.getLineStarts(node); + currentIdentifiers = node.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(node); enclosingDeclaration = node; - ts.emitDetachedComments(currentSourceFile, writer, ts.writeCommentRange, node, newLine, true /* remove comments */); + ts.emitDetachedComments(currentText, currentLineMap, writer, ts.writeCommentRange, node, newLine, true /* remove comments */); emitLines(node.statements); } // Return a temp variable name to be used in `export default` statements. @@ -28601,13 +28975,13 @@ var ts; // do not need to keep track of created temp names. function getExportDefaultTempVariableName() { var baseName = "_default"; - if (!ts.hasProperty(currentSourceFile.identifiers, baseName)) { + if (!ts.hasProperty(currentIdentifiers, baseName)) { return baseName; } var count = 0; while (true) { var name_18 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_18)) { + if (!ts.hasProperty(currentIdentifiers, name_18)) { return name_18; } } @@ -28615,7 +28989,7 @@ var ts; function emitExportAssignment(node) { if (node.expression.kind === 69 /* Identifier */) { write(node.isExportEquals ? "export = " : "export default "); - writeTextOfNode(currentSourceFile, node.expression); + writeTextOfNode(currentText, node.expression); } else { // Expression @@ -28653,7 +29027,7 @@ var ts; writeModuleElement(node); } else if (node.kind === 221 /* ImportEqualsDeclaration */ || - (node.parent.kind === 248 /* SourceFile */ && ts.isExternalModule(currentSourceFile))) { + (node.parent.kind === 248 /* SourceFile */ && isCurrentFileExternalModule)) { var isVisible; if (asynchronousSubModuleDeclarationEmitInfo && node.parent.kind !== 248 /* SourceFile */) { // Import declaration of another module that is visited async so lets put it in right spot @@ -28707,7 +29081,7 @@ var ts; } function emitModuleElementDeclarationFlags(node) { // If the node is parented in the current source file we need to emit export declare or just export - if (node.parent === currentSourceFile) { + if (node.parent.kind === 248 /* SourceFile */) { // If the node is exported if (node.flags & 2 /* Export */) { write("export "); @@ -28715,7 +29089,7 @@ var ts; if (node.flags & 512 /* Default */) { write("default "); } - else if (node.kind !== 215 /* InterfaceDeclaration */) { + else if (node.kind !== 215 /* InterfaceDeclaration */ && !noDeclare) { write("declare "); } } @@ -28742,7 +29116,7 @@ var ts; write("export "); } write("import "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" = "); if (ts.isInternalModuleImportEqualsDeclaration(node)) { emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.moduleReference, getImportEntityNameVisibilityError); @@ -28750,7 +29124,7 @@ var ts; } else { write("require("); - writeTextOfNode(currentSourceFile, ts.getExternalModuleImportEqualsDeclarationExpression(node)); + writeTextOfNode(currentText, ts.getExternalModuleImportEqualsDeclarationExpression(node)); write(");"); } writer.writeLine(); @@ -28785,7 +29159,7 @@ var ts; if (node.importClause) { var currentWriterPos = writer.getTextPos(); if (node.importClause.name && resolver.isDeclarationVisible(node.importClause)) { - writeTextOfNode(currentSourceFile, node.importClause.name); + writeTextOfNode(currentText, node.importClause.name); } if (node.importClause.namedBindings && isVisibleNamedBinding(node.importClause.namedBindings)) { if (currentWriterPos !== writer.getTextPos()) { @@ -28794,7 +29168,7 @@ var ts; } if (node.importClause.namedBindings.kind === 224 /* NamespaceImport */) { write("* as "); - writeTextOfNode(currentSourceFile, node.importClause.namedBindings.name); + writeTextOfNode(currentText, node.importClause.namedBindings.name); } else { write("{ "); @@ -28804,16 +29178,28 @@ var ts; } write(" from "); } - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); write(";"); writer.writeLine(); } + function emitExternalModuleSpecifier(moduleSpecifier) { + if (moduleSpecifier.kind === 9 /* StringLiteral */ && (!root) && (compilerOptions.out || compilerOptions.outFile)) { + var moduleName = ts.getExternalModuleNameFromDeclaration(host, resolver, moduleSpecifier.parent); + if (moduleName) { + write("\""); + write(moduleName); + write("\""); + return; + } + } + writeTextOfNode(currentText, moduleSpecifier); + } function emitImportOrExportSpecifier(node) { if (node.propertyName) { - writeTextOfNode(currentSourceFile, node.propertyName); + writeTextOfNode(currentText, node.propertyName); write(" as "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } function emitExportSpecifier(node) { emitImportOrExportSpecifier(node); @@ -28835,7 +29221,7 @@ var ts; } if (node.moduleSpecifier) { write(" from "); - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); } write(";"); writer.writeLine(); @@ -28849,11 +29235,11 @@ var ts; else { write("module "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); while (node.body.kind !== 219 /* ModuleBlock */) { node = node.body; write("."); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; @@ -28872,7 +29258,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("type "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); emitTypeParameters(node.typeParameters); write(" = "); emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.type, getTypeAliasDeclarationVisibilityError); @@ -28894,7 +29280,7 @@ var ts; write("const "); } write("enum "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" {"); writeLine(); increaseIndent(); @@ -28905,7 +29291,7 @@ var ts; } function emitEnumMemberDeclaration(node) { emitJsDocComments(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var enumMemberValue = resolver.getConstantValue(node); if (enumMemberValue !== undefined) { write(" = "); @@ -28922,7 +29308,7 @@ var ts; increaseIndent(); emitJsDocComments(node); decreaseIndent(); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); // If there is constraint present and this is not a type parameter of the private method emit the constraint if (node.constraint && !isPrivateMethodTypeParameter(node)) { write(" extends "); @@ -29037,7 +29423,7 @@ var ts; write("abstract "); } write("class "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -29060,7 +29446,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("interface "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -29095,7 +29481,7 @@ var ts; // If this node is a computed name, it can only be a symbol, because we've already skipped // it if it's not a well known symbol. In that case, the text of the name will be exactly // what we want, namely the name expression enclosed in brackets. - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); // If optional property emit ? if ((node.kind === 141 /* PropertyDeclaration */ || node.kind === 140 /* PropertySignature */) && ts.hasQuestionToken(node)) { write("?"); @@ -29177,7 +29563,7 @@ var ts; emitBindingPattern(bindingElement.name); } else { - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); writeTypeOfDeclaration(bindingElement, /*type*/ undefined, getBindingElementTypeVisibilityError); } } @@ -29221,7 +29607,7 @@ var ts; emitJsDocComments(accessors.getAccessor); emitJsDocComments(accessors.setAccessor); emitClassMemberDeclarationFlags(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (!(node.flags & 16 /* Private */)) { accessorWithTypeAnnotation = node; var type = getTypeAnnotationFromAccessor(node); @@ -29307,13 +29693,13 @@ var ts; } if (node.kind === 213 /* FunctionDeclaration */) { write("function "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } else if (node.kind === 144 /* Constructor */) { write("constructor"); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (ts.hasQuestionToken(node)) { write("?"); } @@ -29437,7 +29823,7 @@ var ts; emitBindingPattern(node.name); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } if (resolver.isOptionalParameter(node)) { write("?"); @@ -29552,7 +29938,7 @@ var ts; // Example: // original: function foo({y: [a,b,c]}) {} // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; - writeTextOfNode(currentSourceFile, bindingElement.propertyName); + writeTextOfNode(currentText, bindingElement.propertyName); write(": "); } if (bindingElement.name) { @@ -29575,7 +29961,7 @@ var ts; if (bindingElement.dotDotDotToken) { write("..."); } - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); } } } @@ -29667,6 +30053,18 @@ var ts; return ts.isExternalModule(sourceFile) || ts.isDeclarationFile(sourceFile); } ts.isExternalModuleOrDeclarationFile = isExternalModuleOrDeclarationFile; + function getResolvedExternalModuleName(host, file) { + return file.moduleName || ts.getExternalModuleNameFromPath(host, file.fileName); + } + ts.getResolvedExternalModuleName = getResolvedExternalModuleName; + function getExternalModuleNameFromDeclaration(host, resolver, declaration) { + var file = resolver.getExternalModuleFileFromDeclaration(declaration); + if (!file || ts.isDeclarationFile(file)) { + return undefined; + } + return getResolvedExternalModuleName(host, file); + } + ts.getExternalModuleNameFromDeclaration = getExternalModuleNameFromDeclaration; var Jump; (function (Jump) { Jump[Jump["Break"] = 2] = "Break"; @@ -29954,15 +30352,19 @@ var ts; var newLine = host.getNewLine(); var jsxDesugaring = host.getCompilerOptions().jsx !== 1 /* Preserve */; var shouldEmitJsx = function (s) { return (s.languageVariant === 1 /* JSX */ && !jsxDesugaring); }; + var outFile = compilerOptions.outFile || compilerOptions.out; + var emitJavaScript = createFileEmitter(); if (targetSourceFile === undefined) { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { - var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); - emitFile(jsFilePath, sourceFile); - } - }); - if (compilerOptions.outFile || compilerOptions.out) { - emitFile(compilerOptions.outFile || compilerOptions.out); + if (outFile) { + emitFile(outFile); + } + else { + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { + var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); + emitFile(jsFilePath, sourceFile); + } + }); } } else { @@ -29971,8 +30373,8 @@ var ts; var jsFilePath = ts.getOwnEmitOutputFilePath(targetSourceFile, host, shouldEmitJsx(targetSourceFile) ? ".jsx" : ".js"); emitFile(jsFilePath, targetSourceFile); } - else if (!ts.isDeclarationFile(targetSourceFile) && (compilerOptions.outFile || compilerOptions.out)) { - emitFile(compilerOptions.outFile || compilerOptions.out); + else if (!ts.isDeclarationFile(targetSourceFile) && outFile) { + emitFile(outFile); } } // Sort and make the unique list of diagnostics @@ -30024,10 +30426,16 @@ var ts; } } } - function emitJavaScript(jsFilePath, root) { + function createFileEmitter() { var writer = ts.createTextWriter(newLine); var write = writer.write, writeTextOfNode = writer.writeTextOfNode, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; 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 () {...}) // exporting in System modules looks like: @@ -30035,15 +30443,15 @@ var ts; // => // var x;... exporter("x", x = 1) var exportFunctionForFile; - var generatedNameSet = {}; - var nodeToGeneratedName = []; + var generatedNameSet; + var nodeToGeneratedName; var computedPropertyNamesToGeneratedNames; var convertedLoopState; - var extendsEmitted = false; - var decorateEmitted = false; - var paramEmitted = false; - var awaiterEmitted = false; - var tempFlags = 0; + var extendsEmitted; + var decorateEmitted; + var paramEmitted; + var awaiterEmitted; + var tempFlags; var tempVariables; var tempParameters; var externalImports; @@ -30075,6 +30483,8 @@ var ts; var scopeEmitEnd = function () { }; /** Sourcemap data that will get encoded */ var sourceMapData; + /** The root file passed to the emit function (if present) */ + var root; /** If removeComments is true, no leading-comments needed to be emitted **/ var emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos) { } : emitLeadingCommentsOfPositionWorker; var moduleEmitDelegates = (_a = {}, @@ -30085,31 +30495,77 @@ var ts; _a[1 /* CommonJS */] = emitCommonJSModule, _a ); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - initializeEmitterWithSourceMaps(); - } - if (root) { - // Do not call emit directly. It does not set the currentSourceFile. - emitSourceFile(root); - } - else { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!isExternalModuleOrDeclarationFile(sourceFile)) { - emitSourceFile(sourceFile); + var bundleEmitDelegates = (_b = {}, + _b[5 /* ES6 */] = function () { }, + _b[2 /* AMD */] = emitAMDModule, + _b[4 /* System */] = emitSystemModule, + _b[3 /* UMD */] = function () { }, + _b[1 /* CommonJS */] = function () { }, + _b + ); + return doEmit; + function doEmit(jsFilePath, rootFile) { + // reset the state + writer.reset(); + currentSourceFile = undefined; + currentText = undefined; + currentLineMap = undefined; + exportFunctionForFile = undefined; + generatedNameSet = {}; + nodeToGeneratedName = []; + computedPropertyNamesToGeneratedNames = undefined; + convertedLoopState = undefined; + extendsEmitted = false; + decorateEmitted = false; + paramEmitted = false; + awaiterEmitted = false; + tempFlags = 0; + tempVariables = undefined; + tempParameters = undefined; + externalImports = undefined; + exportSpecifiers = undefined; + exportEquals = undefined; + hasExportStars = undefined; + detachedCommentsInfo = undefined; + sourceMapData = undefined; + isEs6Module = false; + renamedDependencies = undefined; + isCurrentFileExternalModule = false; + root = rootFile; + if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { + initializeEmitterWithSourceMaps(jsFilePath, root); + } + if (root) { + // Do not call emit directly. It does not set the currentSourceFile. + emitSourceFile(root); + } + else { + if (modulekind) { + ts.forEach(host.getSourceFiles(), emitEmitHelpers); } - }); + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if ((!isExternalModuleOrDeclarationFile(sourceFile)) || (modulekind && ts.isExternalModule(sourceFile))) { + emitSourceFile(sourceFile); + } + }); + } + writeLine(); + writeEmittedFiles(writer.getText(), jsFilePath, /*writeByteOrderMark*/ compilerOptions.emitBOM); } - writeLine(); - writeEmittedFiles(writer.getText(), /*writeByteOrderMark*/ compilerOptions.emitBOM); - return; function emitSourceFile(sourceFile) { currentSourceFile = sourceFile; + currentText = sourceFile.text; + currentLineMap = ts.getLineStarts(sourceFile); exportFunctionForFile = undefined; + isEs6Module = sourceFile.symbol && sourceFile.symbol.exports && !!sourceFile.symbol.exports["___esModule"]; + renamedDependencies = sourceFile.renamedDependencies; + currentFileIdentifiers = sourceFile.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(sourceFile); emit(sourceFile); } function isUniqueName(name) { return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentSourceFile.identifiers, name) && + !ts.hasProperty(currentFileIdentifiers, name) && !ts.hasProperty(generatedNameSet, name); } // Return the next available name in the pattern _a ... _z, _0, _1, ... @@ -30192,7 +30648,7 @@ var ts; var id = ts.getNodeId(node); return nodeToGeneratedName[id] || (nodeToGeneratedName[id] = ts.unescapeIdentifier(generateNameForNode(node))); } - function initializeEmitterWithSourceMaps() { + function initializeEmitterWithSourceMaps(jsFilePath, root) { var sourceMapDir; // The directory in which sourcemap will be // Current source map file and its index in the sources list var sourceMapSourceIndex = -1; @@ -30280,7 +30736,7 @@ var ts; } } function recordSourceMapSpan(pos) { - var sourceLinePos = ts.getLineAndCharacterOfPosition(currentSourceFile, pos); + var sourceLinePos = ts.computeLineAndCharacterOfPosition(currentLineMap, pos); // Convert the location to be one-based. sourceLinePos.line++; sourceLinePos.character++; @@ -30314,13 +30770,13 @@ var ts; } function recordEmitNodeStartSpan(node) { // Get the token pos after skipping to the token (ignoring the leading trivia) - recordSourceMapSpan(ts.skipTrivia(currentSourceFile.text, node.pos)); + recordSourceMapSpan(ts.skipTrivia(currentText, node.pos)); } function recordEmitNodeEndSpan(node) { recordSourceMapSpan(node.end); } function writeTextWithSpanRecord(tokenKind, startPos, emitFn) { - var tokenStartPos = ts.skipTrivia(currentSourceFile.text, startPos); + var tokenStartPos = ts.skipTrivia(currentText, startPos); recordSourceMapSpan(tokenStartPos); var tokenEndPos = emitTokenText(tokenKind, tokenStartPos, emitFn); recordSourceMapSpan(tokenEndPos); @@ -30402,9 +30858,9 @@ var ts; sourceMapNameIndices.pop(); } ; - function writeCommentRangeWithMap(curentSourceFile, writer, comment, newLine) { + function writeCommentRangeWithMap(currentText, currentLineMap, writer, comment, newLine) { recordSourceMapSpan(comment.pos); - ts.writeCommentRange(currentSourceFile, writer, comment, newLine); + ts.writeCommentRange(currentText, currentLineMap, writer, comment, newLine); recordSourceMapSpan(comment.end); } function serializeSourceMapContents(version, file, sourceRoot, sources, names, mappings, sourcesContent) { @@ -30434,7 +30890,7 @@ var ts; return output; } } - function writeJavaScriptAndSourceMapFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptAndSourceMapFile(emitOutput, jsFilePath, writeByteOrderMark) { encodeLastRecordedSourceMapSpan(); var sourceMapText = serializeSourceMapContents(3, sourceMapData.sourceMapFile, sourceMapData.sourceMapSourceRoot, sourceMapData.sourceMapSources, sourceMapData.sourceMapNames, sourceMapData.sourceMapMappings, sourceMapData.sourceMapSourcesContent); sourceMapDataList.push(sourceMapData); @@ -30450,7 +30906,7 @@ var ts; sourceMapUrl = "//# sourceMappingURL=" + sourceMapData.jsSourceMappingURL; } // Write sourcemap url to the js file and write the js file - writeJavaScriptFile(emitOutput + sourceMapUrl, writeByteOrderMark); + writeJavaScriptFile(emitOutput + sourceMapUrl, jsFilePath, writeByteOrderMark); } // Initialize source map data var sourceMapJsFile = ts.getBaseFileName(ts.normalizeSlashes(jsFilePath)); @@ -30522,7 +30978,7 @@ var ts; scopeEmitEnd = recordScopeNameEnd; writeComment = writeCommentRangeWithMap; } - function writeJavaScriptFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptFile(emitOutput, jsFilePath, writeByteOrderMark) { ts.writeFile(host, diagnostics, jsFilePath, emitOutput, writeByteOrderMark); } // Create a temporary variable with a unique unused name. @@ -30702,7 +31158,7 @@ var ts; // 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.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + 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. @@ -30729,7 +31185,7 @@ var ts; // 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.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + var text = ts.getTextOfNodeFromSourceText(currentText, node); // text contains the original source, it will also contain quotes ("`"), dolar signs and braces ("${" and "}"), // thus we need to remove those characters. // First template piece starts with "`", others with "}" @@ -31146,7 +31602,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } write("\""); } @@ -31246,7 +31702,7 @@ var ts; // Identifier references named import write(getGeneratedNameForNode(declaration.parent.parent.parent)); var name_23 = declaration.propertyName || declaration.name; - var identifier = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, name_23); + var identifier = ts.getTextOfNodeFromSourceText(currentText, name_23); if (languageVersion === 0 /* ES3 */ && identifier === "default") { write("[\"default\"]"); } @@ -31270,7 +31726,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function isNameOfNestedRedeclaration(node) { @@ -31308,7 +31764,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function emitThis(node) { @@ -31712,7 +32168,7 @@ var ts; 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(currentSourceFile, node.name); + 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 { @@ -31722,7 +32178,7 @@ var ts; // let obj = { y }; // } // Here we need to emit obj = { y : m.y } regardless of the output target. - if (languageVersion < 2 /* ES6 */ || isNamespaceExportReference(node.name)) { + if (modulekind !== 5 /* ES6 */ || isNamespaceExportReference(node.name)) { // Emit identifier as an identifier write(": "); emit(node.name); @@ -31779,11 +32235,11 @@ var ts; var indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.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; + var shouldEmitSpace = false; if (!indentedBeforeDot) { if (node.expression.kind === 8 /* NumericLiteral */) { // check if numeric literal was originally written with a dot - var text = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node.expression); + var text = ts.getTextOfNodeFromSourceText(currentText, node.expression); shouldEmitSpace = text.indexOf(ts.tokenToString(21 /* DotToken */)) < 0; } else { @@ -32962,16 +33418,16 @@ var ts; emitToken(16 /* CloseBraceToken */, node.clauses.end); } function nodeStartPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node1.pos)) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node1.pos)) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function nodeEndPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, node2.end); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, node2.end); } function nodeEndIsOnSameLineAsNodeStart(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function emitCaseOrDefaultClause(node) { if (node.kind === 241 /* CaseClause */) { @@ -33077,7 +33533,7 @@ var ts; ts.Debug.assert(!!(node.flags & 512 /* Default */) || node.kind === 227 /* ExportAssignment */); // only allow export default at a source file level if (modulekind === 1 /* CommonJS */ || modulekind === 2 /* AMD */ || modulekind === 3 /* UMD */) { - if (!currentSourceFile.symbol.exports["___esModule"]) { + if (!isEs6Module) { if (languageVersion === 1 /* ES5 */) { // default value of configurable, enumerable, writable are `false`. write("Object.defineProperty(exports, \"__esModule\", { value: true });"); @@ -33272,14 +33728,20 @@ var ts; return node; } function createPropertyAccessForDestructuringProperty(object, propName) { - // We create a synthetic copy of the identifier in order to avoid the rewriting that might - // otherwise occur when the identifier is emitted. - var syntheticName = ts.createSynthesizedNode(propName.kind); - syntheticName.text = propName.text; - if (syntheticName.kind !== 69 /* Identifier */) { - return createElementAccessExpression(object, syntheticName); + var index; + var nameIsComputed = propName.kind === 136 /* ComputedPropertyName */; + if (nameIsComputed) { + index = ensureIdentifier(propName.expression, /* reuseIdentifierExpression */ false); } - return createPropertyAccessExpression(object, syntheticName); + 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); + index.text = propName.text; + } + return !nameIsComputed && index.kind === 69 /* Identifier */ + ? createPropertyAccessExpression(object, index) + : createElementAccessExpression(object, index); } function createSliceCall(value, sliceIndex) { var call = ts.createSynthesizedNode(168 /* CallExpression */); @@ -33742,7 +34204,6 @@ var ts; var promiseConstructor = ts.getEntityNameFromTypeNode(node.type); var isArrowFunction = node.kind === 174 /* ArrowFunction */; var hasLexicalArguments = (resolver.getNodeCheckFlags(node) & 4096 /* CaptureArguments */) !== 0; - var args; // 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 @@ -35197,8 +35658,8 @@ var ts; * Here we check if alternative name was provided for a given moduleName and return it if possible. */ function tryRenameExternalModule(moduleName) { - if (currentSourceFile.renamedDependencies && ts.hasProperty(currentSourceFile.renamedDependencies, moduleName.text)) { - return "\"" + currentSourceFile.renamedDependencies[moduleName.text] + "\""; + if (renamedDependencies && ts.hasProperty(renamedDependencies, moduleName.text)) { + return "\"" + renamedDependencies[moduleName.text] + "\""; } return undefined; } @@ -35351,7 +35812,7 @@ var ts; // - current file is not external module // - import declaration is top level and target is value imported by entity name if (resolver.isReferencedAliasDeclaration(node) || - (!ts.isExternalModule(currentSourceFile) && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { + (!isCurrentFileExternalModule && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { emitLeadingComments(node); emitStart(node); // variable declaration for import-equals declaration can be hoisted in system modules @@ -35579,7 +36040,7 @@ var ts; function getLocalNameForExternalImport(node) { var namespaceDeclaration = getNamespaceDeclarationNode(node); if (namespaceDeclaration && !isDefaultImport(node)) { - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, namespaceDeclaration.name); + return ts.getTextOfNodeFromSourceText(currentText, namespaceDeclaration.name); } if (node.kind === 222 /* ImportDeclaration */ && node.importClause) { return getGeneratedNameForNode(node); @@ -35884,7 +36345,7 @@ var ts; ts.getEnclosingBlockScopeContainer(node).kind === 248 /* SourceFile */; } function isCurrentFileSystemExternalModule() { - return modulekind === 4 /* System */ && ts.isExternalModule(currentSourceFile); + return modulekind === 4 /* System */ && isCurrentFileExternalModule; } function emitSystemModuleBody(node, dependencyGroups, startIndex) { // shape of the body in system modules: @@ -36054,7 +36515,13 @@ var ts; writeLine(); write("}"); // execute } - function emitSystemModule(node) { + function writeModuleName(node, emitRelativePathAsModuleName) { + var moduleName = node.moduleName; + if (moduleName || (emitRelativePathAsModuleName && (moduleName = getResolvedExternalModuleName(host, node)))) { + write("\"" + moduleName + "\", "); + } + } + function emitSystemModule(node, emitRelativePathAsModuleName) { collectExternalModuleInfo(node); // System modules has the following shape // System.register(['dep-1', ... 'dep-n'], function(exports) {/* module body function */}) @@ -36069,9 +36536,7 @@ var ts; exportFunctionForFile = makeUniqueName("exports"); writeLine(); write("System.register("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } + writeModuleName(node, emitRelativePathAsModuleName); write("["); var groupIndices = {}; var dependencyGroups = []; @@ -36090,6 +36555,12 @@ var ts; if (i !== 0) { write(", "); } + if (emitRelativePathAsModuleName) { + var name_29 = getExternalModuleNameFromDeclaration(host, resolver, externalImports[i]); + if (name_29) { + text = "\"" + name_29 + "\""; + } + } write(text); } write("], function(" + exportFunctionForFile + ") {"); @@ -36103,7 +36574,7 @@ var ts; writeLine(); write("});"); } - function getAMDDependencyNames(node, includeNonAmdDependencies) { + function getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { // names of modules with corresponding parameter in the factory function var aliasedModuleNames = []; // names of modules with no corresponding parameters in factory function @@ -36126,6 +36597,12 @@ var ts; var importNode = externalImports_4[_c]; // Find the name of the external module var externalModuleName = getExternalModuleNameText(importNode); + if (emitRelativePathAsModuleName) { + var name_30 = getExternalModuleNameFromDeclaration(host, resolver, importNode); + if (name_30) { + externalModuleName = "\"" + name_30 + "\""; + } + } // Find the name of the module alias, if there is one var importAliasName = getLocalNameForExternalImport(importNode); if (includeNonAmdDependencies && importAliasName) { @@ -36138,7 +36615,7 @@ var ts; } return { aliasedModuleNames: aliasedModuleNames, unaliasedModuleNames: unaliasedModuleNames, importAliasNames: importAliasNames }; } - function emitAMDDependencies(node, includeNonAmdDependencies) { + function emitAMDDependencies(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { // An AMD define function has the following shape: // define(id?, dependencies?, factory); // @@ -36150,7 +36627,7 @@ var ts; // To ensure this is true in cases of modules with no aliases, e.g.: // `import "module"` or `` // we need to add modules without alias names to the end of the dependencies list - var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies); + var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName); emitAMDDependencyList(dependencyNames); write(", "); emitAMDFactoryHeader(dependencyNames); @@ -36177,15 +36654,13 @@ var ts; } write(") {"); } - function emitAMDModule(node) { + function emitAMDModule(node, emitRelativePathAsModuleName) { emitEmitHelpers(node); collectExternalModuleInfo(node); writeLine(); write("define("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } - emitAMDDependencies(node, /*includeNonAmdDependencies*/ true); + writeModuleName(node, emitRelativePathAsModuleName); + emitAMDDependencies(node, /*includeNonAmdDependencies*/ true, emitRelativePathAsModuleName); increaseIndent(); var startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true); emitExportStarHelper(); @@ -36404,8 +36879,13 @@ var ts; emitShebang(); emitDetachedCommentsAndUpdateCommentsInfo(node); if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { - var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1 /* CommonJS */]; - emitModule(node); + if (root || (!ts.isExternalModule(node) && compilerOptions.isolatedModules)) { + var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1 /* CommonJS */]; + emitModule(node); + } + else { + bundleEmitDelegates[modulekind](node, /*emitRelativePathAsModuleName*/ true); + } } else { // emit prologue directives prior to __extends @@ -36666,7 +37146,7 @@ var ts; } function getLeadingCommentsWithoutDetachedComments() { // get the leading comments from detachedPos - var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); + var leadingComments = ts.getLeadingCommentRanges(currentText, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); if (detachedCommentsInfo.length - 1) { detachedCommentsInfo.pop(); } @@ -36683,10 +37163,10 @@ var ts; 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 (currentSourceFile.text.charCodeAt(comment.pos + 1) === 47 /* slash */ && + if (currentText.charCodeAt(comment.pos + 1) === 47 /* slash */ && comment.pos + 2 < comment.end && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 47 /* slash */) { - var textSubStr = currentSourceFile.text.substring(comment.pos, comment.end); + currentText.charCodeAt(comment.pos + 2) === 47 /* slash */) { + var textSubStr = currentText.substring(comment.pos, comment.end); return textSubStr.match(ts.fullTripleSlashReferencePathRegEx) || textSubStr.match(ts.fullTripleSlashAMDReferencePathRegEx) ? true : false; @@ -36703,7 +37183,7 @@ var ts; } else { // get the leading comments from the node - return ts.getLeadingCommentRangesOfNode(node, currentSourceFile); + return ts.getLeadingCommentRangesOfNodeFromText(node, currentText); } } } @@ -36712,7 +37192,7 @@ var ts; // Emit the trailing comments only if the parent's pos doesn't match because parent should take care of emitting these comments if (node.parent) { if (node.parent.kind === 248 /* SourceFile */ || node.end !== node.parent.end) { - return ts.getTrailingCommentRanges(currentSourceFile.text, node.end); + return ts.getTrailingCommentRanges(currentText, node.end); } } } @@ -36746,9 +37226,9 @@ var ts; leadingComments = ts.filter(getLeadingCommentsToEmit(node), isTripleSlashComment); } } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, node, leadingComments); // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space - ts.emitComments(currentSourceFile, writer, leadingComments, /*trailingSeparator:*/ true, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, /*trailingSeparator:*/ true, newLine, writeComment); } function emitTrailingComments(node) { if (compilerOptions.removeComments) { @@ -36757,7 +37237,7 @@ var ts; // Emit the trailing comments only if the parent's end doesn't match var trailingComments = getTrailingCommentsToEmit(node); // trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/ - ts.emitComments(currentSourceFile, writer, trailingComments, /*trailingSeparator*/ false, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, /*trailingSeparator*/ false, newLine, writeComment); } /** * Emit trailing comments at the position. The term trailing comment is used here to describe following comment: @@ -36768,9 +37248,9 @@ var ts; if (compilerOptions.removeComments) { return; } - var trailingComments = ts.getTrailingCommentRanges(currentSourceFile.text, pos); + var trailingComments = ts.getTrailingCommentRanges(currentText, pos); // trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/ - ts.emitComments(currentSourceFile, writer, trailingComments, /*trailingSeparator*/ true, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, /*trailingSeparator*/ true, newLine, writeComment); } function emitLeadingCommentsOfPositionWorker(pos) { if (compilerOptions.removeComments) { @@ -36783,14 +37263,14 @@ var ts; } else { // get the leading comments from the node - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, pos); + leadingComments = ts.getLeadingCommentRanges(currentText, pos); } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, { pos: pos, end: pos }, leadingComments); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, { pos: pos, end: pos }, leadingComments); // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space - ts.emitComments(currentSourceFile, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment); } function emitDetachedCommentsAndUpdateCommentsInfo(node) { - var currentDetachedCommentInfo = ts.emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, compilerOptions.removeComments); + var currentDetachedCommentInfo = ts.emitDetachedComments(currentText, currentLineMap, writer, writeComment, node, newLine, compilerOptions.removeComments); if (currentDetachedCommentInfo) { if (detachedCommentsInfo) { detachedCommentsInfo.push(currentDetachedCommentInfo); @@ -36801,12 +37281,12 @@ var ts; } } function emitShebang() { - var shebang = ts.getShebang(currentSourceFile.text); + var shebang = ts.getShebang(currentText); if (shebang) { write(shebang); } } - var _a; + var _a, _b; } function emitFile(jsFilePath, sourceFile) { emitJavaScript(jsFilePath, sourceFile); @@ -36866,11 +37346,11 @@ var ts; if (ts.getRootLength(moduleName) !== 0 || nameStartsWithDotSlashOrDotDotSlash(moduleName)) { var failedLookupLocations = []; var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var resolvedFileName = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var resolvedFileName = loadNodeModuleFromFile(ts.supportedJsExtensions, candidate, failedLookupLocations, host); if (resolvedFileName) { return { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations }; } - resolvedFileName = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + resolvedFileName = loadNodeModuleFromDirectory(ts.supportedJsExtensions, candidate, failedLookupLocations, host); return resolvedFileName ? { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations } : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; @@ -36880,8 +37360,8 @@ var ts; } } ts.nodeModuleNameResolver = nodeModuleNameResolver; - function loadNodeModuleFromFile(candidate, failedLookupLocation, host) { - return ts.forEach(ts.moduleFileExtensions, tryLoad); + function loadNodeModuleFromFile(extensions, candidate, failedLookupLocation, host) { + return ts.forEach(extensions, tryLoad); function tryLoad(ext) { var fileName = ts.fileExtensionIs(candidate, ext) ? candidate : candidate + ext; if (host.fileExists(fileName)) { @@ -36893,7 +37373,7 @@ var ts; } } } - function loadNodeModuleFromDirectory(candidate, failedLookupLocation, host) { + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, host) { var packageJsonPath = ts.combinePaths(candidate, "package.json"); if (host.fileExists(packageJsonPath)) { var jsonContent; @@ -36906,7 +37386,7 @@ var ts; jsonContent = { typings: undefined }; } if (jsonContent.typings) { - var result = loadNodeModuleFromFile(ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); + var result = loadNodeModuleFromFile(extensions, ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); if (result) { return result; } @@ -36916,7 +37396,7 @@ var ts; // record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results failedLookupLocation.push(packageJsonPath); } - return loadNodeModuleFromFile(ts.combinePaths(candidate, "index"), failedLookupLocation, host); + return loadNodeModuleFromFile(extensions, ts.combinePaths(candidate, "index"), failedLookupLocation, host); } function loadModuleFromNodeModules(moduleName, directory, host) { var failedLookupLocations = []; @@ -36926,11 +37406,11 @@ var ts; if (baseName !== "node_modules") { var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var result = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var result = loadNodeModuleFromFile(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } - result = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + result = loadNodeModuleFromDirectory(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } @@ -36956,9 +37436,10 @@ var ts; var searchName; var failedLookupLocations = []; var referencedSourceFile; + var extensions = compilerOptions.allowNonTsExtensions ? ts.supportedJsExtensions : ts.supportedExtensions; while (true) { searchName = ts.normalizePath(ts.combinePaths(searchPath, moduleName)); - referencedSourceFile = ts.forEach(ts.supportedExtensions, function (extension) { + referencedSourceFile = ts.forEach(extensions, function (extension) { if (extension === ".tsx" && !compilerOptions.jsx) { // resolve .tsx files only if jsx support is enabled // 'logical not' handles both undefined and None cases @@ -36989,10 +37470,8 @@ var ts; /* @internal */ ts.defaultInitCompilerOptions = { module: 1 /* CommonJS */, - target: 0 /* ES3 */, + target: 1 /* ES5 */, noImplicitAny: false, - outDir: "built", - rootDir: ".", sourceMap: false }; function createCompilerHost(options, setParentNodes) { @@ -37397,43 +37876,55 @@ var ts; if (file.imports) { return; } + var isJavaScriptFile = ts.isSourceFileJavaScript(file); var imports; for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { var node = _a[_i]; - collect(node, /* allowRelativeModuleNames */ true); + collect(node, /* allowRelativeModuleNames */ true, /* collectOnlyRequireCalls */ false); } file.imports = imports || emptyArray; - function collect(node, allowRelativeModuleNames) { - switch (node.kind) { - case 222 /* ImportDeclaration */: - case 221 /* ImportEqualsDeclaration */: - case 228 /* ExportDeclaration */: - var moduleNameExpr = ts.getExternalModuleName(node); - if (!moduleNameExpr || moduleNameExpr.kind !== 9 /* StringLiteral */) { + return; + function collect(node, allowRelativeModuleNames, collectOnlyRequireCalls) { + if (!collectOnlyRequireCalls) { + switch (node.kind) { + case 222 /* ImportDeclaration */: + case 221 /* ImportEqualsDeclaration */: + case 228 /* ExportDeclaration */: + var moduleNameExpr = ts.getExternalModuleName(node); + if (!moduleNameExpr || moduleNameExpr.kind !== 9 /* StringLiteral */) { + break; + } + if (!moduleNameExpr.text) { + break; + } + if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { + (imports || (imports = [])).push(moduleNameExpr); + } break; - } - if (!moduleNameExpr.text) { - break; - } - if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { - (imports || (imports = [])).push(moduleNameExpr); - } - break; - case 218 /* ModuleDeclaration */: - if (node.name.kind === 9 /* StringLiteral */ && (node.flags & 4 /* Ambient */ || ts.isDeclarationFile(file))) { - // TypeScript 1.0 spec (April 2014): 12.1.6 - // An AmbientExternalModuleDeclaration declares an external module. - // This type of declaration is permitted only in the global module. - // The StringLiteral must specify a top - level external module name. - // Relative external module names are not permitted - ts.forEachChild(node.body, function (node) { + case 218 /* ModuleDeclaration */: + if (node.name.kind === 9 /* StringLiteral */ && (node.flags & 4 /* Ambient */ || ts.isDeclarationFile(file))) { // TypeScript 1.0 spec (April 2014): 12.1.6 - // An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules - // only through top - level external module names. Relative external module names are not permitted. - collect(node, /* allowRelativeModuleNames */ false); - }); - } - break; + // An AmbientExternalModuleDeclaration declares an external module. + // This type of declaration is permitted only in the global module. + // The StringLiteral must specify a top - level external module name. + // Relative external module names are not permitted + ts.forEachChild(node.body, function (node) { + // TypeScript 1.0 spec (April 2014): 12.1.6 + // An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules + // only through top - level external module names. Relative external module names are not permitted. + collect(node, /* allowRelativeModuleNames */ false, collectOnlyRequireCalls); + }); + } + break; + } + } + if (isJavaScriptFile) { + if (ts.isRequireCall(node)) { + (imports || (imports = [])).push(node.arguments[0]); + } + else { + ts.forEachChild(node, function (node) { return collect(node, allowRelativeModuleNames, /* collectOnlyRequireCalls */ true); }); + } } } } @@ -37526,7 +38017,6 @@ var ts; // always process imported modules to record module name resolutions processImportedModules(file, basePath); if (isDefaultLib) { - file.isDefaultLib = true; files.unshift(file); } else { @@ -37604,6 +38094,9 @@ var ts; commonPathComponents.length = sourcePathComponents.length; } }); + if (!commonPathComponents) { + return currentDirectory; + } return ts.getNormalizedPathFromPathComponents(commonPathComponents); } function checkSourceFilesBelongToPath(sourceFiles, rootDirectory) { @@ -37689,12 +38182,15 @@ var ts; if (options.module === 5 /* ES6 */ && languageVersion < 2 /* ES6 */) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_compile_modules_into_es2015_when_targeting_ES5_or_lower)); } + // Cannot specify module gen that isn't amd or system with --out + if (outFile && options.module && !(options.module === 2 /* AMD */ || options.module === 4 /* System */)) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile")); + } // there has to be common source directory if user specified --outdir || --sourceRoot // if user specified --mapRoot, there needs to be common source directory if there would be multiple files being emitted if (options.outDir || options.sourceRoot || - (options.mapRoot && - (!outFile || firstExternalModuleSourceFile !== undefined))) { + options.mapRoot) { if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { // If a rootDir is specified and is valid use it as the commonSourceDirectory commonSourceDirectory = ts.getNormalizedAbsolutePath(options.rootDir, currentDirectory); @@ -38212,20 +38708,20 @@ var ts; var exclude = json["exclude"] instanceof Array ? ts.map(json["exclude"], ts.normalizeSlashes) : undefined; var sysFiles = host.readDirectory(basePath, ".ts", exclude).concat(host.readDirectory(basePath, ".tsx", exclude)); for (var i = 0; i < sysFiles.length; i++) { - var name_29 = sysFiles[i]; - if (ts.fileExtensionIs(name_29, ".d.ts")) { - var baseName = name_29.substr(0, name_29.length - ".d.ts".length); + var name_31 = sysFiles[i]; + if (ts.fileExtensionIs(name_31, ".d.ts")) { + var baseName = name_31.substr(0, name_31.length - ".d.ts".length); if (!ts.contains(sysFiles, baseName + ".tsx") && !ts.contains(sysFiles, baseName + ".ts")) { - fileNames.push(name_29); + fileNames.push(name_31); } } - else if (ts.fileExtensionIs(name_29, ".ts")) { - if (!ts.contains(sysFiles, name_29 + "x")) { - fileNames.push(name_29); + else if (ts.fileExtensionIs(name_31, ".ts")) { + if (!ts.contains(sysFiles, name_31 + "x")) { + fileNames.push(name_31); } } else { - fileNames.push(name_29); + fileNames.push(name_31); } } } @@ -38453,12 +38949,12 @@ var ts; ts.forEach(program.getSourceFiles(), function (sourceFile) { cancellationToken.throwIfCancellationRequested(); var nameToDeclarations = sourceFile.getNamedDeclarations(); - for (var name_30 in nameToDeclarations) { - var declarations = ts.getProperty(nameToDeclarations, name_30); + for (var name_32 in nameToDeclarations) { + var declarations = ts.getProperty(nameToDeclarations, name_32); if (declarations) { // First do a quick check to see if the name of the declaration matches the // last portion of the (possibly) dotted name they're searching for. - var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_30); + var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_32); if (!matches) { continue; } @@ -38471,14 +38967,14 @@ var ts; if (!containers) { return undefined; } - matches = patternMatcher.getMatches(containers, name_30); + matches = patternMatcher.getMatches(containers, name_32); if (!matches) { continue; } } var fileName = sourceFile.fileName; var matchKind = bestMatchKind(matches); - rawItems.push({ name: name_30, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); + rawItems.push({ name: name_32, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); } } } @@ -38859,9 +39355,9 @@ var ts; case 211 /* VariableDeclaration */: case 163 /* BindingElement */: var variableDeclarationNode; - var name_31; + var name_33; if (node.kind === 163 /* BindingElement */) { - name_31 = node.name; + name_33 = node.name; variableDeclarationNode = node; // binding elements are added only for variable declarations // bubble up to the containing variable declaration @@ -38873,16 +39369,16 @@ var ts; else { ts.Debug.assert(!ts.isBindingPattern(node.name)); variableDeclarationNode = node; - name_31 = node.name; + name_33 = node.name; } if (ts.isConst(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.constElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.constElement); } else if (ts.isLet(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.letElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.letElement); } else { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.variableElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.variableElement); } case 144 /* Constructor */: return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); @@ -39802,7 +40298,7 @@ var ts; if (!candidates.length) { // We didn't have any sig help items produced by the TS compiler. If this is a JS // file, then see if we can figure out anything better. - if (ts.isJavaScript(sourceFile.fileName)) { + if (ts.isSourceFileJavaScript(sourceFile)) { return createJavaScriptSignatureHelpItems(argumentInfo); } return undefined; @@ -41662,9 +42158,9 @@ var ts; } Rules.prototype.getRuleName = function (rule) { var o = this; - for (var name_32 in o) { - if (o[name_32] === rule) { - return name_32; + for (var name_34 in o) { + if (o[name_34] === rule) { + return name_34; } } throw new Error("Unknown rule"); @@ -42096,7 +42592,7 @@ var ts; function TokenRangeAccess(from, to, except) { this.tokens = []; for (var token = from; token <= to; token++) { - if (except.indexOf(token) < 0) { + if (ts.indexOf(except, token) < 0) { this.tokens.push(token); } } @@ -43709,13 +44205,18 @@ var ts; ]; var jsDocCompletionEntries; function createNode(kind, pos, end, flags, parent) { - var node = new (ts.getNodeConstructor(kind))(pos, end); + var node = new NodeObject(kind, pos, end); node.flags = flags; node.parent = parent; return node; } var NodeObject = (function () { - function NodeObject() { + function NodeObject(kind, pos, end) { + this.kind = kind; + this.pos = pos; + this.end = end; + this.flags = 0 /* None */; + this.parent = undefined; } NodeObject.prototype.getSourceFile = function () { return ts.getSourceFileOfNode(this); @@ -44198,8 +44699,8 @@ var ts; })(); var SourceFileObject = (function (_super) { __extends(SourceFileObject, _super); - function SourceFileObject() { - _super.apply(this, arguments); + function SourceFileObject(kind, pos, end) { + _super.call(this, kind, pos, end); } SourceFileObject.prototype.update = function (newText, textChangeRange) { return ts.updateSourceFile(this, newText, textChangeRange); @@ -44521,6 +45022,9 @@ var ts; ClassificationTypeNames.typeAliasName = "type alias name"; ClassificationTypeNames.parameterName = "parameter name"; ClassificationTypeNames.docCommentTagName = "doc comment tag name"; + ClassificationTypeNames.jsxOpenTagName = "jsx open tag name"; + ClassificationTypeNames.jsxCloseTagName = "jsx close tag name"; + ClassificationTypeNames.jsxSelfClosingTagName = "jsx self closing tag name"; return ClassificationTypeNames; })(); ts.ClassificationTypeNames = ClassificationTypeNames; @@ -44543,6 +45047,9 @@ var ts; ClassificationType[ClassificationType["typeAliasName"] = 16] = "typeAliasName"; ClassificationType[ClassificationType["parameterName"] = 17] = "parameterName"; ClassificationType[ClassificationType["docCommentTagName"] = 18] = "docCommentTagName"; + ClassificationType[ClassificationType["jsxOpenTagName"] = 19] = "jsxOpenTagName"; + ClassificationType[ClassificationType["jsxCloseTagName"] = 20] = "jsxCloseTagName"; + ClassificationType[ClassificationType["jsxSelfClosingTagName"] = 21] = "jsxSelfClosingTagName"; })(ts.ClassificationType || (ts.ClassificationType = {})); var ClassificationType = ts.ClassificationType; function displayPartsToString(displayParts) { @@ -44922,8 +45429,9 @@ var ts; }; } ts.createDocumentRegistry = createDocumentRegistry; - function preProcessFile(sourceText, readImportFiles) { + function preProcessFile(sourceText, readImportFiles, detectJavaScriptImports) { if (readImportFiles === void 0) { readImportFiles = true; } + if (detectJavaScriptImports === void 0) { detectJavaScriptImports = false; } var referencedFiles = []; var importedFiles = []; var ambientExternalModules; @@ -44957,9 +45465,207 @@ var ts; end: pos + importPath.length }); } - function processImport() { + /** + * Returns true if at least one token was consumed from the stream + */ + function tryConsumeDeclare() { + var token = scanner.getToken(); + if (token === 122 /* DeclareKeyword */) { + // declare module "mod" + token = scanner.scan(); + if (token === 125 /* ModuleKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + recordAmbientExternalModule(); + } + } + return true; + } + return false; + } + /** + * Returns true if at least one token was consumed from the stream + */ + function tryConsumeImport() { + var token = scanner.getToken(); + if (token === 89 /* ImportKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import "mod"; + recordModuleName(); + return true; + } + else { + if (token === 69 /* Identifier */ || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import d from "mod"; + recordModuleName(); + return true; + } + } + else if (token === 56 /* EqualsToken */) { + if (tryConsumeRequireCall(/* skipCurrentToken */ true)) { + return true; + } + } + else if (token === 24 /* CommaToken */) { + // consume comma and keep going + token = scanner.scan(); + } + else { + // unknown syntax + return true; + } + } + if (token === 15 /* OpenBraceToken */) { + token = scanner.scan(); + // consume "{ a as B, c, d as D}" clauses + // make sure that it stops on EOF + while (token !== 16 /* CloseBraceToken */ && token !== 1 /* EndOfFileToken */) { + token = scanner.scan(); + } + if (token === 16 /* CloseBraceToken */) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import {a as A} from "mod"; + // import d, {a, b as B} from "mod" + recordModuleName(); + } + } + } + } + else if (token === 37 /* AsteriskToken */) { + token = scanner.scan(); + if (token === 116 /* AsKeyword */) { + token = scanner.scan(); + if (token === 69 /* Identifier */ || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import * as NS from "mod" + // import d, * as NS from "mod" + recordModuleName(); + } + } + } + } + } + } + return true; + } + return false; + } + function tryConsumeExport() { + var token = scanner.getToken(); + if (token === 82 /* ExportKeyword */) { + token = scanner.scan(); + if (token === 15 /* OpenBraceToken */) { + token = scanner.scan(); + // consume "{ a as B, c, d as D}" clauses + // make sure it stops on EOF + while (token !== 16 /* CloseBraceToken */ && token !== 1 /* EndOfFileToken */) { + token = scanner.scan(); + } + if (token === 16 /* CloseBraceToken */) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // export {a as A} from "mod"; + // export {a, b as B} from "mod" + recordModuleName(); + } + } + } + } + else if (token === 37 /* AsteriskToken */) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // export * from "mod" + recordModuleName(); + } + } + } + else if (token === 89 /* ImportKeyword */) { + token = scanner.scan(); + if (token === 69 /* Identifier */ || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 56 /* EqualsToken */) { + if (tryConsumeRequireCall(/* skipCurrentToken */ true)) { + return true; + } + } + } + } + return true; + } + return false; + } + function tryConsumeRequireCall(skipCurrentToken) { + var token = skipCurrentToken ? scanner.scan() : scanner.getToken(); + if (token === 127 /* RequireKeyword */) { + token = scanner.scan(); + if (token === 17 /* OpenParenToken */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // require("mod"); + recordModuleName(); + } + } + return true; + } + return false; + } + function tryConsumeDefine() { + var token = scanner.getToken(); + if (token === 69 /* Identifier */ && scanner.getTokenValue() === "define") { + token = scanner.scan(); + if (token !== 17 /* OpenParenToken */) { + return true; + } + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // looks like define ("modname", ... - skip string literal and comma + token = scanner.scan(); + if (token === 24 /* CommaToken */) { + token = scanner.scan(); + } + else { + // unexpected token + return true; + } + } + // should be start of dependency list + if (token !== 19 /* OpenBracketToken */) { + return true; + } + // skip open bracket + token = scanner.scan(); + var i = 0; + // scan until ']' or EOF + while (token !== 20 /* CloseBracketToken */ && token !== 1 /* EndOfFileToken */) { + // record string literals as module names + if (token === 9 /* StringLiteral */) { + recordModuleName(); + i++; + } + token = scanner.scan(); + } + return true; + } + return false; + } + function processImports() { scanner.setText(sourceText); - var token = scanner.scan(); + scanner.scan(); // Look for: // import "mod"; // import d from "mod" @@ -44971,152 +45677,26 @@ var ts; // export * from "mod" // export {a as b} from "mod" // export import i = require("mod") - while (token !== 1 /* EndOfFileToken */) { - if (token === 122 /* DeclareKeyword */) { - // declare module "mod" - token = scanner.scan(); - if (token === 125 /* ModuleKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - recordAmbientExternalModule(); - continue; - } - } + // (for JavaScript files) require("mod") + while (true) { + if (scanner.getToken() === 1 /* EndOfFileToken */) { + break; } - else if (token === 89 /* ImportKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import "mod"; - recordModuleName(); - continue; - } - else { - if (token === 69 /* Identifier */ || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import d from "mod"; - recordModuleName(); - continue; - } - } - else if (token === 56 /* EqualsToken */) { - token = scanner.scan(); - if (token === 127 /* RequireKeyword */) { - token = scanner.scan(); - if (token === 17 /* OpenParenToken */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import i = require("mod"); - recordModuleName(); - continue; - } - } - } - } - else if (token === 24 /* CommaToken */) { - // consume comma and keep going - token = scanner.scan(); - } - else { - // unknown syntax - continue; - } - } - if (token === 15 /* OpenBraceToken */) { - token = scanner.scan(); - // consume "{ a as B, c, d as D}" clauses - while (token !== 16 /* CloseBraceToken */) { - token = scanner.scan(); - } - if (token === 16 /* CloseBraceToken */) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import {a as A} from "mod"; - // import d, {a, b as B} from "mod" - recordModuleName(); - } - } - } - } - else if (token === 37 /* AsteriskToken */) { - token = scanner.scan(); - if (token === 116 /* AsKeyword */) { - token = scanner.scan(); - if (token === 69 /* Identifier */ || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import * as NS from "mod" - // import d, * as NS from "mod" - recordModuleName(); - } - } - } - } - } - } + // check if at least one of alternative have moved scanner forward + if (tryConsumeDeclare() || + tryConsumeImport() || + tryConsumeExport() || + (detectJavaScriptImports && (tryConsumeRequireCall(/* skipCurrentToken */ false) || tryConsumeDefine()))) { + continue; } - else if (token === 82 /* ExportKeyword */) { - token = scanner.scan(); - if (token === 15 /* OpenBraceToken */) { - token = scanner.scan(); - // consume "{ a as B, c, d as D}" clauses - while (token !== 16 /* CloseBraceToken */) { - token = scanner.scan(); - } - if (token === 16 /* CloseBraceToken */) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // export {a as A} from "mod"; - // export {a, b as B} from "mod" - recordModuleName(); - } - } - } - } - else if (token === 37 /* AsteriskToken */) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // export * from "mod" - recordModuleName(); - } - } - } - else if (token === 89 /* ImportKeyword */) { - token = scanner.scan(); - if (token === 69 /* Identifier */ || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 56 /* EqualsToken */) { - token = scanner.scan(); - if (token === 127 /* RequireKeyword */) { - token = scanner.scan(); - if (token === 17 /* OpenParenToken */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // export import i = require("mod"); - recordModuleName(); - } - } - } - } - } - } + else { + scanner.scan(); } - token = scanner.scan(); } scanner.setText(undefined); } if (readImportFiles) { - processImport(); + processImports(); } processTripleSlashDirectives(); return { referencedFiles: referencedFiles, importedFiles: importedFiles, isLibFile: isNoDefaultLib, ambientExternalModules: ambientExternalModules }; @@ -45547,7 +46127,7 @@ var ts; // For JavaScript files, we don't want to report the normal typescript semantic errors. // Instead, we just report errors for using TypeScript-only constructs from within a // JavaScript file. - if (ts.isJavaScript(fileName)) { + if (ts.isSourceFileJavaScript(targetSourceFile)) { return getJavaScriptSemanticDiagnostics(targetSourceFile); } // Only perform the action per file regardless of '-out' flag as LanguageServiceHost is expected to call this function per file. @@ -45759,7 +46339,7 @@ var ts; var typeChecker = program.getTypeChecker(); var syntacticStart = new Date().getTime(); var sourceFile = getValidSourceFile(fileName); - var isJavaScriptFile = ts.isJavaScript(fileName); + var isJavaScriptFile = ts.isSourceFileJavaScript(sourceFile); var isJsDocTagName = false; var start = new Date().getTime(); var currentToken = ts.getTokenAtPosition(sourceFile, position); @@ -46393,8 +46973,8 @@ var ts; if (element.getStart() <= position && position <= element.getEnd()) { continue; } - var name_33 = element.propertyName || element.name; - exisingImportsOrExports[name_33.text] = true; + var name_35 = element.propertyName || element.name; + exisingImportsOrExports[name_35.text] = true; } if (ts.isEmpty(exisingImportsOrExports)) { return exportsOfModule; @@ -46426,7 +47006,10 @@ var ts; } var existingName = void 0; if (m.kind === 163 /* BindingElement */ && m.propertyName) { - existingName = m.propertyName.text; + // include only identifiers in completion list + if (m.propertyName.kind === 69 /* Identifier */) { + existingName = m.propertyName.text; + } } else { // TODO(jfreeman): Account for computed property name @@ -46466,46 +47049,43 @@ var ts; return undefined; } var symbols = completionData.symbols, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isRightOfDot = completionData.isRightOfDot, isJsDocTagName = completionData.isJsDocTagName; - var entries; if (isJsDocTagName) { // If the current position is a jsDoc tag name, only tag names should be provided for completion return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: getAllJsDocCompletionEntries() }; } - if (isRightOfDot && ts.isJavaScript(fileName)) { - entries = getCompletionEntriesFromSymbols(symbols); - ts.addRange(entries, getJavaScriptCompletionEntries()); + var sourceFile = getValidSourceFile(fileName); + var entries = []; + if (isRightOfDot && ts.isSourceFileJavaScript(sourceFile)) { + var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries); + ts.addRange(entries, getJavaScriptCompletionEntries(sourceFile, uniqueNames)); } else { if (!symbols || symbols.length === 0) { return undefined; } - entries = getCompletionEntriesFromSymbols(symbols); + getCompletionEntriesFromSymbols(symbols, entries); } // Add keywords if this is not a member completion list if (!isMemberCompletion && !isJsDocTagName) { ts.addRange(entries, keywordCompletions); } return { isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries: entries }; - function getJavaScriptCompletionEntries() { + function getJavaScriptCompletionEntries(sourceFile, uniqueNames) { var entries = []; - var allNames = {}; var target = program.getCompilerOptions().target; - for (var _i = 0, _a = program.getSourceFiles(); _i < _a.length; _i++) { - var sourceFile = _a[_i]; - var nameTable = getNameTable(sourceFile); - for (var name_34 in nameTable) { - if (!allNames[name_34]) { - allNames[name_34] = name_34; - var displayName = getCompletionEntryDisplayName(name_34, target, /*performCharacterChecks:*/ true); - if (displayName) { - var entry = { - name: displayName, - kind: ScriptElementKind.warning, - kindModifiers: "", - sortText: "1" - }; - entries.push(entry); - } + var nameTable = getNameTable(sourceFile); + for (var name_36 in nameTable) { + if (!uniqueNames[name_36]) { + uniqueNames[name_36] = name_36; + var displayName = getCompletionEntryDisplayName(name_36, target, /*performCharacterChecks:*/ true); + if (displayName) { + var entry = { + name: displayName, + kind: ScriptElementKind.warning, + kindModifiers: "", + sortText: "1" + }; + entries.push(entry); } } } @@ -46543,25 +47123,24 @@ var ts; sortText: "0" }; } - function getCompletionEntriesFromSymbols(symbols) { + function getCompletionEntriesFromSymbols(symbols, entries) { var start = new Date().getTime(); - var entries = []; + var uniqueNames = {}; if (symbols) { - var nameToSymbol = {}; for (var _i = 0, symbols_3 = symbols; _i < symbols_3.length; _i++) { var symbol = symbols_3[_i]; var entry = createCompletionEntry(symbol, location); if (entry) { var id = ts.escapeIdentifier(entry.name); - if (!ts.lookUp(nameToSymbol, id)) { + if (!ts.lookUp(uniqueNames, id)) { entries.push(entry); - nameToSymbol[id] = symbol; + uniqueNames[id] = id; } } } } log("getCompletionsAtPosition: getCompletionEntriesFromSymbols: " + (new Date().getTime() - start)); - return entries; + return uniqueNames; } } function getCompletionEntryDetails(fileName, position, entryName) { @@ -46762,16 +47341,16 @@ var ts; case ScriptElementKind.parameterElement: case ScriptElementKind.localVariableElement: // If it is call or construct signature of lambda's write type name - displayParts.push(ts.punctuationPart(54 /* ColonToken */)); + displayParts.push(ts.punctuationPart(ts.SyntaxKind.ColonToken)); displayParts.push(ts.spacePart()); if (useConstructSignatures) { - displayParts.push(ts.keywordPart(92 /* NewKeyword */)); + displayParts.push(ts.keywordPart(ts.SyntaxKind.NewKeyword)); displayParts.push(ts.spacePart()); } - if (!(type.flags & 65536 /* Anonymous */)) { - ts.addRange(displayParts, ts.symbolToDisplayParts(typeChecker, type.symbol, enclosingDeclaration, /*meaning*/ undefined, 1 /* WriteTypeParametersOrArguments */)); + if (!(type.flags & ts.TypeFlags.Anonymous)) { + ts.addRange(displayParts, ts.symbolToDisplayParts(typeChecker, type.symbol, enclosingDeclaration, /*meaning*/ undefined, ts.SymbolFormatFlags.WriteTypeParametersOrArguments)); } - addSignatureDisplayParts(signature, allSignatures, 8 /* WriteArrowStyleSignature */); + addSignatureDisplayParts(signature, allSignatures, ts.TypeFormatFlags.WriteArrowStyleSignature); break; default: // Just signature @@ -48396,19 +48975,19 @@ var ts; if (isNameOfPropertyAssignment(node)) { var objectLiteral = node.parent.parent; var contextualType = typeChecker.getContextualType(objectLiteral); - var name_35 = node.text; + var name_37 = node.text; if (contextualType) { if (contextualType.flags & 16384 /* Union */) { // This is a union type, first see if the property we are looking for is a union property (i.e. exists in all types) // if not, search the constituent types for the property - var unionProperty = contextualType.getProperty(name_35); + var unionProperty = contextualType.getProperty(name_37); if (unionProperty) { return [unionProperty]; } else { var result_4 = []; ts.forEach(contextualType.types, function (t) { - var symbol = t.getProperty(name_35); + var symbol = t.getProperty(name_37); if (symbol) { result_4.push(symbol); } @@ -48417,7 +48996,7 @@ var ts; } } else { - var symbol_1 = contextualType.getProperty(name_35); + var symbol_1 = contextualType.getProperty(name_37); if (symbol_1) { return [symbol_1]; } @@ -48828,6 +49407,9 @@ var ts; case 16 /* typeAliasName */: return ClassificationTypeNames.typeAliasName; case 17 /* parameterName */: return ClassificationTypeNames.parameterName; case 18 /* docCommentTagName */: return ClassificationTypeNames.docCommentTagName; + case 19 /* jsxOpenTagName */: return ClassificationTypeNames.jsxOpenTagName; + case 20 /* jsxCloseTagName */: return ClassificationTypeNames.jsxCloseTagName; + case 21 /* jsxSelfClosingTagName */: return ClassificationTypeNames.jsxSelfClosingTagName; } } function convertClassifications(classifications) { @@ -49097,6 +49679,21 @@ var ts; return 17 /* parameterName */; } return; + case 235 /* JsxOpeningElement */: + if (token.parent.tagName === token) { + return 19 /* jsxOpenTagName */; + } + return; + case 237 /* JsxClosingElement */: + if (token.parent.tagName === token) { + return 20 /* jsxCloseTagName */; + } + return; + case 234 /* JsxSelfClosingElement */: + if (token.parent.tagName === token) { + return 21 /* jsxSelfClosingTagName */; + } + return; } } return 2 /* identifier */; @@ -50021,18 +50618,8 @@ var ts; ts.getDefaultLibFilePath = getDefaultLibFilePath; function initializeServices() { ts.objectAllocator = { - getNodeConstructor: function (kind) { - function Node(pos, end) { - this.pos = pos; - this.end = end; - this.flags = 0 /* None */; - this.parent = undefined; - } - var proto = kind === 248 /* SourceFile */ ? new SourceFileObject() : new NodeObject(); - proto.kind = kind; - Node.prototype = proto; - return Node; - }, + getNodeConstructor: function () { return NodeObject; }, + getSourceFileConstructor: function () { return SourceFileObject; }, getSymbolConstructor: function () { return SymbolObject; }, getTypeConstructor: function () { return TypeObject; }, getSignatureConstructor: function () { return SignatureObject; } @@ -51102,7 +51689,8 @@ var ts; }; CoreServicesShimObject.prototype.getPreProcessedFileInfo = function (fileName, sourceTextSnapshot) { return this.forwardJSONCall("getPreProcessedFileInfo('" + fileName + "')", function () { - var result = ts.preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength())); + // for now treat files as JavaScript + var result = ts.preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), /* readImportFiles */ true, /* detectJavaScriptImports */ true); var convertResult = { referencedFiles: [], importedFiles: [], @@ -51166,7 +51754,7 @@ var ts; TypeScriptServicesFactory.prototype.createLanguageServiceShim = function (host) { try { if (this.documentRegistry === undefined) { - this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames()); + this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(), host.getCurrentDirectory()); } var hostAdapter = new LanguageServiceShimHostAdapter(host); var languageService = ts.createLanguageService(hostAdapter, this.documentRegistry); @@ -51199,7 +51787,7 @@ var ts; TypeScriptServicesFactory.prototype.close = function () { // Forget all the registered shims this._shims = []; - this.documentRegistry = ts.createDocumentRegistry(); + this.documentRegistry = undefined; }; TypeScriptServicesFactory.prototype.registerShim = function (shim) { this._shims.push(shim); diff --git a/lib/typescriptServices.d.ts b/lib/typescriptServices.d.ts index d2758e6d5e9..af9a8bffe35 100644 --- a/lib/typescriptServices.d.ts +++ b/lib/typescriptServices.d.ts @@ -387,6 +387,7 @@ declare namespace ts { right: Identifier; } type EntityName = Identifier | QualifiedName; + type PropertyName = Identifier | LiteralExpression | ComputedPropertyName; type DeclarationName = Identifier | LiteralExpression | ComputedPropertyName | BindingPattern; interface Declaration extends Node { _declarationBrand: any; @@ -425,7 +426,7 @@ declare namespace ts { initializer?: Expression; } interface BindingElement extends Declaration { - propertyName?: Identifier; + propertyName?: PropertyName; dotDotDotToken?: Node; name: Identifier | BindingPattern; initializer?: Expression; @@ -452,7 +453,7 @@ declare namespace ts { objectAssignmentInitializer?: Expression; } interface VariableLikeDeclaration extends Declaration { - propertyName?: Identifier; + propertyName?: PropertyName; dotDotDotToken?: Node; name: DeclarationName; questionToken?: Node; @@ -581,7 +582,7 @@ declare namespace ts { asteriskToken?: Node; expression?: Expression; } - interface BinaryExpression extends Expression { + interface BinaryExpression extends Expression, Declaration { left: Expression; operatorToken: Node; right: Expression; @@ -625,7 +626,7 @@ declare namespace ts { interface ObjectLiteralExpression extends PrimaryExpression, Declaration { properties: NodeArray; } - interface PropertyAccessExpression extends MemberExpression { + interface PropertyAccessExpression extends MemberExpression, Declaration { expression: LeftHandSideExpression; dotToken: Node; name: Identifier; @@ -1220,6 +1221,7 @@ declare namespace ts { ObjectLiteral = 524288, ESSymbol = 16777216, ThisType = 33554432, + ObjectLiteralPatternWithComputedProperties = 67108864, StringLike = 258, NumberLike = 132, ObjectType = 80896, @@ -1537,7 +1539,6 @@ declare namespace ts { function getTypeParameterOwner(d: Declaration): Declaration; } declare namespace ts { - function getNodeConstructor(kind: SyntaxKind): new (pos?: number, end?: number) => Node; function createNode(kind: SyntaxKind, pos?: number, end?: number): Node; function forEachChild(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T; function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean): SourceFile; @@ -2126,6 +2127,9 @@ declare namespace ts { static typeAliasName: string; static parameterName: string; static docCommentTagName: string; + static jsxOpenTagName: string; + static jsxCloseTagName: string; + static jsxSelfClosingTagName: string; } enum ClassificationType { comment = 1, @@ -2146,6 +2150,9 @@ declare namespace ts { typeAliasName = 16, parameterName = 17, docCommentTagName = 18, + jsxOpenTagName = 19, + jsxCloseTagName = 20, + jsxSelfClosingTagName = 21, } interface DisplayPartsSymbolWriter extends SymbolWriter { displayParts(): SymbolDisplayPart[]; @@ -2171,7 +2178,7 @@ declare namespace ts { function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; function createGetCanonicalFileName(useCaseSensitivefileNames: boolean): (fileName: string) => string; function createDocumentRegistry(useCaseSensitiveFileNames?: boolean, currentDirectory?: string): DocumentRegistry; - function preProcessFile(sourceText: string, readImportFiles?: boolean): PreProcessedFileInfo; + function preProcessFile(sourceText: string, readImportFiles?: boolean, detectJavaScriptImports?: boolean): PreProcessedFileInfo; function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry): LanguageService; function createClassifier(): Classifier; /** diff --git a/lib/typescriptServices.js b/lib/typescriptServices.js index 8b0ef04f96e..498ddc37860 100644 --- a/lib/typescriptServices.js +++ b/lib/typescriptServices.js @@ -622,6 +622,7 @@ var ts; TypeFlags[TypeFlags["ContainsAnyFunctionType"] = 8388608] = "ContainsAnyFunctionType"; TypeFlags[TypeFlags["ESSymbol"] = 16777216] = "ESSymbol"; TypeFlags[TypeFlags["ThisType"] = 33554432] = "ThisType"; + TypeFlags[TypeFlags["ObjectLiteralPatternWithComputedProperties"] = 67108864] = "ObjectLiteralPatternWithComputedProperties"; /* @internal */ TypeFlags[TypeFlags["Intrinsic"] = 16777343] = "Intrinsic"; /* @internal */ @@ -1530,12 +1531,7 @@ var ts; * List of supported extensions in order of file resolution precedence. */ ts.supportedExtensions = [".ts", ".tsx", ".d.ts"]; - /** - * List of extensions that will be used to look for external modules. - * This list is kept separate from supportedExtensions to for cases when we'll allow to include .js files in compilation, - * but still would like to load only TypeScript files as modules - */ - ts.moduleFileExtensions = ts.supportedExtensions; + ts.supportedJsExtensions = ts.supportedExtensions.concat(".js", ".jsx"); function isSupportedSourceFileName(fileName) { if (!fileName) { return false; @@ -1586,17 +1582,16 @@ var ts; } function Signature(checker) { } + function Node(kind, pos, end) { + this.kind = kind; + this.pos = pos; + this.end = end; + this.flags = 0 /* None */; + this.parent = undefined; + } ts.objectAllocator = { - getNodeConstructor: function (kind) { - function Node(pos, end) { - this.pos = pos; - this.end = end; - this.flags = 0 /* None */; - this.parent = undefined; - } - Node.prototype = { kind: kind }; - return Node; - }, + getNodeConstructor: function () { return Node; }, + getSourceFileConstructor: function () { return Node; }, getSymbolConstructor: function () { return Symbol; }, getTypeConstructor: function () { return Type; }, getSignatureConstructor: function () { return Signature; } @@ -1913,7 +1908,16 @@ var ts; if (writeByteOrderMark) { data = "\uFEFF" + data; } - _fs.writeFileSync(fileName, data, "utf8"); + var fd; + try { + fd = _fs.openSync(fileName, "w"); + _fs.writeSync(fd, data, undefined, "utf8"); + } + finally { + if (fd !== undefined) { + _fs.closeSync(fd); + } + } } function getCanonicalPath(path) { return useCaseSensitiveFileNames ? path.toLowerCase() : path; @@ -2614,6 +2618,7 @@ var ts; Disallow_inconsistently_cased_references_to_the_same_file: { code: 6078, category: ts.DiagnosticCategory.Message, key: "Disallow_inconsistently_cased_references_to_the_same_file_6078", message: "Disallow inconsistently-cased references to the same file." }, Specify_JSX_code_generation_Colon_preserve_or_react: { code: 6080, category: ts.DiagnosticCategory.Message, key: "Specify_JSX_code_generation_Colon_preserve_or_react_6080", message: "Specify JSX code generation: 'preserve' or 'react'" }, Argument_for_jsx_must_be_preserve_or_react: { code: 6081, category: ts.DiagnosticCategory.Message, key: "Argument_for_jsx_must_be_preserve_or_react_6081", message: "Argument for '--jsx' must be 'preserve' or 'react'." }, + Only_amd_and_system_modules_are_supported_alongside_0: { code: 6082, category: ts.DiagnosticCategory.Error, key: "Only_amd_and_system_modules_are_supported_alongside_0_6082", message: "Only 'amd' and 'system' modules are supported alongside --{0}." }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -3173,7 +3178,7 @@ var ts; function getCommentRanges(text, pos, trailing) { var result; var collecting = trailing || pos === 0; - while (true) { + while (pos < text.length) { var ch = text.charCodeAt(pos); switch (ch) { case 13 /* carriageReturn */: @@ -3242,6 +3247,7 @@ var ts; } return result; } + return result; } function getLeadingCommentRanges(text, pos) { return getCommentRanges(text, pos, /*trailing*/ false); @@ -3343,7 +3349,7 @@ var ts; error(ts.Diagnostics.Digit_expected); } } - return +(text.substring(start, end)); + return "" + +(text.substring(start, end)); } function scanOctalDigits() { var start = pos; @@ -3770,7 +3776,7 @@ var ts; return pos++, token = 36 /* MinusToken */; case 46 /* dot */: if (isDigit(text.charCodeAt(pos + 1))) { - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8 /* NumericLiteral */; } if (text.charCodeAt(pos + 1) === 46 /* dot */ && text.charCodeAt(pos + 2) === 46 /* dot */) { @@ -3873,7 +3879,7 @@ var ts; case 55 /* _7 */: case 56 /* _8 */: case 57 /* _9 */: - tokenValue = "" + scanNumber(); + tokenValue = scanNumber(); return token = 8 /* NumericLiteral */; case 58 /* colon */: return pos++, token = 54 /* ColonToken */; @@ -4177,1321 +4183,6 @@ var ts; } ts.createScanner = createScanner; })(ts || (ts = {})); -/// -/* @internal */ -var ts; -(function (ts) { - ts.bindTime = 0; - (function (ModuleInstanceState) { - ModuleInstanceState[ModuleInstanceState["NonInstantiated"] = 0] = "NonInstantiated"; - ModuleInstanceState[ModuleInstanceState["Instantiated"] = 1] = "Instantiated"; - ModuleInstanceState[ModuleInstanceState["ConstEnumOnly"] = 2] = "ConstEnumOnly"; - })(ts.ModuleInstanceState || (ts.ModuleInstanceState = {})); - var ModuleInstanceState = ts.ModuleInstanceState; - var Reachability; - (function (Reachability) { - Reachability[Reachability["Unintialized"] = 1] = "Unintialized"; - Reachability[Reachability["Reachable"] = 2] = "Reachable"; - Reachability[Reachability["Unreachable"] = 4] = "Unreachable"; - Reachability[Reachability["ReportedUnreachable"] = 8] = "ReportedUnreachable"; - })(Reachability || (Reachability = {})); - function or(state1, state2) { - return (state1 | state2) & 2 /* Reachable */ - ? 2 /* Reachable */ - : (state1 & state2) & 8 /* ReportedUnreachable */ - ? 8 /* ReportedUnreachable */ - : 4 /* Unreachable */; - } - function getModuleInstanceState(node) { - // A module is uninstantiated if it contains only - // 1. interface declarations, type alias declarations - if (node.kind === 215 /* InterfaceDeclaration */ || node.kind === 216 /* TypeAliasDeclaration */) { - return 0 /* NonInstantiated */; - } - else if (ts.isConstEnumDeclaration(node)) { - return 2 /* ConstEnumOnly */; - } - else if ((node.kind === 222 /* ImportDeclaration */ || node.kind === 221 /* ImportEqualsDeclaration */) && !(node.flags & 2 /* Export */)) { - return 0 /* NonInstantiated */; - } - else if (node.kind === 219 /* ModuleBlock */) { - var state = 0 /* NonInstantiated */; - ts.forEachChild(node, function (n) { - switch (getModuleInstanceState(n)) { - case 0 /* NonInstantiated */: - // child is non-instantiated - continue searching - return false; - case 2 /* ConstEnumOnly */: - // child is const enum only - record state and continue searching - state = 2 /* ConstEnumOnly */; - return false; - case 1 /* Instantiated */: - // child is instantiated - record state and stop - state = 1 /* Instantiated */; - return true; - } - }); - return state; - } - else if (node.kind === 218 /* ModuleDeclaration */) { - return getModuleInstanceState(node.body); - } - else { - return 1 /* Instantiated */; - } - } - ts.getModuleInstanceState = getModuleInstanceState; - var ContainerFlags; - (function (ContainerFlags) { - // The current node is not a container, and no container manipulation should happen before - // recursing into it. - ContainerFlags[ContainerFlags["None"] = 0] = "None"; - // The current node is a container. It should be set as the current container (and block- - // container) before recursing into it. The current node does not have locals. Examples: - // - // Classes, ObjectLiterals, TypeLiterals, Interfaces... - ContainerFlags[ContainerFlags["IsContainer"] = 1] = "IsContainer"; - // The current node is a block-scoped-container. It should be set as the current block- - // container before recursing into it. Examples: - // - // Blocks (when not parented by functions), Catch clauses, For/For-in/For-of statements... - ContainerFlags[ContainerFlags["IsBlockScopedContainer"] = 2] = "IsBlockScopedContainer"; - ContainerFlags[ContainerFlags["HasLocals"] = 4] = "HasLocals"; - // If the current node is a container that also container that also contains locals. Examples: - // - // Functions, Methods, Modules, Source-files. - ContainerFlags[ContainerFlags["IsContainerWithLocals"] = 5] = "IsContainerWithLocals"; - })(ContainerFlags || (ContainerFlags = {})); - var binder = createBinder(); - function bindSourceFile(file, options) { - var start = new Date().getTime(); - binder(file, options); - ts.bindTime += new Date().getTime() - start; - } - ts.bindSourceFile = bindSourceFile; - function createBinder() { - var file; - var options; - var parent; - var container; - var blockScopeContainer; - var lastContainer; - var seenThisKeyword; - // state used by reachability checks - var hasExplicitReturn; - var currentReachabilityState; - var labelStack; - var labelIndexMap; - var implicitLabels; - // If this file is an external module, then it is automatically in strict-mode according to - // ES6. If it is not an external module, then we'll determine if it is in strict mode or - // not depending on if we see "use strict" in certain places (or if we hit a class/namespace). - var inStrictMode; - var symbolCount = 0; - var Symbol; - var classifiableNames; - function bindSourceFile(f, opts) { - file = f; - options = opts; - inStrictMode = !!file.externalModuleIndicator; - classifiableNames = {}; - Symbol = ts.objectAllocator.getSymbolConstructor(); - if (!file.locals) { - bind(file); - file.symbolCount = symbolCount; - file.classifiableNames = classifiableNames; - } - parent = undefined; - container = undefined; - blockScopeContainer = undefined; - lastContainer = undefined; - seenThisKeyword = false; - hasExplicitReturn = false; - labelStack = undefined; - labelIndexMap = undefined; - implicitLabels = undefined; - } - return bindSourceFile; - function createSymbol(flags, name) { - symbolCount++; - return new Symbol(flags, name); - } - function addDeclarationToSymbol(symbol, node, symbolFlags) { - symbol.flags |= symbolFlags; - node.symbol = symbol; - if (!symbol.declarations) { - symbol.declarations = []; - } - symbol.declarations.push(node); - if (symbolFlags & 1952 /* HasExports */ && !symbol.exports) { - symbol.exports = {}; - } - if (symbolFlags & 6240 /* HasMembers */ && !symbol.members) { - symbol.members = {}; - } - if (symbolFlags & 107455 /* Value */ && !symbol.valueDeclaration) { - symbol.valueDeclaration = node; - } - } - // Should not be called on a declaration with a computed property name, - // unless it is a well known Symbol. - function getDeclarationName(node) { - if (node.name) { - if (node.kind === 218 /* ModuleDeclaration */ && node.name.kind === 9 /* StringLiteral */) { - return "\"" + node.name.text + "\""; - } - if (node.name.kind === 136 /* ComputedPropertyName */) { - var nameExpression = node.name.expression; - ts.Debug.assert(ts.isWellKnownSymbolSyntactically(nameExpression)); - return ts.getPropertyNameForKnownSymbolName(nameExpression.name.text); - } - return node.name.text; - } - switch (node.kind) { - case 144 /* Constructor */: - return "__constructor"; - case 152 /* FunctionType */: - case 147 /* CallSignature */: - return "__call"; - case 153 /* ConstructorType */: - case 148 /* ConstructSignature */: - return "__new"; - case 149 /* IndexSignature */: - return "__index"; - case 228 /* ExportDeclaration */: - return "__export"; - case 227 /* ExportAssignment */: - return node.isExportEquals ? "export=" : "default"; - case 213 /* FunctionDeclaration */: - case 214 /* ClassDeclaration */: - return node.flags & 512 /* Default */ ? "default" : undefined; - } - } - function getDisplayName(node) { - return node.name ? ts.declarationNameToString(node.name) : getDeclarationName(node); - } - /** - * Declares a Symbol for the node and adds it to symbols. Reports errors for conflicting identifier names. - * @param symbolTable - The symbol table which node will be added to. - * @param parent - node's parent declaration. - * @param node - The declaration to be added to the symbol table - * @param includes - The SymbolFlags that node has in addition to its declaration type (eg: export, ambient, etc.) - * @param excludes - The flags which node cannot be declared alongside in a symbol table. Used to report forbidden declarations. - */ - function declareSymbol(symbolTable, parent, node, includes, excludes) { - ts.Debug.assert(!ts.hasDynamicName(node)); - var isDefaultExport = node.flags & 512 /* Default */; - // The exported symbol for an export default function/class node is always named "default" - var name = isDefaultExport && parent ? "default" : getDeclarationName(node); - var symbol; - if (name !== undefined) { - // Check and see if the symbol table already has a symbol with this name. If not, - // create a new symbol with this name and add it to the table. Note that we don't - // give the new symbol any flags *yet*. This ensures that it will not conflict - // with the 'excludes' flags we pass in. - // - // If we do get an existing symbol, see if it conflicts with the new symbol we're - // creating. For example, a 'var' symbol and a 'class' symbol will conflict within - // the same symbol table. If we have a conflict, report the issue on each - // declaration we have for this symbol, and then create a new symbol for this - // declaration. - // - // If we created a new symbol, either because we didn't have a symbol with this name - // in the symbol table, or we conflicted with an existing symbol, then just add this - // node as the sole declaration of the new symbol. - // - // Otherwise, we'll be merging into a compatible existing symbol (for example when - // you have multiple 'vars' with the same name in the same container). In this case - // just add this node into the declarations list of the symbol. - symbol = ts.hasProperty(symbolTable, name) - ? symbolTable[name] - : (symbolTable[name] = createSymbol(0 /* None */, name)); - if (name && (includes & 788448 /* Classifiable */)) { - classifiableNames[name] = name; - } - if (symbol.flags & excludes) { - if (node.name) { - node.name.parent = node; - } - // Report errors every position with duplicate declaration - // Report errors on previous encountered declarations - var message = symbol.flags & 2 /* BlockScopedVariable */ - ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 - : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (declaration.flags & 512 /* Default */) { - message = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; - } - }); - ts.forEach(symbol.declarations, function (declaration) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message, getDisplayName(declaration))); - }); - file.bindDiagnostics.push(ts.createDiagnosticForNode(node.name || node, message, getDisplayName(node))); - symbol = createSymbol(0 /* None */, name); - } - } - else { - symbol = createSymbol(0 /* None */, "__missing"); - } - addDeclarationToSymbol(symbol, node, includes); - symbol.parent = parent; - return symbol; - } - function declareModuleMember(node, symbolFlags, symbolExcludes) { - var hasExportModifier = ts.getCombinedNodeFlags(node) & 2 /* Export */; - if (symbolFlags & 8388608 /* Alias */) { - if (node.kind === 230 /* ExportSpecifier */ || (node.kind === 221 /* ImportEqualsDeclaration */ && hasExportModifier)) { - return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - } - else { - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - else { - // Exported module members are given 2 symbols: A local symbol that is classified with an ExportValue, - // ExportType, or ExportContainer flag, and an associated export symbol with all the correct flags set - // on it. There are 2 main reasons: - // - // 1. We treat locals and exports of the same name as mutually exclusive within a container. - // That means the binder will issue a Duplicate Identifier error if you mix locals and exports - // with the same name in the same container. - // TODO: Make this a more specific error and decouple it from the exclusion logic. - // 2. When we checkIdentifier in the checker, we set its resolved symbol to the local symbol, - // but return the export symbol (by calling getExportSymbolOfValueSymbolIfExported). That way - // when the emitter comes back to it, it knows not to qualify the name if it was found in a containing scope. - if (hasExportModifier || container.flags & 131072 /* ExportContext */) { - var exportKind = (symbolFlags & 107455 /* Value */ ? 1048576 /* ExportValue */ : 0) | - (symbolFlags & 793056 /* Type */ ? 2097152 /* ExportType */ : 0) | - (symbolFlags & 1536 /* Namespace */ ? 4194304 /* ExportNamespace */ : 0); - var local = declareSymbol(container.locals, undefined, node, exportKind, symbolExcludes); - local.exportSymbol = declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - node.localSymbol = local; - return local; - } - else { - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - } - // All container nodes are kept on a linked list in declaration order. This list is used by - // the getLocalNameOfContainer function in the type checker to validate that the local name - // used for a container is unique. - function bindChildren(node) { - // Before we recurse into a node's chilren, we first save the existing parent, container - // and block-container. Then after we pop out of processing the children, we restore - // these saved values. - var saveParent = parent; - var saveContainer = container; - var savedBlockScopeContainer = blockScopeContainer; - // This node will now be set as the parent of all of its children as we recurse into them. - parent = node; - // Depending on what kind of node this is, we may have to adjust the current container - // and block-container. If the current node is a container, then it is automatically - // considered the current block-container as well. Also, for containers that we know - // may contain locals, we proactively initialize the .locals field. We do this because - // it's highly likely that the .locals will be needed to place some child in (for example, - // a parameter, or variable declaration). - // - // However, we do not proactively create the .locals for block-containers because it's - // totally normal and common for block-containers to never actually have a block-scoped - // variable in them. We don't want to end up allocating an object for every 'block' we - // run into when most of them won't be necessary. - // - // Finally, if this is a block-container, then we clear out any existing .locals object - // it may contain within it. This happens in incremental scenarios. Because we can be - // reusing a node from a previous compilation, that node may have had 'locals' created - // for it. We must clear this so we don't accidently move any stale data forward from - // a previous compilation. - var containerFlags = getContainerFlags(node); - if (containerFlags & 1 /* IsContainer */) { - container = blockScopeContainer = node; - if (containerFlags & 4 /* HasLocals */) { - container.locals = {}; - } - addToContainerChain(container); - } - else if (containerFlags & 2 /* IsBlockScopedContainer */) { - blockScopeContainer = node; - blockScopeContainer.locals = undefined; - } - var savedReachabilityState; - var savedLabelStack; - var savedLabels; - var savedImplicitLabels; - var savedHasExplicitReturn; - var kind = node.kind; - var flags = node.flags; - // reset all reachability check related flags on node (for incremental scenarios) - flags &= ~1572864 /* ReachabilityCheckFlags */; - if (kind === 215 /* InterfaceDeclaration */) { - seenThisKeyword = false; - } - var saveState = kind === 248 /* SourceFile */ || kind === 219 /* ModuleBlock */ || ts.isFunctionLikeKind(kind); - if (saveState) { - savedReachabilityState = currentReachabilityState; - savedLabelStack = labelStack; - savedLabels = labelIndexMap; - savedImplicitLabels = implicitLabels; - savedHasExplicitReturn = hasExplicitReturn; - currentReachabilityState = 2 /* Reachable */; - hasExplicitReturn = false; - labelStack = labelIndexMap = implicitLabels = undefined; - } - bindReachableStatement(node); - if (currentReachabilityState === 2 /* Reachable */ && ts.isFunctionLikeKind(kind) && ts.nodeIsPresent(node.body)) { - flags |= 524288 /* HasImplicitReturn */; - if (hasExplicitReturn) { - flags |= 1048576 /* HasExplicitReturn */; - } - } - if (kind === 215 /* InterfaceDeclaration */) { - flags = seenThisKeyword ? flags | 262144 /* ContainsThis */ : flags & ~262144 /* ContainsThis */; - } - node.flags = flags; - if (saveState) { - hasExplicitReturn = savedHasExplicitReturn; - currentReachabilityState = savedReachabilityState; - labelStack = savedLabelStack; - labelIndexMap = savedLabels; - implicitLabels = savedImplicitLabels; - } - container = saveContainer; - parent = saveParent; - blockScopeContainer = savedBlockScopeContainer; - } - /** - * Returns true if node and its subnodes were successfully traversed. - * Returning false means that node was not examined and caller needs to dive into the node himself. - */ - function bindReachableStatement(node) { - if (checkUnreachable(node)) { - ts.forEachChild(node, bind); - return; - } - switch (node.kind) { - case 198 /* WhileStatement */: - bindWhileStatement(node); - break; - case 197 /* DoStatement */: - bindDoStatement(node); - break; - case 199 /* ForStatement */: - bindForStatement(node); - break; - case 200 /* ForInStatement */: - case 201 /* ForOfStatement */: - bindForInOrForOfStatement(node); - break; - case 196 /* IfStatement */: - bindIfStatement(node); - break; - case 204 /* ReturnStatement */: - case 208 /* ThrowStatement */: - bindReturnOrThrow(node); - break; - case 203 /* BreakStatement */: - case 202 /* ContinueStatement */: - bindBreakOrContinueStatement(node); - break; - case 209 /* TryStatement */: - bindTryStatement(node); - break; - case 206 /* SwitchStatement */: - bindSwitchStatement(node); - break; - case 220 /* CaseBlock */: - bindCaseBlock(node); - break; - case 207 /* LabeledStatement */: - bindLabeledStatement(node); - break; - default: - ts.forEachChild(node, bind); - break; - } - } - function bindWhileStatement(n) { - var preWhileState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - var postWhileState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - // bind expressions (don't affect reachability) - bind(n.expression); - currentReachabilityState = preWhileState; - var postWhileLabel = pushImplicitLabel(); - bind(n.statement); - popImplicitLabel(postWhileLabel, postWhileState); - } - function bindDoStatement(n) { - var preDoState = currentReachabilityState; - var postDoLabel = pushImplicitLabel(); - bind(n.statement); - var postDoState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : preDoState; - popImplicitLabel(postDoLabel, postDoState); - // bind expressions (don't affect reachability) - bind(n.expression); - } - function bindForStatement(n) { - var preForState = currentReachabilityState; - var postForLabel = pushImplicitLabel(); - // bind expressions (don't affect reachability) - bind(n.initializer); - bind(n.condition); - bind(n.incrementor); - bind(n.statement); - // for statement is considered infinite when it condition is either omitted or is true keyword - // - for(..;;..) - // - for(..;true;..) - var isInfiniteLoop = (!n.condition || n.condition.kind === 99 /* TrueKeyword */); - var postForState = isInfiniteLoop ? 4 /* Unreachable */ : preForState; - popImplicitLabel(postForLabel, postForState); - } - function bindForInOrForOfStatement(n) { - var preStatementState = currentReachabilityState; - var postStatementLabel = pushImplicitLabel(); - // bind expressions (don't affect reachability) - bind(n.initializer); - bind(n.expression); - bind(n.statement); - popImplicitLabel(postStatementLabel, preStatementState); - } - function bindIfStatement(n) { - // denotes reachability state when entering 'thenStatement' part of the if statement: - // i.e. if condition is false then thenStatement is unreachable - var ifTrueState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - // denotes reachability state when entering 'elseStatement': - // i.e. if condition is true then elseStatement is unreachable - var ifFalseState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; - currentReachabilityState = ifTrueState; - // bind expression (don't affect reachability) - bind(n.expression); - bind(n.thenStatement); - if (n.elseStatement) { - var preElseState = currentReachabilityState; - currentReachabilityState = ifFalseState; - bind(n.elseStatement); - currentReachabilityState = or(currentReachabilityState, preElseState); - } - else { - currentReachabilityState = or(currentReachabilityState, ifFalseState); - } - } - function bindReturnOrThrow(n) { - // bind expression (don't affect reachability) - bind(n.expression); - if (n.kind === 204 /* ReturnStatement */) { - hasExplicitReturn = true; - } - currentReachabilityState = 4 /* Unreachable */; - } - function bindBreakOrContinueStatement(n) { - // call bind on label (don't affect reachability) - bind(n.label); - // for continue case touch label so it will be marked a used - var isValidJump = jumpToLabel(n.label, n.kind === 203 /* BreakStatement */ ? currentReachabilityState : 4 /* Unreachable */); - if (isValidJump) { - currentReachabilityState = 4 /* Unreachable */; - } - } - function bindTryStatement(n) { - // catch\finally blocks has the same reachability as try block - var preTryState = currentReachabilityState; - bind(n.tryBlock); - var postTryState = currentReachabilityState; - currentReachabilityState = preTryState; - bind(n.catchClause); - var postCatchState = currentReachabilityState; - currentReachabilityState = preTryState; - bind(n.finallyBlock); - // post catch/finally state is reachable if - // - post try state is reachable - control flow can fall out of try block - // - post catch state is reachable - control flow can fall out of catch block - currentReachabilityState = or(postTryState, postCatchState); - } - function bindSwitchStatement(n) { - var preSwitchState = currentReachabilityState; - var postSwitchLabel = pushImplicitLabel(); - // bind expression (don't affect reachability) - bind(n.expression); - bind(n.caseBlock); - var hasDefault = ts.forEach(n.caseBlock.clauses, function (c) { return c.kind === 242 /* DefaultClause */; }); - // post switch state is unreachable if switch is exaustive (has a default case ) and does not have fallthrough from the last case - var postSwitchState = hasDefault && currentReachabilityState !== 2 /* Reachable */ ? 4 /* Unreachable */ : preSwitchState; - popImplicitLabel(postSwitchLabel, postSwitchState); - } - function bindCaseBlock(n) { - var startState = currentReachabilityState; - for (var _i = 0, _a = n.clauses; _i < _a.length; _i++) { - var clause = _a[_i]; - currentReachabilityState = startState; - bind(clause); - if (clause.statements.length && currentReachabilityState === 2 /* Reachable */ && options.noFallthroughCasesInSwitch) { - errorOnFirstToken(clause, ts.Diagnostics.Fallthrough_case_in_switch); - } - } - } - function bindLabeledStatement(n) { - // call bind on label (don't affect reachability) - bind(n.label); - var ok = pushNamedLabel(n.label); - bind(n.statement); - if (ok) { - popNamedLabel(n.label, currentReachabilityState); - } - } - function getContainerFlags(node) { - switch (node.kind) { - case 186 /* ClassExpression */: - case 214 /* ClassDeclaration */: - case 215 /* InterfaceDeclaration */: - case 217 /* EnumDeclaration */: - case 155 /* TypeLiteral */: - case 165 /* ObjectLiteralExpression */: - return 1 /* IsContainer */; - case 147 /* CallSignature */: - case 148 /* ConstructSignature */: - case 149 /* IndexSignature */: - case 143 /* MethodDeclaration */: - case 142 /* MethodSignature */: - case 213 /* FunctionDeclaration */: - case 144 /* Constructor */: - case 145 /* GetAccessor */: - case 146 /* SetAccessor */: - case 152 /* FunctionType */: - case 153 /* ConstructorType */: - case 173 /* FunctionExpression */: - case 174 /* ArrowFunction */: - case 218 /* ModuleDeclaration */: - case 248 /* SourceFile */: - case 216 /* TypeAliasDeclaration */: - return 5 /* IsContainerWithLocals */; - case 244 /* CatchClause */: - case 199 /* ForStatement */: - case 200 /* ForInStatement */: - case 201 /* ForOfStatement */: - case 220 /* CaseBlock */: - return 2 /* IsBlockScopedContainer */; - case 192 /* Block */: - // do not treat blocks directly inside a function as a block-scoped-container. - // Locals that reside in this block should go to the function locals. Othewise 'x' - // would not appear to be a redeclaration of a block scoped local in the following - // example: - // - // function foo() { - // var x; - // let x; - // } - // - // If we placed 'var x' into the function locals and 'let x' into the locals of - // the block, then there would be no collision. - // - // By not creating a new block-scoped-container here, we ensure that both 'var x' - // and 'let x' go into the Function-container's locals, and we do get a collision - // conflict. - return ts.isFunctionLike(node.parent) ? 0 /* None */ : 2 /* IsBlockScopedContainer */; - } - return 0 /* None */; - } - function addToContainerChain(next) { - if (lastContainer) { - lastContainer.nextContainer = next; - } - lastContainer = next; - } - function declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes) { - // Just call this directly so that the return type of this function stays "void". - declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes); - } - function declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes) { - switch (container.kind) { - // Modules, source files, and classes need specialized handling for how their - // members are declared (for example, a member of a class will go into a specific - // symbol table depending on if it is static or not). We defer to specialized - // handlers to take care of declaring these child members. - case 218 /* ModuleDeclaration */: - return declareModuleMember(node, symbolFlags, symbolExcludes); - case 248 /* SourceFile */: - return declareSourceFileMember(node, symbolFlags, symbolExcludes); - case 186 /* ClassExpression */: - case 214 /* ClassDeclaration */: - return declareClassMember(node, symbolFlags, symbolExcludes); - case 217 /* EnumDeclaration */: - return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); - case 155 /* TypeLiteral */: - case 165 /* ObjectLiteralExpression */: - case 215 /* InterfaceDeclaration */: - // Interface/Object-types always have their children added to the 'members' of - // their container. They are only accessible through an instance of their - // container, and are never in scope otherwise (even inside the body of the - // object / type / interface declaring them). An exception is type parameters, - // which are in scope without qualification (similar to 'locals'). - return declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); - case 152 /* FunctionType */: - case 153 /* ConstructorType */: - case 147 /* CallSignature */: - case 148 /* ConstructSignature */: - case 149 /* IndexSignature */: - case 143 /* MethodDeclaration */: - case 142 /* MethodSignature */: - case 144 /* Constructor */: - case 145 /* GetAccessor */: - case 146 /* SetAccessor */: - case 213 /* FunctionDeclaration */: - case 173 /* FunctionExpression */: - case 174 /* ArrowFunction */: - case 216 /* TypeAliasDeclaration */: - // All the children of these container types are never visible through another - // symbol (i.e. through another symbol's 'exports' or 'members'). Instead, - // they're only accessed 'lexically' (i.e. from code that exists underneath - // their container in the tree. To accomplish this, we simply add their declared - // symbol to the 'locals' of the container. These symbols can then be found as - // the type checker walks up the containers, checking them for matching names. - return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - function declareClassMember(node, symbolFlags, symbolExcludes) { - return node.flags & 64 /* Static */ - ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) - : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); - } - function declareSourceFileMember(node, symbolFlags, symbolExcludes) { - return ts.isExternalModule(file) - ? declareModuleMember(node, symbolFlags, symbolExcludes) - : declareSymbol(file.locals, undefined, node, symbolFlags, symbolExcludes); - } - function hasExportDeclarations(node) { - var body = node.kind === 248 /* SourceFile */ ? node : node.body; - if (body.kind === 248 /* SourceFile */ || body.kind === 219 /* ModuleBlock */) { - for (var _i = 0, _a = body.statements; _i < _a.length; _i++) { - var stat = _a[_i]; - if (stat.kind === 228 /* ExportDeclaration */ || stat.kind === 227 /* ExportAssignment */) { - return true; - } - } - } - return false; - } - function setExportContextFlag(node) { - // A declaration source file or ambient module declaration that contains no export declarations (but possibly regular - // declarations with export modifiers) is an export context in which declarations are implicitly exported. - if (ts.isInAmbientContext(node) && !hasExportDeclarations(node)) { - node.flags |= 131072 /* ExportContext */; - } - else { - node.flags &= ~131072 /* ExportContext */; - } - } - function bindModuleDeclaration(node) { - setExportContextFlag(node); - if (node.name.kind === 9 /* StringLiteral */) { - declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); - } - else { - var state = getModuleInstanceState(node); - if (state === 0 /* NonInstantiated */) { - declareSymbolAndAddToSymbolTable(node, 1024 /* NamespaceModule */, 0 /* NamespaceModuleExcludes */); - } - else { - declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); - if (node.symbol.flags & (16 /* Function */ | 32 /* Class */ | 256 /* RegularEnum */)) { - // if module was already merged with some function, class or non-const enum - // treat is a non-const-enum-only - node.symbol.constEnumOnlyModule = false; - } - else { - var currentModuleIsConstEnumOnly = state === 2 /* ConstEnumOnly */; - if (node.symbol.constEnumOnlyModule === undefined) { - // non-merged case - use the current state - node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly; - } - else { - // merged case: module is const enum only if all its pieces are non-instantiated or const enum - node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly; - } - } - } - } - } - function bindFunctionOrConstructorType(node) { - // For a given function symbol "<...>(...) => T" we want to generate a symbol identical - // to the one we would get for: { <...>(...): T } - // - // We do that by making an anonymous type literal symbol, and then setting the function - // symbol as its sole member. To the rest of the system, this symbol will be indistinguishable - // from an actual type literal symbol you would have gotten had you used the long form. - var symbol = createSymbol(131072 /* Signature */, getDeclarationName(node)); - addDeclarationToSymbol(symbol, node, 131072 /* Signature */); - var typeLiteralSymbol = createSymbol(2048 /* TypeLiteral */, "__type"); - addDeclarationToSymbol(typeLiteralSymbol, node, 2048 /* TypeLiteral */); - typeLiteralSymbol.members = (_a = {}, _a[symbol.name] = symbol, _a); - var _a; - } - function bindObjectLiteralExpression(node) { - var ElementKind; - (function (ElementKind) { - ElementKind[ElementKind["Property"] = 1] = "Property"; - ElementKind[ElementKind["Accessor"] = 2] = "Accessor"; - })(ElementKind || (ElementKind = {})); - if (inStrictMode) { - var seen = {}; - for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { - var prop = _a[_i]; - if (prop.name.kind !== 69 /* Identifier */) { - continue; - } - var identifier = prop.name; - // ECMA-262 11.1.5 Object Initialiser - // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true - // a.This production is contained in strict code and IsDataDescriptor(previous) is true and - // IsDataDescriptor(propId.descriptor) is true. - // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. - // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. - // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true - // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields - var currentKind = prop.kind === 245 /* PropertyAssignment */ || prop.kind === 246 /* ShorthandPropertyAssignment */ || prop.kind === 143 /* MethodDeclaration */ - ? 1 /* Property */ - : 2 /* Accessor */; - var existingKind = seen[identifier.text]; - if (!existingKind) { - seen[identifier.text] = currentKind; - continue; - } - if (currentKind === 1 /* Property */ && existingKind === 1 /* Property */) { - var span = ts.getErrorSpanForNode(file, identifier); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode)); - } - } - } - return bindAnonymousDeclaration(node, 4096 /* ObjectLiteral */, "__object"); - } - function bindAnonymousDeclaration(node, symbolFlags, name) { - var symbol = createSymbol(symbolFlags, name); - addDeclarationToSymbol(symbol, node, symbolFlags); - } - function bindBlockScopedDeclaration(node, symbolFlags, symbolExcludes) { - switch (blockScopeContainer.kind) { - case 218 /* ModuleDeclaration */: - declareModuleMember(node, symbolFlags, symbolExcludes); - break; - case 248 /* SourceFile */: - if (ts.isExternalModule(container)) { - declareModuleMember(node, symbolFlags, symbolExcludes); - break; - } - // fall through. - default: - if (!blockScopeContainer.locals) { - blockScopeContainer.locals = {}; - addToContainerChain(blockScopeContainer); - } - declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes); - } - } - function bindBlockScopedVariableDeclaration(node) { - bindBlockScopedDeclaration(node, 2 /* BlockScopedVariable */, 107455 /* BlockScopedVariableExcludes */); - } - // The binder visits every node in the syntax tree so it is a convenient place to perform a single localized - // check for reserved words used as identifiers in strict mode code. - function checkStrictModeIdentifier(node) { - if (inStrictMode && - node.originalKeywordKind >= 106 /* FirstFutureReservedWord */ && - node.originalKeywordKind <= 114 /* LastFutureReservedWord */ && - !ts.isIdentifierName(node)) { - // Report error only if there are no parse errors in file - if (!file.parseDiagnostics.length) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(node, getStrictModeIdentifierMessage(node), ts.declarationNameToString(node))); - } - } - } - function getStrictModeIdentifierMessage(node) { - // Provide specialized messages to help the user understand why we think they're in - // strict mode. - if (ts.getContainingClass(node)) { - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; - } - if (file.externalModuleIndicator) { - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; - } - return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode; - } - function checkStrictModeBinaryExpression(node) { - if (inStrictMode && ts.isLeftHandSideExpression(node.left) && ts.isAssignmentOperator(node.operatorToken.kind)) { - // ECMA 262 (Annex C) The identifier eval or arguments may not appear as the LeftHandSideExpression of an - // Assignment operator(11.13) or of a PostfixExpression(11.3) - checkStrictModeEvalOrArguments(node, node.left); - } - } - function checkStrictModeCatchClause(node) { - // It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the - // Catch production is eval or arguments - if (inStrictMode && node.variableDeclaration) { - checkStrictModeEvalOrArguments(node, node.variableDeclaration.name); - } - } - function checkStrictModeDeleteExpression(node) { - // Grammar checking - if (inStrictMode && node.expression.kind === 69 /* Identifier */) { - // When a delete operator occurs within strict mode code, a SyntaxError is thrown if its - // UnaryExpression is a direct reference to a variable, function argument, or function name - var span = ts.getErrorSpanForNode(file, node.expression); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.delete_cannot_be_called_on_an_identifier_in_strict_mode)); - } - } - function isEvalOrArgumentsIdentifier(node) { - return node.kind === 69 /* Identifier */ && - (node.text === "eval" || node.text === "arguments"); - } - function checkStrictModeEvalOrArguments(contextNode, name) { - if (name && name.kind === 69 /* Identifier */) { - var identifier = name; - if (isEvalOrArgumentsIdentifier(identifier)) { - // We check first if the name is inside class declaration or class expression; if so give explicit message - // otherwise report generic error message. - var span = ts.getErrorSpanForNode(file, name); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, getStrictModeEvalOrArgumentsMessage(contextNode), identifier.text)); - } - } - } - function getStrictModeEvalOrArgumentsMessage(node) { - // Provide specialized messages to help the user understand why we think they're in - // strict mode. - if (ts.getContainingClass(node)) { - return ts.Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; - } - if (file.externalModuleIndicator) { - return ts.Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; - } - return ts.Diagnostics.Invalid_use_of_0_in_strict_mode; - } - function checkStrictModeFunctionName(node) { - if (inStrictMode) { - // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression (13.1)) - checkStrictModeEvalOrArguments(node, node.name); - } - } - function checkStrictModeNumericLiteral(node) { - if (inStrictMode && node.flags & 32768 /* OctalLiteral */) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Octal_literals_are_not_allowed_in_strict_mode)); - } - } - function checkStrictModePostfixUnaryExpression(node) { - // Grammar checking - // The identifier eval or arguments may not appear as the LeftHandSideExpression of an - // Assignment operator(11.13) or of a PostfixExpression(11.3) or as the UnaryExpression - // operated upon by a Prefix Increment(11.4.4) or a Prefix Decrement(11.4.5) operator. - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.operand); - } - } - function checkStrictModePrefixUnaryExpression(node) { - // Grammar checking - if (inStrictMode) { - if (node.operator === 41 /* PlusPlusToken */ || node.operator === 42 /* MinusMinusToken */) { - checkStrictModeEvalOrArguments(node, node.operand); - } - } - } - function checkStrictModeWithStatement(node) { - // Grammar checking for withStatement - if (inStrictMode) { - errorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_strict_mode); - } - } - function errorOnFirstToken(node, message, arg0, arg1, arg2) { - var span = ts.getSpanOfTokenAtPosition(file, node.pos); - file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2)); - } - function getDestructuringParameterName(node) { - return "__" + ts.indexOf(node.parent.parameters, node); - } - function bind(node) { - if (!node) { - return; - } - node.parent = parent; - var savedInStrictMode = inStrictMode; - if (!savedInStrictMode) { - updateStrictMode(node); - } - // First we bind declaration nodes to a symbol if possible. We'll both create a symbol - // and then potentially add the symbol to an appropriate symbol table. Possible - // destination symbol tables are: - // - // 1) The 'exports' table of the current container's symbol. - // 2) The 'members' table of the current container's symbol. - // 3) The 'locals' table of the current container. - // - // However, not all symbols will end up in any of these tables. 'Anonymous' symbols - // (like TypeLiterals for example) will not be put in any table. - bindWorker(node); - // Then we recurse into the children of the node to bind them as well. For certain - // symbols we do specialized work when we recurse. For example, we'll keep track of - // the current 'container' node when it changes. This helps us know which symbol table - // a local should go into for example. - bindChildren(node); - inStrictMode = savedInStrictMode; - } - function updateStrictMode(node) { - switch (node.kind) { - case 248 /* SourceFile */: - case 219 /* ModuleBlock */: - updateStrictModeStatementList(node.statements); - return; - case 192 /* Block */: - if (ts.isFunctionLike(node.parent)) { - updateStrictModeStatementList(node.statements); - } - return; - case 214 /* ClassDeclaration */: - case 186 /* ClassExpression */: - // All classes are automatically in strict mode in ES6. - inStrictMode = true; - return; - } - } - function updateStrictModeStatementList(statements) { - for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) { - var statement = statements_1[_i]; - if (!ts.isPrologueDirective(statement)) { - return; - } - if (isUseStrictPrologueDirective(statement)) { - inStrictMode = true; - return; - } - } - } - /// Should be called only on prologue directives (isPrologueDirective(node) should be true) - function isUseStrictPrologueDirective(node) { - var nodeText = ts.getTextOfNodeFromSourceText(file.text, node.expression); - // Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the - // string to contain unicode escapes (as per ES5). - return nodeText === "\"use strict\"" || nodeText === "'use strict'"; - } - function bindWorker(node) { - switch (node.kind) { - case 69 /* Identifier */: - return checkStrictModeIdentifier(node); - case 181 /* BinaryExpression */: - return checkStrictModeBinaryExpression(node); - case 244 /* CatchClause */: - return checkStrictModeCatchClause(node); - case 175 /* DeleteExpression */: - return checkStrictModeDeleteExpression(node); - case 8 /* NumericLiteral */: - return checkStrictModeNumericLiteral(node); - case 180 /* PostfixUnaryExpression */: - return checkStrictModePostfixUnaryExpression(node); - case 179 /* PrefixUnaryExpression */: - return checkStrictModePrefixUnaryExpression(node); - case 205 /* WithStatement */: - return checkStrictModeWithStatement(node); - case 97 /* ThisKeyword */: - seenThisKeyword = true; - return; - case 137 /* TypeParameter */: - return declareSymbolAndAddToSymbolTable(node, 262144 /* TypeParameter */, 530912 /* TypeParameterExcludes */); - case 138 /* Parameter */: - return bindParameter(node); - case 211 /* VariableDeclaration */: - case 163 /* BindingElement */: - return bindVariableDeclarationOrBindingElement(node); - case 141 /* PropertyDeclaration */: - case 140 /* PropertySignature */: - return bindPropertyOrMethodOrAccessor(node, 4 /* Property */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), 107455 /* PropertyExcludes */); - case 245 /* PropertyAssignment */: - case 246 /* ShorthandPropertyAssignment */: - return bindPropertyOrMethodOrAccessor(node, 4 /* Property */, 107455 /* PropertyExcludes */); - case 247 /* EnumMember */: - return bindPropertyOrMethodOrAccessor(node, 8 /* EnumMember */, 107455 /* EnumMemberExcludes */); - case 147 /* CallSignature */: - case 148 /* ConstructSignature */: - case 149 /* IndexSignature */: - return declareSymbolAndAddToSymbolTable(node, 131072 /* Signature */, 0 /* None */); - case 143 /* MethodDeclaration */: - case 142 /* MethodSignature */: - // If this is an ObjectLiteralExpression method, then it sits in the same space - // as other properties in the object literal. So we use SymbolFlags.PropertyExcludes - // so that it will conflict with any other object literal members with the same - // name. - return bindPropertyOrMethodOrAccessor(node, 8192 /* Method */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), ts.isObjectLiteralMethod(node) ? 107455 /* PropertyExcludes */ : 99263 /* MethodExcludes */); - case 213 /* FunctionDeclaration */: - checkStrictModeFunctionName(node); - return declareSymbolAndAddToSymbolTable(node, 16 /* Function */, 106927 /* FunctionExcludes */); - case 144 /* Constructor */: - return declareSymbolAndAddToSymbolTable(node, 16384 /* Constructor */, /*symbolExcludes:*/ 0 /* None */); - case 145 /* GetAccessor */: - return bindPropertyOrMethodOrAccessor(node, 32768 /* GetAccessor */, 41919 /* GetAccessorExcludes */); - case 146 /* SetAccessor */: - return bindPropertyOrMethodOrAccessor(node, 65536 /* SetAccessor */, 74687 /* SetAccessorExcludes */); - case 152 /* FunctionType */: - case 153 /* ConstructorType */: - return bindFunctionOrConstructorType(node); - case 155 /* TypeLiteral */: - return bindAnonymousDeclaration(node, 2048 /* TypeLiteral */, "__type"); - case 165 /* ObjectLiteralExpression */: - return bindObjectLiteralExpression(node); - case 173 /* FunctionExpression */: - case 174 /* ArrowFunction */: - checkStrictModeFunctionName(node); - var bindingName = node.name ? node.name.text : "__function"; - return bindAnonymousDeclaration(node, 16 /* Function */, bindingName); - case 186 /* ClassExpression */: - case 214 /* ClassDeclaration */: - return bindClassLikeDeclaration(node); - case 215 /* InterfaceDeclaration */: - return bindBlockScopedDeclaration(node, 64 /* Interface */, 792960 /* InterfaceExcludes */); - case 216 /* TypeAliasDeclaration */: - return bindBlockScopedDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */); - case 217 /* EnumDeclaration */: - return bindEnumDeclaration(node); - case 218 /* ModuleDeclaration */: - return bindModuleDeclaration(node); - case 221 /* ImportEqualsDeclaration */: - case 224 /* NamespaceImport */: - case 226 /* ImportSpecifier */: - case 230 /* ExportSpecifier */: - return declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); - case 223 /* ImportClause */: - return bindImportClause(node); - case 228 /* ExportDeclaration */: - return bindExportDeclaration(node); - case 227 /* ExportAssignment */: - return bindExportAssignment(node); - case 248 /* SourceFile */: - return bindSourceFileIfExternalModule(); - } - } - function bindSourceFileIfExternalModule() { - setExportContextFlag(file); - if (ts.isExternalModule(file)) { - bindAnonymousDeclaration(file, 512 /* ValueModule */, "\"" + ts.removeFileExtension(file.fileName) + "\""); - } - } - function bindExportAssignment(node) { - if (!container.symbol || !container.symbol.exports) { - // Export assignment in some sort of block construct - bindAnonymousDeclaration(node, 8388608 /* Alias */, getDeclarationName(node)); - } - else if (node.expression.kind === 69 /* Identifier */) { - // An export default clause with an identifier exports all meanings of that identifier - declareSymbol(container.symbol.exports, container.symbol, node, 8388608 /* Alias */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); - } - else { - // An export default clause with an expression exports a value - declareSymbol(container.symbol.exports, container.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); - } - } - function bindExportDeclaration(node) { - if (!container.symbol || !container.symbol.exports) { - // Export * in some sort of block construct - bindAnonymousDeclaration(node, 1073741824 /* ExportStar */, getDeclarationName(node)); - } - else if (!node.exportClause) { - // All export * declarations are collected in an __export symbol - declareSymbol(container.symbol.exports, container.symbol, node, 1073741824 /* ExportStar */, 0 /* None */); - } - } - function bindImportClause(node) { - if (node.name) { - declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); - } - } - function bindClassLikeDeclaration(node) { - if (node.kind === 214 /* ClassDeclaration */) { - bindBlockScopedDeclaration(node, 32 /* Class */, 899519 /* ClassExcludes */); - } - else { - var bindingName = node.name ? node.name.text : "__class"; - bindAnonymousDeclaration(node, 32 /* Class */, bindingName); - // Add name of class expression into the map for semantic classifier - if (node.name) { - classifiableNames[node.name.text] = node.name.text; - } - } - var symbol = node.symbol; - // TypeScript 1.0 spec (April 2014): 8.4 - // Every class automatically contains a static property member named 'prototype', the - // type of which is an instantiation of the class type with type Any supplied as a type - // argument for each type parameter. It is an error to explicitly declare a static - // property member with the name 'prototype'. - // - // Note: we check for this here because this class may be merging into a module. The - // module might have an exported variable called 'prototype'. We can't allow that as - // that would clash with the built-in 'prototype' for the class. - var prototypeSymbol = createSymbol(4 /* Property */ | 134217728 /* Prototype */, "prototype"); - if (ts.hasProperty(symbol.exports, prototypeSymbol.name)) { - if (node.name) { - node.name.parent = node; - } - file.bindDiagnostics.push(ts.createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0], ts.Diagnostics.Duplicate_identifier_0, prototypeSymbol.name)); - } - symbol.exports[prototypeSymbol.name] = prototypeSymbol; - prototypeSymbol.parent = symbol; - } - function bindEnumDeclaration(node) { - return ts.isConst(node) - ? bindBlockScopedDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */) - : bindBlockScopedDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */); - } - function bindVariableDeclarationOrBindingElement(node) { - if (inStrictMode) { - checkStrictModeEvalOrArguments(node, node.name); - } - if (!ts.isBindingPattern(node.name)) { - if (ts.isBlockOrCatchScoped(node)) { - bindBlockScopedVariableDeclaration(node); - } - else if (ts.isParameterDeclaration(node)) { - // It is safe to walk up parent chain to find whether the node is a destructing parameter declaration - // because its parent chain has already been set up, since parents are set before descending into children. - // - // If node is a binding element in parameter declaration, we need to use ParameterExcludes. - // Using ParameterExcludes flag allows the compiler to report an error on duplicate identifiers in Parameter Declaration - // For example: - // function foo([a,a]) {} // Duplicate Identifier error - // function bar(a,a) {} // Duplicate Identifier error, parameter declaration in this case is handled in bindParameter - // // which correctly set excluded symbols - declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); - } - else { - declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107454 /* FunctionScopedVariableExcludes */); - } - } - } - function bindParameter(node) { - if (inStrictMode) { - // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a - // strict mode FunctionLikeDeclaration or FunctionExpression(13.1) - checkStrictModeEvalOrArguments(node, node.name); - } - if (ts.isBindingPattern(node.name)) { - bindAnonymousDeclaration(node, 1 /* FunctionScopedVariable */, getDestructuringParameterName(node)); - } - else { - declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); - } - // If this is a property-parameter, then also declare the property symbol into the - // containing class. - if (node.flags & 56 /* AccessibilityModifier */ && - node.parent.kind === 144 /* Constructor */ && - ts.isClassLike(node.parent.parent)) { - var classDeclaration = node.parent.parent; - declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */); - } - } - function bindPropertyOrMethodOrAccessor(node, symbolFlags, symbolExcludes) { - return ts.hasDynamicName(node) - ? bindAnonymousDeclaration(node, symbolFlags, "__computed") - : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); - } - // reachability checks - function pushNamedLabel(name) { - initializeReachabilityStateIfNecessary(); - if (ts.hasProperty(labelIndexMap, name.text)) { - return false; - } - labelIndexMap[name.text] = labelStack.push(1 /* Unintialized */) - 1; - return true; - } - function pushImplicitLabel() { - initializeReachabilityStateIfNecessary(); - var index = labelStack.push(1 /* Unintialized */) - 1; - implicitLabels.push(index); - return index; - } - function popNamedLabel(label, outerState) { - var index = labelIndexMap[label.text]; - ts.Debug.assert(index !== undefined); - ts.Debug.assert(labelStack.length == index + 1); - labelIndexMap[label.text] = undefined; - setCurrentStateAtLabel(labelStack.pop(), outerState, label); - } - function popImplicitLabel(implicitLabelIndex, outerState) { - if (labelStack.length !== implicitLabelIndex + 1) { - ts.Debug.assert(false, "Label stack: " + labelStack.length + ", index:" + implicitLabelIndex); - } - var i = implicitLabels.pop(); - if (implicitLabelIndex !== i) { - ts.Debug.assert(false, "i: " + i + ", index: " + implicitLabelIndex); - } - setCurrentStateAtLabel(labelStack.pop(), outerState, /*name*/ undefined); - } - function setCurrentStateAtLabel(innerMergedState, outerState, label) { - if (innerMergedState === 1 /* Unintialized */) { - if (label && !options.allowUnusedLabels) { - file.bindDiagnostics.push(ts.createDiagnosticForNode(label, ts.Diagnostics.Unused_label)); - } - currentReachabilityState = outerState; - } - else { - currentReachabilityState = or(innerMergedState, outerState); - } - } - function jumpToLabel(label, outerState) { - initializeReachabilityStateIfNecessary(); - var index = label ? labelIndexMap[label.text] : ts.lastOrUndefined(implicitLabels); - if (index === undefined) { - // reference to unknown label or - // break/continue used outside of loops - return false; - } - var stateAtLabel = labelStack[index]; - labelStack[index] = stateAtLabel === 1 /* Unintialized */ ? outerState : or(stateAtLabel, outerState); - return true; - } - function checkUnreachable(node) { - switch (currentReachabilityState) { - case 4 /* Unreachable */: - var reportError = - // report error on all statements - ts.isStatement(node) || - // report error on class declarations - node.kind === 214 /* ClassDeclaration */ || - // report error on instantiated modules or const-enums only modules if preserveConstEnums is set - (node.kind === 218 /* ModuleDeclaration */ && shouldReportErrorOnModuleDeclaration(node)) || - // report error on regular enums and const enums if preserveConstEnums is set - (node.kind === 217 /* EnumDeclaration */ && (!ts.isConstEnumDeclaration(node) || options.preserveConstEnums)); - if (reportError) { - currentReachabilityState = 8 /* ReportedUnreachable */; - // unreachable code is reported if - // - user has explicitly asked about it AND - // - statement is in not ambient context (statements in ambient context is already an error - // so we should not report extras) AND - // - node is not variable statement OR - // - node is block scoped variable statement OR - // - node is not block scoped variable statement and at least one variable declaration has initializer - // Rationale: we don't want to report errors on non-initialized var's since they are hoisted - // On the other side we do want to report errors on non-initialized 'lets' because of TDZ - var reportUnreachableCode = !options.allowUnreachableCode && - !ts.isInAmbientContext(node) && - (node.kind !== 193 /* VariableStatement */ || - ts.getCombinedNodeFlags(node.declarationList) & 24576 /* BlockScoped */ || - ts.forEach(node.declarationList.declarations, function (d) { return d.initializer; })); - if (reportUnreachableCode) { - errorOnFirstToken(node, ts.Diagnostics.Unreachable_code_detected); - } - } - case 8 /* ReportedUnreachable */: - return true; - default: - return false; - } - function shouldReportErrorOnModuleDeclaration(node) { - var instanceState = getModuleInstanceState(node); - return instanceState === 1 /* Instantiated */ || (instanceState === 2 /* ConstEnumOnly */ && options.preserveConstEnums); - } - } - function initializeReachabilityStateIfNecessary() { - if (labelIndexMap) { - return; - } - currentReachabilityState = 2 /* Reachable */; - labelIndexMap = {}; - labelStack = []; - implicitLabels = []; - } - } -})(ts || (ts = {})); -/// /// /* @internal */ var ts; @@ -5812,6 +4503,10 @@ var ts; return file.externalModuleIndicator !== undefined; } ts.isExternalModule = isExternalModule; + function isExternalOrCommonJsModule(file) { + return (file.externalModuleIndicator || file.commonJsModuleIndicator) !== undefined; + } + ts.isExternalOrCommonJsModule = isExternalOrCommonJsModule; function isDeclarationFile(file) { return (file.flags & 4096 /* DeclarationFile */) !== 0; } @@ -5865,19 +4560,27 @@ var ts; return ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos); } ts.getLeadingCommentRangesOfNode = getLeadingCommentRangesOfNode; + function getLeadingCommentRangesOfNodeFromText(node, text) { + return ts.getLeadingCommentRanges(text, node.pos); + } + ts.getLeadingCommentRangesOfNodeFromText = getLeadingCommentRangesOfNodeFromText; function getJsDocComments(node, sourceFileOfNode) { + return getJsDocCommentsFromText(node, sourceFileOfNode.text); + } + ts.getJsDocComments = getJsDocComments; + function getJsDocCommentsFromText(node, text) { var commentRanges = (node.kind === 138 /* Parameter */ || node.kind === 137 /* TypeParameter */) ? - ts.concatenate(ts.getTrailingCommentRanges(sourceFileOfNode.text, node.pos), ts.getLeadingCommentRanges(sourceFileOfNode.text, node.pos)) : - getLeadingCommentRangesOfNode(node, sourceFileOfNode); + ts.concatenate(ts.getTrailingCommentRanges(text, node.pos), ts.getLeadingCommentRanges(text, node.pos)) : + getLeadingCommentRangesOfNodeFromText(node, text); return ts.filter(commentRanges, isJsDocComment); function isJsDocComment(comment) { // True if the comment starts with '/**' but not if it is '/**/' - return sourceFileOfNode.text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && - sourceFileOfNode.text.charCodeAt(comment.pos + 2) === 42 /* asterisk */ && - sourceFileOfNode.text.charCodeAt(comment.pos + 3) !== 47 /* slash */; + return text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && + text.charCodeAt(comment.pos + 2) === 42 /* asterisk */ && + text.charCodeAt(comment.pos + 3) !== 47 /* slash */; } } - ts.getJsDocComments = getJsDocComments; + ts.getJsDocCommentsFromText = getJsDocCommentsFromText; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; ts.fullTripleSlashAMDReferencePathRegEx = /^(\/\/\/\s*/; function isTypeNode(node) { @@ -6464,6 +5167,57 @@ var ts; return node.kind === 221 /* ImportEqualsDeclaration */ && node.moduleReference.kind !== 232 /* ExternalModuleReference */; } ts.isInternalModuleImportEqualsDeclaration = isInternalModuleImportEqualsDeclaration; + function isSourceFileJavaScript(file) { + return isInJavaScriptFile(file); + } + ts.isSourceFileJavaScript = isSourceFileJavaScript; + function isInJavaScriptFile(node) { + return node && !!(node.parserContextFlags & 32 /* JavaScriptFile */); + } + ts.isInJavaScriptFile = isInJavaScriptFile; + /** + * Returns true if the node is a CallExpression to the identifier 'require' with + * exactly one string literal argument. + * This function does not test if the node is in a JavaScript file or not. + */ + function isRequireCall(expression) { + // of the form 'require("name")' + return expression.kind === 168 /* CallExpression */ && + expression.expression.kind === 69 /* Identifier */ && + expression.expression.text === "require" && + expression.arguments.length === 1 && + expression.arguments[0].kind === 9 /* StringLiteral */; + } + ts.isRequireCall = isRequireCall; + /** + * Returns true if the node is an assignment to a property on the identifier 'exports'. + * This function does not test if the node is in a JavaScript file or not. + */ + function isExportsPropertyAssignment(expression) { + // of the form 'exports.name = expr' where 'name' and 'expr' are arbitrary + return isInJavaScriptFile(expression) && + (expression.kind === 181 /* BinaryExpression */) && + (expression.operatorToken.kind === 56 /* EqualsToken */) && + (expression.left.kind === 166 /* PropertyAccessExpression */) && + (expression.left.expression.kind === 69 /* Identifier */) && + ((expression.left.expression).text === "exports"); + } + ts.isExportsPropertyAssignment = isExportsPropertyAssignment; + /** + * Returns true if the node is an assignment to the property access expression 'module.exports'. + * This function does not test if the node is in a JavaScript file or not. + */ + function isModuleExportsAssignment(expression) { + // of the form 'module.exports = expr' where 'expr' is arbitrary + return isInJavaScriptFile(expression) && + (expression.kind === 181 /* BinaryExpression */) && + (expression.operatorToken.kind === 56 /* EqualsToken */) && + (expression.left.kind === 166 /* PropertyAccessExpression */) && + (expression.left.expression.kind === 69 /* Identifier */) && + ((expression.left.expression).text === "module") && + (expression.left.name.text === "exports"); + } + ts.isModuleExportsAssignment = isModuleExportsAssignment; function getExternalModuleName(node) { if (node.kind === 222 /* ImportDeclaration */) { return node.moduleSpecifier; @@ -6791,8 +5545,8 @@ var ts; function getFileReferenceFromReferencePath(comment, commentRange) { var simpleReferenceRegEx = /^\/\/\/\s*/gim; - if (simpleReferenceRegEx.exec(comment)) { - if (isNoDefaultLibRegEx.exec(comment)) { + if (simpleReferenceRegEx.test(comment)) { + if (isNoDefaultLibRegEx.test(comment)) { return { isNoDefaultLib: true }; @@ -6834,6 +5588,10 @@ var ts; return isFunctionLike(node) && (node.flags & 256 /* Async */) !== 0 && !isAccessor(node); } ts.isAsyncFunctionLike = isAsyncFunctionLike; + function isStringOrNumericLiteral(kind) { + return kind === 9 /* StringLiteral */ || kind === 8 /* NumericLiteral */; + } + ts.isStringOrNumericLiteral = isStringOrNumericLiteral; /** * A declaration has a dynamic name if both of the following are true: * 1. The declaration has a computed property name @@ -6842,11 +5600,15 @@ var ts; * Symbol. */ function hasDynamicName(declaration) { - return declaration.name && - declaration.name.kind === 136 /* ComputedPropertyName */ && - !isWellKnownSymbolSyntactically(declaration.name.expression); + return declaration.name && isDynamicName(declaration.name); } ts.hasDynamicName = hasDynamicName; + function isDynamicName(name) { + return name.kind === 136 /* ComputedPropertyName */ && + !isStringOrNumericLiteral(name.expression.kind) && + !isWellKnownSymbolSyntactically(name.expression); + } + ts.isDynamicName = isDynamicName; /** * Checks if the expression is of the form: * Symbol.name @@ -7087,11 +5849,11 @@ var ts; } ts.getIndentSize = getIndentSize; function createTextWriter(newLine) { - var output = ""; - var indent = 0; - var lineStart = true; - var lineCount = 0; - var linePos = 0; + var output; + var indent; + var lineStart; + var lineCount; + var linePos; function write(s) { if (s && s.length) { if (lineStart) { @@ -7101,6 +5863,13 @@ var ts; output += s; } } + function reset() { + output = ""; + indent = 0; + lineStart = true; + lineCount = 0; + linePos = 0; + } function rawWrite(s) { if (s !== undefined) { if (lineStart) { @@ -7127,9 +5896,10 @@ var ts; lineStart = true; } } - function writeTextOfNode(sourceFile, node) { - write(getSourceTextOfNodeFromSourceFile(sourceFile, node)); + function writeTextOfNode(text, node) { + write(getTextOfNodeFromSourceText(text, node)); } + reset(); return { write: write, rawWrite: rawWrite, @@ -7142,10 +5912,20 @@ var ts; getTextPos: function () { return output.length; }, getLine: function () { return lineCount + 1; }, getColumn: function () { return lineStart ? indent * getIndentSize() + 1 : output.length - linePos + 1; }, - getText: function () { return output; } + getText: function () { return output; }, + reset: reset }; } ts.createTextWriter = createTextWriter; + /** + * Resolves a local path to a path which is absolute to the base of the emit + */ + function getExternalModuleNameFromPath(host, fileName) { + var dir = host.getCurrentDirectory(); + var relativePath = ts.getRelativePathToDirectoryOrUrl(dir, fileName, dir, function (f) { return host.getCanonicalFileName(f); }, /*isAbsolutePathAnUrl*/ false); + return ts.removeFileExtension(relativePath); + } + ts.getExternalModuleNameFromPath = getExternalModuleNameFromPath; function getOwnEmitOutputFilePath(sourceFile, host, extension) { var compilerOptions = host.getCompilerOptions(); var emitOutputFilePathWithoutExtension; @@ -7174,6 +5954,10 @@ var ts; return ts.getLineAndCharacterOfPosition(currentSourceFile, pos).line; } ts.getLineOfLocalPosition = getLineOfLocalPosition; + function getLineOfLocalPositionFromLineMap(lineMap, pos) { + return ts.computeLineAndCharacterOfPosition(lineMap, pos).line; + } + ts.getLineOfLocalPositionFromLineMap = getLineOfLocalPositionFromLineMap; function getFirstConstructorWithBody(node) { return ts.forEach(node.members, function (member) { if (member.kind === 144 /* Constructor */ && nodeIsPresent(member.body)) { @@ -7246,22 +6030,22 @@ var ts; }; } ts.getAllAccessorDeclarations = getAllAccessorDeclarations; - function emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments) { + function emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments) { // If the leading comments start on different line than the start of node, write new line if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos && - getLineOfLocalPosition(currentSourceFile, node.pos) !== getLineOfLocalPosition(currentSourceFile, leadingComments[0].pos)) { + getLineOfLocalPositionFromLineMap(lineMap, node.pos) !== getLineOfLocalPositionFromLineMap(lineMap, leadingComments[0].pos)) { writer.writeLine(); } } ts.emitNewLineBeforeLeadingComments = emitNewLineBeforeLeadingComments; - function emitComments(currentSourceFile, writer, comments, trailingSeparator, newLine, writeComment) { + function emitComments(text, lineMap, writer, comments, trailingSeparator, newLine, writeComment) { var emitLeadingSpace = !trailingSeparator; ts.forEach(comments, function (comment) { if (emitLeadingSpace) { writer.write(" "); emitLeadingSpace = false; } - writeComment(currentSourceFile, writer, comment, newLine); + writeComment(text, lineMap, writer, comment, newLine); if (comment.hasTrailingNewLine) { writer.writeLine(); } @@ -7279,7 +6063,7 @@ var ts; * Detached comment is a comment at the top of file or function body that is separated from * the next statement by space. */ - function emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, removeComments) { + function emitDetachedComments(text, lineMap, writer, writeComment, node, newLine, removeComments) { var leadingComments; var currentDetachedCommentInfo; if (removeComments) { @@ -7289,12 +6073,12 @@ var ts; // // var x = 10; if (node.pos === 0) { - leadingComments = ts.filter(ts.getLeadingCommentRanges(currentSourceFile.text, node.pos), isPinnedComment); + leadingComments = ts.filter(ts.getLeadingCommentRanges(text, node.pos), isPinnedComment); } } else { // removeComments is false, just get detached as normal and bypass the process to filter comment - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + leadingComments = ts.getLeadingCommentRanges(text, node.pos); } if (leadingComments) { var detachedComments = []; @@ -7302,8 +6086,8 @@ var ts; for (var _i = 0, leadingComments_1 = leadingComments; _i < leadingComments_1.length; _i++) { var comment = leadingComments_1[_i]; if (lastComment) { - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, lastComment.end); - var commentLine = getLineOfLocalPosition(currentSourceFile, comment.pos); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, lastComment.end); + var commentLine = getLineOfLocalPositionFromLineMap(lineMap, comment.pos); if (commentLine >= lastCommentLine + 2) { // There was a blank line between the last comment and this comment. This // comment is not part of the copyright comments. Return what we have so @@ -7318,36 +6102,36 @@ var ts; // All comments look like they could have been part of the copyright header. Make // sure there is at least one blank line between it and the node. If not, it's not // a copyright header. - var lastCommentLine = getLineOfLocalPosition(currentSourceFile, ts.lastOrUndefined(detachedComments).end); - var nodeLine = getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node.pos)); + var lastCommentLine = getLineOfLocalPositionFromLineMap(lineMap, ts.lastOrUndefined(detachedComments).end); + var nodeLine = getLineOfLocalPositionFromLineMap(lineMap, ts.skipTrivia(text, node.pos)); if (nodeLine >= lastCommentLine + 2) { // Valid detachedComments - emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); - emitComments(currentSourceFile, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment); + emitNewLineBeforeLeadingComments(lineMap, writer, node, leadingComments); + emitComments(text, lineMap, writer, detachedComments, /*trailingSeparator*/ true, newLine, writeComment); currentDetachedCommentInfo = { nodePos: node.pos, detachedCommentEndPos: ts.lastOrUndefined(detachedComments).end }; } } } return currentDetachedCommentInfo; function isPinnedComment(comment) { - return currentSourceFile.text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 33 /* exclamation */; + return text.charCodeAt(comment.pos + 1) === 42 /* asterisk */ && + text.charCodeAt(comment.pos + 2) === 33 /* exclamation */; } } ts.emitDetachedComments = emitDetachedComments; - function writeCommentRange(currentSourceFile, writer, comment, newLine) { - if (currentSourceFile.text.charCodeAt(comment.pos + 1) === 42 /* asterisk */) { - var firstCommentLineAndCharacter = ts.getLineAndCharacterOfPosition(currentSourceFile, comment.pos); - var lineCount = ts.getLineStarts(currentSourceFile).length; + function writeCommentRange(text, lineMap, writer, comment, newLine) { + if (text.charCodeAt(comment.pos + 1) === 42 /* asterisk */) { + var firstCommentLineAndCharacter = ts.computeLineAndCharacterOfPosition(lineMap, comment.pos); + var lineCount = lineMap.length; var firstCommentLineIndent; for (var pos = comment.pos, currentLine = firstCommentLineAndCharacter.line; pos < comment.end; currentLine++) { var nextLineStart = (currentLine + 1) === lineCount - ? currentSourceFile.text.length + 1 - : getStartPositionOfLine(currentLine + 1, currentSourceFile); + ? text.length + 1 + : lineMap[currentLine + 1]; if (pos !== comment.pos) { // If we are not emitting first line, we need to write the spaces to adjust the alignment if (firstCommentLineIndent === undefined) { - firstCommentLineIndent = calculateIndent(getStartPositionOfLine(firstCommentLineAndCharacter.line, currentSourceFile), comment.pos); + firstCommentLineIndent = calculateIndent(text, lineMap[firstCommentLineAndCharacter.line], comment.pos); } // These are number of spaces writer is going to write at current indent var currentWriterIndentSpacing = writer.getIndent() * getIndentSize(); @@ -7365,7 +6149,7 @@ var ts; // More right indented comment */ --4 = 8 - 4 + 11 // class c { } // } - var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(pos, nextLineStart); + var spacesToEmit = currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(text, pos, nextLineStart); if (spacesToEmit > 0) { var numberOfSingleSpacesToEmit = spacesToEmit % getIndentSize(); var indentSizeSpaceString = getIndentString((spacesToEmit - numberOfSingleSpacesToEmit) / getIndentSize()); @@ -7383,45 +6167,45 @@ var ts; } } // Write the comment line text - writeTrimmedCurrentLine(pos, nextLineStart); + writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart); pos = nextLineStart; } } else { // Single line comment of style //.... - writer.write(currentSourceFile.text.substring(comment.pos, comment.end)); - } - function writeTrimmedCurrentLine(pos, nextLineStart) { - var end = Math.min(comment.end, nextLineStart - 1); - var currentLineText = currentSourceFile.text.substring(pos, end).replace(/^\s+|\s+$/g, ""); - if (currentLineText) { - // trimmed forward and ending spaces text - writer.write(currentLineText); - if (end !== comment.end) { - writer.writeLine(); - } - } - else { - // Empty string - make sure we write empty line - writer.writeLiteral(newLine); - } - } - function calculateIndent(pos, end) { - var currentLineIndent = 0; - for (; pos < end && ts.isWhiteSpace(currentSourceFile.text.charCodeAt(pos)); pos++) { - if (currentSourceFile.text.charCodeAt(pos) === 9 /* tab */) { - // Tabs = TabSize = indent size and go to next tabStop - currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); - } - else { - // Single space - currentLineIndent++; - } - } - return currentLineIndent; + writer.write(text.substring(comment.pos, comment.end)); } } ts.writeCommentRange = writeCommentRange; + function writeTrimmedCurrentLine(text, comment, writer, newLine, pos, nextLineStart) { + var end = Math.min(comment.end, nextLineStart - 1); + var currentLineText = text.substring(pos, end).replace(/^\s+|\s+$/g, ""); + if (currentLineText) { + // trimmed forward and ending spaces text + writer.write(currentLineText); + if (end !== comment.end) { + writer.writeLine(); + } + } + else { + // Empty string - make sure we write empty line + writer.writeLiteral(newLine); + } + } + function calculateIndent(text, pos, end) { + var currentLineIndent = 0; + for (; pos < end && ts.isWhiteSpace(text.charCodeAt(pos)); pos++) { + if (text.charCodeAt(pos) === 9 /* tab */) { + // Tabs = TabSize = indent size and go to next tabStop + currentLineIndent += getIndentSize() - (currentLineIndent % getIndentSize()); + } + else { + // Single space + currentLineIndent++; + } + } + return currentLineIndent; + } function modifierToFlag(token) { switch (token) { case 113 /* StaticKeyword */: return 64 /* Static */; @@ -7517,14 +6301,14 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 512 /* Default */) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function isJavaScript(fileName) { - return ts.fileExtensionIs(fileName, ".js"); + function hasJavaScriptFileExtension(fileName) { + return ts.fileExtensionIs(fileName, ".js") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isJavaScript = isJavaScript; - function isTsx(fileName) { - return ts.fileExtensionIs(fileName, ".tsx"); + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function allowsJsxExpressions(fileName) { + return ts.fileExtensionIs(fileName, ".tsx") || ts.fileExtensionIs(fileName, ".jsx"); } - ts.isTsx = isTsx; + ts.allowsJsxExpressions = allowsJsxExpressions; /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences * representing the UTF-8 encoding of the character, and return the expanded char code list. @@ -7835,18 +6619,20 @@ var ts; } ts.getTypeParameterOwner = getTypeParameterOwner; })(ts || (ts = {})); -/// /// +/// var ts; (function (ts) { - var nodeConstructors = new Array(272 /* Count */); /* @internal */ ts.parseTime = 0; - function getNodeConstructor(kind) { - return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); - } - ts.getNodeConstructor = getNodeConstructor; + var NodeConstructor; + var SourceFileConstructor; function createNode(kind, pos, end) { - return new (getNodeConstructor(kind))(pos, end); + if (kind === 248 /* SourceFile */) { + return new (SourceFileConstructor || (SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor()))(kind, pos, end); + } + else { + return new (NodeConstructor || (NodeConstructor = ts.objectAllocator.getNodeConstructor()))(kind, pos, end); + } } ts.createNode = createNode; function visitNode(cbNode, node) { @@ -8269,6 +7055,9 @@ var ts; // up by avoiding the cost of creating/compiling scanners over and over again. var scanner = ts.createScanner(2 /* Latest */, /*skipTrivia*/ true); var disallowInAndDecoratorContext = 1 /* DisallowIn */ | 4 /* Decorator */; + // capture constructors in 'initializeState' to avoid null checks + var NodeConstructor; + var SourceFileConstructor; var sourceFile; var parseDiagnostics; var syntaxCursor; @@ -8354,13 +7143,16 @@ var ts; // attached to the EOF token. var parseErrorBeforeNextFinishedNode = false; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { - initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var isJavaScriptFile = ts.hasJavaScriptFileExtension(fileName) || _sourceText.lastIndexOf("// @language=javascript", 0) === 0; + initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor); var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); clearState(); return result; } Parser.parseSourceFile = parseSourceFile; - function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { + function initializeState(fileName, _sourceText, languageVersion, isJavaScriptFile, _syntaxCursor) { + NodeConstructor = ts.objectAllocator.getNodeConstructor(); + SourceFileConstructor = ts.objectAllocator.getSourceFileConstructor(); sourceText = _sourceText; syntaxCursor = _syntaxCursor; parseDiagnostics = []; @@ -8368,13 +7160,13 @@ var ts; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = ts.isJavaScript(fileName) ? 32 /* JavaScriptFile */ : 0 /* None */; + contextFlags = isJavaScriptFile ? 32 /* JavaScriptFile */ : 0 /* None */; parseErrorBeforeNextFinishedNode = false; // Initialize and prime the scanner before parsing the source elements. scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); - scanner.setLanguageVariant(ts.isTsx(fileName) ? 1 /* JSX */ : 0 /* Standard */); + scanner.setLanguageVariant(ts.allowsJsxExpressions(fileName) ? 1 /* JSX */ : 0 /* Standard */); } function clearState() { // Clear out the text the scanner is pointing at, so it doesn't keep anything alive unnecessarily. @@ -8389,6 +7181,9 @@ var ts; } function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { sourceFile = createSourceFile(fileName, languageVersion); + if (contextFlags & 32 /* JavaScriptFile */) { + sourceFile.parserContextFlags = 32 /* JavaScriptFile */; + } // Prime the scanner. token = nextToken(); processReferenceComments(sourceFile); @@ -8406,7 +7201,7 @@ var ts; // If this is a javascript file, proactively see if we can get JSDoc comments for // relevant nodes in the file. We'll use these to provide typing informaion if they're // available. - if (ts.isJavaScript(fileName)) { + if (ts.isSourceFileJavaScript(sourceFile)) { addJSDocComments(); } return sourceFile; @@ -8461,15 +7256,16 @@ var ts; } Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - var sourceFile = createNode(248 /* SourceFile */, /*pos*/ 0); - sourceFile.pos = 0; - sourceFile.end = sourceText.length; + // code from createNode is inlined here so createNode won't have to deal with special case of creating source files + // this is quite rare comparing to other nodes and createNode should be as fast as possible + var sourceFile = new SourceFileConstructor(248 /* SourceFile */, /*pos*/ 0, /* end */ sourceText.length); + nodeCount++; sourceFile.text = sourceText; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 4096 /* DeclarationFile */ : 0; - sourceFile.languageVariant = ts.isTsx(sourceFile.fileName) ? 1 /* JSX */ : 0 /* Standard */; + sourceFile.languageVariant = ts.allowsJsxExpressions(sourceFile.fileName) ? 1 /* JSX */ : 0 /* Standard */; return sourceFile; } function setContextFlag(val, flag) { @@ -8734,12 +7530,13 @@ var ts; return parseExpected(23 /* SemicolonToken */); } } + // note: this function creates only node function createNode(kind, pos) { nodeCount++; if (!(pos >= 0)) { pos = scanner.getStartPos(); } - return new (nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)))(pos, pos); + return new NodeConstructor(kind, pos, pos); } function finishNode(node, end) { node.end = end === undefined ? scanner.getStartPos() : end; @@ -8904,7 +7701,7 @@ var ts; case 12 /* ObjectLiteralMembers */: return token === 19 /* OpenBracketToken */ || token === 37 /* AsteriskToken */ || isLiteralPropertyName(); case 9 /* ObjectBindingElements */: - return isLiteralPropertyName(); + return token === 19 /* OpenBracketToken */ || isLiteralPropertyName(); case 7 /* HeritageClauseElement */: // If we see { } then only consume it as an expression if it is followed by , or { // That way we won't consume the body of a class in its heritage clause. @@ -9584,9 +8381,7 @@ var ts; } function parseParameterType() { if (parseOptional(54 /* ColonToken */)) { - return token === 9 /* StringLiteral */ - ? parseLiteralNode(/*internName*/ true) - : parseType(); + return parseType(); } return undefined; } @@ -9917,6 +8712,8 @@ var ts; // If these are followed by a dot, then parse these out as a dotted type reference instead. var node = tryParse(parseKeywordAndNoDot); return node || parseTypeReferenceOrTypePredicate(); + case 9 /* StringLiteral */: + return parseLiteralNode(/*internName*/ true); case 103 /* VoidKeyword */: case 97 /* ThisKeyword */: return parseTokenNode(); @@ -9946,6 +8743,7 @@ var ts; case 19 /* OpenBracketToken */: case 25 /* LessThanToken */: case 92 /* NewKeyword */: + case 9 /* StringLiteral */: return true; case 17 /* OpenParenToken */: // Only consider '(' the start of a type if followed by ')', '...', an identifier, a modifier, @@ -10362,7 +9160,7 @@ var ts; return 1 /* True */; } // This *could* be a parenthesized arrow function. - // Return Unknown to let the caller know. + // Return Unknown to const the caller know. return 2 /* Unknown */; } else { @@ -10448,7 +9246,7 @@ var ts; // user meant to supply a block. For example, if the user wrote: // // a => - // let v = 0; + // const v = 0; // } // // they may be missing an open brace. Check to see if that's the case so we can @@ -10664,7 +9462,6 @@ var ts; var unaryOperator = token; var simpleUnaryExpression = parseSimpleUnaryExpression(); if (token === 38 /* AsteriskAsteriskToken */) { - var diagnostic; var start = ts.skipTrivia(sourceText, simpleUnaryExpression.pos); if (simpleUnaryExpression.kind === 171 /* TypeAssertionExpression */) { parseErrorAtPosition(start, simpleUnaryExpression.end - start, ts.Diagnostics.A_type_assertion_expression_is_not_allowed_in_the_left_hand_side_of_an_exponentiation_expression_Consider_enclosing_the_expression_in_parentheses); @@ -11868,7 +10665,6 @@ var ts; } function parseObjectBindingElement() { var node = createNode(163 /* BindingElement */); - // TODO(andersh): Handle computed properties var tokenIsIdentifier = isIdentifier(); var propertyName = parsePropertyName(); if (tokenIsIdentifier && token !== 54 /* ColonToken */) { @@ -12691,7 +11487,7 @@ var ts; } JSDocParser.isJSDocType = isJSDocType; function parseJSDocTypeExpressionForTests(content, start, length) { - initializeState("file.js", content, 2 /* Latest */, /*_syntaxCursor:*/ undefined); + initializeState("file.js", content, 2 /* Latest */, /*isJavaScriptFile*/ true, /*_syntaxCursor:*/ undefined); var jsDocTypeExpression = parseJSDocTypeExpression(start, length); var diagnostics = parseDiagnostics; clearState(); @@ -12786,6 +11582,7 @@ var ts; case 103 /* VoidKeyword */: return parseTokenNode(); } + // TODO (drosen): Parse string literal types in JSDoc as well. return parseJSDocTypeReference(); } function parseJSDocThisType() { @@ -12957,7 +11754,7 @@ var ts; } } function parseIsolatedJSDocComment(content, start, length) { - initializeState("file.js", content, 2 /* Latest */, /*_syntaxCursor:*/ undefined); + initializeState("file.js", content, 2 /* Latest */, /*isJavaScriptFile*/ true, /*_syntaxCursor:*/ undefined); var jsDocComment = parseJSDocComment(/*parent:*/ undefined, start, length); var diagnostics = parseDiagnostics; clearState(); @@ -13682,6 +12479,1372 @@ var ts; })(InvalidPosition || (InvalidPosition = {})); })(IncrementalParser || (IncrementalParser = {})); })(ts || (ts = {})); +/// +/// +/* @internal */ +var ts; +(function (ts) { + ts.bindTime = 0; + (function (ModuleInstanceState) { + ModuleInstanceState[ModuleInstanceState["NonInstantiated"] = 0] = "NonInstantiated"; + ModuleInstanceState[ModuleInstanceState["Instantiated"] = 1] = "Instantiated"; + ModuleInstanceState[ModuleInstanceState["ConstEnumOnly"] = 2] = "ConstEnumOnly"; + })(ts.ModuleInstanceState || (ts.ModuleInstanceState = {})); + var ModuleInstanceState = ts.ModuleInstanceState; + var Reachability; + (function (Reachability) { + Reachability[Reachability["Unintialized"] = 1] = "Unintialized"; + Reachability[Reachability["Reachable"] = 2] = "Reachable"; + Reachability[Reachability["Unreachable"] = 4] = "Unreachable"; + Reachability[Reachability["ReportedUnreachable"] = 8] = "ReportedUnreachable"; + })(Reachability || (Reachability = {})); + function or(state1, state2) { + return (state1 | state2) & 2 /* Reachable */ + ? 2 /* Reachable */ + : (state1 & state2) & 8 /* ReportedUnreachable */ + ? 8 /* ReportedUnreachable */ + : 4 /* Unreachable */; + } + function getModuleInstanceState(node) { + // A module is uninstantiated if it contains only + // 1. interface declarations, type alias declarations + if (node.kind === 215 /* InterfaceDeclaration */ || node.kind === 216 /* TypeAliasDeclaration */) { + return 0 /* NonInstantiated */; + } + else if (ts.isConstEnumDeclaration(node)) { + return 2 /* ConstEnumOnly */; + } + else if ((node.kind === 222 /* ImportDeclaration */ || node.kind === 221 /* ImportEqualsDeclaration */) && !(node.flags & 2 /* Export */)) { + return 0 /* NonInstantiated */; + } + else if (node.kind === 219 /* ModuleBlock */) { + var state = 0 /* NonInstantiated */; + ts.forEachChild(node, function (n) { + switch (getModuleInstanceState(n)) { + case 0 /* NonInstantiated */: + // child is non-instantiated - continue searching + return false; + case 2 /* ConstEnumOnly */: + // child is const enum only - record state and continue searching + state = 2 /* ConstEnumOnly */; + return false; + case 1 /* Instantiated */: + // child is instantiated - record state and stop + state = 1 /* Instantiated */; + return true; + } + }); + return state; + } + else if (node.kind === 218 /* ModuleDeclaration */) { + return getModuleInstanceState(node.body); + } + else { + return 1 /* Instantiated */; + } + } + ts.getModuleInstanceState = getModuleInstanceState; + var ContainerFlags; + (function (ContainerFlags) { + // The current node is not a container, and no container manipulation should happen before + // recursing into it. + ContainerFlags[ContainerFlags["None"] = 0] = "None"; + // The current node is a container. It should be set as the current container (and block- + // container) before recursing into it. The current node does not have locals. Examples: + // + // Classes, ObjectLiterals, TypeLiterals, Interfaces... + ContainerFlags[ContainerFlags["IsContainer"] = 1] = "IsContainer"; + // The current node is a block-scoped-container. It should be set as the current block- + // container before recursing into it. Examples: + // + // Blocks (when not parented by functions), Catch clauses, For/For-in/For-of statements... + ContainerFlags[ContainerFlags["IsBlockScopedContainer"] = 2] = "IsBlockScopedContainer"; + ContainerFlags[ContainerFlags["HasLocals"] = 4] = "HasLocals"; + // If the current node is a container that also container that also contains locals. Examples: + // + // Functions, Methods, Modules, Source-files. + ContainerFlags[ContainerFlags["IsContainerWithLocals"] = 5] = "IsContainerWithLocals"; + })(ContainerFlags || (ContainerFlags = {})); + var binder = createBinder(); + function bindSourceFile(file, options) { + var start = new Date().getTime(); + binder(file, options); + ts.bindTime += new Date().getTime() - start; + } + ts.bindSourceFile = bindSourceFile; + function createBinder() { + var file; + var options; + var parent; + var container; + var blockScopeContainer; + var lastContainer; + var seenThisKeyword; + // state used by reachability checks + var hasExplicitReturn; + var currentReachabilityState; + var labelStack; + var labelIndexMap; + var implicitLabels; + // If this file is an external module, then it is automatically in strict-mode according to + // ES6. If it is not an external module, then we'll determine if it is in strict mode or + // not depending on if we see "use strict" in certain places (or if we hit a class/namespace). + var inStrictMode; + var symbolCount = 0; + var Symbol; + var classifiableNames; + function bindSourceFile(f, opts) { + file = f; + options = opts; + inStrictMode = !!file.externalModuleIndicator; + classifiableNames = {}; + Symbol = ts.objectAllocator.getSymbolConstructor(); + if (!file.locals) { + bind(file); + file.symbolCount = symbolCount; + file.classifiableNames = classifiableNames; + } + parent = undefined; + container = undefined; + blockScopeContainer = undefined; + lastContainer = undefined; + seenThisKeyword = false; + hasExplicitReturn = false; + labelStack = undefined; + labelIndexMap = undefined; + implicitLabels = undefined; + } + return bindSourceFile; + function createSymbol(flags, name) { + symbolCount++; + return new Symbol(flags, name); + } + function addDeclarationToSymbol(symbol, node, symbolFlags) { + symbol.flags |= symbolFlags; + node.symbol = symbol; + if (!symbol.declarations) { + symbol.declarations = []; + } + symbol.declarations.push(node); + if (symbolFlags & 1952 /* HasExports */ && !symbol.exports) { + symbol.exports = {}; + } + if (symbolFlags & 6240 /* HasMembers */ && !symbol.members) { + symbol.members = {}; + } + if (symbolFlags & 107455 /* Value */ && !symbol.valueDeclaration) { + symbol.valueDeclaration = node; + } + } + // Should not be called on a declaration with a computed property name, + // unless it is a well known Symbol. + function getDeclarationName(node) { + if (node.name) { + if (node.kind === 218 /* ModuleDeclaration */ && node.name.kind === 9 /* StringLiteral */) { + return "\"" + node.name.text + "\""; + } + if (node.name.kind === 136 /* ComputedPropertyName */) { + var nameExpression = node.name.expression; + // treat computed property names where expression is string/numeric literal as just string/numeric literal + if (ts.isStringOrNumericLiteral(nameExpression.kind)) { + return nameExpression.text; + } + ts.Debug.assert(ts.isWellKnownSymbolSyntactically(nameExpression)); + return ts.getPropertyNameForKnownSymbolName(nameExpression.name.text); + } + return node.name.text; + } + switch (node.kind) { + case 144 /* Constructor */: + return "__constructor"; + case 152 /* FunctionType */: + case 147 /* CallSignature */: + return "__call"; + case 153 /* ConstructorType */: + case 148 /* ConstructSignature */: + return "__new"; + case 149 /* IndexSignature */: + return "__index"; + case 228 /* ExportDeclaration */: + return "__export"; + case 227 /* ExportAssignment */: + return node.isExportEquals ? "export=" : "default"; + case 181 /* BinaryExpression */: + // Binary expression case is for JS module 'module.exports = expr' + return "export="; + case 213 /* FunctionDeclaration */: + case 214 /* ClassDeclaration */: + return node.flags & 512 /* Default */ ? "default" : undefined; + } + } + function getDisplayName(node) { + return node.name ? ts.declarationNameToString(node.name) : getDeclarationName(node); + } + /** + * Declares a Symbol for the node and adds it to symbols. Reports errors for conflicting identifier names. + * @param symbolTable - The symbol table which node will be added to. + * @param parent - node's parent declaration. + * @param node - The declaration to be added to the symbol table + * @param includes - The SymbolFlags that node has in addition to its declaration type (eg: export, ambient, etc.) + * @param excludes - The flags which node cannot be declared alongside in a symbol table. Used to report forbidden declarations. + */ + function declareSymbol(symbolTable, parent, node, includes, excludes) { + ts.Debug.assert(!ts.hasDynamicName(node)); + var isDefaultExport = node.flags & 512 /* Default */; + // The exported symbol for an export default function/class node is always named "default" + var name = isDefaultExport && parent ? "default" : getDeclarationName(node); + var symbol; + if (name !== undefined) { + // Check and see if the symbol table already has a symbol with this name. If not, + // create a new symbol with this name and add it to the table. Note that we don't + // give the new symbol any flags *yet*. This ensures that it will not conflict + // with the 'excludes' flags we pass in. + // + // If we do get an existing symbol, see if it conflicts with the new symbol we're + // creating. For example, a 'var' symbol and a 'class' symbol will conflict within + // the same symbol table. If we have a conflict, report the issue on each + // declaration we have for this symbol, and then create a new symbol for this + // declaration. + // + // If we created a new symbol, either because we didn't have a symbol with this name + // in the symbol table, or we conflicted with an existing symbol, then just add this + // node as the sole declaration of the new symbol. + // + // Otherwise, we'll be merging into a compatible existing symbol (for example when + // you have multiple 'vars' with the same name in the same container). In this case + // just add this node into the declarations list of the symbol. + symbol = ts.hasProperty(symbolTable, name) + ? symbolTable[name] + : (symbolTable[name] = createSymbol(0 /* None */, name)); + if (name && (includes & 788448 /* Classifiable */)) { + classifiableNames[name] = name; + } + if (symbol.flags & excludes) { + if (node.name) { + node.name.parent = node; + } + // Report errors every position with duplicate declaration + // Report errors on previous encountered declarations + var message = symbol.flags & 2 /* BlockScopedVariable */ + ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 + : ts.Diagnostics.Duplicate_identifier_0; + ts.forEach(symbol.declarations, function (declaration) { + if (declaration.flags & 512 /* Default */) { + message = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + }); + ts.forEach(symbol.declarations, function (declaration) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message, getDisplayName(declaration))); + }); + file.bindDiagnostics.push(ts.createDiagnosticForNode(node.name || node, message, getDisplayName(node))); + symbol = createSymbol(0 /* None */, name); + } + } + else { + symbol = createSymbol(0 /* None */, "__missing"); + } + addDeclarationToSymbol(symbol, node, includes); + symbol.parent = parent; + return symbol; + } + function declareModuleMember(node, symbolFlags, symbolExcludes) { + var hasExportModifier = ts.getCombinedNodeFlags(node) & 2 /* Export */; + if (symbolFlags & 8388608 /* Alias */) { + if (node.kind === 230 /* ExportSpecifier */ || (node.kind === 221 /* ImportEqualsDeclaration */ && hasExportModifier)) { + return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + } + else { + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + else { + // Exported module members are given 2 symbols: A local symbol that is classified with an ExportValue, + // ExportType, or ExportContainer flag, and an associated export symbol with all the correct flags set + // on it. There are 2 main reasons: + // + // 1. We treat locals and exports of the same name as mutually exclusive within a container. + // That means the binder will issue a Duplicate Identifier error if you mix locals and exports + // with the same name in the same container. + // TODO: Make this a more specific error and decouple it from the exclusion logic. + // 2. When we checkIdentifier in the checker, we set its resolved symbol to the local symbol, + // but return the export symbol (by calling getExportSymbolOfValueSymbolIfExported). That way + // when the emitter comes back to it, it knows not to qualify the name if it was found in a containing scope. + if (hasExportModifier || container.flags & 131072 /* ExportContext */) { + var exportKind = (symbolFlags & 107455 /* Value */ ? 1048576 /* ExportValue */ : 0) | + (symbolFlags & 793056 /* Type */ ? 2097152 /* ExportType */ : 0) | + (symbolFlags & 1536 /* Namespace */ ? 4194304 /* ExportNamespace */ : 0); + var local = declareSymbol(container.locals, undefined, node, exportKind, symbolExcludes); + local.exportSymbol = declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + node.localSymbol = local; + return local; + } + else { + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + } + // All container nodes are kept on a linked list in declaration order. This list is used by + // the getLocalNameOfContainer function in the type checker to validate that the local name + // used for a container is unique. + function bindChildren(node) { + // Before we recurse into a node's chilren, we first save the existing parent, container + // and block-container. Then after we pop out of processing the children, we restore + // these saved values. + var saveParent = parent; + var saveContainer = container; + var savedBlockScopeContainer = blockScopeContainer; + // This node will now be set as the parent of all of its children as we recurse into them. + parent = node; + // Depending on what kind of node this is, we may have to adjust the current container + // and block-container. If the current node is a container, then it is automatically + // considered the current block-container as well. Also, for containers that we know + // may contain locals, we proactively initialize the .locals field. We do this because + // it's highly likely that the .locals will be needed to place some child in (for example, + // a parameter, or variable declaration). + // + // However, we do not proactively create the .locals for block-containers because it's + // totally normal and common for block-containers to never actually have a block-scoped + // variable in them. We don't want to end up allocating an object for every 'block' we + // run into when most of them won't be necessary. + // + // Finally, if this is a block-container, then we clear out any existing .locals object + // it may contain within it. This happens in incremental scenarios. Because we can be + // reusing a node from a previous compilation, that node may have had 'locals' created + // for it. We must clear this so we don't accidently move any stale data forward from + // a previous compilation. + var containerFlags = getContainerFlags(node); + if (containerFlags & 1 /* IsContainer */) { + container = blockScopeContainer = node; + if (containerFlags & 4 /* HasLocals */) { + container.locals = {}; + } + addToContainerChain(container); + } + else if (containerFlags & 2 /* IsBlockScopedContainer */) { + blockScopeContainer = node; + blockScopeContainer.locals = undefined; + } + var savedReachabilityState; + var savedLabelStack; + var savedLabels; + var savedImplicitLabels; + var savedHasExplicitReturn; + var kind = node.kind; + var flags = node.flags; + // reset all reachability check related flags on node (for incremental scenarios) + flags &= ~1572864 /* ReachabilityCheckFlags */; + if (kind === 215 /* InterfaceDeclaration */) { + seenThisKeyword = false; + } + var saveState = kind === 248 /* SourceFile */ || kind === 219 /* ModuleBlock */ || ts.isFunctionLikeKind(kind); + if (saveState) { + savedReachabilityState = currentReachabilityState; + savedLabelStack = labelStack; + savedLabels = labelIndexMap; + savedImplicitLabels = implicitLabels; + savedHasExplicitReturn = hasExplicitReturn; + currentReachabilityState = 2 /* Reachable */; + hasExplicitReturn = false; + labelStack = labelIndexMap = implicitLabels = undefined; + } + bindReachableStatement(node); + if (currentReachabilityState === 2 /* Reachable */ && ts.isFunctionLikeKind(kind) && ts.nodeIsPresent(node.body)) { + flags |= 524288 /* HasImplicitReturn */; + if (hasExplicitReturn) { + flags |= 1048576 /* HasExplicitReturn */; + } + } + if (kind === 215 /* InterfaceDeclaration */) { + flags = seenThisKeyword ? flags | 262144 /* ContainsThis */ : flags & ~262144 /* ContainsThis */; + } + node.flags = flags; + if (saveState) { + hasExplicitReturn = savedHasExplicitReturn; + currentReachabilityState = savedReachabilityState; + labelStack = savedLabelStack; + labelIndexMap = savedLabels; + implicitLabels = savedImplicitLabels; + } + container = saveContainer; + parent = saveParent; + blockScopeContainer = savedBlockScopeContainer; + } + /** + * Returns true if node and its subnodes were successfully traversed. + * Returning false means that node was not examined and caller needs to dive into the node himself. + */ + function bindReachableStatement(node) { + if (checkUnreachable(node)) { + ts.forEachChild(node, bind); + return; + } + switch (node.kind) { + case 198 /* WhileStatement */: + bindWhileStatement(node); + break; + case 197 /* DoStatement */: + bindDoStatement(node); + break; + case 199 /* ForStatement */: + bindForStatement(node); + break; + case 200 /* ForInStatement */: + case 201 /* ForOfStatement */: + bindForInOrForOfStatement(node); + break; + case 196 /* IfStatement */: + bindIfStatement(node); + break; + case 204 /* ReturnStatement */: + case 208 /* ThrowStatement */: + bindReturnOrThrow(node); + break; + case 203 /* BreakStatement */: + case 202 /* ContinueStatement */: + bindBreakOrContinueStatement(node); + break; + case 209 /* TryStatement */: + bindTryStatement(node); + break; + case 206 /* SwitchStatement */: + bindSwitchStatement(node); + break; + case 220 /* CaseBlock */: + bindCaseBlock(node); + break; + case 207 /* LabeledStatement */: + bindLabeledStatement(node); + break; + default: + ts.forEachChild(node, bind); + break; + } + } + function bindWhileStatement(n) { + var preWhileState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + var postWhileState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + // bind expressions (don't affect reachability) + bind(n.expression); + currentReachabilityState = preWhileState; + var postWhileLabel = pushImplicitLabel(); + bind(n.statement); + popImplicitLabel(postWhileLabel, postWhileState); + } + function bindDoStatement(n) { + var preDoState = currentReachabilityState; + var postDoLabel = pushImplicitLabel(); + bind(n.statement); + var postDoState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : preDoState; + popImplicitLabel(postDoLabel, postDoState); + // bind expressions (don't affect reachability) + bind(n.expression); + } + function bindForStatement(n) { + var preForState = currentReachabilityState; + var postForLabel = pushImplicitLabel(); + // bind expressions (don't affect reachability) + bind(n.initializer); + bind(n.condition); + bind(n.incrementor); + bind(n.statement); + // for statement is considered infinite when it condition is either omitted or is true keyword + // - for(..;;..) + // - for(..;true;..) + var isInfiniteLoop = (!n.condition || n.condition.kind === 99 /* TrueKeyword */); + var postForState = isInfiniteLoop ? 4 /* Unreachable */ : preForState; + popImplicitLabel(postForLabel, postForState); + } + function bindForInOrForOfStatement(n) { + var preStatementState = currentReachabilityState; + var postStatementLabel = pushImplicitLabel(); + // bind expressions (don't affect reachability) + bind(n.initializer); + bind(n.expression); + bind(n.statement); + popImplicitLabel(postStatementLabel, preStatementState); + } + function bindIfStatement(n) { + // denotes reachability state when entering 'thenStatement' part of the if statement: + // i.e. if condition is false then thenStatement is unreachable + var ifTrueState = n.expression.kind === 84 /* FalseKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + // denotes reachability state when entering 'elseStatement': + // i.e. if condition is true then elseStatement is unreachable + var ifFalseState = n.expression.kind === 99 /* TrueKeyword */ ? 4 /* Unreachable */ : currentReachabilityState; + currentReachabilityState = ifTrueState; + // bind expression (don't affect reachability) + bind(n.expression); + bind(n.thenStatement); + if (n.elseStatement) { + var preElseState = currentReachabilityState; + currentReachabilityState = ifFalseState; + bind(n.elseStatement); + currentReachabilityState = or(currentReachabilityState, preElseState); + } + else { + currentReachabilityState = or(currentReachabilityState, ifFalseState); + } + } + function bindReturnOrThrow(n) { + // bind expression (don't affect reachability) + bind(n.expression); + if (n.kind === 204 /* ReturnStatement */) { + hasExplicitReturn = true; + } + currentReachabilityState = 4 /* Unreachable */; + } + function bindBreakOrContinueStatement(n) { + // call bind on label (don't affect reachability) + bind(n.label); + // for continue case touch label so it will be marked a used + var isValidJump = jumpToLabel(n.label, n.kind === 203 /* BreakStatement */ ? currentReachabilityState : 4 /* Unreachable */); + if (isValidJump) { + currentReachabilityState = 4 /* Unreachable */; + } + } + function bindTryStatement(n) { + // catch\finally blocks has the same reachability as try block + var preTryState = currentReachabilityState; + bind(n.tryBlock); + var postTryState = currentReachabilityState; + currentReachabilityState = preTryState; + bind(n.catchClause); + var postCatchState = currentReachabilityState; + currentReachabilityState = preTryState; + bind(n.finallyBlock); + // post catch/finally state is reachable if + // - post try state is reachable - control flow can fall out of try block + // - post catch state is reachable - control flow can fall out of catch block + currentReachabilityState = or(postTryState, postCatchState); + } + function bindSwitchStatement(n) { + var preSwitchState = currentReachabilityState; + var postSwitchLabel = pushImplicitLabel(); + // bind expression (don't affect reachability) + bind(n.expression); + bind(n.caseBlock); + var hasDefault = ts.forEach(n.caseBlock.clauses, function (c) { return c.kind === 242 /* DefaultClause */; }); + // post switch state is unreachable if switch is exaustive (has a default case ) and does not have fallthrough from the last case + var postSwitchState = hasDefault && currentReachabilityState !== 2 /* Reachable */ ? 4 /* Unreachable */ : preSwitchState; + popImplicitLabel(postSwitchLabel, postSwitchState); + } + function bindCaseBlock(n) { + var startState = currentReachabilityState; + for (var _i = 0, _a = n.clauses; _i < _a.length; _i++) { + var clause = _a[_i]; + currentReachabilityState = startState; + bind(clause); + if (clause.statements.length && currentReachabilityState === 2 /* Reachable */ && options.noFallthroughCasesInSwitch) { + errorOnFirstToken(clause, ts.Diagnostics.Fallthrough_case_in_switch); + } + } + } + function bindLabeledStatement(n) { + // call bind on label (don't affect reachability) + bind(n.label); + var ok = pushNamedLabel(n.label); + bind(n.statement); + if (ok) { + popNamedLabel(n.label, currentReachabilityState); + } + } + function getContainerFlags(node) { + switch (node.kind) { + case 186 /* ClassExpression */: + case 214 /* ClassDeclaration */: + case 215 /* InterfaceDeclaration */: + case 217 /* EnumDeclaration */: + case 155 /* TypeLiteral */: + case 165 /* ObjectLiteralExpression */: + return 1 /* IsContainer */; + case 147 /* CallSignature */: + case 148 /* ConstructSignature */: + case 149 /* IndexSignature */: + case 143 /* MethodDeclaration */: + case 142 /* MethodSignature */: + case 213 /* FunctionDeclaration */: + case 144 /* Constructor */: + case 145 /* GetAccessor */: + case 146 /* SetAccessor */: + case 152 /* FunctionType */: + case 153 /* ConstructorType */: + case 173 /* FunctionExpression */: + case 174 /* ArrowFunction */: + case 218 /* ModuleDeclaration */: + case 248 /* SourceFile */: + case 216 /* TypeAliasDeclaration */: + return 5 /* IsContainerWithLocals */; + case 244 /* CatchClause */: + case 199 /* ForStatement */: + case 200 /* ForInStatement */: + case 201 /* ForOfStatement */: + case 220 /* CaseBlock */: + return 2 /* IsBlockScopedContainer */; + case 192 /* Block */: + // do not treat blocks directly inside a function as a block-scoped-container. + // Locals that reside in this block should go to the function locals. Othewise 'x' + // would not appear to be a redeclaration of a block scoped local in the following + // example: + // + // function foo() { + // var x; + // let x; + // } + // + // If we placed 'var x' into the function locals and 'let x' into the locals of + // the block, then there would be no collision. + // + // By not creating a new block-scoped-container here, we ensure that both 'var x' + // and 'let x' go into the Function-container's locals, and we do get a collision + // conflict. + return ts.isFunctionLike(node.parent) ? 0 /* None */ : 2 /* IsBlockScopedContainer */; + } + return 0 /* None */; + } + function addToContainerChain(next) { + if (lastContainer) { + lastContainer.nextContainer = next; + } + lastContainer = next; + } + function declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes) { + // Just call this directly so that the return type of this function stays "void". + declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes); + } + function declareSymbolAndAddToSymbolTableWorker(node, symbolFlags, symbolExcludes) { + switch (container.kind) { + // Modules, source files, and classes need specialized handling for how their + // members are declared (for example, a member of a class will go into a specific + // symbol table depending on if it is static or not). We defer to specialized + // handlers to take care of declaring these child members. + case 218 /* ModuleDeclaration */: + return declareModuleMember(node, symbolFlags, symbolExcludes); + case 248 /* SourceFile */: + return declareSourceFileMember(node, symbolFlags, symbolExcludes); + case 186 /* ClassExpression */: + case 214 /* ClassDeclaration */: + return declareClassMember(node, symbolFlags, symbolExcludes); + case 217 /* EnumDeclaration */: + return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes); + case 155 /* TypeLiteral */: + case 165 /* ObjectLiteralExpression */: + case 215 /* InterfaceDeclaration */: + // Interface/Object-types always have their children added to the 'members' of + // their container. They are only accessible through an instance of their + // container, and are never in scope otherwise (even inside the body of the + // object / type / interface declaring them). An exception is type parameters, + // which are in scope without qualification (similar to 'locals'). + return declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); + case 152 /* FunctionType */: + case 153 /* ConstructorType */: + case 147 /* CallSignature */: + case 148 /* ConstructSignature */: + case 149 /* IndexSignature */: + case 143 /* MethodDeclaration */: + case 142 /* MethodSignature */: + case 144 /* Constructor */: + case 145 /* GetAccessor */: + case 146 /* SetAccessor */: + case 213 /* FunctionDeclaration */: + case 173 /* FunctionExpression */: + case 174 /* ArrowFunction */: + case 216 /* TypeAliasDeclaration */: + // All the children of these container types are never visible through another + // symbol (i.e. through another symbol's 'exports' or 'members'). Instead, + // they're only accessed 'lexically' (i.e. from code that exists underneath + // their container in the tree. To accomplish this, we simply add their declared + // symbol to the 'locals' of the container. These symbols can then be found as + // the type checker walks up the containers, checking them for matching names. + return declareSymbol(container.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + function declareClassMember(node, symbolFlags, symbolExcludes) { + return node.flags & 64 /* Static */ + ? declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes) + : declareSymbol(container.symbol.members, container.symbol, node, symbolFlags, symbolExcludes); + } + function declareSourceFileMember(node, symbolFlags, symbolExcludes) { + return ts.isExternalModule(file) + ? declareModuleMember(node, symbolFlags, symbolExcludes) + : declareSymbol(file.locals, undefined, node, symbolFlags, symbolExcludes); + } + function hasExportDeclarations(node) { + var body = node.kind === 248 /* SourceFile */ ? node : node.body; + if (body.kind === 248 /* SourceFile */ || body.kind === 219 /* ModuleBlock */) { + for (var _i = 0, _a = body.statements; _i < _a.length; _i++) { + var stat = _a[_i]; + if (stat.kind === 228 /* ExportDeclaration */ || stat.kind === 227 /* ExportAssignment */) { + return true; + } + } + } + return false; + } + function setExportContextFlag(node) { + // A declaration source file or ambient module declaration that contains no export declarations (but possibly regular + // declarations with export modifiers) is an export context in which declarations are implicitly exported. + if (ts.isInAmbientContext(node) && !hasExportDeclarations(node)) { + node.flags |= 131072 /* ExportContext */; + } + else { + node.flags &= ~131072 /* ExportContext */; + } + } + function bindModuleDeclaration(node) { + setExportContextFlag(node); + if (node.name.kind === 9 /* StringLiteral */) { + declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); + } + else { + var state = getModuleInstanceState(node); + if (state === 0 /* NonInstantiated */) { + declareSymbolAndAddToSymbolTable(node, 1024 /* NamespaceModule */, 0 /* NamespaceModuleExcludes */); + } + else { + declareSymbolAndAddToSymbolTable(node, 512 /* ValueModule */, 106639 /* ValueModuleExcludes */); + if (node.symbol.flags & (16 /* Function */ | 32 /* Class */ | 256 /* RegularEnum */)) { + // if module was already merged with some function, class or non-const enum + // treat is a non-const-enum-only + node.symbol.constEnumOnlyModule = false; + } + else { + var currentModuleIsConstEnumOnly = state === 2 /* ConstEnumOnly */; + if (node.symbol.constEnumOnlyModule === undefined) { + // non-merged case - use the current state + node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly; + } + else { + // merged case: module is const enum only if all its pieces are non-instantiated or const enum + node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly; + } + } + } + } + } + function bindFunctionOrConstructorType(node) { + // For a given function symbol "<...>(...) => T" we want to generate a symbol identical + // to the one we would get for: { <...>(...): T } + // + // We do that by making an anonymous type literal symbol, and then setting the function + // symbol as its sole member. To the rest of the system, this symbol will be indistinguishable + // from an actual type literal symbol you would have gotten had you used the long form. + var symbol = createSymbol(131072 /* Signature */, getDeclarationName(node)); + addDeclarationToSymbol(symbol, node, 131072 /* Signature */); + var typeLiteralSymbol = createSymbol(2048 /* TypeLiteral */, "__type"); + addDeclarationToSymbol(typeLiteralSymbol, node, 2048 /* TypeLiteral */); + typeLiteralSymbol.members = (_a = {}, _a[symbol.name] = symbol, _a); + var _a; + } + function bindObjectLiteralExpression(node) { + var ElementKind; + (function (ElementKind) { + ElementKind[ElementKind["Property"] = 1] = "Property"; + ElementKind[ElementKind["Accessor"] = 2] = "Accessor"; + })(ElementKind || (ElementKind = {})); + if (inStrictMode) { + var seen = {}; + for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { + var prop = _a[_i]; + if (prop.name.kind !== 69 /* Identifier */) { + continue; + } + var identifier = prop.name; + // ECMA-262 11.1.5 Object Initialiser + // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true + // a.This production is contained in strict code and IsDataDescriptor(previous) is true and + // IsDataDescriptor(propId.descriptor) is true. + // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. + // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. + // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true + // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields + var currentKind = prop.kind === 245 /* PropertyAssignment */ || prop.kind === 246 /* ShorthandPropertyAssignment */ || prop.kind === 143 /* MethodDeclaration */ + ? 1 /* Property */ + : 2 /* Accessor */; + var existingKind = seen[identifier.text]; + if (!existingKind) { + seen[identifier.text] = currentKind; + continue; + } + if (currentKind === 1 /* Property */ && existingKind === 1 /* Property */) { + var span = ts.getErrorSpanForNode(file, identifier); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode)); + } + } + } + return bindAnonymousDeclaration(node, 4096 /* ObjectLiteral */, "__object"); + } + function bindAnonymousDeclaration(node, symbolFlags, name) { + var symbol = createSymbol(symbolFlags, name); + addDeclarationToSymbol(symbol, node, symbolFlags); + } + function bindBlockScopedDeclaration(node, symbolFlags, symbolExcludes) { + switch (blockScopeContainer.kind) { + case 218 /* ModuleDeclaration */: + declareModuleMember(node, symbolFlags, symbolExcludes); + break; + case 248 /* SourceFile */: + if (ts.isExternalModule(container)) { + declareModuleMember(node, symbolFlags, symbolExcludes); + break; + } + // fall through. + default: + if (!blockScopeContainer.locals) { + blockScopeContainer.locals = {}; + addToContainerChain(blockScopeContainer); + } + declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes); + } + } + function bindBlockScopedVariableDeclaration(node) { + bindBlockScopedDeclaration(node, 2 /* BlockScopedVariable */, 107455 /* BlockScopedVariableExcludes */); + } + // The binder visits every node in the syntax tree so it is a convenient place to perform a single localized + // check for reserved words used as identifiers in strict mode code. + function checkStrictModeIdentifier(node) { + if (inStrictMode && + node.originalKeywordKind >= 106 /* FirstFutureReservedWord */ && + node.originalKeywordKind <= 114 /* LastFutureReservedWord */ && + !ts.isIdentifierName(node)) { + // Report error only if there are no parse errors in file + if (!file.parseDiagnostics.length) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(node, getStrictModeIdentifierMessage(node), ts.declarationNameToString(node))); + } + } + } + function getStrictModeIdentifierMessage(node) { + // Provide specialized messages to help the user understand why we think they're in + // strict mode. + if (ts.getContainingClass(node)) { + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Modules_are_automatically_in_strict_mode; + } + return ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode; + } + function checkStrictModeBinaryExpression(node) { + if (inStrictMode && ts.isLeftHandSideExpression(node.left) && ts.isAssignmentOperator(node.operatorToken.kind)) { + // ECMA 262 (Annex C) The identifier eval or arguments may not appear as the LeftHandSideExpression of an + // Assignment operator(11.13) or of a PostfixExpression(11.3) + checkStrictModeEvalOrArguments(node, node.left); + } + } + function checkStrictModeCatchClause(node) { + // It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the + // Catch production is eval or arguments + if (inStrictMode && node.variableDeclaration) { + checkStrictModeEvalOrArguments(node, node.variableDeclaration.name); + } + } + function checkStrictModeDeleteExpression(node) { + // Grammar checking + if (inStrictMode && node.expression.kind === 69 /* Identifier */) { + // When a delete operator occurs within strict mode code, a SyntaxError is thrown if its + // UnaryExpression is a direct reference to a variable, function argument, or function name + var span = ts.getErrorSpanForNode(file, node.expression); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, ts.Diagnostics.delete_cannot_be_called_on_an_identifier_in_strict_mode)); + } + } + function isEvalOrArgumentsIdentifier(node) { + return node.kind === 69 /* Identifier */ && + (node.text === "eval" || node.text === "arguments"); + } + function checkStrictModeEvalOrArguments(contextNode, name) { + if (name && name.kind === 69 /* Identifier */) { + var identifier = name; + if (isEvalOrArgumentsIdentifier(identifier)) { + // We check first if the name is inside class declaration or class expression; if so give explicit message + // otherwise report generic error message. + var span = ts.getErrorSpanForNode(file, name); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, getStrictModeEvalOrArgumentsMessage(contextNode), identifier.text)); + } + } + } + function getStrictModeEvalOrArgumentsMessage(node) { + // Provide specialized messages to help the user understand why we think they're in + // strict mode. + if (ts.getContainingClass(node)) { + return ts.Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode; + } + if (file.externalModuleIndicator) { + return ts.Diagnostics.Invalid_use_of_0_Modules_are_automatically_in_strict_mode; + } + return ts.Diagnostics.Invalid_use_of_0_in_strict_mode; + } + function checkStrictModeFunctionName(node) { + if (inStrictMode) { + // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression (13.1)) + checkStrictModeEvalOrArguments(node, node.name); + } + } + function checkStrictModeNumericLiteral(node) { + if (inStrictMode && node.flags & 32768 /* OctalLiteral */) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Octal_literals_are_not_allowed_in_strict_mode)); + } + } + function checkStrictModePostfixUnaryExpression(node) { + // Grammar checking + // The identifier eval or arguments may not appear as the LeftHandSideExpression of an + // Assignment operator(11.13) or of a PostfixExpression(11.3) or as the UnaryExpression + // operated upon by a Prefix Increment(11.4.4) or a Prefix Decrement(11.4.5) operator. + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.operand); + } + } + function checkStrictModePrefixUnaryExpression(node) { + // Grammar checking + if (inStrictMode) { + if (node.operator === 41 /* PlusPlusToken */ || node.operator === 42 /* MinusMinusToken */) { + checkStrictModeEvalOrArguments(node, node.operand); + } + } + } + function checkStrictModeWithStatement(node) { + // Grammar checking for withStatement + if (inStrictMode) { + errorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_strict_mode); + } + } + function errorOnFirstToken(node, message, arg0, arg1, arg2) { + var span = ts.getSpanOfTokenAtPosition(file, node.pos); + file.bindDiagnostics.push(ts.createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2)); + } + function getDestructuringParameterName(node) { + return "__" + ts.indexOf(node.parent.parameters, node); + } + function bind(node) { + if (!node) { + return; + } + node.parent = parent; + var savedInStrictMode = inStrictMode; + if (!savedInStrictMode) { + updateStrictMode(node); + } + // First we bind declaration nodes to a symbol if possible. We'll both create a symbol + // and then potentially add the symbol to an appropriate symbol table. Possible + // destination symbol tables are: + // + // 1) The 'exports' table of the current container's symbol. + // 2) The 'members' table of the current container's symbol. + // 3) The 'locals' table of the current container. + // + // However, not all symbols will end up in any of these tables. 'Anonymous' symbols + // (like TypeLiterals for example) will not be put in any table. + bindWorker(node); + // Then we recurse into the children of the node to bind them as well. For certain + // symbols we do specialized work when we recurse. For example, we'll keep track of + // the current 'container' node when it changes. This helps us know which symbol table + // a local should go into for example. + bindChildren(node); + inStrictMode = savedInStrictMode; + } + function updateStrictMode(node) { + switch (node.kind) { + case 248 /* SourceFile */: + case 219 /* ModuleBlock */: + updateStrictModeStatementList(node.statements); + return; + case 192 /* Block */: + if (ts.isFunctionLike(node.parent)) { + updateStrictModeStatementList(node.statements); + } + return; + case 214 /* ClassDeclaration */: + case 186 /* ClassExpression */: + // All classes are automatically in strict mode in ES6. + inStrictMode = true; + return; + } + } + function updateStrictModeStatementList(statements) { + for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) { + var statement = statements_1[_i]; + if (!ts.isPrologueDirective(statement)) { + return; + } + if (isUseStrictPrologueDirective(statement)) { + inStrictMode = true; + return; + } + } + } + /// Should be called only on prologue directives (isPrologueDirective(node) should be true) + function isUseStrictPrologueDirective(node) { + var nodeText = ts.getTextOfNodeFromSourceText(file.text, node.expression); + // Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the + // string to contain unicode escapes (as per ES5). + return nodeText === "\"use strict\"" || nodeText === "'use strict'"; + } + function bindWorker(node) { + switch (node.kind) { + /* Strict mode checks */ + case 69 /* Identifier */: + return checkStrictModeIdentifier(node); + case 181 /* BinaryExpression */: + if (ts.isInJavaScriptFile(node)) { + if (ts.isExportsPropertyAssignment(node)) { + bindExportsPropertyAssignment(node); + } + else if (ts.isModuleExportsAssignment(node)) { + bindModuleExportsAssignment(node); + } + } + return checkStrictModeBinaryExpression(node); + case 244 /* CatchClause */: + return checkStrictModeCatchClause(node); + case 175 /* DeleteExpression */: + return checkStrictModeDeleteExpression(node); + case 8 /* NumericLiteral */: + return checkStrictModeNumericLiteral(node); + case 180 /* PostfixUnaryExpression */: + return checkStrictModePostfixUnaryExpression(node); + case 179 /* PrefixUnaryExpression */: + return checkStrictModePrefixUnaryExpression(node); + case 205 /* WithStatement */: + return checkStrictModeWithStatement(node); + case 97 /* ThisKeyword */: + seenThisKeyword = true; + return; + case 137 /* TypeParameter */: + return declareSymbolAndAddToSymbolTable(node, 262144 /* TypeParameter */, 530912 /* TypeParameterExcludes */); + case 138 /* Parameter */: + return bindParameter(node); + case 211 /* VariableDeclaration */: + case 163 /* BindingElement */: + return bindVariableDeclarationOrBindingElement(node); + case 141 /* PropertyDeclaration */: + case 140 /* PropertySignature */: + return bindPropertyOrMethodOrAccessor(node, 4 /* Property */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), 107455 /* PropertyExcludes */); + case 245 /* PropertyAssignment */: + case 246 /* ShorthandPropertyAssignment */: + return bindPropertyOrMethodOrAccessor(node, 4 /* Property */, 107455 /* PropertyExcludes */); + case 247 /* EnumMember */: + return bindPropertyOrMethodOrAccessor(node, 8 /* EnumMember */, 107455 /* EnumMemberExcludes */); + case 147 /* CallSignature */: + case 148 /* ConstructSignature */: + case 149 /* IndexSignature */: + return declareSymbolAndAddToSymbolTable(node, 131072 /* Signature */, 0 /* None */); + case 143 /* MethodDeclaration */: + case 142 /* MethodSignature */: + // If this is an ObjectLiteralExpression method, then it sits in the same space + // as other properties in the object literal. So we use SymbolFlags.PropertyExcludes + // so that it will conflict with any other object literal members with the same + // name. + return bindPropertyOrMethodOrAccessor(node, 8192 /* Method */ | (node.questionToken ? 536870912 /* Optional */ : 0 /* None */), ts.isObjectLiteralMethod(node) ? 107455 /* PropertyExcludes */ : 99263 /* MethodExcludes */); + case 213 /* FunctionDeclaration */: + checkStrictModeFunctionName(node); + return declareSymbolAndAddToSymbolTable(node, 16 /* Function */, 106927 /* FunctionExcludes */); + case 144 /* Constructor */: + return declareSymbolAndAddToSymbolTable(node, 16384 /* Constructor */, /*symbolExcludes:*/ 0 /* None */); + case 145 /* GetAccessor */: + return bindPropertyOrMethodOrAccessor(node, 32768 /* GetAccessor */, 41919 /* GetAccessorExcludes */); + case 146 /* SetAccessor */: + return bindPropertyOrMethodOrAccessor(node, 65536 /* SetAccessor */, 74687 /* SetAccessorExcludes */); + case 152 /* FunctionType */: + case 153 /* ConstructorType */: + return bindFunctionOrConstructorType(node); + case 155 /* TypeLiteral */: + return bindAnonymousDeclaration(node, 2048 /* TypeLiteral */, "__type"); + case 165 /* ObjectLiteralExpression */: + return bindObjectLiteralExpression(node); + case 173 /* FunctionExpression */: + case 174 /* ArrowFunction */: + checkStrictModeFunctionName(node); + var bindingName = node.name ? node.name.text : "__function"; + return bindAnonymousDeclaration(node, 16 /* Function */, bindingName); + case 168 /* CallExpression */: + if (ts.isInJavaScriptFile(node)) { + bindCallExpression(node); + } + break; + // Members of classes, interfaces, and modules + case 186 /* ClassExpression */: + case 214 /* ClassDeclaration */: + return bindClassLikeDeclaration(node); + case 215 /* InterfaceDeclaration */: + return bindBlockScopedDeclaration(node, 64 /* Interface */, 792960 /* InterfaceExcludes */); + case 216 /* TypeAliasDeclaration */: + return bindBlockScopedDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */); + case 217 /* EnumDeclaration */: + return bindEnumDeclaration(node); + case 218 /* ModuleDeclaration */: + return bindModuleDeclaration(node); + // Imports and exports + case 221 /* ImportEqualsDeclaration */: + case 224 /* NamespaceImport */: + case 226 /* ImportSpecifier */: + case 230 /* ExportSpecifier */: + return declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); + case 223 /* ImportClause */: + return bindImportClause(node); + case 228 /* ExportDeclaration */: + return bindExportDeclaration(node); + case 227 /* ExportAssignment */: + return bindExportAssignment(node); + case 248 /* SourceFile */: + return bindSourceFileIfExternalModule(); + } + } + function bindSourceFileIfExternalModule() { + setExportContextFlag(file); + if (ts.isExternalModule(file)) { + bindSourceFileAsExternalModule(); + } + } + function bindSourceFileAsExternalModule() { + bindAnonymousDeclaration(file, 512 /* ValueModule */, "\"" + ts.removeFileExtension(file.fileName) + "\""); + } + function bindExportAssignment(node) { + var boundExpression = node.kind === 227 /* ExportAssignment */ ? node.expression : node.right; + if (!container.symbol || !container.symbol.exports) { + // Export assignment in some sort of block construct + bindAnonymousDeclaration(node, 8388608 /* Alias */, getDeclarationName(node)); + } + else if (boundExpression.kind === 69 /* Identifier */) { + // An export default clause with an identifier exports all meanings of that identifier + declareSymbol(container.symbol.exports, container.symbol, node, 8388608 /* Alias */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); + } + else { + // An export default clause with an expression exports a value + declareSymbol(container.symbol.exports, container.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); + } + } + function bindExportDeclaration(node) { + if (!container.symbol || !container.symbol.exports) { + // Export * in some sort of block construct + bindAnonymousDeclaration(node, 1073741824 /* ExportStar */, getDeclarationName(node)); + } + else if (!node.exportClause) { + // All export * declarations are collected in an __export symbol + declareSymbol(container.symbol.exports, container.symbol, node, 1073741824 /* ExportStar */, 0 /* None */); + } + } + function bindImportClause(node) { + if (node.name) { + declareSymbolAndAddToSymbolTable(node, 8388608 /* Alias */, 8388608 /* AliasExcludes */); + } + } + function setCommonJsModuleIndicator(node) { + if (!file.commonJsModuleIndicator) { + file.commonJsModuleIndicator = node; + bindSourceFileAsExternalModule(); + } + } + function bindExportsPropertyAssignment(node) { + // When we create a property via 'exports.foo = bar', the 'exports.foo' property access + // expression is the declaration + setCommonJsModuleIndicator(node); + declareSymbol(file.symbol.exports, file.symbol, node.left, 4 /* Property */ | 7340032 /* Export */, 0 /* None */); + } + function bindModuleExportsAssignment(node) { + // 'module.exports = expr' assignment + setCommonJsModuleIndicator(node); + bindExportAssignment(node); + } + function bindCallExpression(node) { + // We're only inspecting call expressions to detect CommonJS modules, so we can skip + // this check if we've already seen the module indicator + if (!file.commonJsModuleIndicator && ts.isRequireCall(node)) { + setCommonJsModuleIndicator(node); + } + } + function bindClassLikeDeclaration(node) { + if (node.kind === 214 /* ClassDeclaration */) { + bindBlockScopedDeclaration(node, 32 /* Class */, 899519 /* ClassExcludes */); + } + else { + var bindingName = node.name ? node.name.text : "__class"; + bindAnonymousDeclaration(node, 32 /* Class */, bindingName); + // Add name of class expression into the map for semantic classifier + if (node.name) { + classifiableNames[node.name.text] = node.name.text; + } + } + var symbol = node.symbol; + // TypeScript 1.0 spec (April 2014): 8.4 + // Every class automatically contains a static property member named 'prototype', the + // type of which is an instantiation of the class type with type Any supplied as a type + // argument for each type parameter. It is an error to explicitly declare a static + // property member with the name 'prototype'. + // + // Note: we check for this here because this class may be merging into a module. The + // module might have an exported variable called 'prototype'. We can't allow that as + // that would clash with the built-in 'prototype' for the class. + var prototypeSymbol = createSymbol(4 /* Property */ | 134217728 /* Prototype */, "prototype"); + if (ts.hasProperty(symbol.exports, prototypeSymbol.name)) { + if (node.name) { + node.name.parent = node; + } + file.bindDiagnostics.push(ts.createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0], ts.Diagnostics.Duplicate_identifier_0, prototypeSymbol.name)); + } + symbol.exports[prototypeSymbol.name] = prototypeSymbol; + prototypeSymbol.parent = symbol; + } + function bindEnumDeclaration(node) { + return ts.isConst(node) + ? bindBlockScopedDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */) + : bindBlockScopedDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */); + } + function bindVariableDeclarationOrBindingElement(node) { + if (inStrictMode) { + checkStrictModeEvalOrArguments(node, node.name); + } + if (!ts.isBindingPattern(node.name)) { + if (ts.isBlockOrCatchScoped(node)) { + bindBlockScopedVariableDeclaration(node); + } + else if (ts.isParameterDeclaration(node)) { + // It is safe to walk up parent chain to find whether the node is a destructing parameter declaration + // because its parent chain has already been set up, since parents are set before descending into children. + // + // If node is a binding element in parameter declaration, we need to use ParameterExcludes. + // Using ParameterExcludes flag allows the compiler to report an error on duplicate identifiers in Parameter Declaration + // For example: + // function foo([a,a]) {} // Duplicate Identifier error + // function bar(a,a) {} // Duplicate Identifier error, parameter declaration in this case is handled in bindParameter + // // which correctly set excluded symbols + declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); + } + else { + declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107454 /* FunctionScopedVariableExcludes */); + } + } + } + function bindParameter(node) { + if (inStrictMode) { + // It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a + // strict mode FunctionLikeDeclaration or FunctionExpression(13.1) + checkStrictModeEvalOrArguments(node, node.name); + } + if (ts.isBindingPattern(node.name)) { + bindAnonymousDeclaration(node, 1 /* FunctionScopedVariable */, getDestructuringParameterName(node)); + } + else { + declareSymbolAndAddToSymbolTable(node, 1 /* FunctionScopedVariable */, 107455 /* ParameterExcludes */); + } + // If this is a property-parameter, then also declare the property symbol into the + // containing class. + if (node.flags & 56 /* AccessibilityModifier */ && + node.parent.kind === 144 /* Constructor */ && + ts.isClassLike(node.parent.parent)) { + var classDeclaration = node.parent.parent; + declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, 4 /* Property */, 107455 /* PropertyExcludes */); + } + } + function bindPropertyOrMethodOrAccessor(node, symbolFlags, symbolExcludes) { + return ts.hasDynamicName(node) + ? bindAnonymousDeclaration(node, symbolFlags, "__computed") + : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); + } + // reachability checks + function pushNamedLabel(name) { + initializeReachabilityStateIfNecessary(); + if (ts.hasProperty(labelIndexMap, name.text)) { + return false; + } + labelIndexMap[name.text] = labelStack.push(1 /* Unintialized */) - 1; + return true; + } + function pushImplicitLabel() { + initializeReachabilityStateIfNecessary(); + var index = labelStack.push(1 /* Unintialized */) - 1; + implicitLabels.push(index); + return index; + } + function popNamedLabel(label, outerState) { + var index = labelIndexMap[label.text]; + ts.Debug.assert(index !== undefined); + ts.Debug.assert(labelStack.length == index + 1); + labelIndexMap[label.text] = undefined; + setCurrentStateAtLabel(labelStack.pop(), outerState, label); + } + function popImplicitLabel(implicitLabelIndex, outerState) { + if (labelStack.length !== implicitLabelIndex + 1) { + ts.Debug.assert(false, "Label stack: " + labelStack.length + ", index:" + implicitLabelIndex); + } + var i = implicitLabels.pop(); + if (implicitLabelIndex !== i) { + ts.Debug.assert(false, "i: " + i + ", index: " + implicitLabelIndex); + } + setCurrentStateAtLabel(labelStack.pop(), outerState, /*name*/ undefined); + } + function setCurrentStateAtLabel(innerMergedState, outerState, label) { + if (innerMergedState === 1 /* Unintialized */) { + if (label && !options.allowUnusedLabels) { + file.bindDiagnostics.push(ts.createDiagnosticForNode(label, ts.Diagnostics.Unused_label)); + } + currentReachabilityState = outerState; + } + else { + currentReachabilityState = or(innerMergedState, outerState); + } + } + function jumpToLabel(label, outerState) { + initializeReachabilityStateIfNecessary(); + var index = label ? labelIndexMap[label.text] : ts.lastOrUndefined(implicitLabels); + if (index === undefined) { + // reference to unknown label or + // break/continue used outside of loops + return false; + } + var stateAtLabel = labelStack[index]; + labelStack[index] = stateAtLabel === 1 /* Unintialized */ ? outerState : or(stateAtLabel, outerState); + return true; + } + function checkUnreachable(node) { + switch (currentReachabilityState) { + case 4 /* Unreachable */: + var reportError = + // report error on all statements except empty ones + (ts.isStatement(node) && node.kind !== 194 /* EmptyStatement */) || + // report error on class declarations + node.kind === 214 /* ClassDeclaration */ || + // report error on instantiated modules or const-enums only modules if preserveConstEnums is set + (node.kind === 218 /* ModuleDeclaration */ && shouldReportErrorOnModuleDeclaration(node)) || + // report error on regular enums and const enums if preserveConstEnums is set + (node.kind === 217 /* EnumDeclaration */ && (!ts.isConstEnumDeclaration(node) || options.preserveConstEnums)); + if (reportError) { + currentReachabilityState = 8 /* ReportedUnreachable */; + // unreachable code is reported if + // - user has explicitly asked about it AND + // - statement is in not ambient context (statements in ambient context is already an error + // so we should not report extras) AND + // - node is not variable statement OR + // - node is block scoped variable statement OR + // - node is not block scoped variable statement and at least one variable declaration has initializer + // Rationale: we don't want to report errors on non-initialized var's since they are hoisted + // On the other side we do want to report errors on non-initialized 'lets' because of TDZ + var reportUnreachableCode = !options.allowUnreachableCode && + !ts.isInAmbientContext(node) && + (node.kind !== 193 /* VariableStatement */ || + ts.getCombinedNodeFlags(node.declarationList) & 24576 /* BlockScoped */ || + ts.forEach(node.declarationList.declarations, function (d) { return d.initializer; })); + if (reportUnreachableCode) { + errorOnFirstToken(node, ts.Diagnostics.Unreachable_code_detected); + } + } + case 8 /* ReportedUnreachable */: + return true; + default: + return false; + } + function shouldReportErrorOnModuleDeclaration(node) { + var instanceState = getModuleInstanceState(node); + return instanceState === 1 /* Instantiated */ || (instanceState === 2 /* ConstEnumOnly */ && options.preserveConstEnums); + } + } + function initializeReachabilityStateIfNecessary() { + if (labelIndexMap) { + return; + } + currentReachabilityState = 2 /* Reachable */; + labelIndexMap = {}; + labelStack = []; + implicitLabels = []; + } + } +})(ts || (ts = {})); /// /* @internal */ var ts; @@ -13755,7 +13918,7 @@ var ts; symbolToString: symbolToString, getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, getRootSymbols: getRootSymbols, - getContextualType: getContextualType, + getContextualType: getApparentTypeOfContextualType, getFullyQualifiedName: getFullyQualifiedName, getResolvedSignature: getResolvedSignature, getConstantValue: getConstantValue, @@ -14025,7 +14188,7 @@ var ts; return ts.getAncestor(node, 248 /* SourceFile */); } function isGlobalSourceFile(node) { - return node.kind === 248 /* SourceFile */ && !ts.isExternalModule(node); + return node.kind === 248 /* SourceFile */ && !ts.isExternalOrCommonJsModule(node); } function getSymbol(symbols, name, meaning) { if (meaning && ts.hasProperty(symbols, name)) { @@ -14127,15 +14290,24 @@ var ts; } switch (location.kind) { case 248 /* SourceFile */: - if (!ts.isExternalModule(location)) + if (!ts.isExternalOrCommonJsModule(location)) break; case 218 /* ModuleDeclaration */: var moduleExports = getSymbolOfNode(location).exports; if (location.kind === 248 /* SourceFile */ || (location.kind === 218 /* ModuleDeclaration */ && location.name.kind === 9 /* StringLiteral */)) { - // It's an external module. Because of module/namespace merging, a module's exports are in scope, - // yet we never want to treat an export specifier as putting a member in scope. Therefore, - // if the name we find is purely an export specifier, it is not actually considered in scope. + // It's an external module. First see if the module has an export default and if the local + // name of that export default matches. + if (result = moduleExports["default"]) { + var localSymbol = ts.getLocalSymbolForExportDefault(result); + if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { + break loop; + } + result = undefined; + } + // Because of module/namespace merging, a module's exports are in scope, + // yet we never want to treat an export specifier as putting a member in scope. + // Therefore, if the name we find is purely an export specifier, it is not actually considered in scope. // Two things to note about this: // 1. We have to check this without calling getSymbol. The problem with calling getSymbol // on an export specifier is that it might find the export specifier itself, and try to @@ -14149,12 +14321,6 @@ var ts; ts.getDeclarationOfKind(moduleExports[name], 230 /* ExportSpecifier */)) { break; } - result = moduleExports["default"]; - var localSymbol = ts.getLocalSymbolForExportDefault(result); - if (result && localSymbol && (result.flags & meaning) && localSymbol.name === name) { - break loop; - } - result = undefined; } if (result = getSymbol(moduleExports, name, meaning & 8914931 /* ModuleMember */)) { break loop; @@ -14297,7 +14463,7 @@ var ts; // declare module foo { // interface bar {} // } - // let foo/*1*/: foo/*2*/.bar; + // const foo/*1*/: foo/*2*/.bar; // The foo at /*1*/ and /*2*/ will share same symbol with two meaning // block - scope variable and namespace module. However, only when we // try to resolve name in /*1*/ which is used in variable position, @@ -14601,6 +14767,9 @@ var ts; if (moduleName === undefined) { return; } + if (moduleName.indexOf("!") >= 0) { + moduleName = moduleName.substr(0, moduleName.indexOf("!")); + } var isRelative = ts.isExternalModuleNameRelative(moduleName); if (!isRelative) { var symbol = getSymbol(globals, "\"" + moduleName + "\"", 512 /* ValueModule */); @@ -14788,7 +14957,7 @@ var ts; } switch (location_1.kind) { case 248 /* SourceFile */: - if (!ts.isExternalModule(location_1)) { + if (!ts.isExternalOrCommonJsModule(location_1)) { break; } case 218 /* ModuleDeclaration */: @@ -14910,7 +15079,7 @@ var ts; // export class c { // } // } - // let x: typeof m.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 @@ -14949,7 +15118,7 @@ var ts; } function hasExternalModuleSymbol(declaration) { return (declaration.kind === 218 /* ModuleDeclaration */ && declaration.name.kind === 9 /* StringLiteral */) || - (declaration.kind === 248 /* SourceFile */ && ts.isExternalModule(declaration)); + (declaration.kind === 248 /* SourceFile */ && ts.isExternalOrCommonJsModule(declaration)); } function hasVisibleDeclarations(symbol) { var aliasesToMakeVisible; @@ -15100,7 +15269,7 @@ var ts; parentSymbol = symbol; appendSymbolNameOnly(symbol, writer); } - // Let the writer know we just wrote out a symbol. The declaration emitter writer uses + // 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. // @@ -15181,7 +15350,7 @@ var ts; writeAnonymousType(type, flags); } else if (type.flags & 256 /* StringLiteral */) { - writer.writeStringLiteral(type.text); + writer.writeStringLiteral("\"" + ts.escapeString(type.text) + "\""); } else { // Should never get here @@ -15568,7 +15737,7 @@ var ts; } } else if (node.kind === 248 /* SourceFile */) { - return ts.isExternalModule(node) ? node : undefined; + return ts.isExternalOrCommonJsModule(node) ? node : undefined; } } ts.Debug.fail("getContainingModule cant reach here"); @@ -15650,7 +15819,7 @@ var ts; // Private/protected properties/methods are not visible return false; } - // Public properties/methods are visible if its parents are visible, so let it fall into next case statement + // Public properties/methods are visible if its parents are visible, so const it fall into next case statement case 144 /* Constructor */: case 148 /* ConstructSignature */: case 147 /* CallSignature */: @@ -15678,7 +15847,7 @@ var ts; // Source file is always visible case 248 /* SourceFile */: return true; - // Export assignements do not create name bindings outside the module + // Export assignments do not create name bindings outside the module case 227 /* ExportAssignment */: return false; default: @@ -15814,6 +15983,23 @@ var ts; var symbol = getSymbolOfNode(node); return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node); } + function getTextOfPropertyName(name) { + switch (name.kind) { + case 69 /* Identifier */: + return name.text; + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + return name.text; + case 136 /* ComputedPropertyName */: + if (ts.isStringOrNumericLiteral(name.expression.kind)) { + return name.expression.text; + } + } + return undefined; + } + function isComputedNonLiteralName(name) { + return name.kind === 136 /* ComputedPropertyName */ && !ts.isStringOrNumericLiteral(name.expression.kind); + } // Return the inferred type for a binding element function getTypeForBindingElement(declaration) { var pattern = declaration.parent; @@ -15835,10 +16021,15 @@ var ts; if (pattern.kind === 161 /* ObjectBindingPattern */) { // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) var name_10 = declaration.propertyName || declaration.name; + if (isComputedNonLiteralName(name_10)) { + // computed properties with non-literal names are treated as 'any' + return anyType; + } // 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. - type = getTypeOfPropertyOfType(parentType, name_10.text) || - isNumericLiteralName(name_10.text) && getIndexTypeOfType(parentType, 1 /* Number */) || + var text = getTextOfPropertyName(name_10); + type = getTypeOfPropertyOfType(parentType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1 /* Number */) || getIndexTypeOfType(parentType, 0 /* String */); if (!type) { error(name_10, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_10)); @@ -15938,10 +16129,17 @@ var ts; // Return the type implied by an object binding pattern function getTypeFromObjectBindingPattern(pattern, includePatternInType) { var members = {}; + var hasComputedProperties = false; ts.forEach(pattern.elements, function (e) { - var flags = 4 /* Property */ | 67108864 /* Transient */ | (e.initializer ? 536870912 /* Optional */ : 0); var name = e.propertyName || e.name; - var symbol = createSymbol(flags, name.text); + 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); symbol.type = getTypeFromBindingElement(e, includePatternInType); symbol.bindingElement = e; members[symbol.name] = symbol; @@ -15950,6 +16148,9 @@ var ts; if (includePatternInType) { result.pattern = pattern; } + if (hasComputedProperties) { + result.flags |= 67108864 /* ObjectLiteralPatternWithComputedProperties */; + } return result; } // Return the type implied by an array binding pattern @@ -16026,6 +16227,14 @@ var ts; if (declaration.kind === 227 /* ExportAssignment */) { return links.type = checkExpression(declaration.expression); } + // Handle module.exports = expr + if (declaration.kind === 181 /* BinaryExpression */) { + return links.type = checkExpression(declaration.right); + } + // Handle exports.p = expr + if (declaration.kind === 166 /* PropertyAccessExpression */) { + return checkExpressionCached(declaration.parent.right); + } // Handle variable, parameter or property if (!pushTypeResolution(symbol, 0 /* Type */)) { return unknownType; @@ -16304,23 +16513,25 @@ var ts; } function resolveBaseTypesOfClass(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - var baseContructorType = getBaseConstructorTypeOfClass(type); - if (!(baseContructorType.flags & 80896 /* ObjectType */)) { + var baseConstructorType = getBaseConstructorTypeOfClass(type); + if (!(baseConstructorType.flags & 80896 /* ObjectType */)) { return; } var baseTypeNode = getBaseTypeNodeOfClass(type); var baseType; - if (baseContructorType.symbol && baseContructorType.symbol.flags & 32 /* Class */) { - // When base constructor type is a class we know that the constructors all have the same type parameters as the + var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; + if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 /* Class */ && + areAllOuterTypeParametersApplied(originalBaseType)) { + // When base constructor type is a class with no captured type arguments we know that the constructors all have the same type parameters as the // class and all return the instance type of the class. There is no need for further checks and we can apply the // type arguments in the same manner as a type reference to get the same error reporting experience. - baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseContructorType.symbol); + baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); } else { // The class derives from a "class-like" constructor function, check that we have at least one construct signature // with a matching number of type parameters and use the return type of the first instantiated signature. Elsewhere // we check that all instantiated signatures return the same type. - var constructors = getInstantiatedConstructorsForTypeArguments(baseContructorType, baseTypeNode.typeArguments); + var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); if (!constructors.length) { error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); return; @@ -16345,6 +16556,17 @@ var ts; type.resolvedBaseTypes.push(baseType); } } + function areAllOuterTypeParametersApplied(type) { + // An unapplied type parameter has its symbol still the same as the matching argument symbol. + // Since parameters are applied outer-to-inner, only the last outer parameter needs to be checked. + var outerTypeParameters = type.outerTypeParameters; + if (outerTypeParameters) { + var last = outerTypeParameters.length - 1; + var typeArguments = type.typeArguments; + return outerTypeParameters[last].symbol !== typeArguments[last].symbol; + } + return true; + } function resolveBaseTypesOfInterface(type) { type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { @@ -16424,7 +16646,7 @@ var ts; type.typeArguments = type.typeParameters; type.thisType = createType(512 /* TypeParameter */ | 33554432 /* ThisType */); type.thisType.symbol = symbol; - type.thisType.constraint = getTypeWithThisArgument(type); + type.thisType.constraint = type; } } return links.declaredType; @@ -16939,6 +17161,20 @@ var ts; type = getApparentType(type); return type.flags & 49152 /* UnionOrIntersection */ ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); } + /** + * The apparent type of a type parameter is the base constraint instantiated with the type parameter + * as the type argument for the 'this' type. + */ + function getApparentTypeOfTypeParameter(type) { + if (!type.resolvedApparentType) { + var constraintType = getConstraintOfTypeParameter(type); + while (constraintType && constraintType.flags & 512 /* TypeParameter */) { + constraintType = getConstraintOfTypeParameter(constraintType); + } + type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); + } + return type.resolvedApparentType; + } /** * For a type parameter, return the base constraint of the type parameter. For the string, number, * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the @@ -16946,12 +17182,7 @@ var ts; */ function getApparentType(type) { if (type.flags & 512 /* TypeParameter */) { - do { - type = getConstraintOfTypeParameter(type); - } while (type && type.flags & 512 /* TypeParameter */); - if (!type) { - type = emptyObjectType; - } + type = getApparentTypeOfTypeParameter(type); } if (type.flags & 258 /* StringLike */) { type = globalStringType; @@ -17116,7 +17347,7 @@ var ts; if (node.initializer) { var signatureDeclaration = node.parent; var signature = getSignatureFromDeclaration(signatureDeclaration); - var parameterIndex = signatureDeclaration.parameters.indexOf(node); + var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); ts.Debug.assert(parameterIndex >= 0); return parameterIndex >= signature.minArgumentCount; } @@ -17217,6 +17448,16 @@ var ts; } return result; } + function resolveExternalModuleTypeByLiteral(name) { + var moduleSym = resolveExternalModuleName(name, name); + if (moduleSym) { + var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); + if (resolvedModuleSymbol) { + return getTypeOfSymbol(resolvedModuleSymbol); + } + } + return anyType; + } function getReturnTypeOfSignature(signature) { if (!signature.resolvedReturnType) { if (!pushTypeResolution(signature, 3 /* ResolvedReturnType */)) { @@ -17740,11 +17981,12 @@ var ts; return links.resolvedType; } function getStringLiteralType(node) { - if (ts.hasProperty(stringLiteralTypes, node.text)) { - return stringLiteralTypes[node.text]; + var text = node.text; + if (ts.hasProperty(stringLiteralTypes, text)) { + return stringLiteralTypes[text]; } - var type = stringLiteralTypes[node.text] = createType(256 /* StringLiteral */); - type.text = ts.getTextOfNode(node); + var type = stringLiteralTypes[text] = createType(256 /* StringLiteral */); + type.text = text; return type; } function getTypeFromStringLiteral(node) { @@ -18265,7 +18507,7 @@ var ts; return false; } function hasExcessProperties(source, target, reportErrors) { - if (someConstituentTypeHasKind(target, 80896 /* ObjectType */)) { + if (!(target.flags & 67108864 /* ObjectLiteralPatternWithComputedProperties */) && someConstituentTypeHasKind(target, 80896 /* ObjectType */)) { for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { var prop = _a[_i]; if (!isKnownProperty(target, prop.name)) { @@ -18358,9 +18600,6 @@ var ts; return result; } function typeParameterIdenticalTo(source, target) { - if (source.symbol.name !== target.symbol.name) { - return 0 /* False */; - } // covers case when both type parameters does not have constraint (both equal to noConstraintType) if (source.constraint === target.constraint) { return -1 /* True */; @@ -18852,18 +19091,29 @@ var ts; } return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } + function isMatchingSignature(source, target, partialMatch) { + // A source signature matches a target signature if the two signatures have the same number of required, + // optional, and rest parameters. + if (source.parameters.length === target.parameters.length && + source.minArgumentCount === target.minArgumentCount && + source.hasRestParameter === target.hasRestParameter) { + return true; + } + // A source signature partially matches a target signature if the target signature has no fewer required + // parameters and no more overall parameters than the source signature (where a signature with a rest + // parameter is always considered to have more overall parameters than one without). + if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (source.hasRestParameter && !target.hasRestParameter || + source.hasRestParameter === target.hasRestParameter && source.parameters.length >= target.parameters.length)) { + return true; + } + return false; + } function compareSignatures(source, target, partialMatch, ignoreReturnTypes, compareTypes) { if (source === target) { return -1 /* True */; } - if (source.parameters.length !== target.parameters.length || - source.minArgumentCount !== target.minArgumentCount || - source.hasRestParameter !== target.hasRestParameter) { - if (!partialMatch || - source.parameters.length < target.parameters.length && !source.hasRestParameter || - source.minArgumentCount > target.minArgumentCount) { - return 0 /* False */; - } + if (!(isMatchingSignature(source, target, partialMatch))) { + return 0 /* False */; } var result = -1 /* True */; if (source.typeParameters && target.typeParameters) { @@ -18957,6 +19207,9 @@ var ts; function isTupleLikeType(type) { return !!getPropertyOfType(type, "0"); } + function isStringLiteralType(type) { + return type.flags & 256 /* StringLiteral */; + } /** * Check if a Type was written as a tuple type literal. * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. @@ -19629,7 +19882,7 @@ var ts; } function narrowTypeByInstanceof(type, expr, assumeTrue) { // Check that type is not any, assumed result is true, and we have variable symbol on the left - if (isTypeAny(type) || !assumeTrue || expr.left.kind !== 69 /* Identifier */ || getResolvedSymbol(expr.left) !== symbol) { + if (isTypeAny(type) || expr.left.kind !== 69 /* Identifier */ || getResolvedSymbol(expr.left) !== symbol) { return type; } // Check that right operand is a function type with a prototype property @@ -19660,6 +19913,12 @@ var ts; } } if (targetType) { + if (!assumeTrue) { + if (type.flags & 16384 /* Union */) { + return getUnionType(ts.filter(type.types, function (t) { return !isTypeSubtypeOf(t, targetType); })); + } + return type; + } return getNarrowedType(type, targetType); } return type; @@ -20129,6 +20388,9 @@ var ts; function getIndexTypeOfContextualType(type, kind) { return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); } + function contextualTypeIsStringLiteralType(type) { + return !!(type.flags & 16384 /* Union */ ? ts.forEach(type.types, isStringLiteralType) : isStringLiteralType(type)); + } // Return true if the given contextual type is a tuple-like type function contextualTypeIsTupleLikeType(type) { return !!(type.flags & 16384 /* Union */ ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); @@ -20150,7 +20412,7 @@ var ts; } function getContextualTypeForObjectLiteralElement(element) { var objectLiteral = element.parent; - var type = getContextualType(objectLiteral); + var type = getApparentTypeOfContextualType(objectLiteral); if (type) { if (!ts.hasDynamicName(element)) { // For a (non-symbol) computed property, there is no reason to look up the name @@ -20173,7 +20435,7 @@ var ts; // type of T. function getContextualTypeForElementExpression(node) { var arrayLiteral = node.parent; - var type = getContextualType(arrayLiteral); + var type = getApparentTypeOfContextualType(arrayLiteral); if (type) { var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) @@ -20206,11 +20468,28 @@ var ts; } // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily // be "pushed" onto a node using the contextualType property. - function getContextualType(node) { - var type = getContextualTypeWorker(node); + function getApparentTypeOfContextualType(node) { + var type = getContextualType(node); return type && getApparentType(type); } - function getContextualTypeWorker(node) { + /** + * Woah! Do you really want to use this function? + * + * Unless you're trying to get the *non-apparent* type for a + * value-literal type or you're authoring relevant portions of this algorithm, + * you probably meant to use 'getApparentTypeOfContextualType'. + * Otherwise this may not be very useful. + * + * In cases where you *are* working on this function, you should understand + * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContetxualType'. + * + * - Use 'getContextualType' when you are simply going to propagate the result to the expression. + * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type. + * + * @param node the expression whose contextual type will be returned. + * @returns the contextual type of an expression. + */ + function getContextualType(node) { if (isInsideWithStatementBody(node)) { // We cannot answer semantic questions within a with block, do not proceed any further return undefined; @@ -20285,7 +20564,7 @@ var ts; ts.Debug.assert(node.kind !== 143 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); var type = ts.isObjectLiteralMethod(node) ? getContextualTypeForObjectLiteralMethod(node) - : getContextualType(node); + : getApparentTypeOfContextualType(node); if (!type) { return undefined; } @@ -20411,7 +20690,7 @@ var ts; type.pattern = node; return type; } - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { var pattern = contextualType.pattern; // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting @@ -20494,10 +20773,11 @@ var ts; checkGrammarObjectLiteralExpression(node, inDestructuringPattern); var propertiesTable = {}; var propertiesArray = []; - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); var contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === 161 /* ObjectBindingPattern */ || contextualType.pattern.kind === 165 /* ObjectLiteralExpression */); var typeFlags = 0; + var patternWithComputedProperties = false; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var memberDecl = _a[_i]; var member = memberDecl.symbol; @@ -20525,8 +20805,11 @@ var ts; if (isOptional) { prop.flags |= 536870912 /* Optional */; } + if (ts.hasDynamicName(memberDecl)) { + patternWithComputedProperties = true; + } } - else if (contextualTypeHasPattern) { + else if (contextualTypeHasPattern && !(contextualType.flags & 67108864 /* ObjectLiteralPatternWithComputedProperties */)) { // If object literal is contextually typed by the implied type of a binding pattern, and if the // binding pattern specifies a default value for the property, make the property optional. var impliedProp = getPropertyOfType(contextualType, member.name); @@ -20578,7 +20861,7 @@ var ts; var numberIndexType = getIndexType(1 /* Number */); var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexType, numberIndexType); var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 1048576 /* FreshObjectLiteral */; - result.flags |= 524288 /* ObjectLiteral */ | 4194304 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 14680064 /* PropagatingFlags */); + result.flags |= 524288 /* ObjectLiteral */ | 4194304 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 14680064 /* PropagatingFlags */) | (patternWithComputedProperties ? 67108864 /* ObjectLiteralPatternWithComputedProperties */ : 0); if (inDestructuringPattern) { result.pattern = node; } @@ -21289,7 +21572,7 @@ var ts; // so order how inherited signatures are processed is still preserved. // interface A { (x: string): void } // interface B extends A { (x: 'foo'): string } - // let b: B; + // const b: B; // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void] function reorderCandidates(signatures, result) { var lastParent; @@ -22247,6 +22530,10 @@ var ts; return anyType; } } + // In JavaScript files, calls to any identifier 'require' are treated as external module imports + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node)) { + return resolveExternalModuleTypeByLiteral(node.arguments[0]); + } return getReturnTypeOfSignature(signature); } function checkTaggedTemplateExpression(node) { @@ -22257,7 +22544,10 @@ var ts; var targetType = getTypeFromTypeNode(node.type); if (produceDiagnostics && targetType !== unknownType) { var widenedType = getWidenedType(exprType); - if (!(isTypeAssignableTo(targetType, widenedType))) { + // Permit 'number[] | "foo"' to be asserted to 'string'. + var bothAreStringLike = someConstituentTypeHasKind(targetType, 258 /* StringLike */) && + someConstituentTypeHasKind(widenedType, 258 /* StringLike */); + if (!bothAreStringLike && !(isTypeAssignableTo(targetType, widenedType))) { checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } @@ -22801,19 +23091,26 @@ var ts; for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) { var p = properties_3[_i]; if (p.kind === 245 /* PropertyAssignment */ || p.kind === 246 /* ShorthandPropertyAssignment */) { - // TODO(andersh): Computed property support var name_13 = p.name; + if (name_13.kind === 136 /* ComputedPropertyName */) { + checkComputedPropertyName(name_13); + } + if (isComputedNonLiteralName(name_13)) { + continue; + } + var text = getTextOfPropertyName(name_13); var type = isTypeAny(sourceType) ? sourceType - : getTypeOfPropertyOfType(sourceType, name_13.text) || - isNumericLiteralName(name_13.text) && getIndexTypeOfType(sourceType, 1 /* Number */) || + : getTypeOfPropertyOfType(sourceType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(sourceType, 1 /* Number */) || getIndexTypeOfType(sourceType, 0 /* String */); if (type) { if (p.kind === 246 /* ShorthandPropertyAssignment */) { checkDestructuringAssignment(p, type); } else { - checkDestructuringAssignment(p.initializer || name_13, type); + // non-shorthand property assignments should always have initializers + checkDestructuringAssignment(p.initializer, type); } } else { @@ -23014,6 +23311,10 @@ var ts; case 31 /* ExclamationEqualsToken */: case 32 /* EqualsEqualsEqualsToken */: case 33 /* ExclamationEqualsEqualsToken */: + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (someConstituentTypeHasKind(leftType, 258 /* StringLike */) && someConstituentTypeHasKind(rightType, 258 /* StringLike */)) { + return booleanType; + } if (!isTypeAssignableTo(leftType, rightType) && !isTypeAssignableTo(rightType, leftType)) { reportOperatorError(); } @@ -23137,6 +23438,13 @@ var ts; var type2 = checkExpression(node.whenFalse, contextualMapper); return getUnionType([type1, type2]); } + function checkStringLiteralExpression(node) { + var contextualType = getContextualType(node); + if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { + return getStringLiteralType(node); + } + return stringType; + } function checkTemplateExpression(node) { // We just want to check each expressions, but we are unconcerned with // the type of each expression, as any value may be coerced into a string. @@ -23187,7 +23495,7 @@ var ts; if (isInferentialContext(contextualMapper)) { var signature = getSingleCallSignature(type); if (signature && signature.typeParameters) { - var contextualType = getContextualType(node); + var contextualType = getApparentTypeOfContextualType(node); if (contextualType) { var contextualSignature = getSingleCallSignature(contextualType); if (contextualSignature && !contextualSignature.typeParameters) { @@ -23251,6 +23559,7 @@ var ts; case 183 /* TemplateExpression */: return checkTemplateExpression(node); case 9 /* StringLiteral */: + return checkStringLiteralExpression(node); case 11 /* NoSubstitutionTemplateLiteral */: return stringType; case 10 /* RegularExpressionLiteral */: @@ -24580,7 +24889,7 @@ var ts; } // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent var parent = getDeclarationContainer(node); - if (parent.kind === 248 /* SourceFile */ && ts.isExternalModule(parent)) { + if (parent.kind === 248 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent)) { // If the declaration happens to be in external module, report error that require and exports are reserved keywords error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); } @@ -24598,15 +24907,15 @@ var ts; // A non-initialized declaration is a no-op as the block declaration will resolve before the var // declaration. the problem is if the declaration has an initializer. this will act as a write to the // block declared value. this is fine for let, but not const. - // Only consider declarations with initializers, uninitialized let declarations will not + // Only consider declarations with initializers, uninitialized const declarations will not // step on a let/const variable. - // Do not consider let and const declarations, as duplicate block-scoped declarations + // Do not consider const and const declarations, as duplicate block-scoped declarations // are handled by the binder. - // We are only looking for let declarations that step on let\const declarations from a + // We are only looking for const declarations that step on let\const declarations from a // different scope. e.g.: // { // const x = 0; // localDeclarationSymbol obtained after name resolution will correspond to this declaration - // let x = 0; // symbol for this declaration will be 'symbol' + // const x = 0; // symbol for this declaration will be 'symbol' // } // skip block-scoped variables and parameters if ((ts.getCombinedNodeFlags(node) & 24576 /* BlockScoped */) !== 0 || ts.isParameterDeclaration(node)) { @@ -24693,6 +25002,12 @@ var ts; checkExpressionCached(node.initializer); } } + if (node.kind === 163 /* BindingElement */) { + // check computed properties inside property names of binding elements + if (node.propertyName && node.propertyName.kind === 136 /* ComputedPropertyName */) { + checkComputedPropertyName(node.propertyName); + } + } // For a binding pattern, check contained binding elements if (ts.isBindingPattern(node.name)) { ts.forEach(node.name.elements, checkSourceElement); @@ -25176,6 +25491,7 @@ var ts; var firstDefaultClause; var hasDuplicateDefaultClause = false; var expressionType = checkExpression(node.expression); + var expressionTypeIsStringLike = someConstituentTypeHasKind(expressionType, 258 /* StringLike */); ts.forEach(node.caseBlock.clauses, function (clause) { // Grammar check for duplicate default clauses, skip if we already report duplicate default clause if (clause.kind === 242 /* DefaultClause */ && !hasDuplicateDefaultClause) { @@ -25195,6 +25511,10 @@ var ts; // TypeScript 1.0 spec (April 2014):5.9 // In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression. var caseType = checkExpression(caseClause.expression); + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, 258 /* StringLike */)) { + return; + } if (!isTypeAssignableTo(expressionType, caseType)) { // check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); @@ -25659,11 +25979,14 @@ var ts; var enumIsConst = ts.isConst(node); for (var _i = 0, _a = node.members; _i < _a.length; _i++) { var member = _a[_i]; - if (member.name.kind === 136 /* ComputedPropertyName */) { + if (isComputedNonLiteralName(member.name)) { error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); } - else if (isNumericLiteralName(member.name.text)) { - error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + else { + var text = getTextOfPropertyName(member.name); + if (isNumericLiteralName(text)) { + error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); + } } var previousEnumMemberIsNonConstant = autoValue === undefined; var initializer = member.initializer; @@ -26305,8 +26628,8 @@ var ts; } // Function and class expression bodies are checked after all statements in the enclosing body. This is // to ensure constructs like the following are permitted: - // let foo = function () { - // let s = foo(); + // const foo = function () { + // const s = foo(); // return "hello"; // } // Here, performing a full type check of the body of the function expression whilst in the process of @@ -26421,8 +26744,12 @@ var ts; if (!(links.flags & 1 /* TypeChecked */)) { // Check whether the file has declared it is the default lib, // and whether the user has specifically chosen to avoid checking it. - if (node.isDefaultLib && compilerOptions.skipDefaultLibCheck) { - return; + if (compilerOptions.skipDefaultLibCheck) { + // If the user specified '--noLib' and a file has a '/// ', + // then we should treat that file as a default lib. + if (node.hasNoDefaultLib) { + return; + } } // Grammar checking checkGrammarSourceFile(node); @@ -26432,7 +26759,7 @@ var ts; potentialThisCollisions.length = 0; ts.forEach(node.statements, checkSourceElement); checkFunctionAndClassExpressionBodies(node); - if (ts.isExternalModule(node)) { + if (ts.isExternalOrCommonJsModule(node)) { checkExternalModuleExports(node); } if (potentialThisCollisions.length) { @@ -26515,7 +26842,7 @@ var ts; } switch (location.kind) { case 248 /* SourceFile */: - if (!ts.isExternalModule(location)) { + if (!ts.isExternalOrCommonJsModule(location)) { break; } case 218 /* ModuleDeclaration */: @@ -27153,9 +27480,18 @@ var ts; getReferencedValueDeclaration: getReferencedValueDeclaration, getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, isOptionalParameter: isOptionalParameter, - isArgumentsLocalBinding: isArgumentsLocalBinding + isArgumentsLocalBinding: isArgumentsLocalBinding, + getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration }; } + function getExternalModuleFileFromDeclaration(declaration) { + var specifier = ts.getExternalModuleName(declaration); + var moduleSymbol = getSymbolAtLocation(specifier); + if (!moduleSymbol) { + return undefined; + } + return ts.getDeclarationOfKind(moduleSymbol, 248 /* SourceFile */); + } function initializeTypeChecker() { // Bind all source files and propagate errors ts.forEach(host.getSourceFiles(), function (file) { @@ -27163,11 +27499,10 @@ var ts; }); // Initialize global symbol table ts.forEach(host.getSourceFiles(), function (file) { - if (!ts.isExternalModule(file)) { + if (!ts.isExternalOrCommonJsModule(file)) { mergeSymbolTable(globals, file.locals); } }); - // Initialize special symbols getSymbolLinks(undefinedSymbol).type = undefinedType; getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); getSymbolLinks(unknownSymbol).type = unknownType; @@ -27866,7 +28201,7 @@ var ts; } } function checkGrammarForNonSymbolComputedProperty(node, message) { - if (node.kind === 136 /* ComputedPropertyName */ && !ts.isWellKnownSymbolSyntactically(node.expression)) { + if (ts.isDynamicName(node)) { return grammarErrorOnNode(node, message); } } @@ -28225,11 +28560,15 @@ var ts; var writeTextOfNode; var writer = createAndSetNewTextWriterWithSymbolWriter(); var enclosingDeclaration; - var currentSourceFile; + var currentText; + var currentLineMap; + var currentIdentifiers; + var isCurrentFileExternalModule; var reportedDeclarationError = false; var errorNameNode; var emitJsDocComments = compilerOptions.removeComments ? function (declaration) { } : writeJsDocComments; var emit = compilerOptions.stripInternal ? stripInternal : emitNode; + var noDeclare = !root; var moduleElementDeclarationEmitInfo = []; var asynchronousSubModuleDeclarationEmitInfo; // Contains the reference paths that needs to go in the declaration file. @@ -28272,23 +28611,56 @@ var ts; else { // Emit references corresponding to this file var emittedReferencedFiles = []; + var prevModuleElementDeclarationEmitInfo = []; ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + if (!ts.isDeclarationFile(sourceFile)) { // Check what references need to be added if (!compilerOptions.noResolve) { ts.forEach(sourceFile.referencedFiles, function (fileReference) { var referencedFile = ts.tryResolveScriptReference(host, sourceFile, fileReference); - // If the reference file is a declaration file or an external module, emit that reference - if (referencedFile && (ts.isExternalModuleOrDeclarationFile(referencedFile) && + // If the reference file is a declaration file, emit that reference + if (referencedFile && (ts.isDeclarationFile(referencedFile) && !ts.contains(emittedReferencedFiles, referencedFile))) { writeReferencePath(referencedFile); emittedReferencedFiles.push(referencedFile); } }); } + } + if (!ts.isExternalModuleOrDeclarationFile(sourceFile)) { + noDeclare = false; emitSourceFile(sourceFile); } + else if (ts.isExternalModule(sourceFile)) { + noDeclare = true; + write("declare module \"" + ts.getResolvedExternalModuleName(host, sourceFile) + "\" {"); + writeLine(); + increaseIndent(); + emitSourceFile(sourceFile); + decreaseIndent(); + write("}"); + writeLine(); + // create asynchronous output for the importDeclarations + if (moduleElementDeclarationEmitInfo.length) { + var oldWriter = writer; + ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { + if (aliasEmitInfo.isVisible && !aliasEmitInfo.asynchronousOutput) { + ts.Debug.assert(aliasEmitInfo.node.kind === 222 /* ImportDeclaration */); + createAndSetNewTextWriterWithSymbolWriter(); + ts.Debug.assert(aliasEmitInfo.indent === 1); + increaseIndent(); + writeImportDeclaration(aliasEmitInfo.node); + aliasEmitInfo.asynchronousOutput = writer.getText(); + decreaseIndent(); + } + }); + setWriter(oldWriter); + } + prevModuleElementDeclarationEmitInfo = prevModuleElementDeclarationEmitInfo.concat(moduleElementDeclarationEmitInfo); + moduleElementDeclarationEmitInfo = []; + } }); + moduleElementDeclarationEmitInfo = moduleElementDeclarationEmitInfo.concat(prevModuleElementDeclarationEmitInfo); } return { reportedDeclarationError: reportedDeclarationError, @@ -28297,13 +28669,12 @@ var ts; referencePathsOutput: referencePathsOutput }; function hasInternalAnnotation(range) { - var text = currentSourceFile.text; - var comment = text.substring(range.pos, range.end); + var comment = currentText.substring(range.pos, range.end); return comment.indexOf("@internal") >= 0; } function stripInternal(node) { if (node) { - var leadingCommentRanges = ts.getLeadingCommentRanges(currentSourceFile.text, node.pos); + var leadingCommentRanges = ts.getLeadingCommentRanges(currentText, node.pos); if (ts.forEach(leadingCommentRanges, hasInternalAnnotation)) { return; } @@ -28395,7 +28766,7 @@ var ts; var errorInfo = writer.getSymbolAccessibilityDiagnostic(symbolAccesibilityResult); if (errorInfo) { if (errorInfo.typeName) { - diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); + diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, ts.getTextOfNodeFromSourceText(currentText, errorInfo.typeName), symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); } else { diagnostics.push(ts.createDiagnosticForNode(symbolAccesibilityResult.errorNode || errorInfo.errorNode, errorInfo.diagnosticMessage, symbolAccesibilityResult.errorSymbolName, symbolAccesibilityResult.errorModuleName)); @@ -28461,10 +28832,10 @@ var ts; } function writeJsDocComments(declaration) { if (declaration) { - var jsDocComments = ts.getJsDocComments(declaration, currentSourceFile); - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, declaration, jsDocComments); + var jsDocComments = ts.getJsDocCommentsFromText(declaration, currentText); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments); // jsDoc comments are emitted at /*leading comment1 */space/*leading comment*/space - ts.emitComments(currentSourceFile, writer, jsDocComments, /*trailingSeparator*/ true, newLine, ts.writeCommentRange); + ts.emitComments(currentText, currentLineMap, writer, jsDocComments, /*trailingSeparator*/ true, newLine, ts.writeCommentRange); } } function emitTypeWithNewGetSymbolAccessibilityDiagnostic(type, getSymbolAccessibilityDiagnostic) { @@ -28481,7 +28852,7 @@ var ts; case 103 /* VoidKeyword */: case 97 /* ThisKeyword */: case 9 /* StringLiteral */: - return writeTextOfNode(currentSourceFile, type); + return writeTextOfNode(currentText, type); case 188 /* ExpressionWithTypeArguments */: return emitExpressionWithTypeArguments(type); case 151 /* TypeReference */: @@ -28512,14 +28883,14 @@ var ts; } function writeEntityName(entityName) { if (entityName.kind === 69 /* Identifier */) { - writeTextOfNode(currentSourceFile, entityName); + writeTextOfNode(currentText, entityName); } else { var left = entityName.kind === 135 /* QualifiedName */ ? entityName.left : entityName.expression; var right = entityName.kind === 135 /* QualifiedName */ ? entityName.right : entityName.name; writeEntityName(left); write("."); - writeTextOfNode(currentSourceFile, right); + writeTextOfNode(currentText, right); } } function emitEntityName(entityName) { @@ -28549,7 +28920,7 @@ var ts; } } function emitTypePredicate(type) { - writeTextOfNode(currentSourceFile, type.parameterName); + writeTextOfNode(currentText, type.parameterName); write(" is "); emitType(type.type); } @@ -28590,9 +28961,12 @@ var ts; } } function emitSourceFile(node) { - currentSourceFile = node; + currentText = node.text; + currentLineMap = ts.getLineStarts(node); + currentIdentifiers = node.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(node); enclosingDeclaration = node; - ts.emitDetachedComments(currentSourceFile, writer, ts.writeCommentRange, node, newLine, true /* remove comments */); + ts.emitDetachedComments(currentText, currentLineMap, writer, ts.writeCommentRange, node, newLine, true /* remove comments */); emitLines(node.statements); } // Return a temp variable name to be used in `export default` statements. @@ -28601,13 +28975,13 @@ var ts; // do not need to keep track of created temp names. function getExportDefaultTempVariableName() { var baseName = "_default"; - if (!ts.hasProperty(currentSourceFile.identifiers, baseName)) { + if (!ts.hasProperty(currentIdentifiers, baseName)) { return baseName; } var count = 0; while (true) { var name_18 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_18)) { + if (!ts.hasProperty(currentIdentifiers, name_18)) { return name_18; } } @@ -28615,7 +28989,7 @@ var ts; function emitExportAssignment(node) { if (node.expression.kind === 69 /* Identifier */) { write(node.isExportEquals ? "export = " : "export default "); - writeTextOfNode(currentSourceFile, node.expression); + writeTextOfNode(currentText, node.expression); } else { // Expression @@ -28653,7 +29027,7 @@ var ts; writeModuleElement(node); } else if (node.kind === 221 /* ImportEqualsDeclaration */ || - (node.parent.kind === 248 /* SourceFile */ && ts.isExternalModule(currentSourceFile))) { + (node.parent.kind === 248 /* SourceFile */ && isCurrentFileExternalModule)) { var isVisible; if (asynchronousSubModuleDeclarationEmitInfo && node.parent.kind !== 248 /* SourceFile */) { // Import declaration of another module that is visited async so lets put it in right spot @@ -28707,7 +29081,7 @@ var ts; } function emitModuleElementDeclarationFlags(node) { // If the node is parented in the current source file we need to emit export declare or just export - if (node.parent === currentSourceFile) { + if (node.parent.kind === 248 /* SourceFile */) { // If the node is exported if (node.flags & 2 /* Export */) { write("export "); @@ -28715,7 +29089,7 @@ var ts; if (node.flags & 512 /* Default */) { write("default "); } - else if (node.kind !== 215 /* InterfaceDeclaration */) { + else if (node.kind !== 215 /* InterfaceDeclaration */ && !noDeclare) { write("declare "); } } @@ -28742,7 +29116,7 @@ var ts; write("export "); } write("import "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" = "); if (ts.isInternalModuleImportEqualsDeclaration(node)) { emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.moduleReference, getImportEntityNameVisibilityError); @@ -28750,7 +29124,7 @@ var ts; } else { write("require("); - writeTextOfNode(currentSourceFile, ts.getExternalModuleImportEqualsDeclarationExpression(node)); + writeTextOfNode(currentText, ts.getExternalModuleImportEqualsDeclarationExpression(node)); write(");"); } writer.writeLine(); @@ -28785,7 +29159,7 @@ var ts; if (node.importClause) { var currentWriterPos = writer.getTextPos(); if (node.importClause.name && resolver.isDeclarationVisible(node.importClause)) { - writeTextOfNode(currentSourceFile, node.importClause.name); + writeTextOfNode(currentText, node.importClause.name); } if (node.importClause.namedBindings && isVisibleNamedBinding(node.importClause.namedBindings)) { if (currentWriterPos !== writer.getTextPos()) { @@ -28794,7 +29168,7 @@ var ts; } if (node.importClause.namedBindings.kind === 224 /* NamespaceImport */) { write("* as "); - writeTextOfNode(currentSourceFile, node.importClause.namedBindings.name); + writeTextOfNode(currentText, node.importClause.namedBindings.name); } else { write("{ "); @@ -28804,16 +29178,28 @@ var ts; } write(" from "); } - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); write(";"); writer.writeLine(); } + function emitExternalModuleSpecifier(moduleSpecifier) { + if (moduleSpecifier.kind === 9 /* StringLiteral */ && (!root) && (compilerOptions.out || compilerOptions.outFile)) { + var moduleName = ts.getExternalModuleNameFromDeclaration(host, resolver, moduleSpecifier.parent); + if (moduleName) { + write("\""); + write(moduleName); + write("\""); + return; + } + } + writeTextOfNode(currentText, moduleSpecifier); + } function emitImportOrExportSpecifier(node) { if (node.propertyName) { - writeTextOfNode(currentSourceFile, node.propertyName); + writeTextOfNode(currentText, node.propertyName); write(" as "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } function emitExportSpecifier(node) { emitImportOrExportSpecifier(node); @@ -28835,7 +29221,7 @@ var ts; } if (node.moduleSpecifier) { write(" from "); - writeTextOfNode(currentSourceFile, node.moduleSpecifier); + emitExternalModuleSpecifier(node.moduleSpecifier); } write(";"); writer.writeLine(); @@ -28849,11 +29235,11 @@ var ts; else { write("module "); } - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); while (node.body.kind !== 219 /* ModuleBlock */) { node = node.body; write("."); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; @@ -28872,7 +29258,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("type "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); emitTypeParameters(node.typeParameters); write(" = "); emitTypeWithNewGetSymbolAccessibilityDiagnostic(node.type, getTypeAliasDeclarationVisibilityError); @@ -28894,7 +29280,7 @@ var ts; write("const "); } write("enum "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); write(" {"); writeLine(); increaseIndent(); @@ -28905,7 +29291,7 @@ var ts; } function emitEnumMemberDeclaration(node) { emitJsDocComments(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var enumMemberValue = resolver.getConstantValue(node); if (enumMemberValue !== undefined) { write(" = "); @@ -28922,7 +29308,7 @@ var ts; increaseIndent(); emitJsDocComments(node); decreaseIndent(); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); // If there is constraint present and this is not a type parameter of the private method emit the constraint if (node.constraint && !isPrivateMethodTypeParameter(node)) { write(" extends "); @@ -29037,7 +29423,7 @@ var ts; write("abstract "); } write("class "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -29060,7 +29446,7 @@ var ts; emitJsDocComments(node); emitModuleElementDeclarationFlags(node); write("interface "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); var prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; emitTypeParameters(node.typeParameters); @@ -29095,7 +29481,7 @@ var ts; // If this node is a computed name, it can only be a symbol, because we've already skipped // it if it's not a well known symbol. In that case, the text of the name will be exactly // what we want, namely the name expression enclosed in brackets. - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); // If optional property emit ? if ((node.kind === 141 /* PropertyDeclaration */ || node.kind === 140 /* PropertySignature */) && ts.hasQuestionToken(node)) { write("?"); @@ -29177,7 +29563,7 @@ var ts; emitBindingPattern(bindingElement.name); } else { - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); writeTypeOfDeclaration(bindingElement, /*type*/ undefined, getBindingElementTypeVisibilityError); } } @@ -29221,7 +29607,7 @@ var ts; emitJsDocComments(accessors.getAccessor); emitJsDocComments(accessors.setAccessor); emitClassMemberDeclarationFlags(node); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (!(node.flags & 16 /* Private */)) { accessorWithTypeAnnotation = node; var type = getTypeAnnotationFromAccessor(node); @@ -29307,13 +29693,13 @@ var ts; } if (node.kind === 213 /* FunctionDeclaration */) { write("function "); - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } else if (node.kind === 144 /* Constructor */) { write("constructor"); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); if (ts.hasQuestionToken(node)) { write("?"); } @@ -29437,7 +29823,7 @@ var ts; emitBindingPattern(node.name); } else { - writeTextOfNode(currentSourceFile, node.name); + writeTextOfNode(currentText, node.name); } if (resolver.isOptionalParameter(node)) { write("?"); @@ -29552,7 +29938,7 @@ var ts; // Example: // original: function foo({y: [a,b,c]}) {} // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; - writeTextOfNode(currentSourceFile, bindingElement.propertyName); + writeTextOfNode(currentText, bindingElement.propertyName); write(": "); } if (bindingElement.name) { @@ -29575,7 +29961,7 @@ var ts; if (bindingElement.dotDotDotToken) { write("..."); } - writeTextOfNode(currentSourceFile, bindingElement.name); + writeTextOfNode(currentText, bindingElement.name); } } } @@ -29667,6 +30053,18 @@ var ts; return ts.isExternalModule(sourceFile) || ts.isDeclarationFile(sourceFile); } ts.isExternalModuleOrDeclarationFile = isExternalModuleOrDeclarationFile; + function getResolvedExternalModuleName(host, file) { + return file.moduleName || ts.getExternalModuleNameFromPath(host, file.fileName); + } + ts.getResolvedExternalModuleName = getResolvedExternalModuleName; + function getExternalModuleNameFromDeclaration(host, resolver, declaration) { + var file = resolver.getExternalModuleFileFromDeclaration(declaration); + if (!file || ts.isDeclarationFile(file)) { + return undefined; + } + return getResolvedExternalModuleName(host, file); + } + ts.getExternalModuleNameFromDeclaration = getExternalModuleNameFromDeclaration; var Jump; (function (Jump) { Jump[Jump["Break"] = 2] = "Break"; @@ -29954,15 +30352,19 @@ var ts; var newLine = host.getNewLine(); var jsxDesugaring = host.getCompilerOptions().jsx !== 1 /* Preserve */; var shouldEmitJsx = function (s) { return (s.languageVariant === 1 /* JSX */ && !jsxDesugaring); }; + var outFile = compilerOptions.outFile || compilerOptions.out; + var emitJavaScript = createFileEmitter(); if (targetSourceFile === undefined) { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { - var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); - emitFile(jsFilePath, sourceFile); - } - }); - if (compilerOptions.outFile || compilerOptions.out) { - emitFile(compilerOptions.outFile || compilerOptions.out); + if (outFile) { + emitFile(outFile); + } + else { + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if (ts.shouldEmitToOwnFile(sourceFile, compilerOptions)) { + var jsFilePath = ts.getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); + emitFile(jsFilePath, sourceFile); + } + }); } } else { @@ -29971,8 +30373,8 @@ var ts; var jsFilePath = ts.getOwnEmitOutputFilePath(targetSourceFile, host, shouldEmitJsx(targetSourceFile) ? ".jsx" : ".js"); emitFile(jsFilePath, targetSourceFile); } - else if (!ts.isDeclarationFile(targetSourceFile) && (compilerOptions.outFile || compilerOptions.out)) { - emitFile(compilerOptions.outFile || compilerOptions.out); + else if (!ts.isDeclarationFile(targetSourceFile) && outFile) { + emitFile(outFile); } } // Sort and make the unique list of diagnostics @@ -30024,10 +30426,16 @@ var ts; } } } - function emitJavaScript(jsFilePath, root) { + function createFileEmitter() { var writer = ts.createTextWriter(newLine); var write = writer.write, writeTextOfNode = writer.writeTextOfNode, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; 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 () {...}) // exporting in System modules looks like: @@ -30035,15 +30443,15 @@ var ts; // => // var x;... exporter("x", x = 1) var exportFunctionForFile; - var generatedNameSet = {}; - var nodeToGeneratedName = []; + var generatedNameSet; + var nodeToGeneratedName; var computedPropertyNamesToGeneratedNames; var convertedLoopState; - var extendsEmitted = false; - var decorateEmitted = false; - var paramEmitted = false; - var awaiterEmitted = false; - var tempFlags = 0; + var extendsEmitted; + var decorateEmitted; + var paramEmitted; + var awaiterEmitted; + var tempFlags; var tempVariables; var tempParameters; var externalImports; @@ -30075,6 +30483,8 @@ var ts; var scopeEmitEnd = function () { }; /** Sourcemap data that will get encoded */ var sourceMapData; + /** The root file passed to the emit function (if present) */ + var root; /** If removeComments is true, no leading-comments needed to be emitted **/ var emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos) { } : emitLeadingCommentsOfPositionWorker; var moduleEmitDelegates = (_a = {}, @@ -30085,31 +30495,77 @@ var ts; _a[1 /* CommonJS */] = emitCommonJSModule, _a ); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - initializeEmitterWithSourceMaps(); - } - if (root) { - // Do not call emit directly. It does not set the currentSourceFile. - emitSourceFile(root); - } - else { - ts.forEach(host.getSourceFiles(), function (sourceFile) { - if (!isExternalModuleOrDeclarationFile(sourceFile)) { - emitSourceFile(sourceFile); + var bundleEmitDelegates = (_b = {}, + _b[5 /* ES6 */] = function () { }, + _b[2 /* AMD */] = emitAMDModule, + _b[4 /* System */] = emitSystemModule, + _b[3 /* UMD */] = function () { }, + _b[1 /* CommonJS */] = function () { }, + _b + ); + return doEmit; + function doEmit(jsFilePath, rootFile) { + // reset the state + writer.reset(); + currentSourceFile = undefined; + currentText = undefined; + currentLineMap = undefined; + exportFunctionForFile = undefined; + generatedNameSet = {}; + nodeToGeneratedName = []; + computedPropertyNamesToGeneratedNames = undefined; + convertedLoopState = undefined; + extendsEmitted = false; + decorateEmitted = false; + paramEmitted = false; + awaiterEmitted = false; + tempFlags = 0; + tempVariables = undefined; + tempParameters = undefined; + externalImports = undefined; + exportSpecifiers = undefined; + exportEquals = undefined; + hasExportStars = undefined; + detachedCommentsInfo = undefined; + sourceMapData = undefined; + isEs6Module = false; + renamedDependencies = undefined; + isCurrentFileExternalModule = false; + root = rootFile; + if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { + initializeEmitterWithSourceMaps(jsFilePath, root); + } + if (root) { + // Do not call emit directly. It does not set the currentSourceFile. + emitSourceFile(root); + } + else { + if (modulekind) { + ts.forEach(host.getSourceFiles(), emitEmitHelpers); } - }); + ts.forEach(host.getSourceFiles(), function (sourceFile) { + if ((!isExternalModuleOrDeclarationFile(sourceFile)) || (modulekind && ts.isExternalModule(sourceFile))) { + emitSourceFile(sourceFile); + } + }); + } + writeLine(); + writeEmittedFiles(writer.getText(), jsFilePath, /*writeByteOrderMark*/ compilerOptions.emitBOM); } - writeLine(); - writeEmittedFiles(writer.getText(), /*writeByteOrderMark*/ compilerOptions.emitBOM); - return; function emitSourceFile(sourceFile) { currentSourceFile = sourceFile; + currentText = sourceFile.text; + currentLineMap = ts.getLineStarts(sourceFile); exportFunctionForFile = undefined; + isEs6Module = sourceFile.symbol && sourceFile.symbol.exports && !!sourceFile.symbol.exports["___esModule"]; + renamedDependencies = sourceFile.renamedDependencies; + currentFileIdentifiers = sourceFile.identifiers; + isCurrentFileExternalModule = ts.isExternalModule(sourceFile); emit(sourceFile); } function isUniqueName(name) { return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentSourceFile.identifiers, name) && + !ts.hasProperty(currentFileIdentifiers, name) && !ts.hasProperty(generatedNameSet, name); } // Return the next available name in the pattern _a ... _z, _0, _1, ... @@ -30192,7 +30648,7 @@ var ts; var id = ts.getNodeId(node); return nodeToGeneratedName[id] || (nodeToGeneratedName[id] = ts.unescapeIdentifier(generateNameForNode(node))); } - function initializeEmitterWithSourceMaps() { + function initializeEmitterWithSourceMaps(jsFilePath, root) { var sourceMapDir; // The directory in which sourcemap will be // Current source map file and its index in the sources list var sourceMapSourceIndex = -1; @@ -30280,7 +30736,7 @@ var ts; } } function recordSourceMapSpan(pos) { - var sourceLinePos = ts.getLineAndCharacterOfPosition(currentSourceFile, pos); + var sourceLinePos = ts.computeLineAndCharacterOfPosition(currentLineMap, pos); // Convert the location to be one-based. sourceLinePos.line++; sourceLinePos.character++; @@ -30314,13 +30770,13 @@ var ts; } function recordEmitNodeStartSpan(node) { // Get the token pos after skipping to the token (ignoring the leading trivia) - recordSourceMapSpan(ts.skipTrivia(currentSourceFile.text, node.pos)); + recordSourceMapSpan(ts.skipTrivia(currentText, node.pos)); } function recordEmitNodeEndSpan(node) { recordSourceMapSpan(node.end); } function writeTextWithSpanRecord(tokenKind, startPos, emitFn) { - var tokenStartPos = ts.skipTrivia(currentSourceFile.text, startPos); + var tokenStartPos = ts.skipTrivia(currentText, startPos); recordSourceMapSpan(tokenStartPos); var tokenEndPos = emitTokenText(tokenKind, tokenStartPos, emitFn); recordSourceMapSpan(tokenEndPos); @@ -30402,9 +30858,9 @@ var ts; sourceMapNameIndices.pop(); } ; - function writeCommentRangeWithMap(curentSourceFile, writer, comment, newLine) { + function writeCommentRangeWithMap(currentText, currentLineMap, writer, comment, newLine) { recordSourceMapSpan(comment.pos); - ts.writeCommentRange(currentSourceFile, writer, comment, newLine); + ts.writeCommentRange(currentText, currentLineMap, writer, comment, newLine); recordSourceMapSpan(comment.end); } function serializeSourceMapContents(version, file, sourceRoot, sources, names, mappings, sourcesContent) { @@ -30434,7 +30890,7 @@ var ts; return output; } } - function writeJavaScriptAndSourceMapFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptAndSourceMapFile(emitOutput, jsFilePath, writeByteOrderMark) { encodeLastRecordedSourceMapSpan(); var sourceMapText = serializeSourceMapContents(3, sourceMapData.sourceMapFile, sourceMapData.sourceMapSourceRoot, sourceMapData.sourceMapSources, sourceMapData.sourceMapNames, sourceMapData.sourceMapMappings, sourceMapData.sourceMapSourcesContent); sourceMapDataList.push(sourceMapData); @@ -30450,7 +30906,7 @@ var ts; sourceMapUrl = "//# sourceMappingURL=" + sourceMapData.jsSourceMappingURL; } // Write sourcemap url to the js file and write the js file - writeJavaScriptFile(emitOutput + sourceMapUrl, writeByteOrderMark); + writeJavaScriptFile(emitOutput + sourceMapUrl, jsFilePath, writeByteOrderMark); } // Initialize source map data var sourceMapJsFile = ts.getBaseFileName(ts.normalizeSlashes(jsFilePath)); @@ -30522,7 +30978,7 @@ var ts; scopeEmitEnd = recordScopeNameEnd; writeComment = writeCommentRangeWithMap; } - function writeJavaScriptFile(emitOutput, writeByteOrderMark) { + function writeJavaScriptFile(emitOutput, jsFilePath, writeByteOrderMark) { ts.writeFile(host, diagnostics, jsFilePath, emitOutput, writeByteOrderMark); } // Create a temporary variable with a unique unused name. @@ -30702,7 +31158,7 @@ var ts; // 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.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + 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. @@ -30729,7 +31185,7 @@ var ts; // 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.getSourceTextOfNodeFromSourceFile(currentSourceFile, node); + var text = ts.getTextOfNodeFromSourceText(currentText, node); // text contains the original source, it will also contain quotes ("`"), dolar signs and braces ("${" and "}"), // thus we need to remove those characters. // First template piece starts with "`", others with "}" @@ -31146,7 +31602,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } write("\""); } @@ -31246,7 +31702,7 @@ var ts; // Identifier references named import write(getGeneratedNameForNode(declaration.parent.parent.parent)); var name_23 = declaration.propertyName || declaration.name; - var identifier = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, name_23); + var identifier = ts.getTextOfNodeFromSourceText(currentText, name_23); if (languageVersion === 0 /* ES3 */ && identifier === "default") { write("[\"default\"]"); } @@ -31270,7 +31726,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function isNameOfNestedRedeclaration(node) { @@ -31308,7 +31764,7 @@ var ts; write(node.text); } else { - writeTextOfNode(currentSourceFile, node); + writeTextOfNode(currentText, node); } } function emitThis(node) { @@ -31712,7 +32168,7 @@ var ts; 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(currentSourceFile, node.name); + 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 { @@ -31722,7 +32178,7 @@ var ts; // let obj = { y }; // } // Here we need to emit obj = { y : m.y } regardless of the output target. - if (languageVersion < 2 /* ES6 */ || isNamespaceExportReference(node.name)) { + if (modulekind !== 5 /* ES6 */ || isNamespaceExportReference(node.name)) { // Emit identifier as an identifier write(": "); emit(node.name); @@ -31779,11 +32235,11 @@ var ts; var indentedBeforeDot = indentIfOnDifferentLines(node, node.expression, node.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; + var shouldEmitSpace = false; if (!indentedBeforeDot) { if (node.expression.kind === 8 /* NumericLiteral */) { // check if numeric literal was originally written with a dot - var text = ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node.expression); + var text = ts.getTextOfNodeFromSourceText(currentText, node.expression); shouldEmitSpace = text.indexOf(ts.tokenToString(21 /* DotToken */)) < 0; } else { @@ -32962,16 +33418,16 @@ var ts; emitToken(16 /* CloseBraceToken */, node.clauses.end); } function nodeStartPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node1.pos)) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node1.pos)) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function nodeEndPositionsAreOnSameLine(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, node2.end); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, node2.end); } function nodeEndIsOnSameLineAsNodeStart(node1, node2) { - return ts.getLineOfLocalPosition(currentSourceFile, node1.end) === - ts.getLineOfLocalPosition(currentSourceFile, ts.skipTrivia(currentSourceFile.text, node2.pos)); + return ts.getLineOfLocalPositionFromLineMap(currentLineMap, node1.end) === + ts.getLineOfLocalPositionFromLineMap(currentLineMap, ts.skipTrivia(currentText, node2.pos)); } function emitCaseOrDefaultClause(node) { if (node.kind === 241 /* CaseClause */) { @@ -33077,7 +33533,7 @@ var ts; ts.Debug.assert(!!(node.flags & 512 /* Default */) || node.kind === 227 /* ExportAssignment */); // only allow export default at a source file level if (modulekind === 1 /* CommonJS */ || modulekind === 2 /* AMD */ || modulekind === 3 /* UMD */) { - if (!currentSourceFile.symbol.exports["___esModule"]) { + if (!isEs6Module) { if (languageVersion === 1 /* ES5 */) { // default value of configurable, enumerable, writable are `false`. write("Object.defineProperty(exports, \"__esModule\", { value: true });"); @@ -33272,14 +33728,20 @@ var ts; return node; } function createPropertyAccessForDestructuringProperty(object, propName) { - // We create a synthetic copy of the identifier in order to avoid the rewriting that might - // otherwise occur when the identifier is emitted. - var syntheticName = ts.createSynthesizedNode(propName.kind); - syntheticName.text = propName.text; - if (syntheticName.kind !== 69 /* Identifier */) { - return createElementAccessExpression(object, syntheticName); + var index; + var nameIsComputed = propName.kind === 136 /* ComputedPropertyName */; + if (nameIsComputed) { + index = ensureIdentifier(propName.expression, /* reuseIdentifierExpression */ false); } - return createPropertyAccessExpression(object, syntheticName); + 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); + index.text = propName.text; + } + return !nameIsComputed && index.kind === 69 /* Identifier */ + ? createPropertyAccessExpression(object, index) + : createElementAccessExpression(object, index); } function createSliceCall(value, sliceIndex) { var call = ts.createSynthesizedNode(168 /* CallExpression */); @@ -33742,7 +34204,6 @@ var ts; var promiseConstructor = ts.getEntityNameFromTypeNode(node.type); var isArrowFunction = node.kind === 174 /* ArrowFunction */; var hasLexicalArguments = (resolver.getNodeCheckFlags(node) & 4096 /* CaptureArguments */) !== 0; - var args; // 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 @@ -35197,8 +35658,8 @@ var ts; * Here we check if alternative name was provided for a given moduleName and return it if possible. */ function tryRenameExternalModule(moduleName) { - if (currentSourceFile.renamedDependencies && ts.hasProperty(currentSourceFile.renamedDependencies, moduleName.text)) { - return "\"" + currentSourceFile.renamedDependencies[moduleName.text] + "\""; + if (renamedDependencies && ts.hasProperty(renamedDependencies, moduleName.text)) { + return "\"" + renamedDependencies[moduleName.text] + "\""; } return undefined; } @@ -35351,7 +35812,7 @@ var ts; // - current file is not external module // - import declaration is top level and target is value imported by entity name if (resolver.isReferencedAliasDeclaration(node) || - (!ts.isExternalModule(currentSourceFile) && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { + (!isCurrentFileExternalModule && resolver.isTopLevelValueImportEqualsWithEntityName(node))) { emitLeadingComments(node); emitStart(node); // variable declaration for import-equals declaration can be hoisted in system modules @@ -35579,7 +36040,7 @@ var ts; function getLocalNameForExternalImport(node) { var namespaceDeclaration = getNamespaceDeclarationNode(node); if (namespaceDeclaration && !isDefaultImport(node)) { - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, namespaceDeclaration.name); + return ts.getTextOfNodeFromSourceText(currentText, namespaceDeclaration.name); } if (node.kind === 222 /* ImportDeclaration */ && node.importClause) { return getGeneratedNameForNode(node); @@ -35884,7 +36345,7 @@ var ts; ts.getEnclosingBlockScopeContainer(node).kind === 248 /* SourceFile */; } function isCurrentFileSystemExternalModule() { - return modulekind === 4 /* System */ && ts.isExternalModule(currentSourceFile); + return modulekind === 4 /* System */ && isCurrentFileExternalModule; } function emitSystemModuleBody(node, dependencyGroups, startIndex) { // shape of the body in system modules: @@ -36054,7 +36515,13 @@ var ts; writeLine(); write("}"); // execute } - function emitSystemModule(node) { + function writeModuleName(node, emitRelativePathAsModuleName) { + var moduleName = node.moduleName; + if (moduleName || (emitRelativePathAsModuleName && (moduleName = getResolvedExternalModuleName(host, node)))) { + write("\"" + moduleName + "\", "); + } + } + function emitSystemModule(node, emitRelativePathAsModuleName) { collectExternalModuleInfo(node); // System modules has the following shape // System.register(['dep-1', ... 'dep-n'], function(exports) {/* module body function */}) @@ -36069,9 +36536,7 @@ var ts; exportFunctionForFile = makeUniqueName("exports"); writeLine(); write("System.register("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } + writeModuleName(node, emitRelativePathAsModuleName); write("["); var groupIndices = {}; var dependencyGroups = []; @@ -36090,6 +36555,12 @@ var ts; if (i !== 0) { write(", "); } + if (emitRelativePathAsModuleName) { + var name_29 = getExternalModuleNameFromDeclaration(host, resolver, externalImports[i]); + if (name_29) { + text = "\"" + name_29 + "\""; + } + } write(text); } write("], function(" + exportFunctionForFile + ") {"); @@ -36103,7 +36574,7 @@ var ts; writeLine(); write("});"); } - function getAMDDependencyNames(node, includeNonAmdDependencies) { + function getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { // names of modules with corresponding parameter in the factory function var aliasedModuleNames = []; // names of modules with no corresponding parameters in factory function @@ -36126,6 +36597,12 @@ var ts; var importNode = externalImports_4[_c]; // Find the name of the external module var externalModuleName = getExternalModuleNameText(importNode); + if (emitRelativePathAsModuleName) { + var name_30 = getExternalModuleNameFromDeclaration(host, resolver, importNode); + if (name_30) { + externalModuleName = "\"" + name_30 + "\""; + } + } // Find the name of the module alias, if there is one var importAliasName = getLocalNameForExternalImport(importNode); if (includeNonAmdDependencies && importAliasName) { @@ -36138,7 +36615,7 @@ var ts; } return { aliasedModuleNames: aliasedModuleNames, unaliasedModuleNames: unaliasedModuleNames, importAliasNames: importAliasNames }; } - function emitAMDDependencies(node, includeNonAmdDependencies) { + function emitAMDDependencies(node, includeNonAmdDependencies, emitRelativePathAsModuleName) { // An AMD define function has the following shape: // define(id?, dependencies?, factory); // @@ -36150,7 +36627,7 @@ var ts; // To ensure this is true in cases of modules with no aliases, e.g.: // `import "module"` or `` // we need to add modules without alias names to the end of the dependencies list - var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies); + var dependencyNames = getAMDDependencyNames(node, includeNonAmdDependencies, emitRelativePathAsModuleName); emitAMDDependencyList(dependencyNames); write(", "); emitAMDFactoryHeader(dependencyNames); @@ -36177,15 +36654,13 @@ var ts; } write(") {"); } - function emitAMDModule(node) { + function emitAMDModule(node, emitRelativePathAsModuleName) { emitEmitHelpers(node); collectExternalModuleInfo(node); writeLine(); write("define("); - if (node.moduleName) { - write("\"" + node.moduleName + "\", "); - } - emitAMDDependencies(node, /*includeNonAmdDependencies*/ true); + writeModuleName(node, emitRelativePathAsModuleName); + emitAMDDependencies(node, /*includeNonAmdDependencies*/ true, emitRelativePathAsModuleName); increaseIndent(); var startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true); emitExportStarHelper(); @@ -36404,8 +36879,13 @@ var ts; emitShebang(); emitDetachedCommentsAndUpdateCommentsInfo(node); if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { - var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1 /* CommonJS */]; - emitModule(node); + if (root || (!ts.isExternalModule(node) && compilerOptions.isolatedModules)) { + var emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[1 /* CommonJS */]; + emitModule(node); + } + else { + bundleEmitDelegates[modulekind](node, /*emitRelativePathAsModuleName*/ true); + } } else { // emit prologue directives prior to __extends @@ -36666,7 +37146,7 @@ var ts; } function getLeadingCommentsWithoutDetachedComments() { // get the leading comments from detachedPos - var leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); + var leadingComments = ts.getLeadingCommentRanges(currentText, ts.lastOrUndefined(detachedCommentsInfo).detachedCommentEndPos); if (detachedCommentsInfo.length - 1) { detachedCommentsInfo.pop(); } @@ -36683,10 +37163,10 @@ var ts; 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 (currentSourceFile.text.charCodeAt(comment.pos + 1) === 47 /* slash */ && + if (currentText.charCodeAt(comment.pos + 1) === 47 /* slash */ && comment.pos + 2 < comment.end && - currentSourceFile.text.charCodeAt(comment.pos + 2) === 47 /* slash */) { - var textSubStr = currentSourceFile.text.substring(comment.pos, comment.end); + currentText.charCodeAt(comment.pos + 2) === 47 /* slash */) { + var textSubStr = currentText.substring(comment.pos, comment.end); return textSubStr.match(ts.fullTripleSlashReferencePathRegEx) || textSubStr.match(ts.fullTripleSlashAMDReferencePathRegEx) ? true : false; @@ -36703,7 +37183,7 @@ var ts; } else { // get the leading comments from the node - return ts.getLeadingCommentRangesOfNode(node, currentSourceFile); + return ts.getLeadingCommentRangesOfNodeFromText(node, currentText); } } } @@ -36712,7 +37192,7 @@ var ts; // Emit the trailing comments only if the parent's pos doesn't match because parent should take care of emitting these comments if (node.parent) { if (node.parent.kind === 248 /* SourceFile */ || node.end !== node.parent.end) { - return ts.getTrailingCommentRanges(currentSourceFile.text, node.end); + return ts.getTrailingCommentRanges(currentText, node.end); } } } @@ -36746,9 +37226,9 @@ var ts; leadingComments = ts.filter(getLeadingCommentsToEmit(node), isTripleSlashComment); } } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, node, leadingComments); // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space - ts.emitComments(currentSourceFile, writer, leadingComments, /*trailingSeparator:*/ true, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, /*trailingSeparator:*/ true, newLine, writeComment); } function emitTrailingComments(node) { if (compilerOptions.removeComments) { @@ -36757,7 +37237,7 @@ var ts; // Emit the trailing comments only if the parent's end doesn't match var trailingComments = getTrailingCommentsToEmit(node); // trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/ - ts.emitComments(currentSourceFile, writer, trailingComments, /*trailingSeparator*/ false, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, /*trailingSeparator*/ false, newLine, writeComment); } /** * Emit trailing comments at the position. The term trailing comment is used here to describe following comment: @@ -36768,9 +37248,9 @@ var ts; if (compilerOptions.removeComments) { return; } - var trailingComments = ts.getTrailingCommentRanges(currentSourceFile.text, pos); + var trailingComments = ts.getTrailingCommentRanges(currentText, pos); // trailing comments are emitted at space/*trailing comment1 */space/*trailing comment*/ - ts.emitComments(currentSourceFile, writer, trailingComments, /*trailingSeparator*/ true, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, trailingComments, /*trailingSeparator*/ true, newLine, writeComment); } function emitLeadingCommentsOfPositionWorker(pos) { if (compilerOptions.removeComments) { @@ -36783,14 +37263,14 @@ var ts; } else { // get the leading comments from the node - leadingComments = ts.getLeadingCommentRanges(currentSourceFile.text, pos); + leadingComments = ts.getLeadingCommentRanges(currentText, pos); } - ts.emitNewLineBeforeLeadingComments(currentSourceFile, writer, { pos: pos, end: pos }, leadingComments); + ts.emitNewLineBeforeLeadingComments(currentLineMap, writer, { pos: pos, end: pos }, leadingComments); // Leading comments are emitted at /*leading comment1 */space/*leading comment*/space - ts.emitComments(currentSourceFile, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment); + ts.emitComments(currentText, currentLineMap, writer, leadingComments, /*trailingSeparator*/ true, newLine, writeComment); } function emitDetachedCommentsAndUpdateCommentsInfo(node) { - var currentDetachedCommentInfo = ts.emitDetachedComments(currentSourceFile, writer, writeComment, node, newLine, compilerOptions.removeComments); + var currentDetachedCommentInfo = ts.emitDetachedComments(currentText, currentLineMap, writer, writeComment, node, newLine, compilerOptions.removeComments); if (currentDetachedCommentInfo) { if (detachedCommentsInfo) { detachedCommentsInfo.push(currentDetachedCommentInfo); @@ -36801,12 +37281,12 @@ var ts; } } function emitShebang() { - var shebang = ts.getShebang(currentSourceFile.text); + var shebang = ts.getShebang(currentText); if (shebang) { write(shebang); } } - var _a; + var _a, _b; } function emitFile(jsFilePath, sourceFile) { emitJavaScript(jsFilePath, sourceFile); @@ -36866,11 +37346,11 @@ var ts; if (ts.getRootLength(moduleName) !== 0 || nameStartsWithDotSlashOrDotDotSlash(moduleName)) { var failedLookupLocations = []; var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var resolvedFileName = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var resolvedFileName = loadNodeModuleFromFile(ts.supportedJsExtensions, candidate, failedLookupLocations, host); if (resolvedFileName) { return { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations }; } - resolvedFileName = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + resolvedFileName = loadNodeModuleFromDirectory(ts.supportedJsExtensions, candidate, failedLookupLocations, host); return resolvedFileName ? { resolvedModule: { resolvedFileName: resolvedFileName }, failedLookupLocations: failedLookupLocations } : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; @@ -36880,8 +37360,8 @@ var ts; } } ts.nodeModuleNameResolver = nodeModuleNameResolver; - function loadNodeModuleFromFile(candidate, failedLookupLocation, host) { - return ts.forEach(ts.moduleFileExtensions, tryLoad); + function loadNodeModuleFromFile(extensions, candidate, failedLookupLocation, host) { + return ts.forEach(extensions, tryLoad); function tryLoad(ext) { var fileName = ts.fileExtensionIs(candidate, ext) ? candidate : candidate + ext; if (host.fileExists(fileName)) { @@ -36893,7 +37373,7 @@ var ts; } } } - function loadNodeModuleFromDirectory(candidate, failedLookupLocation, host) { + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, host) { var packageJsonPath = ts.combinePaths(candidate, "package.json"); if (host.fileExists(packageJsonPath)) { var jsonContent; @@ -36906,7 +37386,7 @@ var ts; jsonContent = { typings: undefined }; } if (jsonContent.typings) { - var result = loadNodeModuleFromFile(ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); + var result = loadNodeModuleFromFile(extensions, ts.normalizePath(ts.combinePaths(candidate, jsonContent.typings)), failedLookupLocation, host); if (result) { return result; } @@ -36916,7 +37396,7 @@ var ts; // record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results failedLookupLocation.push(packageJsonPath); } - return loadNodeModuleFromFile(ts.combinePaths(candidate, "index"), failedLookupLocation, host); + return loadNodeModuleFromFile(extensions, ts.combinePaths(candidate, "index"), failedLookupLocation, host); } function loadModuleFromNodeModules(moduleName, directory, host) { var failedLookupLocations = []; @@ -36926,11 +37406,11 @@ var ts; if (baseName !== "node_modules") { var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var result = loadNodeModuleFromFile(candidate, failedLookupLocations, host); + var result = loadNodeModuleFromFile(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } - result = loadNodeModuleFromDirectory(candidate, failedLookupLocations, host); + result = loadNodeModuleFromDirectory(ts.supportedExtensions, candidate, failedLookupLocations, host); if (result) { return { resolvedModule: { resolvedFileName: result, isExternalLibraryImport: true }, failedLookupLocations: failedLookupLocations }; } @@ -36956,9 +37436,10 @@ var ts; var searchName; var failedLookupLocations = []; var referencedSourceFile; + var extensions = compilerOptions.allowNonTsExtensions ? ts.supportedJsExtensions : ts.supportedExtensions; while (true) { searchName = ts.normalizePath(ts.combinePaths(searchPath, moduleName)); - referencedSourceFile = ts.forEach(ts.supportedExtensions, function (extension) { + referencedSourceFile = ts.forEach(extensions, function (extension) { if (extension === ".tsx" && !compilerOptions.jsx) { // resolve .tsx files only if jsx support is enabled // 'logical not' handles both undefined and None cases @@ -36989,10 +37470,8 @@ var ts; /* @internal */ ts.defaultInitCompilerOptions = { module: 1 /* CommonJS */, - target: 0 /* ES3 */, + target: 1 /* ES5 */, noImplicitAny: false, - outDir: "built", - rootDir: ".", sourceMap: false }; function createCompilerHost(options, setParentNodes) { @@ -37397,43 +37876,55 @@ var ts; if (file.imports) { return; } + var isJavaScriptFile = ts.isSourceFileJavaScript(file); var imports; for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { var node = _a[_i]; - collect(node, /* allowRelativeModuleNames */ true); + collect(node, /* allowRelativeModuleNames */ true, /* collectOnlyRequireCalls */ false); } file.imports = imports || emptyArray; - function collect(node, allowRelativeModuleNames) { - switch (node.kind) { - case 222 /* ImportDeclaration */: - case 221 /* ImportEqualsDeclaration */: - case 228 /* ExportDeclaration */: - var moduleNameExpr = ts.getExternalModuleName(node); - if (!moduleNameExpr || moduleNameExpr.kind !== 9 /* StringLiteral */) { + return; + function collect(node, allowRelativeModuleNames, collectOnlyRequireCalls) { + if (!collectOnlyRequireCalls) { + switch (node.kind) { + case 222 /* ImportDeclaration */: + case 221 /* ImportEqualsDeclaration */: + case 228 /* ExportDeclaration */: + var moduleNameExpr = ts.getExternalModuleName(node); + if (!moduleNameExpr || moduleNameExpr.kind !== 9 /* StringLiteral */) { + break; + } + if (!moduleNameExpr.text) { + break; + } + if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { + (imports || (imports = [])).push(moduleNameExpr); + } break; - } - if (!moduleNameExpr.text) { - break; - } - if (allowRelativeModuleNames || !ts.isExternalModuleNameRelative(moduleNameExpr.text)) { - (imports || (imports = [])).push(moduleNameExpr); - } - break; - case 218 /* ModuleDeclaration */: - if (node.name.kind === 9 /* StringLiteral */ && (node.flags & 4 /* Ambient */ || ts.isDeclarationFile(file))) { - // TypeScript 1.0 spec (April 2014): 12.1.6 - // An AmbientExternalModuleDeclaration declares an external module. - // This type of declaration is permitted only in the global module. - // The StringLiteral must specify a top - level external module name. - // Relative external module names are not permitted - ts.forEachChild(node.body, function (node) { + case 218 /* ModuleDeclaration */: + if (node.name.kind === 9 /* StringLiteral */ && (node.flags & 4 /* Ambient */ || ts.isDeclarationFile(file))) { // TypeScript 1.0 spec (April 2014): 12.1.6 - // An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules - // only through top - level external module names. Relative external module names are not permitted. - collect(node, /* allowRelativeModuleNames */ false); - }); - } - break; + // An AmbientExternalModuleDeclaration declares an external module. + // This type of declaration is permitted only in the global module. + // The StringLiteral must specify a top - level external module name. + // Relative external module names are not permitted + ts.forEachChild(node.body, function (node) { + // TypeScript 1.0 spec (April 2014): 12.1.6 + // An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules + // only through top - level external module names. Relative external module names are not permitted. + collect(node, /* allowRelativeModuleNames */ false, collectOnlyRequireCalls); + }); + } + break; + } + } + if (isJavaScriptFile) { + if (ts.isRequireCall(node)) { + (imports || (imports = [])).push(node.arguments[0]); + } + else { + ts.forEachChild(node, function (node) { return collect(node, allowRelativeModuleNames, /* collectOnlyRequireCalls */ true); }); + } } } } @@ -37526,7 +38017,6 @@ var ts; // always process imported modules to record module name resolutions processImportedModules(file, basePath); if (isDefaultLib) { - file.isDefaultLib = true; files.unshift(file); } else { @@ -37604,6 +38094,9 @@ var ts; commonPathComponents.length = sourcePathComponents.length; } }); + if (!commonPathComponents) { + return currentDirectory; + } return ts.getNormalizedPathFromPathComponents(commonPathComponents); } function checkSourceFilesBelongToPath(sourceFiles, rootDirectory) { @@ -37689,12 +38182,15 @@ var ts; if (options.module === 5 /* ES6 */ && languageVersion < 2 /* ES6 */) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Cannot_compile_modules_into_es2015_when_targeting_ES5_or_lower)); } + // Cannot specify module gen that isn't amd or system with --out + if (outFile && options.module && !(options.module === 2 /* AMD */ || options.module === 4 /* System */)) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile")); + } // there has to be common source directory if user specified --outdir || --sourceRoot // if user specified --mapRoot, there needs to be common source directory if there would be multiple files being emitted if (options.outDir || options.sourceRoot || - (options.mapRoot && - (!outFile || firstExternalModuleSourceFile !== undefined))) { + options.mapRoot) { if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { // If a rootDir is specified and is valid use it as the commonSourceDirectory commonSourceDirectory = ts.getNormalizedAbsolutePath(options.rootDir, currentDirectory); @@ -38212,20 +38708,20 @@ var ts; var exclude = json["exclude"] instanceof Array ? ts.map(json["exclude"], ts.normalizeSlashes) : undefined; var sysFiles = host.readDirectory(basePath, ".ts", exclude).concat(host.readDirectory(basePath, ".tsx", exclude)); for (var i = 0; i < sysFiles.length; i++) { - var name_29 = sysFiles[i]; - if (ts.fileExtensionIs(name_29, ".d.ts")) { - var baseName = name_29.substr(0, name_29.length - ".d.ts".length); + var name_31 = sysFiles[i]; + if (ts.fileExtensionIs(name_31, ".d.ts")) { + var baseName = name_31.substr(0, name_31.length - ".d.ts".length); if (!ts.contains(sysFiles, baseName + ".tsx") && !ts.contains(sysFiles, baseName + ".ts")) { - fileNames.push(name_29); + fileNames.push(name_31); } } - else if (ts.fileExtensionIs(name_29, ".ts")) { - if (!ts.contains(sysFiles, name_29 + "x")) { - fileNames.push(name_29); + else if (ts.fileExtensionIs(name_31, ".ts")) { + if (!ts.contains(sysFiles, name_31 + "x")) { + fileNames.push(name_31); } } else { - fileNames.push(name_29); + fileNames.push(name_31); } } } @@ -38453,12 +38949,12 @@ var ts; ts.forEach(program.getSourceFiles(), function (sourceFile) { cancellationToken.throwIfCancellationRequested(); var nameToDeclarations = sourceFile.getNamedDeclarations(); - for (var name_30 in nameToDeclarations) { - var declarations = ts.getProperty(nameToDeclarations, name_30); + for (var name_32 in nameToDeclarations) { + var declarations = ts.getProperty(nameToDeclarations, name_32); if (declarations) { // First do a quick check to see if the name of the declaration matches the // last portion of the (possibly) dotted name they're searching for. - var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_30); + var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_32); if (!matches) { continue; } @@ -38471,14 +38967,14 @@ var ts; if (!containers) { return undefined; } - matches = patternMatcher.getMatches(containers, name_30); + matches = patternMatcher.getMatches(containers, name_32); if (!matches) { continue; } } var fileName = sourceFile.fileName; var matchKind = bestMatchKind(matches); - rawItems.push({ name: name_30, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); + rawItems.push({ name: name_32, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); } } } @@ -38859,9 +39355,9 @@ var ts; case 211 /* VariableDeclaration */: case 163 /* BindingElement */: var variableDeclarationNode; - var name_31; + var name_33; if (node.kind === 163 /* BindingElement */) { - name_31 = node.name; + name_33 = node.name; variableDeclarationNode = node; // binding elements are added only for variable declarations // bubble up to the containing variable declaration @@ -38873,16 +39369,16 @@ var ts; else { ts.Debug.assert(!ts.isBindingPattern(node.name)); variableDeclarationNode = node; - name_31 = node.name; + name_33 = node.name; } if (ts.isConst(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.constElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.constElement); } else if (ts.isLet(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.letElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.letElement); } else { - return createItem(node, getTextOfNode(name_31), ts.ScriptElementKind.variableElement); + return createItem(node, getTextOfNode(name_33), ts.ScriptElementKind.variableElement); } case 144 /* Constructor */: return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); @@ -39802,7 +40298,7 @@ var ts; if (!candidates.length) { // We didn't have any sig help items produced by the TS compiler. If this is a JS // file, then see if we can figure out anything better. - if (ts.isJavaScript(sourceFile.fileName)) { + if (ts.isSourceFileJavaScript(sourceFile)) { return createJavaScriptSignatureHelpItems(argumentInfo); } return undefined; @@ -41662,9 +42158,9 @@ var ts; } Rules.prototype.getRuleName = function (rule) { var o = this; - for (var name_32 in o) { - if (o[name_32] === rule) { - return name_32; + for (var name_34 in o) { + if (o[name_34] === rule) { + return name_34; } } throw new Error("Unknown rule"); @@ -42096,7 +42592,7 @@ var ts; function TokenRangeAccess(from, to, except) { this.tokens = []; for (var token = from; token <= to; token++) { - if (except.indexOf(token) < 0) { + if (ts.indexOf(except, token) < 0) { this.tokens.push(token); } } @@ -43709,13 +44205,18 @@ var ts; ]; var jsDocCompletionEntries; function createNode(kind, pos, end, flags, parent) { - var node = new (ts.getNodeConstructor(kind))(pos, end); + var node = new NodeObject(kind, pos, end); node.flags = flags; node.parent = parent; return node; } var NodeObject = (function () { - function NodeObject() { + function NodeObject(kind, pos, end) { + this.kind = kind; + this.pos = pos; + this.end = end; + this.flags = 0 /* None */; + this.parent = undefined; } NodeObject.prototype.getSourceFile = function () { return ts.getSourceFileOfNode(this); @@ -44198,8 +44699,8 @@ var ts; })(); var SourceFileObject = (function (_super) { __extends(SourceFileObject, _super); - function SourceFileObject() { - _super.apply(this, arguments); + function SourceFileObject(kind, pos, end) { + _super.call(this, kind, pos, end); } SourceFileObject.prototype.update = function (newText, textChangeRange) { return ts.updateSourceFile(this, newText, textChangeRange); @@ -44521,6 +45022,9 @@ var ts; ClassificationTypeNames.typeAliasName = "type alias name"; ClassificationTypeNames.parameterName = "parameter name"; ClassificationTypeNames.docCommentTagName = "doc comment tag name"; + ClassificationTypeNames.jsxOpenTagName = "jsx open tag name"; + ClassificationTypeNames.jsxCloseTagName = "jsx close tag name"; + ClassificationTypeNames.jsxSelfClosingTagName = "jsx self closing tag name"; return ClassificationTypeNames; })(); ts.ClassificationTypeNames = ClassificationTypeNames; @@ -44543,6 +45047,9 @@ var ts; ClassificationType[ClassificationType["typeAliasName"] = 16] = "typeAliasName"; ClassificationType[ClassificationType["parameterName"] = 17] = "parameterName"; ClassificationType[ClassificationType["docCommentTagName"] = 18] = "docCommentTagName"; + ClassificationType[ClassificationType["jsxOpenTagName"] = 19] = "jsxOpenTagName"; + ClassificationType[ClassificationType["jsxCloseTagName"] = 20] = "jsxCloseTagName"; + ClassificationType[ClassificationType["jsxSelfClosingTagName"] = 21] = "jsxSelfClosingTagName"; })(ts.ClassificationType || (ts.ClassificationType = {})); var ClassificationType = ts.ClassificationType; function displayPartsToString(displayParts) { @@ -44922,8 +45429,9 @@ var ts; }; } ts.createDocumentRegistry = createDocumentRegistry; - function preProcessFile(sourceText, readImportFiles) { + function preProcessFile(sourceText, readImportFiles, detectJavaScriptImports) { if (readImportFiles === void 0) { readImportFiles = true; } + if (detectJavaScriptImports === void 0) { detectJavaScriptImports = false; } var referencedFiles = []; var importedFiles = []; var ambientExternalModules; @@ -44957,9 +45465,207 @@ var ts; end: pos + importPath.length }); } - function processImport() { + /** + * Returns true if at least one token was consumed from the stream + */ + function tryConsumeDeclare() { + var token = scanner.getToken(); + if (token === 122 /* DeclareKeyword */) { + // declare module "mod" + token = scanner.scan(); + if (token === 125 /* ModuleKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + recordAmbientExternalModule(); + } + } + return true; + } + return false; + } + /** + * Returns true if at least one token was consumed from the stream + */ + function tryConsumeImport() { + var token = scanner.getToken(); + if (token === 89 /* ImportKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import "mod"; + recordModuleName(); + return true; + } + else { + if (token === 69 /* Identifier */ || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import d from "mod"; + recordModuleName(); + return true; + } + } + else if (token === 56 /* EqualsToken */) { + if (tryConsumeRequireCall(/* skipCurrentToken */ true)) { + return true; + } + } + else if (token === 24 /* CommaToken */) { + // consume comma and keep going + token = scanner.scan(); + } + else { + // unknown syntax + return true; + } + } + if (token === 15 /* OpenBraceToken */) { + token = scanner.scan(); + // consume "{ a as B, c, d as D}" clauses + // make sure that it stops on EOF + while (token !== 16 /* CloseBraceToken */ && token !== 1 /* EndOfFileToken */) { + token = scanner.scan(); + } + if (token === 16 /* CloseBraceToken */) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import {a as A} from "mod"; + // import d, {a, b as B} from "mod" + recordModuleName(); + } + } + } + } + else if (token === 37 /* AsteriskToken */) { + token = scanner.scan(); + if (token === 116 /* AsKeyword */) { + token = scanner.scan(); + if (token === 69 /* Identifier */ || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // import * as NS from "mod" + // import d, * as NS from "mod" + recordModuleName(); + } + } + } + } + } + } + return true; + } + return false; + } + function tryConsumeExport() { + var token = scanner.getToken(); + if (token === 82 /* ExportKeyword */) { + token = scanner.scan(); + if (token === 15 /* OpenBraceToken */) { + token = scanner.scan(); + // consume "{ a as B, c, d as D}" clauses + // make sure it stops on EOF + while (token !== 16 /* CloseBraceToken */ && token !== 1 /* EndOfFileToken */) { + token = scanner.scan(); + } + if (token === 16 /* CloseBraceToken */) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // export {a as A} from "mod"; + // export {a, b as B} from "mod" + recordModuleName(); + } + } + } + } + else if (token === 37 /* AsteriskToken */) { + token = scanner.scan(); + if (token === 133 /* FromKeyword */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // export * from "mod" + recordModuleName(); + } + } + } + else if (token === 89 /* ImportKeyword */) { + token = scanner.scan(); + if (token === 69 /* Identifier */ || ts.isKeyword(token)) { + token = scanner.scan(); + if (token === 56 /* EqualsToken */) { + if (tryConsumeRequireCall(/* skipCurrentToken */ true)) { + return true; + } + } + } + } + return true; + } + return false; + } + function tryConsumeRequireCall(skipCurrentToken) { + var token = skipCurrentToken ? scanner.scan() : scanner.getToken(); + if (token === 127 /* RequireKeyword */) { + token = scanner.scan(); + if (token === 17 /* OpenParenToken */) { + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // require("mod"); + recordModuleName(); + } + } + return true; + } + return false; + } + function tryConsumeDefine() { + var token = scanner.getToken(); + if (token === 69 /* Identifier */ && scanner.getTokenValue() === "define") { + token = scanner.scan(); + if (token !== 17 /* OpenParenToken */) { + return true; + } + token = scanner.scan(); + if (token === 9 /* StringLiteral */) { + // looks like define ("modname", ... - skip string literal and comma + token = scanner.scan(); + if (token === 24 /* CommaToken */) { + token = scanner.scan(); + } + else { + // unexpected token + return true; + } + } + // should be start of dependency list + if (token !== 19 /* OpenBracketToken */) { + return true; + } + // skip open bracket + token = scanner.scan(); + var i = 0; + // scan until ']' or EOF + while (token !== 20 /* CloseBracketToken */ && token !== 1 /* EndOfFileToken */) { + // record string literals as module names + if (token === 9 /* StringLiteral */) { + recordModuleName(); + i++; + } + token = scanner.scan(); + } + return true; + } + return false; + } + function processImports() { scanner.setText(sourceText); - var token = scanner.scan(); + scanner.scan(); // Look for: // import "mod"; // import d from "mod" @@ -44971,152 +45677,26 @@ var ts; // export * from "mod" // export {a as b} from "mod" // export import i = require("mod") - while (token !== 1 /* EndOfFileToken */) { - if (token === 122 /* DeclareKeyword */) { - // declare module "mod" - token = scanner.scan(); - if (token === 125 /* ModuleKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - recordAmbientExternalModule(); - continue; - } - } + // (for JavaScript files) require("mod") + while (true) { + if (scanner.getToken() === 1 /* EndOfFileToken */) { + break; } - else if (token === 89 /* ImportKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import "mod"; - recordModuleName(); - continue; - } - else { - if (token === 69 /* Identifier */ || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import d from "mod"; - recordModuleName(); - continue; - } - } - else if (token === 56 /* EqualsToken */) { - token = scanner.scan(); - if (token === 127 /* RequireKeyword */) { - token = scanner.scan(); - if (token === 17 /* OpenParenToken */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import i = require("mod"); - recordModuleName(); - continue; - } - } - } - } - else if (token === 24 /* CommaToken */) { - // consume comma and keep going - token = scanner.scan(); - } - else { - // unknown syntax - continue; - } - } - if (token === 15 /* OpenBraceToken */) { - token = scanner.scan(); - // consume "{ a as B, c, d as D}" clauses - while (token !== 16 /* CloseBraceToken */) { - token = scanner.scan(); - } - if (token === 16 /* CloseBraceToken */) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import {a as A} from "mod"; - // import d, {a, b as B} from "mod" - recordModuleName(); - } - } - } - } - else if (token === 37 /* AsteriskToken */) { - token = scanner.scan(); - if (token === 116 /* AsKeyword */) { - token = scanner.scan(); - if (token === 69 /* Identifier */ || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // import * as NS from "mod" - // import d, * as NS from "mod" - recordModuleName(); - } - } - } - } - } - } + // check if at least one of alternative have moved scanner forward + if (tryConsumeDeclare() || + tryConsumeImport() || + tryConsumeExport() || + (detectJavaScriptImports && (tryConsumeRequireCall(/* skipCurrentToken */ false) || tryConsumeDefine()))) { + continue; } - else if (token === 82 /* ExportKeyword */) { - token = scanner.scan(); - if (token === 15 /* OpenBraceToken */) { - token = scanner.scan(); - // consume "{ a as B, c, d as D}" clauses - while (token !== 16 /* CloseBraceToken */) { - token = scanner.scan(); - } - if (token === 16 /* CloseBraceToken */) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // export {a as A} from "mod"; - // export {a, b as B} from "mod" - recordModuleName(); - } - } - } - } - else if (token === 37 /* AsteriskToken */) { - token = scanner.scan(); - if (token === 133 /* FromKeyword */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // export * from "mod" - recordModuleName(); - } - } - } - else if (token === 89 /* ImportKeyword */) { - token = scanner.scan(); - if (token === 69 /* Identifier */ || ts.isKeyword(token)) { - token = scanner.scan(); - if (token === 56 /* EqualsToken */) { - token = scanner.scan(); - if (token === 127 /* RequireKeyword */) { - token = scanner.scan(); - if (token === 17 /* OpenParenToken */) { - token = scanner.scan(); - if (token === 9 /* StringLiteral */) { - // export import i = require("mod"); - recordModuleName(); - } - } - } - } - } - } + else { + scanner.scan(); } - token = scanner.scan(); } scanner.setText(undefined); } if (readImportFiles) { - processImport(); + processImports(); } processTripleSlashDirectives(); return { referencedFiles: referencedFiles, importedFiles: importedFiles, isLibFile: isNoDefaultLib, ambientExternalModules: ambientExternalModules }; @@ -45547,7 +46127,7 @@ var ts; // For JavaScript files, we don't want to report the normal typescript semantic errors. // Instead, we just report errors for using TypeScript-only constructs from within a // JavaScript file. - if (ts.isJavaScript(fileName)) { + if (ts.isSourceFileJavaScript(targetSourceFile)) { return getJavaScriptSemanticDiagnostics(targetSourceFile); } // Only perform the action per file regardless of '-out' flag as LanguageServiceHost is expected to call this function per file. @@ -45759,7 +46339,7 @@ var ts; var typeChecker = program.getTypeChecker(); var syntacticStart = new Date().getTime(); var sourceFile = getValidSourceFile(fileName); - var isJavaScriptFile = ts.isJavaScript(fileName); + var isJavaScriptFile = ts.isSourceFileJavaScript(sourceFile); var isJsDocTagName = false; var start = new Date().getTime(); var currentToken = ts.getTokenAtPosition(sourceFile, position); @@ -46393,8 +46973,8 @@ var ts; if (element.getStart() <= position && position <= element.getEnd()) { continue; } - var name_33 = element.propertyName || element.name; - exisingImportsOrExports[name_33.text] = true; + var name_35 = element.propertyName || element.name; + exisingImportsOrExports[name_35.text] = true; } if (ts.isEmpty(exisingImportsOrExports)) { return exportsOfModule; @@ -46426,7 +47006,10 @@ var ts; } var existingName = void 0; if (m.kind === 163 /* BindingElement */ && m.propertyName) { - existingName = m.propertyName.text; + // include only identifiers in completion list + if (m.propertyName.kind === 69 /* Identifier */) { + existingName = m.propertyName.text; + } } else { // TODO(jfreeman): Account for computed property name @@ -46466,46 +47049,43 @@ var ts; return undefined; } var symbols = completionData.symbols, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isRightOfDot = completionData.isRightOfDot, isJsDocTagName = completionData.isJsDocTagName; - var entries; if (isJsDocTagName) { // If the current position is a jsDoc tag name, only tag names should be provided for completion return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: getAllJsDocCompletionEntries() }; } - if (isRightOfDot && ts.isJavaScript(fileName)) { - entries = getCompletionEntriesFromSymbols(symbols); - ts.addRange(entries, getJavaScriptCompletionEntries()); + var sourceFile = getValidSourceFile(fileName); + var entries = []; + if (isRightOfDot && ts.isSourceFileJavaScript(sourceFile)) { + var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries); + ts.addRange(entries, getJavaScriptCompletionEntries(sourceFile, uniqueNames)); } else { if (!symbols || symbols.length === 0) { return undefined; } - entries = getCompletionEntriesFromSymbols(symbols); + getCompletionEntriesFromSymbols(symbols, entries); } // Add keywords if this is not a member completion list if (!isMemberCompletion && !isJsDocTagName) { ts.addRange(entries, keywordCompletions); } return { isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries: entries }; - function getJavaScriptCompletionEntries() { + function getJavaScriptCompletionEntries(sourceFile, uniqueNames) { var entries = []; - var allNames = {}; var target = program.getCompilerOptions().target; - for (var _i = 0, _a = program.getSourceFiles(); _i < _a.length; _i++) { - var sourceFile = _a[_i]; - var nameTable = getNameTable(sourceFile); - for (var name_34 in nameTable) { - if (!allNames[name_34]) { - allNames[name_34] = name_34; - var displayName = getCompletionEntryDisplayName(name_34, target, /*performCharacterChecks:*/ true); - if (displayName) { - var entry = { - name: displayName, - kind: ScriptElementKind.warning, - kindModifiers: "", - sortText: "1" - }; - entries.push(entry); - } + var nameTable = getNameTable(sourceFile); + for (var name_36 in nameTable) { + if (!uniqueNames[name_36]) { + uniqueNames[name_36] = name_36; + var displayName = getCompletionEntryDisplayName(name_36, target, /*performCharacterChecks:*/ true); + if (displayName) { + var entry = { + name: displayName, + kind: ScriptElementKind.warning, + kindModifiers: "", + sortText: "1" + }; + entries.push(entry); } } } @@ -46543,25 +47123,24 @@ var ts; sortText: "0" }; } - function getCompletionEntriesFromSymbols(symbols) { + function getCompletionEntriesFromSymbols(symbols, entries) { var start = new Date().getTime(); - var entries = []; + var uniqueNames = {}; if (symbols) { - var nameToSymbol = {}; for (var _i = 0, symbols_3 = symbols; _i < symbols_3.length; _i++) { var symbol = symbols_3[_i]; var entry = createCompletionEntry(symbol, location); if (entry) { var id = ts.escapeIdentifier(entry.name); - if (!ts.lookUp(nameToSymbol, id)) { + if (!ts.lookUp(uniqueNames, id)) { entries.push(entry); - nameToSymbol[id] = symbol; + uniqueNames[id] = id; } } } } log("getCompletionsAtPosition: getCompletionEntriesFromSymbols: " + (new Date().getTime() - start)); - return entries; + return uniqueNames; } } function getCompletionEntryDetails(fileName, position, entryName) { @@ -46762,16 +47341,16 @@ var ts; case ScriptElementKind.parameterElement: case ScriptElementKind.localVariableElement: // If it is call or construct signature of lambda's write type name - displayParts.push(ts.punctuationPart(54 /* ColonToken */)); + displayParts.push(ts.punctuationPart(ts.SyntaxKind.ColonToken)); displayParts.push(ts.spacePart()); if (useConstructSignatures) { - displayParts.push(ts.keywordPart(92 /* NewKeyword */)); + displayParts.push(ts.keywordPart(ts.SyntaxKind.NewKeyword)); displayParts.push(ts.spacePart()); } - if (!(type.flags & 65536 /* Anonymous */)) { - ts.addRange(displayParts, ts.symbolToDisplayParts(typeChecker, type.symbol, enclosingDeclaration, /*meaning*/ undefined, 1 /* WriteTypeParametersOrArguments */)); + if (!(type.flags & ts.TypeFlags.Anonymous)) { + ts.addRange(displayParts, ts.symbolToDisplayParts(typeChecker, type.symbol, enclosingDeclaration, /*meaning*/ undefined, ts.SymbolFormatFlags.WriteTypeParametersOrArguments)); } - addSignatureDisplayParts(signature, allSignatures, 8 /* WriteArrowStyleSignature */); + addSignatureDisplayParts(signature, allSignatures, ts.TypeFormatFlags.WriteArrowStyleSignature); break; default: // Just signature @@ -48396,19 +48975,19 @@ var ts; if (isNameOfPropertyAssignment(node)) { var objectLiteral = node.parent.parent; var contextualType = typeChecker.getContextualType(objectLiteral); - var name_35 = node.text; + var name_37 = node.text; if (contextualType) { if (contextualType.flags & 16384 /* Union */) { // This is a union type, first see if the property we are looking for is a union property (i.e. exists in all types) // if not, search the constituent types for the property - var unionProperty = contextualType.getProperty(name_35); + var unionProperty = contextualType.getProperty(name_37); if (unionProperty) { return [unionProperty]; } else { var result_4 = []; ts.forEach(contextualType.types, function (t) { - var symbol = t.getProperty(name_35); + var symbol = t.getProperty(name_37); if (symbol) { result_4.push(symbol); } @@ -48417,7 +48996,7 @@ var ts; } } else { - var symbol_1 = contextualType.getProperty(name_35); + var symbol_1 = contextualType.getProperty(name_37); if (symbol_1) { return [symbol_1]; } @@ -48828,6 +49407,9 @@ var ts; case 16 /* typeAliasName */: return ClassificationTypeNames.typeAliasName; case 17 /* parameterName */: return ClassificationTypeNames.parameterName; case 18 /* docCommentTagName */: return ClassificationTypeNames.docCommentTagName; + case 19 /* jsxOpenTagName */: return ClassificationTypeNames.jsxOpenTagName; + case 20 /* jsxCloseTagName */: return ClassificationTypeNames.jsxCloseTagName; + case 21 /* jsxSelfClosingTagName */: return ClassificationTypeNames.jsxSelfClosingTagName; } } function convertClassifications(classifications) { @@ -49097,6 +49679,21 @@ var ts; return 17 /* parameterName */; } return; + case 235 /* JsxOpeningElement */: + if (token.parent.tagName === token) { + return 19 /* jsxOpenTagName */; + } + return; + case 237 /* JsxClosingElement */: + if (token.parent.tagName === token) { + return 20 /* jsxCloseTagName */; + } + return; + case 234 /* JsxSelfClosingElement */: + if (token.parent.tagName === token) { + return 21 /* jsxSelfClosingTagName */; + } + return; } } return 2 /* identifier */; @@ -50021,18 +50618,8 @@ var ts; ts.getDefaultLibFilePath = getDefaultLibFilePath; function initializeServices() { ts.objectAllocator = { - getNodeConstructor: function (kind) { - function Node(pos, end) { - this.pos = pos; - this.end = end; - this.flags = 0 /* None */; - this.parent = undefined; - } - var proto = kind === 248 /* SourceFile */ ? new SourceFileObject() : new NodeObject(); - proto.kind = kind; - Node.prototype = proto; - return Node; - }, + getNodeConstructor: function () { return NodeObject; }, + getSourceFileConstructor: function () { return SourceFileObject; }, getSymbolConstructor: function () { return SymbolObject; }, getTypeConstructor: function () { return TypeObject; }, getSignatureConstructor: function () { return SignatureObject; } @@ -51102,7 +51689,8 @@ var ts; }; CoreServicesShimObject.prototype.getPreProcessedFileInfo = function (fileName, sourceTextSnapshot) { return this.forwardJSONCall("getPreProcessedFileInfo('" + fileName + "')", function () { - var result = ts.preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength())); + // for now treat files as JavaScript + var result = ts.preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), /* readImportFiles */ true, /* detectJavaScriptImports */ true); var convertResult = { referencedFiles: [], importedFiles: [], @@ -51166,7 +51754,7 @@ var ts; TypeScriptServicesFactory.prototype.createLanguageServiceShim = function (host) { try { if (this.documentRegistry === undefined) { - this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames()); + this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(), host.getCurrentDirectory()); } var hostAdapter = new LanguageServiceShimHostAdapter(host); var languageService = ts.createLanguageService(hostAdapter, this.documentRegistry); @@ -51199,7 +51787,7 @@ var ts; TypeScriptServicesFactory.prototype.close = function () { // Forget all the registered shims this._shims = []; - this.documentRegistry = ts.createDocumentRegistry(); + this.documentRegistry = undefined; }; TypeScriptServicesFactory.prototype.registerShim = function (shim) { this._shims.push(shim);