Skip to content

Commit d5f5d92

Browse files
committed
Merge branch 'transforms' into transforms-importHelpers
2 parents a096b87 + beb72f4 commit d5f5d92

File tree

146 files changed

+1368
-571
lines changed

Some content is hidden

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

146 files changed

+1368
-571
lines changed

Jakefile.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,13 +794,14 @@ function runConsoleTests(defaultReporter, runInParallel) {
794794
colors = process.env.colors || process.env.color;
795795
colors = colors ? ' --no-colors ' : ' --colors ';
796796
reporter = process.env.reporter || process.env.r || defaultReporter;
797+
var bail = (process.env.bail || process.env.b) ? "--bail" : "";
797798
var lintFlag = process.env.lint !== 'false';
798799

799800
// timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally
800801
// default timeout is 2sec which really should be enough, but maybe we just need a small amount longer
801802
if(!runInParallel) {
802803
tests = tests ? ' -g "' + tests + '"' : '';
803-
var cmd = "mocha" + (debug ? " --debug-brk" : "") + " -R " + reporter + tests + colors + ' -t ' + testTimeout + ' ' + run;
804+
var cmd = "mocha" + (debug ? " --debug-brk" : "") + " -R " + reporter + tests + colors + bail + ' -t ' + testTimeout + ' ' + run;
804805
console.log(cmd);
805806

806807
var savedNodeEnv = process.env.NODE_ENV;
@@ -865,7 +866,7 @@ task("runtests-parallel", ["build-rules", "tests", builtLocalDirectory], functio
865866
runConsoleTests('min', /*runInParallel*/ true);
866867
}, {async: true});
867868

868-
desc("Runs the tests using the built run.js file. Optional arguments are: t[ests]=regex r[eporter]=[list|spec|json|<more>] d[ebug]=true color[s]=false lint=true dirty=false.");
869+
desc("Runs the tests using the built run.js file. Optional arguments are: t[ests]=regex r[eporter]=[list|spec|json|<more>] d[ebug]=true color[s]=false lint=true bail=false dirty=false.");
869870
task("runtests", ["build-rules", "tests", builtLocalDirectory], function() {
870871
runConsoleTests('mocha-fivemat-progress-reporter', /*runInParallel*/ false);
871872
}, {async: true});

scripts/mocha-parallel.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ function runTests(taskConfigsFolder, run, options, cb) {
193193
counter--;
194194

195195
if (counter <= 0) {
196-
var failed = 0;
197196
var reporter = new Base(),
198197
stats = reporter.stats,
199198
failures = reporter.failures;
@@ -224,8 +223,8 @@ function runTests(taskConfigsFolder, run, options, cb) {
224223
reporter.epilogue();
225224
}
226225

227-
if (failed) {
228-
return cb(new Error("Test failures reported: " + failed));
226+
if (stats.failures) {
227+
return cb(new Error("Test failures reported: " + stats.failures));
229228
}
230229
else {
231230
return cb();

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,7 @@ namespace ts {
19851985
classPrototype.parent = leftSideOfAssignment;
19861986

19871987
const funcSymbol = container.locals[constructorFunction.text];
1988-
if (!funcSymbol || !(funcSymbol.flags & SymbolFlags.Function)) {
1988+
if (!funcSymbol || !(funcSymbol.flags & SymbolFlags.Function || isDeclarationOfFunctionExpression(funcSymbol))) {
19891989
return;
19901990
}
19911991

src/compiler/checker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11500,8 +11500,12 @@ namespace ts {
1150011500
// When resolved signature is a call signature (and not a construct signature) the result type is any, unless
1150111501
// the declaring function had members created through 'x.prototype.y = expr' or 'this.y = expr' psuedodeclarations
1150211502
// in a JS file
11503-
const funcSymbol = checkExpression(node.expression).symbol;
11504-
if (funcSymbol && funcSymbol.members && (funcSymbol.flags & SymbolFlags.Function)) {
11503+
// Note:JS inferred classes might come from a variable declaration instead of a function declaration.
11504+
// In this case, using getResolvedSymbol directly is required to avoid losing the members from the declaration.
11505+
const funcSymbol = node.expression.kind === SyntaxKind.Identifier ?
11506+
getResolvedSymbol(node.expression as Identifier) :
11507+
checkExpression(node.expression).symbol;
11508+
if (funcSymbol && funcSymbol.members && (funcSymbol.flags & SymbolFlags.Function || isDeclarationOfFunctionExpression(funcSymbol))) {
1150511509
return getInferredClassType(funcSymbol);
1150611510
}
1150711511
else if (compilerOptions.noImplicitAny) {

src/compiler/commandLineParser.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,13 @@ namespace ts {
342342
}
343343
},
344344
{
345-
name: "typesRoot",
346-
type: "string"
345+
name: "typeRoots",
346+
type: "list",
347+
element: {
348+
name: "typeRoots",
349+
type: "string",
350+
isFilePath: true
351+
}
347352
},
348353
{
349354
name: "types",
@@ -498,13 +503,20 @@ namespace ts {
498503
}
499504

500505
/* @internal */
501-
export function parseListTypeOption(opt: CommandLineOptionOfListType, value: string, errors: Diagnostic[]): (string | number)[] {
502-
const values = trimString((value || "")).split(",");
506+
export function parseListTypeOption(opt: CommandLineOptionOfListType, value = "", errors: Diagnostic[]): (string | number)[] | undefined {
507+
value = trimString(value);
508+
if (startsWith(value, "-")) {
509+
return undefined;
510+
}
511+
if (value === "") {
512+
return [];
513+
}
514+
const values = value.split(",");
503515
switch (opt.element.type) {
504516
case "number":
505-
return ts.map(values, parseInt);
517+
return map(values, parseInt);
506518
case "string":
507-
return ts.map(values, v => v || "");
519+
return map(values, v => v || "");
508520
default:
509521
return filter(map(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v, errors)), v => !!v);
510522
}
@@ -565,8 +577,11 @@ namespace ts {
565577
i++;
566578
break;
567579
case "list":
568-
options[opt.name] = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
569-
i++;
580+
const result = parseListTypeOption(<CommandLineOptionOfListType>opt, args[i], errors);
581+
options[opt.name] = result || [];
582+
if (result) {
583+
i++;
584+
}
570585
break;
571586
// If not a primitive, the possible types are specified in what is effectively a map of options.
572587
default:

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,10 @@
19311931
"category": "Error",
19321932
"code": 2687
19331933
},
1934+
"Cannot find type definition file for '{0}'.": {
1935+
"category": "Error",
1936+
"code": 2688
1937+
},
19341938
"Import declaration '{0}' is using private name '{1}'.": {
19351939
"category": "Error",
19361940
"code": 4000

src/compiler/program.ts

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44

55
namespace ts {
66
/** The version of the TypeScript compiler release */
7+
export const version = "1.9.0";
78

89
const emptyArray: any[] = [];
910

10-
const defaultLibrarySearchPaths = [
11-
"types/",
12-
"node_modules/",
13-
"node_modules/@types/",
14-
];
15-
16-
export const version = "1.9.0";
11+
const defaultTypeRoots = ["node_modules/@types"];
1712

1813
export function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean): string {
1914
while (true) {
@@ -178,6 +173,11 @@ namespace ts {
178173

179174
const typeReferenceExtensions = [".d.ts"];
180175

176+
function getEffectiveTypeRoots(options: CompilerOptions, host: ModuleResolutionHost) {
177+
return options.typeRoots ||
178+
defaultTypeRoots.map(d => combinePaths(options.configFilePath ? getDirectoryPath(options.configFilePath) : host.getCurrentDirectory(), d));
179+
}
180+
181181
/**
182182
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
183183
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
@@ -192,39 +192,36 @@ namespace ts {
192192
traceEnabled
193193
};
194194

195-
// use typesRoot and fallback to directory that contains tsconfig or current directory if typesRoot is not set
196-
const rootDir = options.typesRoot || (options.configFilePath ? getDirectoryPath(options.configFilePath) : (host.getCurrentDirectory && host.getCurrentDirectory()));
197-
195+
const typeRoots = getEffectiveTypeRoots(options, host);
198196
if (traceEnabled) {
199197
if (containingFile === undefined) {
200-
if (rootDir === undefined) {
198+
if (typeRoots === undefined) {
201199
trace(host, Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName);
202200
}
203201
else {
204-
trace(host, Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, rootDir);
202+
trace(host, Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots);
205203
}
206204
}
207205
else {
208-
if (rootDir === undefined) {
206+
if (typeRoots === undefined) {
209207
trace(host, Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile);
210208
}
211209
else {
212-
trace(host, Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, rootDir);
210+
trace(host, Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots);
213211
}
214212
}
215213
}
216214

217215
const failedLookupLocations: string[] = [];
218216

219217
// Check primary library paths
220-
if (rootDir !== undefined) {
221-
const effectivePrimarySearchPaths = options.typesSearchPaths || defaultLibrarySearchPaths;
222-
for (const searchPath of effectivePrimarySearchPaths) {
223-
const primaryPath = combinePaths(rootDir, searchPath);
224-
if (traceEnabled) {
225-
trace(host, Diagnostics.Resolving_with_primary_search_path_0, primaryPath);
226-
}
227-
const candidate = combinePaths(primaryPath, typeReferenceDirectiveName);
218+
if (typeRoots.length) {
219+
if (traceEnabled) {
220+
trace(host, Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", "));
221+
}
222+
const primarySearchPaths = typeRoots;
223+
for (const typeRoot of primarySearchPaths) {
224+
const candidate = combinePaths(typeRoot, typeReferenceDirectiveName);
228225
const candidateDirectory = getDirectoryPath(candidate);
229226
const resolvedFile = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations,
230227
!directoryProbablyExists(candidateDirectory, host), moduleResolutionState);
@@ -251,9 +248,6 @@ namespace ts {
251248
if (containingFile) {
252249
initialLocationForSecondaryLookup = getDirectoryPath(containingFile);
253250
}
254-
else {
255-
initialLocationForSecondaryLookup = rootDir;
256-
}
257251

258252
if (initialLocationForSecondaryLookup !== undefined) {
259253
// check secondary locations
@@ -932,19 +926,6 @@ namespace ts {
932926
}
933927
}
934928

935-
function getDefaultTypeDirectiveNames(rootPath: string): string[] {
936-
const localTypes = combinePaths(rootPath, "types");
937-
const npmTypes = combinePaths(rootPath, "node_modules/@types");
938-
let result: string[] = [];
939-
if (sys.directoryExists(localTypes)) {
940-
result = result.concat(sys.getDirectories(localTypes));
941-
}
942-
if (sys.directoryExists(npmTypes)) {
943-
result = result.concat(sys.getDirectories(npmTypes));
944-
}
945-
return result;
946-
}
947-
948929
function getDefaultLibLocation(): string {
949930
return getDirectoryPath(normalizePath(sys.getExecutingFilePath()));
950931
}
@@ -953,7 +934,6 @@ namespace ts {
953934
const realpath = sys.realpath && ((path: string) => sys.realpath(path));
954935

955936
return {
956-
getDefaultTypeDirectiveNames,
957937
getSourceFile,
958938
getDefaultLibLocation,
959939
getDefaultLibFileName: options => combinePaths(getDefaultLibLocation(), getDefaultLibFileName(options)),
@@ -967,7 +947,8 @@ namespace ts {
967947
trace: (s: string) => sys.write(s + newLine),
968948
directoryExists: directoryName => sys.directoryExists(directoryName),
969949
realpath,
970-
getEnvironmentVariable: name => getEnvironmentVariable(name, /*host*/ undefined)
950+
getEnvironmentVariable: name => getEnvironmentVariable(name, /*host*/ undefined),
951+
getDirectories: (path: string) => sys.getDirectories(path),
971952
};
972953
}
973954

@@ -1030,21 +1011,35 @@ namespace ts {
10301011
return resolutions;
10311012
}
10321013

1033-
export function getDefaultTypeDirectiveNames(options: CompilerOptions, rootFiles: string[], host: CompilerHost): string[] {
1014+
function getInferredTypesRoot(options: CompilerOptions, rootFiles: string[], host: CompilerHost) {
1015+
return computeCommonSourceDirectoryOfFilenames(rootFiles, host.getCurrentDirectory(), f => host.getCanonicalFileName(f));
1016+
}
1017+
1018+
/**
1019+
* Given a set of options and a set of root files, returns the set of type directive names
1020+
* that should be included for this program automatically.
1021+
* This list could either come from the config file,
1022+
* or from enumerating the types root + initial secondary types lookup location.
1023+
* More type directives might appear in the program later as a result of loading actual source files;
1024+
* this list is only the set of defaults that are implicitly included.
1025+
*/
1026+
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, rootFiles: string[], host: CompilerHost): string[] {
10341027
// Use explicit type list from tsconfig.json
10351028
if (options.types) {
10361029
return options.types;
10371030
}
10381031

1039-
// or load all types from the automatic type import fields
1040-
if (host && host.getDefaultTypeDirectiveNames) {
1041-
const commonRoot = computeCommonSourceDirectoryOfFilenames(rootFiles, host.getCurrentDirectory(), f => host.getCanonicalFileName(f));
1042-
if (commonRoot) {
1043-
return host.getDefaultTypeDirectiveNames(commonRoot);
1032+
// Walk the primary type lookup locations
1033+
let result: string[] = [];
1034+
if (host.directoryExists && host.getDirectories) {
1035+
const typeRoots = getEffectiveTypeRoots(options, host);
1036+
for (const root of typeRoots) {
1037+
if (host.directoryExists(root)) {
1038+
result = result.concat(host.getDirectories(root));
1039+
}
10441040
}
10451041
}
1046-
1047-
return undefined;
1042+
return result;
10481043
}
10491044

10501045
export function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program {
@@ -1096,11 +1091,13 @@ namespace ts {
10961091
if (!tryReuseStructureFromOldProgram()) {
10971092
forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false));
10981093

1099-
// load type declarations specified via 'types' argument
1100-
const typeReferences: string[] = getDefaultTypeDirectiveNames(options, rootNames, host);
1094+
// load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders
1095+
const typeReferences: string[] = getAutomaticTypeDirectiveNames(options, rootNames, host);
11011096

11021097
if (typeReferences) {
1103-
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, /*containingFile*/ undefined);
1098+
const inferredRoot = getInferredTypesRoot(options, rootNames, host);
1099+
const containingFilename = combinePaths(inferredRoot, "__inferred type names__.ts");
1100+
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
11041101
for (let i = 0; i < typeReferences.length; i++) {
11051102
processTypeReferenceDirective(typeReferences[i], resolutions[i]);
11061103
}
@@ -1208,10 +1205,9 @@ namespace ts {
12081205
(oldOptions.jsx !== options.jsx) ||
12091206
(oldOptions.allowJs !== options.allowJs) ||
12101207
(oldOptions.rootDir !== options.rootDir) ||
1211-
(oldOptions.typesSearchPaths !== options.typesSearchPaths) ||
12121208
(oldOptions.configFilePath !== options.configFilePath) ||
12131209
(oldOptions.baseUrl !== options.baseUrl) ||
1214-
(oldOptions.typesRoot !== options.typesRoot) ||
1210+
!arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) ||
12151211
!arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) ||
12161212
!mapIsEqualTo(oldOptions.paths, options.paths)) {
12171213
return false;
@@ -1978,7 +1974,7 @@ namespace ts {
19781974
}
19791975
}
19801976
else {
1981-
fileProcessingDiagnostics.add(createDiagnostic(refFile, refPos, refEnd, Diagnostics.Cannot_find_name_0, typeReferenceDirective));
1977+
fileProcessingDiagnostics.add(createDiagnostic(refFile, refPos, refEnd, Diagnostics.Cannot_find_type_definition_file_for_0, typeReferenceDirective));
19821978
}
19831979

19841980
if (saveResolution) {

src/compiler/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,9 +2633,9 @@ namespace ts {
26332633
target?: ScriptTarget;
26342634
traceResolution?: boolean;
26352635
types?: string[];
2636-
/* @internal */ typesRoot?: string;
2636+
/** Paths used to used to compute primary types search locations */
2637+
typeRoots?: string[];
26372638
typesSearchPaths?: string[];
2638-
/* @internal */ useLegacyEmitter?: boolean;
26392639
/*@internal*/ version?: boolean;
26402640
/*@internal*/ watch?: boolean;
26412641

@@ -2943,6 +2943,7 @@ namespace ts {
29432943
getDefaultTypeDirectiveNames?(rootPath: string): string[];
29442944
writeFile: WriteFileCallback;
29452945
getCurrentDirectory(): string;
2946+
getDirectories(path: string): string[];
29462947
getCanonicalFileName(fileName: string): string;
29472948
useCaseSensitiveFileNames(): boolean;
29482949
getNewLine(): string;

src/compiler/utilities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,18 @@ namespace ts {
13641364
return charCode === CharacterCodes.singleQuote || charCode === CharacterCodes.doubleQuote;
13651365
}
13661366

1367+
/**
1368+
* Returns true if the node is a variable declaration whose initializer is a function expression.
1369+
* This function does not test if the node is in a JavaScript file or not.
1370+
*/
1371+
export function isDeclarationOfFunctionExpression(s: Symbol) {
1372+
if (s.valueDeclaration && s.valueDeclaration.kind === SyntaxKind.VariableDeclaration) {
1373+
const declaration = s.valueDeclaration as VariableDeclaration;
1374+
return declaration.initializer && declaration.initializer.kind === SyntaxKind.FunctionExpression;
1375+
}
1376+
return false;
1377+
}
1378+
13671379
/// Given a BinaryExpression, returns SpecialPropertyAssignmentKind for the various kinds of property
13681380
/// assignments we treat as special in the binder
13691381
export function getSpecialPropertyAssignmentKind(expression: Node): SpecialPropertyAssignmentKind {

0 commit comments

Comments
 (0)