Skip to content

Commit 2d13345

Browse files
author
Yui T
committed
Merge branch 'master' into fix4715
2 parents 62fb5e8 + 2f282a7 commit 2d13345

1,570 files changed

Lines changed: 19630 additions & 16347 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Jakefile.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ var serverCoreSources = [
106106
return path.join(serverDirectory, f);
107107
});
108108

109+
var scriptSources = [
110+
"tslint/booleanTriviaRule.ts",
111+
"tslint/nextLineRule.ts",
112+
"tslint/noNullRule.ts",
113+
"tslint/preferConstRule.ts",
114+
"tslint/typeOperatorSpacingRule.ts"
115+
].map(function (f) {
116+
return path.join(scriptsDirectory, f);
117+
});
118+
109119
var serverSources = serverCoreSources.concat(servicesSources);
110120

111121
var languageServiceLibrarySources = [
@@ -365,7 +375,6 @@ file(builtGeneratedDiagnosticMessagesJSON,[generatedDiagnosticMessagesJSON], fun
365375
desc("Generates a diagnostic file in TypeScript based on an input JSON file");
366376
task("generate-diagnostics", [diagnosticInfoMapTs]);
367377

368-
369378
// Publish nightly
370379
var configureNightlyJs = path.join(scriptsDirectory, "configureNightly.js");
371380
var configureNightlyTs = path.join(scriptsDirectory, "configureNightly.ts");
@@ -909,7 +918,8 @@ function lintFileAsync(options, path, cb) {
909918

910919
var lintTargets = compilerSources
911920
.concat(harnessCoreSources)
912-
.concat(serverCoreSources);
921+
.concat(serverCoreSources)
922+
.concat(scriptSources);
913923

914924
desc("Runs tslint on the compiler sources");
915925
task("lint", ["build-rules"], function() {

scripts/tslint/typeOperatorSpacingRule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export class Rule extends Lint.Rules.AbstractRule {
1313
class TypeOperatorSpacingWalker extends Lint.RuleWalker {
1414
public visitNode(node: ts.Node) {
1515
if (node.kind === ts.SyntaxKind.UnionType || node.kind === ts.SyntaxKind.IntersectionType) {
16-
let types = (<ts.UnionOrIntersectionTypeNode>node).types;
16+
const types = (<ts.UnionOrIntersectionTypeNode>node).types;
1717
let expectedStart = types[0].end + 2; // space, | or &
1818
for (let i = 1; i < types.length; i++) {
19-
let currentType = types[i];
19+
const currentType = types[i];
2020
if (expectedStart !== currentType.pos || currentType.getLeadingTriviaWidth() !== 1) {
2121
const failure = this.createFailure(currentType.pos, currentType.getWidth(), Rule.FAILURE_STRING);
2222
this.addFailure(failure);

src/compiler/checker.ts

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace ts {
4646
const compilerOptions = host.getCompilerOptions();
4747
const languageVersion = compilerOptions.target || ScriptTarget.ES3;
4848
const modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None;
49+
const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
4950

5051
const emitResolver = createResolver();
5152

@@ -532,7 +533,7 @@ namespace ts {
532533
}
533534

534535
// Because of module/namespace merging, a module's exports are in scope,
535-
// yet we never want to treat an export specifier as putting a member in scope.
536+
// yet we never want to treat an export specifier as putting a member in scope.
536537
// Therefore, if the name we find is purely an export specifier, it is not actually considered in scope.
537538
// Two things to note about this:
538539
// 1. We have to check this without calling getSymbol. The problem with calling getSymbol
@@ -768,9 +769,12 @@ namespace ts {
768769
const moduleSymbol = resolveExternalModuleName(node, (<ImportDeclaration>node.parent).moduleSpecifier);
769770
if (moduleSymbol) {
770771
const exportDefaultSymbol = resolveSymbol(moduleSymbol.exports["default"]);
771-
if (!exportDefaultSymbol) {
772+
if (!exportDefaultSymbol && !allowSyntheticDefaultImports) {
772773
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
773774
}
775+
else if (!exportDefaultSymbol && allowSyntheticDefaultImports) {
776+
return resolveSymbol(moduleSymbol.exports["export="]) || resolveSymbol(moduleSymbol);
777+
}
774778
return exportDefaultSymbol;
775779
}
776780
}
@@ -3197,7 +3201,7 @@ namespace ts {
31973201
case SyntaxKind.BooleanKeyword:
31983202
case SyntaxKind.SymbolKeyword:
31993203
case SyntaxKind.VoidKeyword:
3200-
case SyntaxKind.StringLiteral:
3204+
case SyntaxKind.StringLiteralType:
32013205
return true;
32023206
case SyntaxKind.ArrayType:
32033207
return isIndependentType((<ArrayTypeNode>node).elementType);
@@ -3858,7 +3862,7 @@ namespace ts {
38583862
paramSymbol = resolvedSymbol;
38593863
}
38603864
parameters.push(paramSymbol);
3861-
if (param.type && param.type.kind === SyntaxKind.StringLiteral) {
3865+
if (param.type && param.type.kind === SyntaxKind.StringLiteralType) {
38623866
hasStringLiterals = true;
38633867
}
38643868

@@ -4527,8 +4531,7 @@ namespace ts {
45274531
return links.resolvedType;
45284532
}
45294533

4530-
function getStringLiteralType(node: StringLiteral): StringLiteralType {
4531-
const text = node.text;
4534+
function getStringLiteralTypeForText(text: string): StringLiteralType {
45324535
if (hasProperty(stringLiteralTypes, text)) {
45334536
return stringLiteralTypes[text];
45344537
}
@@ -4538,10 +4541,10 @@ namespace ts {
45384541
return type;
45394542
}
45404543

4541-
function getTypeFromStringLiteral(node: StringLiteral): Type {
4544+
function getTypeFromStringLiteralTypeNode(node: StringLiteralTypeNode): Type {
45424545
const links = getNodeLinks(node);
45434546
if (!links.resolvedType) {
4544-
links.resolvedType = getStringLiteralType(node);
4547+
links.resolvedType = getStringLiteralTypeForText(node.text);
45454548
}
45464549
return links.resolvedType;
45474550
}
@@ -4583,8 +4586,8 @@ namespace ts {
45834586
return voidType;
45844587
case SyntaxKind.ThisType:
45854588
return getTypeFromThisTypeNode(node);
4586-
case SyntaxKind.StringLiteral:
4587-
return getTypeFromStringLiteral(<StringLiteral>node);
4589+
case SyntaxKind.StringLiteralType:
4590+
return getTypeFromStringLiteralTypeNode(<StringLiteralTypeNode>node);
45884591
case SyntaxKind.TypeReference:
45894592
return getTypeFromTypeReference(<TypeReferenceNode>node);
45904593
case SyntaxKind.TypePredicate:
@@ -8791,7 +8794,7 @@ namespace ts {
87918794
// for the argument. In that case, we should check the argument.
87928795
if (argType === undefined) {
87938796
argType = arg.kind === SyntaxKind.StringLiteral && !reportErrors
8794-
? getStringLiteralType(<StringLiteral>arg)
8797+
? getStringLiteralTypeForText((<StringLiteral>arg).text)
87958798
: checkExpressionWithContextualType(arg, paramType, excludeArgument && excludeArgument[i] ? identityMapper : undefined);
87968799
}
87978800

@@ -8986,7 +8989,7 @@ namespace ts {
89868989
case SyntaxKind.Identifier:
89878990
case SyntaxKind.NumericLiteral:
89888991
case SyntaxKind.StringLiteral:
8989-
return getStringLiteralType(<StringLiteral>element.name);
8992+
return getStringLiteralTypeForText((<Identifier | LiteralExpression>element.name).text);
89908993

89918994
case SyntaxKind.ComputedPropertyName:
89928995
const nameType = checkComputedPropertyName(<ComputedPropertyName>element.name);
@@ -9855,17 +9858,25 @@ namespace ts {
98559858
return aggregatedTypes;
98569859
}
98579860

9858-
// TypeScript Specification 1.0 (6.3) - July 2014
9859-
// An explicitly typed function whose return type isn't the Void or the Any type
9860-
// must have at least one return statement somewhere in its body.
9861-
// An exception to this rule is if the function implementation consists of a single 'throw' statement.
9861+
/*
9862+
*TypeScript Specification 1.0 (6.3) - July 2014
9863+
* An explicitly typed function whose return type isn't the Void or the Any type
9864+
* must have at least one return statement somewhere in its body.
9865+
* An exception to this rule is if the function implementation consists of a single 'throw' statement.
9866+
* @param returnType - return type of the function, can be undefined if return type is not explicitly specified
9867+
*/
98629868
function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func: FunctionLikeDeclaration, returnType: Type): void {
98639869
if (!produceDiagnostics) {
98649870
return;
98659871
}
98669872

9867-
// Functions that return 'void' or 'any' don't need any return expressions.
9868-
if (returnType === voidType || isTypeAny(returnType)) {
9873+
// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
9874+
if (returnType && (returnType === voidType || isTypeAny(returnType))) {
9875+
return;
9876+
}
9877+
9878+
// if return type is not specified then we'll do the check only if 'noImplicitReturns' option is set
9879+
if (!returnType && !compilerOptions.noImplicitReturns) {
98699880
return;
98709881
}
98719882

@@ -9875,13 +9886,14 @@ namespace ts {
98759886
return;
98769887
}
98779888

9878-
if (func.flags & NodeFlags.HasExplicitReturn) {
9889+
if (!returnType || func.flags & NodeFlags.HasExplicitReturn) {
98799890
if (compilerOptions.noImplicitReturns) {
9880-
error(func.type, Diagnostics.Not_all_code_paths_return_a_value);
9891+
error(func.type || func, Diagnostics.Not_all_code_paths_return_a_value);
98819892
}
98829893
}
98839894
else {
98849895
// This function does not conform to the specification.
9896+
// NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present
98859897
error(func.type, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
98869898
}
98879899
}
@@ -9956,14 +9968,10 @@ namespace ts {
99569968
emitAwaiter = true;
99579969
}
99589970

9959-
const returnType = node.type && getTypeFromTypeNode(node.type);
9960-
let promisedType: Type;
9961-
if (returnType && isAsync) {
9962-
promisedType = checkAsyncFunctionReturnType(node);
9963-
}
9964-
9965-
if (returnType && !node.asteriskToken) {
9966-
checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, isAsync ? promisedType : returnType);
9971+
const returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type));
9972+
if (!node.asteriskToken) {
9973+
// return is not necessary in the body of generators
9974+
checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType);
99679975
}
99689976

99699977
if (node.body) {
@@ -9986,13 +9994,13 @@ namespace ts {
99869994
// check assignability of the awaited type of the expression body against the promised type of
99879995
// its return type annotation.
99889996
const exprType = checkExpression(<Expression>node.body);
9989-
if (returnType) {
9997+
if (returnOrPromisedType) {
99909998
if (isAsync) {
99919999
const awaitedType = checkAwaitedType(exprType, node.body, Diagnostics.Expression_body_for_async_arrow_function_does_not_have_a_valid_callable_then_member);
9992-
checkTypeAssignableTo(awaitedType, promisedType, node.body);
10000+
checkTypeAssignableTo(awaitedType, returnOrPromisedType, node.body);
999310001
}
999410002
else {
9995-
checkTypeAssignableTo(exprType, returnType, node.body);
10003+
checkTypeAssignableTo(exprType, returnOrPromisedType, node.body);
999610004
}
999710005
}
999810006

@@ -10608,7 +10616,7 @@ namespace ts {
1060810616
function checkStringLiteralExpression(node: StringLiteral): Type {
1060910617
const contextualType = getContextualType(node);
1061010618
if (contextualType && contextualTypeIsStringLiteralType(contextualType)) {
10611-
return getStringLiteralType(node);
10619+
return getStringLiteralTypeForText(node.text);
1061210620
}
1061310621

1061410622
return stringType;
@@ -11431,7 +11439,9 @@ namespace ts {
1143111439
seen = c === node;
1143211440
}
1143311441
});
11434-
if (subsequentNode) {
11442+
// We may be here because of some extra junk between overloads that could not be parsed into a valid node.
11443+
// In this case the subsequent node is not really consecutive (.pos !== node.end), and we must ignore it here.
11444+
if (subsequentNode && subsequentNode.pos === node.end) {
1143511445
if (subsequentNode.kind === node.kind) {
1143611446
const errorNode: Node = (<FunctionLikeDeclaration>subsequentNode).name || subsequentNode;
1143711447
// TODO(jfreeman): These are methods, so handle computed name case
@@ -11442,7 +11452,7 @@ namespace ts {
1144211452
// we can get here in two cases
1144311453
// 1. mixed static and instance class members
1144411454
// 2. something with the same name was defined before the set of overloads that prevents them from merging
11445-
// here we'll report error only for the first case since for second we should already report error in binder
11455+
// here we'll report error only for the first case since for second we should already report error in binder
1144611456
if (reportError) {
1144711457
const diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
1144811458
error(errorNode, diagnostic);
@@ -12132,14 +12142,9 @@ namespace ts {
1213212142
}
1213312143

1213412144
checkSourceElement(node.body);
12135-
if (node.type && !isAccessor(node.kind) && !node.asteriskToken) {
12136-
const returnType = getTypeFromTypeNode(node.type);
12137-
let promisedType: Type;
12138-
if (isAsync) {
12139-
promisedType = checkAsyncFunctionReturnType(node);
12140-
}
12141-
12142-
checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, isAsync ? promisedType : returnType);
12145+
if (!isAccessor(node.kind) && !node.asteriskToken) {
12146+
const returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type));
12147+
checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType);
1214312148
}
1214412149

1214512150
if (produceDiagnostics && !node.type) {

src/compiler/commandLineParser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,15 @@ namespace ts {
280280
type: "boolean",
281281
description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file
282282
},
283+
{
284+
name: "allowSyntheticDefaultImports",
285+
type: "boolean",
286+
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
287+
},
283288
{
284289
name: "allowJs",
285290
type: "boolean",
286-
description: Diagnostics.Allow_javascript_files_to_be_compiled,
291+
description: Diagnostics.Allow_javascript_files_to_be_compiled
287292
}
288293
];
289294

@@ -488,7 +493,7 @@ namespace ts {
488493
fileNames: getFileNames(),
489494
errors
490495
};
491-
496+
492497
function getFileNames(): string[] {
493498
let fileNames: string[] = [];
494499
if (hasProperty(json, "files")) {

src/compiler/declarationEmitter.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ namespace ts {
357357
case SyntaxKind.SymbolKeyword:
358358
case SyntaxKind.VoidKeyword:
359359
case SyntaxKind.ThisType:
360-
case SyntaxKind.StringLiteral:
360+
case SyntaxKind.StringLiteralType:
361361
return writeTextOfNode(currentText, type);
362362
case SyntaxKind.ExpressionWithTypeArguments:
363363
return emitExpressionWithTypeArguments(<ExpressionWithTypeArguments>type);
@@ -658,7 +658,7 @@ namespace ts {
658658
}
659659
else {
660660
write("require(");
661-
writeTextOfNode(currentText, getExternalModuleImportEqualsDeclarationExpression(node));
661+
emitExternalModuleSpecifier(node);
662662
write(");");
663663
}
664664
writer.writeLine();
@@ -715,14 +715,23 @@ namespace ts {
715715
}
716716
write(" from ");
717717
}
718-
emitExternalModuleSpecifier(node.moduleSpecifier);
718+
emitExternalModuleSpecifier(node);
719719
write(";");
720720
writer.writeLine();
721721
}
722722

723-
function emitExternalModuleSpecifier(moduleSpecifier: Expression) {
724-
if (moduleSpecifier.kind === SyntaxKind.StringLiteral && isBundledEmit) {
725-
const moduleName = getExternalModuleNameFromDeclaration(host, resolver, moduleSpecifier.parent as (ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration));
723+
function emitExternalModuleSpecifier(parent: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration) {
724+
let moduleSpecifier: Node;
725+
if (parent.kind === SyntaxKind.ImportEqualsDeclaration) {
726+
const node = parent as ImportEqualsDeclaration;
727+
moduleSpecifier = getExternalModuleImportEqualsDeclarationExpression(node);
728+
}
729+
else {
730+
const node = parent as (ImportDeclaration | ExportDeclaration);
731+
moduleSpecifier = node.moduleSpecifier;
732+
}
733+
if (moduleSpecifier.kind === SyntaxKind.StringLiteral && isBundledEmit && (compilerOptions.out || compilerOptions.outFile)) {
734+
const moduleName = getExternalModuleNameFromDeclaration(host, resolver, parent);
726735
if (moduleName) {
727736
write("\"");
728737
write(moduleName);
@@ -765,7 +774,7 @@ namespace ts {
765774
}
766775
if (node.moduleSpecifier) {
767776
write(" from ");
768-
emitExternalModuleSpecifier(node.moduleSpecifier);
777+
emitExternalModuleSpecifier(node);
769778
}
770779
write(";");
771780
writer.writeLine();

src/compiler/diagnosticMessages.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@
16221622
},
16231623
"Cannot assign an abstract constructor type to a non-abstract constructor type.": {
16241624
"category": "Error",
1625-
"code":2517
1625+
"code": 2517
16261626
},
16271627
"Duplicate identifier '{0}'. Compiler uses declaration '{1}' to support async functions.": {
16281628
"category": "Error",
@@ -2068,6 +2068,14 @@
20682068
"category": "Error",
20692069
"code": 5056
20702070
},
2071+
"Cannot find a tsconfig.json file at the specified directory: '{0}'": {
2072+
"category": "Error",
2073+
"code": 5057
2074+
},
2075+
"The specified path does not exist: '{0}'": {
2076+
"category": "Error",
2077+
"code": 5058
2078+
},
20712079

20722080
"Concatenate and emit output to single file.": {
20732081
"category": "Message",
@@ -2109,6 +2117,10 @@
21092117
"category": "Message",
21102118
"code": 6010
21112119
},
2120+
"Allow default imports from modules with no default export. This does not affect code emit, just typechecking.": {
2121+
"category": "Message",
2122+
"code": 6011
2123+
},
21122124
"Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)": {
21132125
"category": "Message",
21142126
"code": 6015

0 commit comments

Comments
 (0)