Skip to content

Commit a871698

Browse files
committed
Merge branch 'master' into sourceMapAndBreakpointDecorators
2 parents ccfa625 + 266600d commit a871698

File tree

144 files changed

+2931
-1102
lines changed

Some content is hidden

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

144 files changed

+2931
-1102
lines changed

Jakefile.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var compilerSources = [
4040
"utilities.ts",
4141
"binder.ts",
4242
"checker.ts",
43+
"sourcemap.ts",
4344
"declarationEmitter.ts",
4445
"emitter.ts",
4546
"program.ts",
@@ -59,6 +60,7 @@ var servicesSources = [
5960
"utilities.ts",
6061
"binder.ts",
6162
"checker.ts",
63+
"sourcemap.ts",
6264
"declarationEmitter.ts",
6365
"emitter.ts",
6466
"program.ts",
@@ -106,6 +108,16 @@ var serverCoreSources = [
106108
return path.join(serverDirectory, f);
107109
});
108110

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

111123
var languageServiceLibrarySources = [
@@ -365,7 +377,6 @@ file(builtGeneratedDiagnosticMessagesJSON,[generatedDiagnosticMessagesJSON], fun
365377
desc("Generates a diagnostic file in TypeScript based on an input JSON file");
366378
task("generate-diagnostics", [diagnosticInfoMapTs]);
367379

368-
369380
// Publish nightly
370381
var configureNightlyJs = path.join(scriptsDirectory, "configureNightly.js");
371382
var configureNightlyTs = path.join(scriptsDirectory, "configureNightly.ts");
@@ -466,7 +477,7 @@ compileFile(servicesFile, servicesSources,[builtLocalDirectory, copyright].conca
466477
var nodeDefinitionsFileContents = definitionFileContents + "\r\nexport = ts;";
467478
fs.writeFileSync(nodeDefinitionsFile, nodeDefinitionsFileContents);
468479

469-
// Node package definition file to be distributed without the package. Created by replacing
480+
// Node package definition file to be distributed without the package. Created by replacing
470481
// 'ts' namespace with '"typescript"' as a module.
471482
var nodeStandaloneDefinitionsFileContents = definitionFileContents.replace(/declare (namespace|module) ts/g, 'declare module "typescript"');
472483
fs.writeFileSync(nodeStandaloneDefinitionsFile, nodeStandaloneDefinitionsFileContents);
@@ -875,7 +886,7 @@ var tslintRulesOutFiles = tslintRules.map(function(p) {
875886
desc("Compiles tslint rules to js");
876887
task("build-rules", tslintRulesOutFiles);
877888
tslintRulesFiles.forEach(function(ruleFile, i) {
878-
compileFile(tslintRulesOutFiles[i], [ruleFile], [ruleFile], [], /*useBuiltCompiler*/ false, /*noOutFile*/ true, /*generateDeclarations*/ false, path.join(builtLocalDirectory, "tslint"));
889+
compileFile(tslintRulesOutFiles[i], [ruleFile], [ruleFile], [], /*useBuiltCompiler*/ false, /*noOutFile*/ true, /*generateDeclarations*/ false, path.join(builtLocalDirectory, "tslint"));
879890
});
880891

881892
function getLinterOptions() {
@@ -909,7 +920,8 @@ function lintFileAsync(options, path, cb) {
909920

910921
var lintTargets = compilerSources
911922
.concat(harnessCoreSources)
912-
.concat(serverCoreSources);
923+
.concat(serverCoreSources)
924+
.concat(scriptSources);
913925

914926
desc("Runs tslint on the compiler sources");
915927
task("lint", ["build-rules"], function() {
@@ -937,7 +949,7 @@ function lintWatchFile(filename) {
937949
if (event !== "change") {
938950
return;
939951
}
940-
952+
941953
if (!lintSemaphores[filename]) {
942954
lintSemaphores[filename] = true;
943955
lintFileAsync(getLinterOptions(), filename, function(err, result) {

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/core.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,33 @@ namespace ts {
356356
return result;
357357
}
358358

359+
/**
360+
* Reduce the properties of a map.
361+
*
362+
* @param map The map to reduce
363+
* @param callback An aggregation function that is called for each entry in the map
364+
* @param initial The initial value for the reduction.
365+
*/
366+
export function reduceProperties<T, U>(map: Map<T>, callback: (aggregate: U, value: T, key: string) => U, initial: U): U {
367+
let result = initial;
368+
if (map) {
369+
for (const key in map) {
370+
if (hasProperty(map, key)) {
371+
result = callback(result, map[key], String(key));
372+
}
373+
}
374+
}
375+
376+
return result;
377+
}
378+
379+
/**
380+
* Tests whether a value is an array.
381+
*/
382+
export function isArray(value: any): value is any[] {
383+
return Array.isArray ? Array.isArray(value) : value instanceof Array;
384+
}
385+
359386
export function memoize<T>(callback: () => T): () => T {
360387
let value: T;
361388
return () => {

0 commit comments

Comments
 (0)