Merge branch 'master' into allow-js-multiple-declaration-of-constructor-properties

This commit is contained in:
Nathan Shively-Sanders 2016-08-17 09:04:10 -07:00
commit 96840c3669
215 changed files with 6839 additions and 1426 deletions

View File

@ -7,12 +7,18 @@ node_js:
sudo: false
env:
- workerCount=3
matrix:
fast_finish: true
include:
- os: osx
node_js: stable
osx_image: xcode7.3
env: workerCount=2
allow_failures:
- os: osx
branches:
only:
@ -28,3 +34,6 @@ install:
cache:
directories:
- node_modules
git:
depth: 1

View File

@ -34,7 +34,7 @@ import through2 = require("through2");
import merge2 = require("merge2");
import intoStream = require("into-stream");
import * as os from "os";
import Linter = require("tslint");
import fold = require("travis-fold");
const gulp = helpMaker(originalGulp);
const mochaParallel = require("./scripts/mocha-parallel.js");
const {runTestsInParallel} = mochaParallel;
@ -449,7 +449,7 @@ gulp.task(tsserverLibraryFile, false, [servicesFile], (done) => {
});
gulp.task("lssl", "Builds language service server library", [tsserverLibraryFile]);
gulp.task("local", "Builds the full compiler and services", [builtLocalCompiler, servicesFile, serverFile, builtGeneratedDiagnosticMessagesJSON]);
gulp.task("local", "Builds the full compiler and services", [builtLocalCompiler, servicesFile, serverFile, builtGeneratedDiagnosticMessagesJSON, tsserverLibraryFile]);
gulp.task("tsc", "Builds only the compiler", [builtLocalCompiler]);
@ -503,7 +503,7 @@ gulp.task("VerifyLKG", false, [], () => {
return gulp.src(expectedFiles).pipe(gulp.dest(LKGDirectory));
});
gulp.task("LKGInternal", false, ["lib", "local", "lssl"]);
gulp.task("LKGInternal", false, ["lib", "local"]);
gulp.task("LKG", "Makes a new LKG out of the built js files", ["clean", "dontUseDebugMode"], () => {
return runSequence("LKGInternal", "VerifyLKG");
@ -928,26 +928,6 @@ gulp.task("build-rules", "Compiles tslint rules to js", () => {
.pipe(gulp.dest(dest));
});
function getLinterOptions() {
return {
configuration: require("./tslint.json"),
formatter: "prose",
formattersDirectory: undefined,
rulesDirectory: "built/local/tslint"
};
}
function lintFileContents(options, path, contents) {
const ll = new Linter(path, contents, options);
console.log("Linting '" + path + "'.");
return ll.lint();
}
function lintFile(options, path) {
const contents = fs.readFileSync(path, "utf8");
return lintFileContents(options, path, contents);
}
const lintTargets = [
"Gulpfile.ts",
"src/compiler/**/*.ts",
@ -959,27 +939,72 @@ const lintTargets = [
"tests/*.ts", "tests/webhost/*.ts" // Note: does *not* descend recursively
];
function sendNextFile(files: {path: string}[], child: cp.ChildProcess, callback: (failures: number) => void, failures: number) {
const file = files.pop();
if (file) {
console.log(`Linting '${file.path}'.`);
child.send({ kind: "file", name: file.path });
}
else {
child.send({ kind: "close" });
callback(failures);
}
}
function spawnLintWorker(files: {path: string}[], callback: (failures: number) => void) {
const child = cp.fork("./scripts/parallel-lint");
let failures = 0;
child.on("message", function(data) {
switch (data.kind) {
case "result":
if (data.failures > 0) {
failures += data.failures;
console.log(data.output);
}
sendNextFile(files, child, callback, failures);
break;
case "error":
console.error(data.error);
failures++;
sendNextFile(files, child, callback, failures);
break;
}
});
sendNextFile(files, child, callback, failures);
}
gulp.task("lint", "Runs tslint on the compiler sources. Optional arguments are: --f[iles]=regex", ["build-rules"], () => {
const fileMatcher = RegExp(cmdLineOptions["files"]);
const lintOptions = getLinterOptions();
let failed = 0;
return gulp.src(lintTargets)
.pipe(insert.transform((contents, file) => {
if (!fileMatcher.test(file.path)) return contents;
const result = lintFile(lintOptions, file.path);
if (result.failureCount > 0) {
console.log(result.output);
failed += result.failureCount;
if (fold.isTravis()) console.log(fold.start("lint"));
let files: {stat: fs.Stats, path: string}[] = [];
return gulp.src(lintTargets, { read: false })
.pipe(through2.obj((chunk, enc, cb) => {
files.push(chunk);
cb();
}, (cb) => {
files = files.filter(file => fileMatcher.test(file.path)).sort((filea, fileb) => filea.stat.size - fileb.stat.size);
const workerCount = (process.env.workerCount && +process.env.workerCount) || os.cpus().length;
for (let i = 0; i < workerCount; i++) {
spawnLintWorker(files, finished);
}
return contents; // TODO (weswig): Automatically apply fixes? :3
}))
.on("end", () => {
if (failed > 0) {
console.error("Linter errors.");
process.exit(1);
let completed = 0;
let failures = 0;
function finished(fails) {
completed++;
failures += fails;
if (completed === workerCount) {
if (fold.isTravis()) console.log(fold.end("lint"));
if (failures > 0) {
throw new Error(`Linter errors: ${failures}`);
}
else {
cb();
}
}
}
});
}));
});

View File

@ -4,7 +4,7 @@ var fs = require("fs");
var os = require("os");
var path = require("path");
var child_process = require("child_process");
var Linter = require("tslint");
var fold = require("travis-fold");
var runTestsInParallel = require("./scripts/mocha-parallel").runTestsInParallel;
// Variables
@ -32,6 +32,28 @@ if (process.env.path !== undefined) {
process.env.PATH = nodeModulesPathPrefix + process.env.PATH;
}
function toNs(diff) {
return diff[0] * 1e9 + diff[1];
}
function mark() {
if (!fold.isTravis()) return;
var stamp = process.hrtime();
var id = Math.floor(Math.random() * 0xFFFFFFFF).toString(16);
console.log("travis_time:start:" + id + "\r");
return {
stamp: stamp,
id: id
};
}
function measure(marker) {
if (!fold.isTravis()) return;
var diff = process.hrtime(marker.stamp);
var total = [marker.stamp[0] + diff[0], marker.stamp[1] + diff[1]];
console.log("travis_time:end:" + marker.id + ":start=" + toNs(marker.stamp) + ",finish=" + toNs(total) + ",duration=" + toNs(diff) + "\r");
}
var compilerSources = [
"core.ts",
"performance.ts",
@ -285,6 +307,7 @@ var builtLocalCompiler = path.join(builtLocalDirectory, compilerFilename);
*/
function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts, callback) {
file(outFile, prereqs, function() {
var startCompileTime = mark();
opts = opts || {};
var compilerPath = useBuiltCompiler ? builtLocalCompiler : LKGCompiler;
var options = "--noImplicitAny --noImplicitThis --noEmitOnError --types "
@ -361,11 +384,13 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, opts
callback();
}
measure(startCompileTime);
complete();
});
ex.addListener("error", function() {
fs.unlinkSync(outFile);
fail("Compilation of " + outFile + " unsuccessful");
measure(startCompileTime);
});
ex.run();
}, {async: true});
@ -551,7 +576,7 @@ var tsserverLibraryDefinitionFile = path.join(builtLocalDirectory, "tsserverlibr
compileFile(
tsserverLibraryFile,
languageServiceLibrarySources,
[builtLocalDirectory, copyright].concat(languageServiceLibrarySources),
[builtLocalDirectory, copyright, builtLocalCompiler].concat(languageServiceLibrarySources).concat(libraryTargets),
/*prefixes*/ [copyright],
/*useBuiltCompiler*/ true,
{ noOutFile: false, generateDeclarations: true });
@ -560,9 +585,19 @@ compileFile(
desc("Builds language service server library");
task("lssl", [tsserverLibraryFile, tsserverLibraryDefinitionFile]);
desc("Emit the start of the build fold");
task("build-fold-start", [] , function() {
if (fold.isTravis()) console.log(fold.start("build"));
});
desc("Emit the end of the build fold");
task("build-fold-end", [] , function() {
if (fold.isTravis()) console.log(fold.end("build"));
});
// Local target to build the compiler and services
desc("Builds the full compiler and services");
task("local", ["generate-diagnostics", "lib", tscFile, servicesFile, nodeDefinitionsFile, serverFile, builtGeneratedDiagnosticMessagesJSON]);
task("local", ["build-fold-start", "generate-diagnostics", "lib", tscFile, servicesFile, nodeDefinitionsFile, serverFile, builtGeneratedDiagnosticMessagesJSON, "lssl", "build-fold-end"]);
// Local target to build only tsc.js
desc("Builds only the compiler");
@ -617,7 +652,7 @@ task("generate-spec", [specMd]);
// Makes a new LKG. This target does not build anything, but errors if not all the outputs are present in the built/local directory
desc("Makes a new LKG out of the built js files");
task("LKG", ["clean", "release", "local", "lssl"].concat(libraryTargets), function() {
task("LKG", ["clean", "release", "local"].concat(libraryTargets), function() {
var expectedFiles = [tscFile, servicesFile, serverFile, nodePackageFile, nodeDefinitionsFile, standaloneDefinitionsFile, tsserverLibraryFile, tsserverLibraryDefinitionFile].concat(libraryTargets);
var missingFiles = expectedFiles.filter(function (f) {
return !fs.existsSync(f);
@ -758,6 +793,7 @@ function runConsoleTests(defaultReporter, runInParallel) {
// timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally
// default timeout is 2sec which really should be enough, but maybe we just need a small amount longer
if(!runInParallel) {
var startTime = mark();
tests = tests ? ' -g "' + tests + '"' : '';
var cmd = "mocha" + (debug ? " --debug-brk" : "") + " -R " + reporter + tests + colors + bail + ' -t ' + testTimeout + ' ' + run;
console.log(cmd);
@ -766,10 +802,12 @@ function runConsoleTests(defaultReporter, runInParallel) {
process.env.NODE_ENV = "development";
exec(cmd, function () {
process.env.NODE_ENV = savedNodeEnv;
measure(startTime);
runLinter();
finish();
}, function(e, status) {
process.env.NODE_ENV = savedNodeEnv;
measure(startTime);
finish(status);
});
@ -777,9 +815,10 @@ function runConsoleTests(defaultReporter, runInParallel) {
else {
var savedNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = "development";
var startTime = mark();
runTestsInParallel(taskConfigsFolder, run, { testTimeout: testTimeout, noColors: colors === " --no-colors " }, function (err) {
process.env.NODE_ENV = savedNodeEnv;
measure(startTime);
// last worker clean everything and runs linter in case if there were no errors
deleteTemporaryProjectOutput();
jake.rmRf(taskConfigsFolder);
@ -998,41 +1037,21 @@ var tslintRulesOutFiles = tslintRules.map(function(p) {
return path.join(builtLocalDirectory, "tslint", p + ".js");
});
desc("Compiles tslint rules to js");
task("build-rules", tslintRulesOutFiles);
task("build-rules", ["build-rules-start"].concat(tslintRulesOutFiles).concat(["build-rules-end"]));
tslintRulesFiles.forEach(function(ruleFile, i) {
compileFile(tslintRulesOutFiles[i], [ruleFile], [ruleFile], [], /*useBuiltCompiler*/ false,
{ noOutFile: true, generateDeclarations: false, outDir: path.join(builtLocalDirectory, "tslint")});
});
function getLinterOptions() {
return {
configuration: require("./tslint.json"),
formatter: "prose",
formattersDirectory: undefined,
rulesDirectory: "built/local/tslint"
};
}
desc("Emit the start of the build-rules fold");
task("build-rules-start", [] , function() {
if (fold.isTravis()) console.log(fold.start("build-rules"));
});
function lintFileContents(options, path, contents) {
var ll = new Linter(path, contents, options);
console.log("Linting '" + path + "'.");
return ll.lint();
}
function lintFile(options, path) {
var contents = fs.readFileSync(path, "utf8");
return lintFileContents(options, path, contents);
}
function lintFileAsync(options, path, cb) {
fs.readFile(path, "utf8", function(err, contents) {
if (err) {
return cb(err);
}
var result = lintFileContents(options, path, contents);
cb(undefined, result);
});
}
desc("Emit the end of the build-rules fold");
task("build-rules-end", [] , function() {
if (fold.isTravis()) console.log(fold.end("build-rules"));
});
var lintTargets = compilerSources
.concat(harnessSources)
@ -1044,71 +1063,78 @@ var lintTargets = compilerSources
.concat(["Gulpfile.ts"])
.concat([nodeServerInFile, perftscPath, "tests/perfsys.ts", webhostPath]);
function sendNextFile(files, child, callback, failures) {
var file = files.pop();
if (file) {
console.log("Linting '" + file + "'.");
child.send({kind: "file", name: file});
}
else {
child.send({kind: "close"});
callback(failures);
}
}
function spawnLintWorker(files, callback) {
var child = child_process.fork("./scripts/parallel-lint");
var failures = 0;
child.on("message", function(data) {
switch (data.kind) {
case "result":
if (data.failures > 0) {
failures += data.failures;
console.log(data.output);
}
sendNextFile(files, child, callback, failures);
break;
case "error":
console.error(data.error);
failures++;
sendNextFile(files, child, callback, failures);
break;
}
});
sendNextFile(files, child, callback, failures);
}
desc("Runs tslint on the compiler sources. Optional arguments are: f[iles]=regex");
task("lint", ["build-rules"], function() {
var lintOptions = getLinterOptions();
if (fold.isTravis()) console.log(fold.start("lint"));
var startTime = mark();
var failed = 0;
var fileMatcher = RegExp(process.env.f || process.env.file || process.env.files || "");
var done = {};
for (var i in lintTargets) {
var target = lintTargets[i];
if (!done[target] && fileMatcher.test(target)) {
var result = lintFile(lintOptions, target);
if (result.failureCount > 0) {
console.log(result.output);
failed += result.failureCount;
}
done[target] = true;
done[target] = fs.statSync(target).size;
}
}
if (failed > 0) {
fail('Linter errors.', failed);
}
});
/**
* This is required because file watches on Windows get fires _twice_
* when a file changes on some node/windows version configuations
* (node v4 and win 10, for example). By not running a lint for a file
* which already has a pending lint, we avoid duplicating our work.
* (And avoid printing duplicate results!)
*/
var lintSemaphores = {};
var workerCount = (process.env.workerCount && +process.env.workerCount) || os.cpus().length;
function lintWatchFile(filename) {
fs.watch(filename, {persistent: true}, function(event) {
if (event !== "change") {
return;
}
if (!lintSemaphores[filename]) {
lintSemaphores[filename] = true;
lintFileAsync(getLinterOptions(), filename, function(err, result) {
delete lintSemaphores[filename];
if (err) {
console.log(err);
return;
}
if (result.failureCount > 0) {
console.log("***Lint failure***");
for (var i = 0; i < result.failures.length; i++) {
var failure = result.failures[i];
var start = failure.startPosition.lineAndCharacter;
var end = failure.endPosition.lineAndCharacter;
console.log("warning " + filename + " (" + (start.line + 1) + "," + (start.character + 1) + "," + (end.line + 1) + "," + (end.character + 1) + "): " + failure.failure);
}
console.log("*** Total " + result.failureCount + " failures.");
}
});
}
var names = Object.keys(done).sort(function(namea, nameb) {
return done[namea] - done[nameb];
});
}
desc("Watches files for changes to rerun a lint pass");
task("lint-server", ["build-rules"], function() {
console.log("Watching ./src for changes to linted files");
for (var i = 0; i < lintTargets.length; i++) {
lintWatchFile(lintTargets[i]);
for (var i = 0; i < workerCount; i++) {
spawnLintWorker(names, finished);
}
});
var completed = 0;
var failures = 0;
function finished(fails) {
completed++;
failures += fails;
if (completed === workerCount) {
measure(startTime);
if (fold.isTravis()) console.log(fold.end("lint"));
if (failures > 0) {
fail('Linter errors.', failed);
}
else {
complete();
}
}
}
}, {async: true});

View File

@ -6,7 +6,7 @@
[![Join the chat at https://gitter.im/Microsoft/TypeScript](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Microsoft/TypeScript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[TypeScript](http://www.typescriptlang.org/) is a language for application-scale JavaScript. TypeScript adds optional types, classes, and modules to JavaScript. TypeScript supports tools for large-scale JavaScript applications for any browser, for any host, on any OS. TypeScript compiles to readable, standards-based JavaScript. Try it out at the [playground](http://www.typescriptlang.org/Playground), and stay up to date via [our blog](http://blogs.msdn.com/typescript) and [Twitter account](https://twitter.com/typescriptlang).
[TypeScript](http://www.typescriptlang.org/) is a language for application-scale JavaScript. TypeScript adds optional types, classes, and modules to JavaScript. TypeScript supports tools for large-scale JavaScript applications for any browser, for any host, on any OS. TypeScript compiles to readable, standards-based JavaScript. Try it out at the [playground](http://www.typescriptlang.org/Playground), and stay up to date via [our blog](https://blogs.msdn.microsoft.com/typescript) and [Twitter account](https://twitter.com/typescriptlang).
## Installing

View File

@ -30,8 +30,8 @@
},
"devDependencies": {
"@types/browserify": "latest",
"@types/convert-source-map": "latest",
"@types/chai": "latest",
"@types/convert-source-map": "latest",
"@types/del": "latest",
"@types/glob": "latest",
"@types/gulp": "latest",
@ -72,13 +72,14 @@
"run-sequence": "latest",
"sorcery": "latest",
"through2": "latest",
"travis-fold": "latest",
"ts-node": "latest",
"tslint": "next",
"typescript": "next"
},
"scripts": {
"pretest": "jake tests",
"test": "jake runtests",
"test": "jake runtests-parallel",
"build": "npm run build:compiler && npm run build:tests",
"build:compiler": "jake local",
"build:tests": "jake tests",

45
scripts/parallel-lint.js Normal file
View File

@ -0,0 +1,45 @@
var Linter = require("tslint");
var fs = require("fs");
function getLinterOptions() {
return {
configuration: require("../tslint.json"),
formatter: "prose",
formattersDirectory: undefined,
rulesDirectory: "built/local/tslint"
};
}
function lintFileContents(options, path, contents) {
var ll = new Linter(path, contents, options);
return ll.lint();
}
function lintFileAsync(options, path, cb) {
fs.readFile(path, "utf8", function (err, contents) {
if (err) {
return cb(err);
}
var result = lintFileContents(options, path, contents);
cb(undefined, result);
});
}
process.on("message", function (data) {
switch (data.kind) {
case "file":
var target = data.name;
var lintOptions = getLinterOptions();
lintFileAsync(lintOptions, target, function (err, result) {
if (err) {
process.send({ kind: "error", error: err.toString() });
return;
}
process.send({ kind: "result", failures: result.failureCount, output: result.output });
});
break;
case "close":
process.exit(0);
break;
}
});

View File

@ -69,7 +69,7 @@ function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosti
}
function buildUniqueNameMap(names: string[]): ts.Map<string> {
var nameMap: ts.Map<string> = {};
var nameMap = ts.createMap<string>();
var uniqueNames = NameGenerator.ensureUniqueness(names, /* isCaseSensitive */ false, /* isFixed */ undefined);

View File

@ -1,7 +1,6 @@
import * as Lint from "tslint/lib/lint";
import * as ts from "typescript";
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING_FACTORY = (identifier: string) => `Identifier '${identifier}' never appears on the LHS of an assignment - use const instead of let for its declaration.`;
@ -64,7 +63,7 @@ interface DeclarationUsages {
}
class PreferConstWalker extends Lint.RuleWalker {
private inScopeLetDeclarations: ts.Map<DeclarationUsages>[] = [];
private inScopeLetDeclarations: ts.MapLike<DeclarationUsages>[] = [];
private errors: Lint.RuleFailure[] = [];
private markAssignment(identifier: ts.Identifier) {
const name = identifier.text;
@ -172,7 +171,7 @@ class PreferConstWalker extends Lint.RuleWalker {
}
private visitAnyForStatement(node: ts.ForOfStatement | ts.ForInStatement) {
const names: ts.Map<DeclarationUsages> = {};
const names: ts.MapLike<DeclarationUsages> = {};
if (isLet(node.initializer)) {
if (node.initializer.kind === ts.SyntaxKind.VariableDeclarationList) {
this.collectLetIdentifiers(node.initializer as ts.VariableDeclarationList, names);
@ -194,7 +193,7 @@ class PreferConstWalker extends Lint.RuleWalker {
}
visitBlock(node: ts.Block) {
const names: ts.Map<DeclarationUsages> = {};
const names: ts.MapLike<DeclarationUsages> = {};
for (const statement of node.statements) {
if (statement.kind === ts.SyntaxKind.VariableStatement) {
this.collectLetIdentifiers((statement as ts.VariableStatement).declarationList, names);
@ -205,7 +204,7 @@ class PreferConstWalker extends Lint.RuleWalker {
this.popDeclarations();
}
private collectLetIdentifiers(list: ts.VariableDeclarationList, ret: ts.Map<DeclarationUsages>) {
private collectLetIdentifiers(list: ts.VariableDeclarationList, ret: ts.MapLike<DeclarationUsages>) {
for (const node of list.declarations) {
if (isLet(node) && !isExported(node)) {
this.collectNameIdentifiers(node, node.name, ret);
@ -213,7 +212,7 @@ class PreferConstWalker extends Lint.RuleWalker {
}
}
private collectNameIdentifiers(declaration: ts.VariableDeclaration, node: ts.Identifier | ts.BindingPattern, table: ts.Map<DeclarationUsages>) {
private collectNameIdentifiers(declaration: ts.VariableDeclaration, node: ts.Identifier | ts.BindingPattern, table: ts.MapLike<DeclarationUsages>) {
if (node.kind === ts.SyntaxKind.Identifier) {
table[(node as ts.Identifier).text] = { declaration, usages: 0 };
}
@ -222,7 +221,7 @@ class PreferConstWalker extends Lint.RuleWalker {
}
}
private collectBindingPatternIdentifiers(value: ts.VariableDeclaration, pattern: ts.BindingPattern, table: ts.Map<DeclarationUsages>) {
private collectBindingPatternIdentifiers(value: ts.VariableDeclaration, pattern: ts.BindingPattern, table: ts.MapLike<DeclarationUsages>) {
for (const element of pattern.elements) {
this.collectNameIdentifiers(value, element.name, table);
}

View File

@ -22,3 +22,4 @@ declare module "into-stream" {
}
declare module "sorcery";
declare module "travis-fold";

View File

@ -89,9 +89,10 @@ namespace ts {
const binder = createBinder();
export function bindSourceFile(file: SourceFile, options: CompilerOptions) {
const start = performance.mark();
performance.mark("beforeBind");
binder(file, options);
performance.measure("Bind", start);
performance.mark("afterBind");
performance.measure("Bind", "beforeBind", "afterBind");
}
function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
@ -135,7 +136,7 @@ namespace ts {
options = opts;
languageVersion = getEmitScriptTarget(options);
inStrictMode = !!file.externalModuleIndicator;
classifiableNames = {};
classifiableNames = createMap<string>();
symbolCount = 0;
Symbol = objectAllocator.getSymbolConstructor();
@ -183,11 +184,11 @@ namespace ts {
symbol.declarations.push(node);
if (symbolFlags & SymbolFlags.HasExports && !symbol.exports) {
symbol.exports = {};
symbol.exports = createMap<Symbol>();
}
if (symbolFlags & SymbolFlags.HasMembers && !symbol.members) {
symbol.members = {};
symbol.members = createMap<Symbol>();
}
if (symbolFlags & SymbolFlags.Value) {
@ -325,9 +326,7 @@ namespace ts {
// 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 = hasProperty(symbolTable, name)
? symbolTable[name]
: (symbolTable[name] = createSymbol(SymbolFlags.None, name));
symbol = symbolTable[name] || (symbolTable[name] = createSymbol(SymbolFlags.None, name));
if (name && (includes & SymbolFlags.Classifiable)) {
classifiableNames[name] = name;
@ -445,7 +444,7 @@ namespace ts {
if (containerFlags & ContainerFlags.IsContainer) {
container = blockScopeContainer = node;
if (containerFlags & ContainerFlags.HasLocals) {
container.locals = {};
container.locals = createMap<Symbol>();
}
addToContainerChain(container);
}
@ -1410,7 +1409,8 @@ namespace ts {
const typeLiteralSymbol = createSymbol(SymbolFlags.TypeLiteral, "__type");
addDeclarationToSymbol(typeLiteralSymbol, node, SymbolFlags.TypeLiteral);
typeLiteralSymbol.members = { [symbol.name]: symbol };
typeLiteralSymbol.members = createMap<Symbol>();
typeLiteralSymbol.members[symbol.name] = symbol;
}
function bindObjectLiteralExpression(node: ObjectLiteralExpression) {
@ -1420,7 +1420,7 @@ namespace ts {
}
if (inStrictMode) {
const seen: Map<ElementKind> = {};
const seen = createMap<ElementKind>();
for (const prop of node.properties) {
if (prop.name.kind !== SyntaxKind.Identifier) {
@ -1476,7 +1476,7 @@ namespace ts {
// fall through.
default:
if (!blockScopeContainer.locals) {
blockScopeContainer.locals = {};
blockScopeContainer.locals = createMap<Symbol>();
addToContainerChain(blockScopeContainer);
}
declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes);
@ -1898,18 +1898,17 @@ namespace ts {
}
function bindExportAssignment(node: ExportAssignment | BinaryExpression) {
const boundExpression = node.kind === SyntaxKind.ExportAssignment ? (<ExportAssignment>node).expression : (<BinaryExpression>node).right;
if (!container.symbol || !container.symbol.exports) {
// Export assignment in some sort of block construct
bindAnonymousDeclaration(node, SymbolFlags.Alias, getDeclarationName(node));
}
else if (boundExpression.kind === SyntaxKind.Identifier && node.kind === SyntaxKind.ExportAssignment) {
// An export default clause with an identifier exports all meanings of that identifier
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.Alias, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
}
else {
// An export default clause with an expression exports a value
declareSymbol(container.symbol.exports, container.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
const flags = node.kind === SyntaxKind.ExportAssignment && exportAssignmentIsAlias(<ExportAssignment>node)
// An export default clause with an EntityNameExpression exports all meanings of that identifier
? SymbolFlags.Alias
// An export default clause with any other expression exports a value
: SymbolFlags.Property;
declareSymbol(container.symbol.exports, container.symbol, node, flags, SymbolFlags.PropertyExcludes | SymbolFlags.AliasExcludes);
}
}
@ -1936,7 +1935,7 @@ namespace ts {
}
}
file.symbol.globalExports = file.symbol.globalExports || {};
file.symbol.globalExports = file.symbol.globalExports || createMap<Symbol>();
declareSymbol(file.symbol.globalExports, file.symbol, node, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
}
@ -1984,12 +1983,23 @@ namespace ts {
assignee = container;
}
else if (container.kind === SyntaxKind.Constructor) {
assignee = container.parent;
if (isInJavaScriptFile(node)) {
// this.foo assignment in a JavaScript class
// Bind this property to the containing class
const saveContainer = container;
container = container.parent;
bindPropertyOrMethodOrAccessor(node, SymbolFlags.Property, SymbolFlags.None);
container = saveContainer;
return;
}
else {
assignee = container.parent;
}
}
else {
return;
}
assignee.symbol.members = assignee.symbol.members || {};
assignee.symbol.members = assignee.symbol.members || createMap<Symbol>();
// It's acceptable for multiple 'this' assignments of the same identifier to occur
// AND it can be overwritten by subsequent method declarations
const symbol = declareSymbol(assignee.symbol.members, assignee.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes & ~SymbolFlags.Property);
@ -2017,7 +2027,7 @@ namespace ts {
// Set up the members collection if it doesn't exist already
if (!funcSymbol.members) {
funcSymbol.members = {};
funcSymbol.members = createMap<Symbol>();
}
// Declare the method/property
@ -2066,7 +2076,7 @@ namespace ts {
// 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.
const prototypeSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Prototype, "prototype");
if (hasProperty(symbol.exports, prototypeSymbol.name)) {
if (symbol.exports[prototypeSymbol.name]) {
if (node.name) {
node.name.parent = node;
}

File diff suppressed because it is too large Load Diff

View File

@ -61,10 +61,10 @@ namespace ts {
},
{
name: "jsx",
type: {
type: createMap({
"preserve": JsxEmit.Preserve,
"react": JsxEmit.React
},
}),
paramType: Diagnostics.KIND,
description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react,
},
@ -91,7 +91,7 @@ namespace ts {
{
name: "module",
shortName: "m",
type: {
type: createMap({
"none": ModuleKind.None,
"commonjs": ModuleKind.CommonJS,
"amd": ModuleKind.AMD,
@ -99,16 +99,16 @@ namespace ts {
"umd": ModuleKind.UMD,
"es6": ModuleKind.ES6,
"es2015": ModuleKind.ES2015,
},
}),
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es2015,
paramType: Diagnostics.KIND,
},
{
name: "newLine",
type: {
type: createMap({
"crlf": NewLineKind.CarriageReturnLineFeed,
"lf": NewLineKind.LineFeed
},
}),
description: Diagnostics.Specify_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix,
paramType: Diagnostics.NEWLINE,
},
@ -250,12 +250,12 @@ namespace ts {
{
name: "target",
shortName: "t",
type: {
type: createMap({
"es3": ScriptTarget.ES3,
"es5": ScriptTarget.ES5,
"es6": ScriptTarget.ES6,
"es2015": ScriptTarget.ES2015,
},
}),
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015,
paramType: Diagnostics.VERSION,
},
@ -284,10 +284,10 @@ namespace ts {
},
{
name: "moduleResolution",
type: {
type: createMap({
"node": ModuleResolutionKind.NodeJs,
"classic": ModuleResolutionKind.Classic,
},
}),
description: Diagnostics.Specify_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6,
},
{
@ -392,7 +392,7 @@ namespace ts {
type: "list",
element: {
name: "lib",
type: {
type: createMap({
// JavaScript only
"es5": "lib.es5.d.ts",
"es6": "lib.es2015.d.ts",
@ -417,7 +417,7 @@ namespace ts {
"es2016.array.include": "lib.es2016.array.include.d.ts",
"es2017.object": "lib.es2017.object.d.ts",
"es2017.sharedmemory": "lib.es2017.sharedmemory.d.ts"
},
}),
},
description: Diagnostics.Specify_library_files_to_be_included_in_the_compilation_Colon
},
@ -470,8 +470,8 @@ namespace ts {
return optionNameMapCache;
}
const optionNameMap: Map<CommandLineOption> = {};
const shortOptionNames: Map<string> = {};
const optionNameMap = createMap<CommandLineOption>();
const shortOptionNames = createMap<string>();
forEach(optionDeclarations, option => {
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
@ -486,10 +486,9 @@ namespace ts {
/* @internal */
export function createCompilerDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType): Diagnostic {
const namesOfType: string[] = [];
forEachKey(opt.type, key => {
for (const key in opt.type) {
namesOfType.push(` '${key}'`);
});
}
return createCompilerDiagnostic(Diagnostics.Argument_for_0_option_must_be_Colon_1, `--${opt.name}`, namesOfType);
}
@ -497,7 +496,7 @@ namespace ts {
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
const key = trimString((value || "")).toLowerCase();
const map = opt.type;
if (hasProperty(map, key)) {
if (key in map) {
return map[key];
}
else {
@ -551,11 +550,11 @@ namespace ts {
s = s.slice(s.charCodeAt(1) === CharacterCodes.minus ? 2 : 1).toLowerCase();
// Try to translate short option names to their full equivalents.
if (hasProperty(shortOptionNames, s)) {
if (s in shortOptionNames) {
s = shortOptionNames[s];
}
if (hasProperty(optionNameMap, s)) {
if (s in optionNameMap) {
const opt = optionNameMap[s];
if (opt.isTSConfigOnly) {
@ -811,7 +810,7 @@ namespace ts {
const optionNameMap = arrayToMap(optionDeclarations, opt => opt.name);
for (const id in jsonOptions) {
if (hasProperty(optionNameMap, id)) {
if (id in optionNameMap) {
const opt = optionNameMap[id];
defaultOptions[opt.name] = convertJsonOption(opt, jsonOptions[id], basePath, errors);
}
@ -848,7 +847,7 @@ namespace ts {
function convertJsonOptionOfCustomType(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
const key = value.toLowerCase();
if (hasProperty(opt.type, key)) {
if (key in opt.type) {
return opt.type[key];
}
else {
@ -958,12 +957,12 @@ namespace ts {
// Literal file names (provided via the "files" array in tsconfig.json) are stored in a
// file map with a possibly case insensitive key. We use this map later when when including
// wildcard paths.
const literalFileMap: Map<string> = {};
const literalFileMap = createMap<string>();
// Wildcard paths (provided via the "includes" array in tsconfig.json) are stored in a
// file map with a possibly case insensitive key. We use this map to store paths matched
// via wildcard, and to handle extension priority.
const wildcardFileMap: Map<string> = {};
const wildcardFileMap = createMap<string>();
if (include) {
include = validateSpecs(include, errors, /*allowTrailingRecursion*/ false);
@ -1011,7 +1010,7 @@ namespace ts {
removeWildcardFilesWithLowerPriorityExtension(file, wildcardFileMap, supportedExtensions, keyMapper);
const key = keyMapper(file);
if (!hasProperty(literalFileMap, key) && !hasProperty(wildcardFileMap, key)) {
if (!(key in literalFileMap) && !(key in wildcardFileMap)) {
wildcardFileMap[key] = file;
}
}
@ -1063,7 +1062,7 @@ namespace ts {
// /a/b/a?z - Watch /a/b directly to catch any new file matching a?z
const rawExcludeRegex = getRegularExpressionForWildcard(exclude, path, "exclude");
const excludeRegex = rawExcludeRegex && new RegExp(rawExcludeRegex, useCaseSensitiveFileNames ? "" : "i");
const wildcardDirectories: Map<WatchDirectoryFlags> = {};
const wildcardDirectories = createMap<WatchDirectoryFlags>();
if (include !== undefined) {
const recursiveKeys: string[] = [];
for (const file of include) {
@ -1076,7 +1075,7 @@ namespace ts {
if (match) {
const key = useCaseSensitiveFileNames ? match[0] : match[0].toLowerCase();
const flags = watchRecursivePattern.test(name) ? WatchDirectoryFlags.Recursive : WatchDirectoryFlags.None;
const existingFlags = getProperty(wildcardDirectories, key);
const existingFlags = wildcardDirectories[key];
if (existingFlags === undefined || existingFlags < flags) {
wildcardDirectories[key] = flags;
if (flags === WatchDirectoryFlags.Recursive) {
@ -1088,11 +1087,9 @@ namespace ts {
// Remove any subpaths under an existing recursively watched directory.
for (const key in wildcardDirectories) {
if (hasProperty(wildcardDirectories, key)) {
for (const recursiveKey of recursiveKeys) {
if (key !== recursiveKey && containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) {
delete wildcardDirectories[key];
}
for (const recursiveKey of recursiveKeys) {
if (key !== recursiveKey && containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) {
delete wildcardDirectories[key];
}
}
}
@ -1115,7 +1112,7 @@ namespace ts {
for (let i = ExtensionPriority.Highest; i < adjustedExtensionPriority; i++) {
const higherPriorityExtension = extensions[i];
const higherPriorityPath = keyMapper(changeExtension(file, higherPriorityExtension));
if (hasProperty(literalFiles, higherPriorityPath) || hasProperty(wildcardFiles, higherPriorityPath)) {
if (higherPriorityPath in literalFiles || higherPriorityPath in wildcardFiles) {
return true;
}
}

View File

@ -19,8 +19,28 @@ namespace ts {
True = -1
}
const createObject = Object.create;
export function createMap<T>(template?: MapLike<T>): Map<T> {
const map: Map<T> = createObject(null); // tslint:disable-line:no-null-keyword
// Using 'delete' on an object causes V8 to put the object in dictionary mode.
// This disables creation of hidden classes, which are expensive when an object is
// constantly changing shape.
map["__"] = undefined;
delete map["__"];
// Copies keys/values from template. Note that for..in will not throw if
// template is undefined, and instead will just exit the loop.
for (const key in template) if (hasOwnProperty.call(template, key)) {
map[key] = template[key];
}
return map;
}
export function createFileMap<T>(keyMapper?: (key: string) => string): FileMap<T> {
let files: Map<T> = {};
let files = createMap<T>();
return {
get,
set,
@ -46,7 +66,7 @@ namespace ts {
}
function contains(path: Path) {
return hasProperty(files, toKey(path));
return toKey(path) in files;
}
function remove(path: Path) {
@ -55,7 +75,7 @@ namespace ts {
}
function clear() {
files = {};
files = createMap<T>();
}
function toKey(path: Path): string {
@ -81,7 +101,7 @@ namespace ts {
* returns a truthy value, then returns that value.
* If no such value is found, the callback is applied to each element of array and undefined is returned.
*/
export function forEach<T, U>(array: T[], callback: (element: T, index: number) => U): U {
export function forEach<T, U>(array: T[] | undefined, callback: (element: T, index: number) => U | undefined): U | undefined {
if (array) {
for (let i = 0, len = array.length; i < len; i++) {
const result = callback(array[i], i);
@ -93,6 +113,17 @@ namespace ts {
return undefined;
}
/** Like `forEach`, but assumes existence of array and fails if no truthy value is found. */
export function find<T, U>(array: T[], callback: (element: T, index: number) => U | undefined): U {
for (let i = 0, len = array.length; i < len; i++) {
const result = callback(array[i], i);
if (result) {
return result;
}
}
Debug.fail();
}
export function contains<T>(array: T[], value: T): boolean {
if (array) {
for (const v of array) {
@ -136,17 +167,44 @@ namespace ts {
return count;
}
/**
* Filters an array by a predicate function. Returns the same array instance if the predicate is
* true for all elements, otherwise returns a new array instance containing the filtered subset.
*/
export function filter<T>(array: T[], f: (x: T) => boolean): T[] {
let result: T[];
if (array) {
result = [];
for (const item of array) {
if (f(item)) {
result.push(item);
const len = array.length;
let i = 0;
while (i < len && f(array[i])) i++;
if (i < len) {
const result = array.slice(0, i);
i++;
while (i < len) {
const item = array[i];
if (f(item)) {
result.push(item);
}
i++;
}
return result;
}
}
return result;
return array;
}
export function removeWhere<T>(array: T[], f: (x: T) => boolean): boolean {
let outIndex = 0;
for (const item of array) {
if (!f(item)) {
array[outIndex] = item;
outIndex++;
}
}
if (outIndex !== array.length) {
array.length = outIndex;
return true;
}
return false;
}
export function filterMutate<T>(array: T[], f: (x: T) => boolean): void {
@ -311,78 +369,142 @@ namespace ts {
const hasOwnProperty = Object.prototype.hasOwnProperty;
export function hasProperty<T>(map: Map<T>, key: string): boolean {
/**
* Indicates whether a map-like contains an own property with the specified key.
*
* NOTE: This is intended for use only with MapLike<T> objects. For Map<T> objects, use
* the 'in' operator.
*
* @param map A map-like.
* @param key A property key.
*/
export function hasProperty<T>(map: MapLike<T>, key: string): boolean {
return hasOwnProperty.call(map, key);
}
export function getKeys<T>(map: Map<T>): string[] {
/**
* Gets the value of an owned property in a map-like.
*
* NOTE: This is intended for use only with MapLike<T> objects. For Map<T> objects, use
* an indexer.
*
* @param map A map-like.
* @param key A property key.
*/
export function getProperty<T>(map: MapLike<T>, key: string): T | undefined {
return hasOwnProperty.call(map, key) ? map[key] : undefined;
}
/**
* Gets the owned, enumerable property keys of a map-like.
*
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
* Object.keys instead as it offers better performance.
*
* @param map A map-like.
*/
export function getOwnKeys<T>(map: MapLike<T>): string[] {
const keys: string[] = [];
for (const key in map) {
for (const key in map) if (hasOwnProperty.call(map, key)) {
keys.push(key);
}
return keys;
}
export function getProperty<T>(map: Map<T>, key: string): T {
return hasOwnProperty.call(map, key) ? map[key] : undefined;
/**
* Enumerates the properties of a Map<T>, invoking a callback and returning the first truthy result.
*
* @param map A map for which properties should be enumerated.
* @param callback A callback to invoke for each property.
*/
export function forEachProperty<T, U>(map: Map<T>, callback: (value: T, key: string) => U): U {
let result: U;
for (const key in map) {
if (result = callback(map[key], key)) break;
}
return result;
}
export function isEmpty<T>(map: Map<T>) {
for (const id in map) {
if (hasProperty(map, id)) {
return false;
}
/**
* Returns true if a Map<T> has some matching property.
*
* @param map A map whose properties should be tested.
* @param predicate An optional callback used to test each property.
*/
export function someProperties<T>(map: Map<T>, predicate?: (value: T, key: string) => boolean) {
for (const key in map) {
if (!predicate || predicate(map[key], key)) return true;
}
return false;
}
/**
* Performs a shallow copy of the properties from a source Map<T> to a target MapLike<T>
*
* @param source A map from which properties should be copied.
* @param target A map to which properties should be copied.
*/
export function copyProperties<T>(source: Map<T>, target: MapLike<T>): void {
for (const key in source) {
target[key] = source[key];
}
}
/**
* Reduce the properties of a map.
*
* NOTE: This is intended for use with Map<T> objects. For MapLike<T> objects, use
* reduceOwnProperties instead as it offers better runtime safety.
*
* @param map The map to reduce
* @param callback An aggregation function that is called for each entry in the map
* @param initial The initial value for the reduction.
*/
export function reduceProperties<T, U>(map: Map<T>, callback: (aggregate: U, value: T, key: string) => U, initial: U): U {
let result = initial;
for (const key in map) {
result = callback(result, map[key], String(key));
}
return result;
}
/**
* Reduce the properties defined on a map-like (but not from its prototype chain).
*
* NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
* reduceProperties instead as it offers better performance.
*
* @param map The map-like to reduce
* @param callback An aggregation function that is called for each entry in the map
* @param initial The initial value for the reduction.
*/
export function reduceOwnProperties<T, U>(map: MapLike<T>, callback: (aggregate: U, value: T, key: string) => U, initial: U): U {
let result = initial;
for (const key in map) if (hasOwnProperty.call(map, key)) {
result = callback(result, map[key], String(key));
}
return result;
}
/**
* Performs a shallow equality comparison of the contents of two map-likes.
*
* @param left A map-like whose properties should be compared.
* @param right A map-like whose properties should be compared.
*/
export function equalOwnProperties<T>(left: MapLike<T>, right: MapLike<T>, equalityComparer?: (left: T, right: T) => boolean) {
if (left === right) return true;
if (!left || !right) return false;
for (const key in left) if (hasOwnProperty.call(left, key)) {
if (!hasOwnProperty.call(right, key) === undefined) return false;
if (equalityComparer ? !equalityComparer(left[key], right[key]) : left[key] !== right[key]) return false;
}
for (const key in right) if (hasOwnProperty.call(right, key)) {
if (!hasOwnProperty.call(left, key)) return false;
}
return true;
}
export function clone<T>(object: T): T {
const result: any = {};
for (const id in object) {
result[id] = (<any>object)[id];
}
return <T>result;
}
export function extend<T1 extends Map<{}>, T2 extends Map<{}>>(first: T1 , second: T2): T1 & T2 {
const result: T1 & T2 = <any>{};
for (const id in first) {
(result as any)[id] = first[id];
}
for (const id in second) {
if (!hasProperty(result, id)) {
(result as any)[id] = second[id];
}
}
return result;
}
export function forEachValue<T, U>(map: Map<T>, callback: (value: T) => U): U {
let result: U;
for (const id in map) {
if (result = callback(map[id])) break;
}
return result;
}
export function forEachKey<T, U>(map: Map<T>, callback: (key: string) => U): U {
let result: U;
for (const id in map) {
if (result = callback(id)) break;
}
return result;
}
export function lookUp<T>(map: Map<T>, key: string): T {
return hasProperty(map, key) ? map[key] : undefined;
}
export function copyMap<T>(source: Map<T>, target: Map<T>): void {
for (const p in source) {
target[p] = source[p];
}
}
/**
* Creates a map from the elements of an array.
*
@ -393,33 +515,40 @@ namespace ts {
* the same key with the given 'makeKey' function, then the element with the higher
* index in the array will be the one associated with the produced key.
*/
export function arrayToMap<T>(array: T[], makeKey: (value: T) => string): Map<T> {
const result: Map<T> = {};
forEach(array, value => {
result[makeKey(value)] = value;
});
export function arrayToMap<T>(array: T[], makeKey: (value: T) => string): Map<T>;
export function arrayToMap<T, U>(array: T[], makeKey: (value: T) => string, makeValue: (value: T) => U): Map<U>;
export function arrayToMap<T, U>(array: T[], makeKey: (value: T) => string, makeValue?: (value: T) => U): Map<T | U> {
const result = createMap<T | U>();
for (const value of array) {
result[makeKey(value)] = makeValue ? makeValue(value) : value;
}
return result;
}
/**
* Reduce the properties of a map.
*
* @param map The map to reduce
* @param callback An aggregation function that is called for each entry in the map
* @param initial The initial value for the reduction.
*/
export function reduceProperties<T, U>(map: Map<T>, callback: (aggregate: U, value: T, key: string) => U, initial: U): U {
let result = initial;
if (map) {
for (const key in map) {
if (hasProperty(map, key)) {
result = callback(result, map[key], String(key));
}
export function cloneMap<T>(map: Map<T>) {
const clone = createMap<T>();
copyProperties(map, clone);
return clone;
}
export function clone<T>(object: T): T {
const result: any = {};
for (const id in object) {
if (hasOwnProperty.call(object, id)) {
result[id] = (<any>object)[id];
}
}
return result;
}
export function extend<T1, T2>(first: T1 , second: T2): T1 & T2 {
const result: T1 & T2 = <any>{};
for (const id in second) if (hasOwnProperty.call(second, id)) {
(result as any)[id] = (second as any)[id];
}
for (const id in first) if (hasOwnProperty.call(first, id)) {
(result as any)[id] = (first as any)[id];
}
return result;
}
@ -450,9 +579,7 @@ namespace ts {
export let localizedDiagnosticMessages: Map<string> = undefined;
export function getLocaleSpecificMessage(message: DiagnosticMessage) {
return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key]
? localizedDiagnosticMessages[message.key]
: message.message;
return localizedDiagnosticMessages && localizedDiagnosticMessages[message.key] || message.message;
}
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: any[]): Diagnostic;
@ -941,7 +1068,7 @@ namespace ts {
* [^./] # matches everything up to the first . character (excluding directory seperators)
* (\\.(?!min\\.js$))? # matches . characters but not if they are part of the .min.js file extension
*/
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
const singleAsteriskRegexFragmentOther = "[^/]*";
export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude") {

View File

@ -157,9 +157,7 @@ namespace ts {
if (usedTypeDirectiveReferences) {
for (const directive in usedTypeDirectiveReferences) {
if (hasProperty(usedTypeDirectiveReferences, directive)) {
referencesOutput += `/// <reference types="${directive}" />${newLine}`;
}
referencesOutput += `/// <reference types="${directive}" />${newLine}`;
}
}
@ -269,10 +267,10 @@ namespace ts {
}
if (!usedTypeDirectiveReferences) {
usedTypeDirectiveReferences = {};
usedTypeDirectiveReferences = createMap<string>();
}
for (const directive of typeReferenceDirectives) {
if (!hasProperty(usedTypeDirectiveReferences, directive)) {
if (!(directive in usedTypeDirectiveReferences)) {
usedTypeDirectiveReferences[directive] = directive;
}
}
@ -441,7 +439,7 @@ namespace ts {
}
}
function emitEntityName(entityName: EntityName | PropertyAccessExpression) {
function emitEntityName(entityName: EntityNameOrEntityNameExpression) {
const visibilityResult = resolver.isEntityNameVisible(entityName,
// Aliases can be written asynchronously so use correct enclosing declaration
entityName.parent.kind === SyntaxKind.ImportEqualsDeclaration ? entityName.parent : enclosingDeclaration);
@ -452,9 +450,9 @@ namespace ts {
}
function emitExpressionWithTypeArguments(node: ExpressionWithTypeArguments) {
if (isSupportedExpressionWithTypeArguments(node)) {
if (isEntityNameExpression(node.expression)) {
Debug.assert(node.expression.kind === SyntaxKind.Identifier || node.expression.kind === SyntaxKind.PropertyAccessExpression);
emitEntityName(<Identifier | PropertyAccessExpression>node.expression);
emitEntityName(node.expression);
if (node.typeArguments) {
write("<");
emitCommaList(node.typeArguments, emitType);
@ -537,14 +535,14 @@ namespace ts {
// do not need to keep track of created temp names.
function getExportDefaultTempVariableName(): string {
const baseName = "_default";
if (!hasProperty(currentIdentifiers, baseName)) {
if (!(baseName in currentIdentifiers)) {
return baseName;
}
let count = 0;
while (true) {
count++;
const name = baseName + "_" + count;
if (!hasProperty(currentIdentifiers, name)) {
if (!(name in currentIdentifiers)) {
return name;
}
}
@ -1019,7 +1017,7 @@ namespace ts {
}
function emitTypeOfTypeReference(node: ExpressionWithTypeArguments) {
if (isSupportedExpressionWithTypeArguments(node)) {
if (isEntityNameExpression(node.expression)) {
emitTypeWithNewGetSymbolAccessibilityDiagnostic(node, getHeritageClauseVisibilityError);
}
else if (!isImplementsList && node.expression.kind === SyntaxKind.NullKeyword) {

View File

@ -2231,7 +2231,7 @@
"category": "Error",
"code": 4082
},
"Conflicting library definitions for '{0}' found at '{1}' and '{2}'. Copy the correct file to the 'typings' folder to resolve this conflict.": {
"Conflicting definitions for '{0}' found at '{1}' and '{2}'. Consider installing a specific version of this library to resolve the conflict.": {
"category": "Message",
"code": 4090
},
@ -2828,6 +2828,10 @@
"category": "Message",
"code": 6137
},
"Property '{0}' is declared but never used.": {
"category": "Error",
"code": 6138
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005

View File

@ -24,7 +24,7 @@ namespace ts {
Return = 1 << 3
}
const entities: Map<number> = {
const entities = createMap({
"quot": 0x0022,
"amp": 0x0026,
"apos": 0x0027,
@ -278,7 +278,7 @@ namespace ts {
"clubs": 0x2663,
"hearts": 0x2665,
"diams": 0x2666
};
});
// Flags enum to track count of temp variables and a few dedicated names
const enum TempFlags {
@ -407,7 +407,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
function isUniqueLocalName(name: string, container: Node): boolean {
for (let node = container; isNodeDescendentOf(node, container); node = node.nextContainer) {
if (node.locals && hasProperty(node.locals, name)) {
if (node.locals && name in node.locals) {
// We conservatively include alias symbols to cover cases where they're emitted as locals
if (node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) {
return false;
@ -489,13 +489,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
function setLabeledJump(state: ConvertedLoopState, isBreak: boolean, labelText: string, labelMarker: string): void {
if (isBreak) {
if (!state.labeledNonLocalBreaks) {
state.labeledNonLocalBreaks = {};
state.labeledNonLocalBreaks = createMap<string>();
}
state.labeledNonLocalBreaks[labelText] = labelMarker;
}
else {
if (!state.labeledNonLocalContinues) {
state.labeledNonLocalContinues = {};
state.labeledNonLocalContinues = createMap<string>();
}
state.labeledNonLocalContinues[labelText] = labelMarker;
}
@ -577,27 +577,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
const setSourceMapWriterEmit = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? changeSourceMapEmit : function (writer: SourceMapWriter) { };
const moduleEmitDelegates: Map<(node: SourceFile, emitRelativePathAsModuleName?: boolean) => void> = {
const moduleEmitDelegates = createMap<(node: SourceFile, emitRelativePathAsModuleName?: boolean) => void>({
[ModuleKind.ES6]: emitES6Module,
[ModuleKind.AMD]: emitAMDModule,
[ModuleKind.System]: emitSystemModule,
[ModuleKind.UMD]: emitUMDModule,
[ModuleKind.CommonJS]: emitCommonJSModule,
};
});
const bundleEmitDelegates: Map<(node: SourceFile, emitRelativePathAsModuleName?: boolean) => void> = {
const bundleEmitDelegates = createMap<(node: SourceFile, emitRelativePathAsModuleName?: boolean) => void>({
[ModuleKind.ES6]() {},
[ModuleKind.AMD]: emitAMDModule,
[ModuleKind.System]: emitSystemModule,
[ModuleKind.UMD]() {},
[ModuleKind.CommonJS]() {},
};
});
return doEmit;
function doEmit(jsFilePath: string, sourceMapFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean) {
sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit);
generatedNameSet = {};
generatedNameSet = createMap<string>();
nodeToGeneratedName = [];
decoratedClassAliases = [];
isOwnFileEmit = !isBundledEmit;
@ -669,8 +669,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
function isUniqueName(name: string): boolean {
return !resolver.hasGlobalName(name) &&
!hasProperty(currentFileIdentifiers, name) &&
!hasProperty(generatedNameSet, name);
!(name in currentFileIdentifiers) &&
!(name in generatedNameSet);
}
// Return the next available name in the pattern _a ... _z, _0, _1, ...
@ -2646,7 +2646,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
return false;
}
return !exportEquals && exportSpecifiers && hasProperty(exportSpecifiers, (<Identifier>node).text);
return !exportEquals && exportSpecifiers && (<Identifier>node).text in exportSpecifiers;
}
function emitPrefixUnaryExpression(node: PrefixUnaryExpression) {
@ -3257,13 +3257,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
// Don't initialize seen unless we have at least one element.
// Emit a comma to separate for all but the first element.
if (!seen) {
seen = {};
seen = createMap<string>();
}
else {
write(", ");
}
if (!hasProperty(seen, id.text)) {
if (!(id.text in seen)) {
emit(id);
seen[id.text] = id.text;
}
@ -3856,7 +3856,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
if (convertedLoopState) {
if (!convertedLoopState.labels) {
convertedLoopState.labels = {};
convertedLoopState.labels = createMap<string>();
}
convertedLoopState.labels[node.label.text] = node.label.text;
}
@ -3970,7 +3970,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
return;
}
if (!exportEquals && exportSpecifiers && hasProperty(exportSpecifiers, name.text)) {
if (!exportEquals && exportSpecifiers && name.text in exportSpecifiers) {
for (const specifier of exportSpecifiers[name.text]) {
writeLine();
emitStart(specifier.name);
@ -5311,18 +5311,7 @@ const _super = (function (geti, seti) {
emitSignatureParameters(ctor);
}
else {
// Based on EcmaScript6 section 14.5.14: Runtime Semantics: ClassDefinitionEvaluation.
// If constructor is empty, then,
// If ClassHeritageopt is present, then
// Let constructor be the result of parsing the String "constructor(... args){ super (...args);}" using the syntactic grammar with the goal symbol MethodDefinition.
// Else,
// Let constructor be the result of parsing the String "constructor( ){ }" using the syntactic grammar with the goal symbol MethodDefinition
if (baseTypeElement) {
write("(...args)");
}
else {
write("()");
}
write("()");
}
}
@ -5360,7 +5349,7 @@ const _super = (function (geti, seti) {
write("_super.apply(this, arguments);");
}
else {
write("super(...args);");
write("super(...arguments);");
}
emitEnd(baseTypeElement);
}
@ -6461,7 +6450,7 @@ const _super = (function (geti, seti) {
* Here we check if alternative name was provided for a given moduleName and return it if possible.
*/
function tryRenameExternalModule(moduleName: LiteralExpression): string {
if (renamedDependencies && hasProperty(renamedDependencies, moduleName.text)) {
if (renamedDependencies && moduleName.text in renamedDependencies) {
return `"${renamedDependencies[moduleName.text]}"`;
}
return undefined;
@ -6803,7 +6792,7 @@ const _super = (function (geti, seti) {
function collectExternalModuleInfo(sourceFile: SourceFile) {
externalImports = [];
exportSpecifiers = {};
exportSpecifiers = createMap<ExportSpecifier[]>();
exportEquals = undefined;
hasExportStarsToExportValues = false;
for (const node of sourceFile.statements) {
@ -6941,7 +6930,7 @@ const _super = (function (geti, seti) {
}
// local names set should only be added if we have anything exported
if (!exportedDeclarations && isEmpty(exportSpecifiers)) {
if (!exportedDeclarations && !someProperties(exportSpecifiers)) {
// no exported declarations (export var ...) or export specifiers (export {x})
// check if we have any non star export declarations.
let hasExportDeclarationWithExportClause = false;
@ -7081,7 +7070,7 @@ const _super = (function (geti, seti) {
if (hoistedVars) {
writeLine();
write("var ");
const seen: Map<string> = {};
const seen = createMap<string>();
for (let i = 0; i < hoistedVars.length; i++) {
const local = hoistedVars[i];
const name = local.kind === SyntaxKind.Identifier
@ -7091,7 +7080,7 @@ const _super = (function (geti, seti) {
if (name) {
// do not emit duplicate entries (in case of declaration merging) in the list of hoisted variables
const text = unescapeIdentifier(name.text);
if (hasProperty(seen, text)) {
if (text in seen) {
continue;
}
else {
@ -7447,7 +7436,7 @@ const _super = (function (geti, seti) {
writeModuleName(node, emitRelativePathAsModuleName);
write("[");
const groupIndices: Map<number> = {};
const groupIndices = createMap<number>();
const dependencyGroups: DependencyGroup[] = [];
for (let i = 0; i < externalImports.length; i++) {
@ -7460,7 +7449,7 @@ const _super = (function (geti, seti) {
// for deduplication purposes in key remove leading and trailing quotes so 'a' and "a" will be considered the same
const key = text.substr(1, text.length - 2);
if (hasProperty(groupIndices, key)) {
if (key in groupIndices) {
// deduplicate/group entries in dependency list by the dependency name
const groupIndex = groupIndices[key];
dependencyGroups[groupIndex].push(externalImports[i]);

View File

@ -421,10 +421,10 @@ namespace ts {
}
export function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes = false, scriptKind?: ScriptKind): SourceFile {
const start = performance.mark();
performance.mark("beforeParse");
const result = Parser.parseSourceFile(fileName, sourceText, languageVersion, /*syntaxCursor*/ undefined, setParentNodes, scriptKind);
performance.measure("Parse", start);
performance.mark("afterParse");
performance.measure("Parse", "beforeParse", "afterParse");
return result;
}
@ -595,7 +595,7 @@ namespace ts {
parseDiagnostics = [];
parsingContext = 0;
identifiers = {};
identifiers = createMap<string>();
identifierCount = 0;
nodeCount = 0;
@ -1084,7 +1084,7 @@ namespace ts {
function internIdentifier(text: string): string {
text = escapeIdentifier(text);
return hasProperty(identifiers, text) ? identifiers[text] : (identifiers[text] = text);
return identifiers[text] || (identifiers[text] = text);
}
// An identifier that starts with two underscores has an extra underscore character prepended to it to avoid issues

View File

@ -6,71 +6,57 @@ namespace ts {
}
/*@internal*/
/** Performance measurements for the compiler. */
namespace ts.performance {
/** Performance measurements for the compiler. */
declare const onProfilerEvent: { (markName: string): void; profiler: boolean; };
let profilerEvent: (markName: string) => void;
let counters: Map<number>;
const profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent.profiler === true
? onProfilerEvent
: (markName: string) => { };
let enabled = false;
let profilerStart = 0;
let counts: Map<number>;
let marks: Map<number>;
let measures: Map<number>;
/**
* Emit a performance event if ts-profiler is connected. This is primarily used
* to generate heap snapshots.
* Marks a performance event.
*
* @param eventName A name for the event.
* @param markName The name of the mark.
*/
export function emit(eventName: string) {
if (profilerEvent) {
profilerEvent(eventName);
export function mark(markName: string) {
if (enabled) {
marks[markName] = timestamp();
counts[markName] = (counts[markName] || 0) + 1;
profilerEvent(markName);
}
}
/**
* Increments a counter with the specified name.
*
* @param counterName The name of the counter.
*/
export function increment(counterName: string) {
if (counters) {
counters[counterName] = (getProperty(counters, counterName) || 0) + 1;
}
}
/**
* Gets the value of the counter with the specified name.
*
* @param counterName The name of the counter.
*/
export function getCount(counterName: string) {
return counters && getProperty(counters, counterName) || 0;
}
/**
* Marks the start of a performance measurement.
*/
export function mark() {
return measures ? timestamp() : 0;
}
/**
* Adds a performance measurement with the specified name.
*
* @param measureName The name of the performance measurement.
* @param marker The timestamp of the starting mark.
* @param startMarkName The name of the starting mark. If not supplied, the point at which the
* profiler was enabled is used.
* @param endMarkName The name of the ending mark. If not supplied, the current timestamp is
* used.
*/
export function measure(measureName: string, marker: number) {
if (measures) {
measures[measureName] = (getProperty(measures, measureName) || 0) + (timestamp() - marker);
export function measure(measureName: string, startMarkName?: string, endMarkName?: string) {
if (enabled) {
const end = endMarkName && marks[endMarkName] || timestamp();
const start = startMarkName && marks[startMarkName] || profilerStart;
measures[measureName] = (measures[measureName] || 0) + (end - start);
}
}
/**
* Iterate over each measure, performing some action
*
* @param cb The action to perform for each measure
* Gets the number of times a marker was encountered.
*
* @param markName The name of the mark.
*/
export function forEachMeasure(cb: (measureName: string, duration: number) => void) {
return forEachKey(measures, key => cb(key, measures[key]));
export function getCount(markName: string) {
return counts && counts[markName] || 0;
}
/**
@ -79,31 +65,31 @@ namespace ts.performance {
* @param measureName The name of the measure whose durations should be accumulated.
*/
export function getDuration(measureName: string) {
return measures && getProperty(measures, measureName) || 0;
return measures && measures[measureName] || 0;
}
/**
* Iterate over each measure, performing some action
*
* @param cb The action to perform for each measure
*/
export function forEachMeasure(cb: (measureName: string, duration: number) => void) {
for (const key in measures) {
cb(key, measures[key]);
}
}
/** Enables (and resets) performance measurements for the compiler. */
export function enable() {
counters = { };
measures = {
"I/O Read": 0,
"I/O Write": 0,
"Program": 0,
"Parse": 0,
"Bind": 0,
"Check": 0,
"Emit": 0,
};
profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent.profiler === true
? onProfilerEvent
: undefined;
counts = createMap<number>();
marks = createMap<number>();
measures = createMap<number>();
enabled = true;
profilerStart = timestamp();
}
/** Disables (and clears) performance measurements for the compiler. */
/** Disables performance measurements for the compiler. */
export function disable() {
counters = undefined;
measures = undefined;
profilerEvent = undefined;
enabled = false;
}
}

View File

@ -119,49 +119,31 @@ namespace ts {
}
function tryReadTypesSection(packageJsonPath: string, baseDirectory: string, state: ModuleResolutionState): string {
let jsonContent: { typings?: string, types?: string, main?: string };
try {
const jsonText = state.host.readFile(packageJsonPath);
jsonContent = jsonText ? <{ typings?: string, types?: string, main?: string }>JSON.parse(jsonText) : {};
}
catch (e) {
// gracefully handle if readFile fails or returns not JSON
jsonContent = {};
const jsonContent = readJson(packageJsonPath, state.host);
function tryReadFromField(fieldName: string) {
if (hasProperty(jsonContent, fieldName)) {
const typesFile = (<any>jsonContent)[fieldName];
if (typeof typesFile === "string") {
const typesFilePath = normalizePath(combinePaths(baseDirectory, typesFile));
if (state.traceEnabled) {
trace(state.host, Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath);
}
return typesFilePath;
}
else {
if (state.traceEnabled) {
trace(state.host, Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile);
}
}
}
}
let typesFile: string;
let fieldName: string;
// first try to read content of 'typings' section (backward compatibility)
if (jsonContent.typings) {
if (typeof jsonContent.typings === "string") {
fieldName = "typings";
typesFile = jsonContent.typings;
}
else {
if (state.traceEnabled) {
trace(state.host, Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, "typings", typeof jsonContent.typings);
}
}
}
// then read 'types'
if (!typesFile && jsonContent.types) {
if (typeof jsonContent.types === "string") {
fieldName = "types";
typesFile = jsonContent.types;
}
else {
if (state.traceEnabled) {
trace(state.host, Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, "types", typeof jsonContent.types);
}
}
}
if (typesFile) {
const typesFilePath = normalizePath(combinePaths(baseDirectory, typesFile));
if (state.traceEnabled) {
trace(state.host, Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath);
}
const typesFilePath = tryReadFromField("typings") || tryReadFromField("types");
if (typesFilePath) {
return typesFilePath;
}
// Use the main module for inferring types if no types package specified and the allowJs is set
if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") {
if (state.traceEnabled) {
@ -173,6 +155,17 @@ namespace ts {
return undefined;
}
function readJson(path: string, host: ModuleResolutionHost): { typings?: string, types?: string, main?: string } {
try {
const jsonText = host.readFile(path);
return jsonText ? JSON.parse(jsonText) : {};
}
catch (e) {
// gracefully handle if readFile fails or returns not JSON
return {};
}
}
const typeReferenceExtensions = [".d.ts"];
function getEffectiveTypeRoots(options: CompilerOptions, host: ModuleResolutionHost) {
@ -508,7 +501,7 @@ namespace ts {
if (state.traceEnabled) {
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
}
matchedPattern = matchPatternOrExact(getKeys(state.compilerOptions.paths), moduleName);
matchedPattern = matchPatternOrExact(getOwnKeys(state.compilerOptions.paths), moduleName);
}
if (matchedPattern) {
@ -717,7 +710,7 @@ namespace ts {
}
function loadNodeModuleFromDirectory(extensions: string[], candidate: string, failedLookupLocation: string[], onlyRecordFailures: boolean, state: ModuleResolutionState): string {
const packageJsonPath = combinePaths(candidate, "package.json");
const packageJsonPath = pathToPackageJson(candidate);
const directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host);
if (directoryExists && state.host.fileExists(packageJsonPath)) {
if (state.traceEnabled) {
@ -747,6 +740,10 @@ namespace ts {
return loadModuleFromFile(combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state);
}
function pathToPackageJson(directory: string): string {
return combinePaths(directory, "package.json");
}
function loadModuleFromNodeModulesFolder(moduleName: string, directory: string, failedLookupLocations: string[], state: ModuleResolutionState): string {
const nodeModulesFolder = combinePaths(directory, "node_modules");
const nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host);
@ -846,7 +843,7 @@ namespace ts {
}
export function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost {
const existingDirectories: Map<boolean> = {};
const existingDirectories = createMap<boolean>();
function getCanonicalFileName(fileName: string): string {
// if underlying system can distinguish between two files whose names differs only in cases then file name already in canonical form.
@ -860,9 +857,10 @@ namespace ts {
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile {
let text: string;
try {
const start = performance.mark();
performance.mark("beforeIORead");
text = sys.readFile(fileName, options.charset);
performance.measure("I/O Read", start);
performance.mark("afterIORead");
performance.measure("I/O Read", "beforeIORead", "afterIORead");
}
catch (e) {
if (onError) {
@ -877,7 +875,7 @@ namespace ts {
}
function directoryExists(directoryPath: string): boolean {
if (hasProperty(existingDirectories, directoryPath)) {
if (directoryPath in existingDirectories) {
return true;
}
if (sys.directoryExists(directoryPath)) {
@ -899,13 +897,13 @@ namespace ts {
function writeFileIfUpdated(fileName: string, data: string, writeByteOrderMark: boolean): void {
if (!outputFingerprints) {
outputFingerprints = {};
outputFingerprints = createMap<OutputFingerprint>();
}
const hash = sys.createHash(data);
const mtimeBefore = sys.getModifiedTime(fileName);
if (mtimeBefore && hasProperty(outputFingerprints, fileName)) {
if (mtimeBefore && fileName in outputFingerprints) {
const fingerprint = outputFingerprints[fileName];
// If output has not been changed, and the file has no external modification
@ -929,7 +927,7 @@ namespace ts {
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
try {
const start = performance.mark();
performance.mark("beforeIOWrite");
ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName)));
if (isWatchSet(options) && sys.createHash && sys.getModifiedTime) {
@ -939,7 +937,8 @@ namespace ts {
sys.writeFile(fileName, data, writeByteOrderMark);
}
performance.measure("I/O Write", start);
performance.mark("afterIOWrite");
performance.measure("I/O Write", "beforeIOWrite", "afterIOWrite");
}
catch (e) {
if (onError) {
@ -1040,47 +1039,47 @@ namespace ts {
return [];
}
const resolutions: T[] = [];
const cache: Map<T> = {};
const cache = createMap<T>();
for (const name of names) {
let result: T;
if (hasProperty(cache, name)) {
result = cache[name];
}
else {
result = loader(name, containingFile);
cache[name] = result;
}
const result = name in cache
? cache[name]
: cache[name] = loader(name, containingFile);
resolutions.push(result);
}
return resolutions;
}
function getInferredTypesRoot(options: CompilerOptions, rootFiles: string[], host: CompilerHost) {
return computeCommonSourceDirectoryOfFilenames(rootFiles, host.getCurrentDirectory(), f => host.getCanonicalFileName(f));
}
/**
* Given a set of options and a set of root files, returns the set of type directive names
* Given a set of options, returns the set of type directive names
* that should be included for this program automatically.
* This list could either come from the config file,
* or from enumerating the types root + initial secondary types lookup location.
* More type directives might appear in the program later as a result of loading actual source files;
* this list is only the set of defaults that are implicitly included.
*/
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, rootFiles: string[], host: CompilerHost): string[] {
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
// Use explicit type list from tsconfig.json
if (options.types) {
return options.types;
}
// Walk the primary type lookup locations
let result: string[] = [];
const result: string[] = [];
if (host.directoryExists && host.getDirectories) {
const typeRoots = getEffectiveTypeRoots(options, host);
if (typeRoots) {
for (const root of typeRoots) {
if (host.directoryExists(root)) {
result = result.concat(host.getDirectories(root));
for (const typeDirectivePath of host.getDirectories(root)) {
const normalized = normalizePath(typeDirectivePath);
const packageJsonPath = pathToPackageJson(combinePaths(root, normalized));
// tslint:disable-next-line:no-null-keyword
const isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null;
if (!isNotNeededPackage) {
// Return just the type directive names
result.push(getBaseFileName(normalized));
}
}
}
}
}
@ -1096,7 +1095,7 @@ namespace ts {
let noDiagnosticsTypeChecker: TypeChecker;
let classifiableNames: Map<string>;
let resolvedTypeReferenceDirectives: Map<ResolvedTypeReferenceDirective> = {};
let resolvedTypeReferenceDirectives = createMap<ResolvedTypeReferenceDirective>();
let fileProcessingDiagnostics = createDiagnosticCollection();
// The below settings are to track if a .js file should be add to the program if loaded via searching under node_modules.
@ -1111,12 +1110,12 @@ namespace ts {
// If a module has some of its imports skipped due to being at the depth limit under node_modules, then track
// this, as it may be imported at a shallower depth later, and then it will need its skipped imports processed.
const modulesWithElidedImports: Map<boolean> = {};
const modulesWithElidedImports = createMap<boolean>();
// Track source files that are source files found by searching under node_modules, as these shouldn't be compiled.
const sourceFilesFoundSearchingNodeModules: Map<boolean> = {};
const sourceFilesFoundSearchingNodeModules = createMap<boolean>();
const start = performance.mark();
performance.mark("beforeProgram");
host = host || createCompilerHost(options);
@ -1155,11 +1154,11 @@ namespace ts {
forEach(rootNames, name => processRootFile(name, /*isDefaultLib*/ false));
// load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders
const typeReferences: string[] = getAutomaticTypeDirectiveNames(options, rootNames, host);
const typeReferences: string[] = getAutomaticTypeDirectiveNames(options, host);
if (typeReferences) {
const inferredRoot = getInferredTypesRoot(options, rootNames, host);
const containingFilename = combinePaths(inferredRoot, "__inferred type names__.ts");
// This containingFilename needs to match with the one used in managed-side
const containingFilename = combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts");
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
for (let i = 0; i < typeReferences.length; i++) {
processTypeReferenceDirective(typeReferences[i], resolutions[i]);
@ -1214,8 +1213,8 @@ namespace ts {
};
verifyCompilerOptions();
performance.measure("Program", start);
performance.mark("afterProgram");
performance.measure("Program", "beforeProgram", "afterProgram");
return program;
@ -1242,10 +1241,10 @@ namespace ts {
if (!classifiableNames) {
// Initialize a checker so that all our files are bound.
getTypeChecker();
classifiableNames = {};
classifiableNames = createMap<string>();
for (const sourceFile of files) {
copyMap(sourceFile.classifiableNames, classifiableNames);
copyProperties(sourceFile.classifiableNames, classifiableNames);
}
}
@ -1273,7 +1272,7 @@ namespace ts {
(oldOptions.maxNodeModuleJsDepth !== options.maxNodeModuleJsDepth) ||
!arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) ||
!arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) ||
!mapIsEqualTo(oldOptions.paths, options.paths)) {
!equalOwnProperties(oldOptions.paths, options.paths)) {
return false;
}
@ -1395,7 +1394,7 @@ namespace ts {
getSourceFile: program.getSourceFile,
getSourceFileByPath: program.getSourceFileByPath,
getSourceFiles: program.getSourceFiles,
isSourceFileFromExternalLibrary: (file: SourceFile) => !!lookUp(sourceFilesFoundSearchingNodeModules, file.path),
isSourceFileFromExternalLibrary: (file: SourceFile) => !!sourceFilesFoundSearchingNodeModules[file.path],
writeFile: writeFileCallback || (
(fileName, data, writeByteOrderMark, onError, sourceFiles) => host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)),
isEmitBlocked,
@ -1458,14 +1457,15 @@ namespace ts {
// checked is to not pass the file to getEmitResolver.
const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile);
const start = performance.mark();
performance.mark("beforeEmit");
const emitResult = emitFiles(
emitResolver,
getEmitHost(writeFileCallback),
sourceFile);
performance.measure("Emit", start);
performance.mark("afterEmit");
performance.measure("Emit", "beforeEmit", "afterEmit");
return emitResult;
}
@ -1932,7 +1932,7 @@ namespace ts {
// If the file was previously found via a node_modules search, but is now being processed as a root file,
// then everything it sucks in may also be marked incorrectly, and needs to be checked again.
if (file && lookUp(sourceFilesFoundSearchingNodeModules, file.path) && currentNodeModulesDepth == 0) {
if (file && sourceFilesFoundSearchingNodeModules[file.path] && currentNodeModulesDepth == 0) {
sourceFilesFoundSearchingNodeModules[file.path] = false;
if (!options.noResolve) {
processReferencedFiles(file, getDirectoryPath(fileName), isDefaultLib);
@ -1943,7 +1943,7 @@ namespace ts {
processImportedModules(file, getDirectoryPath(fileName));
}
// See if we need to reprocess the imports due to prior skipped imports
else if (file && lookUp(modulesWithElidedImports, file.path)) {
else if (file && modulesWithElidedImports[file.path]) {
if (currentNodeModulesDepth < maxNodeModulesJsDepth) {
modulesWithElidedImports[file.path] = false;
processImportedModules(file, getDirectoryPath(fileName));
@ -2010,15 +2010,16 @@ namespace ts {
}
function processTypeReferenceDirectives(file: SourceFile) {
const typeDirectives = map(file.typeReferenceDirectives, l => l.fileName);
const typeDirectives = map(file.typeReferenceDirectives, ref => ref.fileName.toLocaleLowerCase());
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeDirectives, file.fileName);
for (let i = 0; i < typeDirectives.length; i++) {
const ref = file.typeReferenceDirectives[i];
const resolvedTypeReferenceDirective = resolutions[i];
// store resolved type directive on the file
setResolvedTypeReferenceDirective(file, ref.fileName, resolvedTypeReferenceDirective);
processTypeReferenceDirective(ref.fileName, resolvedTypeReferenceDirective, file, ref.pos, ref.end);
const fileName = ref.fileName.toLocaleLowerCase();
setResolvedTypeReferenceDirective(file, fileName, resolvedTypeReferenceDirective);
processTypeReferenceDirective(fileName, resolvedTypeReferenceDirective, file, ref.pos, ref.end);
}
}
@ -2043,7 +2044,7 @@ namespace ts {
const otherFileText = host.readFile(resolvedTypeReferenceDirective.resolvedFileName);
if (otherFileText !== getSourceFile(previousResolution.resolvedFileName).text) {
fileProcessingDiagnostics.add(createDiagnostic(refFile, refPos, refEnd,
Diagnostics.Conflicting_library_definitions_for_0_found_at_1_and_2_Copy_the_correct_file_to_the_typings_folder_to_resolve_this_conflict,
Diagnostics.Conflicting_definitions_for_0_found_at_1_and_2_Consider_installing_a_specific_version_of_this_library_to_resolve_the_conflict,
typeReferenceDirective,
resolvedTypeReferenceDirective.resolvedFileName,
previousResolution.resolvedFileName
@ -2083,7 +2084,7 @@ namespace ts {
function processImportedModules(file: SourceFile, basePath: string) {
collectExternalModuleReferences(file);
if (file.imports.length || file.moduleAugmentations.length) {
file.resolvedModules = {};
file.resolvedModules = createMap<ResolvedModule>();
const moduleNames = map(concatenate(file.imports, file.moduleAugmentations), getTextOfLiteral);
const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(file.fileName, currentDirectory));
for (let i = 0; i < moduleNames.length; i++) {

View File

@ -55,7 +55,7 @@ namespace ts {
tryScan<T>(callback: () => T): T;
}
const textToToken: Map<SyntaxKind> = {
const textToToken = createMap({
"abstract": SyntaxKind.AbstractKeyword,
"any": SyntaxKind.AnyKeyword,
"as": SyntaxKind.AsKeyword,
@ -179,7 +179,7 @@ namespace ts {
"|=": SyntaxKind.BarEqualsToken,
"^=": SyntaxKind.CaretEqualsToken,
"@": SyntaxKind.AtToken,
};
});
/*
As per ECMAScript Language Specification 3th Edition, Section 7.6: Identifiers
@ -274,9 +274,7 @@ namespace ts {
function makeReverseMap(source: Map<number>): string[] {
const result: string[] = [];
for (const name in source) {
if (source.hasOwnProperty(name)) {
result[source[name]] = name;
}
result[source[name]] = name;
}
return result;
}

View File

@ -46,6 +46,7 @@ namespace ts {
export function createSourceMapWriter(host: EmitHost, writer: EmitTextWriter): SourceMapWriter {
const compilerOptions = host.getCompilerOptions();
const extendedDiagnostics = compilerOptions.extendedDiagnostics;
let currentSourceFile: SourceFile;
let sourceMapDir: string; // The directory in which sourcemap will be
let stopOverridingSpan = false;
@ -240,7 +241,9 @@ namespace ts {
return;
}
const start = performance.mark();
if (extendedDiagnostics) {
performance.mark("beforeSourcemap");
}
const sourceLinePos = getLineAndCharacterOfPosition(currentSourceFile, pos);
@ -282,7 +285,10 @@ namespace ts {
updateLastEncodedAndRecordedSpans();
performance.measure("Source Map", start);
if (extendedDiagnostics) {
performance.mark("afterSourcemap");
performance.measure("Source Map", "beforeSourcemap", "afterSourcemap");
}
}
function getStartPos(range: TextRange) {

View File

@ -233,15 +233,15 @@ namespace ts {
const useNonPollingWatchers = process.env["TSC_NONPOLLING_WATCHER"];
function createWatchedFileSet() {
const dirWatchers: Map<DirectoryWatcher> = {};
const dirWatchers = createMap<DirectoryWatcher>();
// One file can have multiple watchers
const fileWatcherCallbacks: Map<FileWatcherCallback[]> = {};
const fileWatcherCallbacks = createMap<FileWatcherCallback[]>();
return { addFile, removeFile };
function reduceDirWatcherRefCountForFile(fileName: string) {
const dirName = getDirectoryPath(fileName);
if (hasProperty(dirWatchers, dirName)) {
const watcher = dirWatchers[dirName];
const watcher = dirWatchers[dirName];
if (watcher) {
watcher.referenceCount -= 1;
if (watcher.referenceCount <= 0) {
watcher.close();
@ -251,13 +251,12 @@ namespace ts {
}
function addDirWatcher(dirPath: string): void {
if (hasProperty(dirWatchers, dirPath)) {
const watcher = dirWatchers[dirPath];
let watcher = dirWatchers[dirPath];
if (watcher) {
watcher.referenceCount += 1;
return;
}
const watcher: DirectoryWatcher = _fs.watch(
watcher = _fs.watch(
dirPath,
{ persistent: true },
(eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, dirPath)
@ -268,12 +267,7 @@ namespace ts {
}
function addFileWatcherCallback(filePath: string, callback: FileWatcherCallback): void {
if (hasProperty(fileWatcherCallbacks, filePath)) {
fileWatcherCallbacks[filePath].push(callback);
}
else {
fileWatcherCallbacks[filePath] = [callback];
}
(fileWatcherCallbacks[filePath] || (fileWatcherCallbacks[filePath] = [])).push(callback);
}
function addFile(fileName: string, callback: FileWatcherCallback): WatchedFile {
@ -289,8 +283,9 @@ namespace ts {
}
function removeFileWatcherCallback(filePath: string, callback: FileWatcherCallback) {
if (hasProperty(fileWatcherCallbacks, filePath)) {
const newCallbacks = copyListRemovingItem(callback, fileWatcherCallbacks[filePath]);
const callbacks = fileWatcherCallbacks[filePath];
if (callbacks) {
const newCallbacks = copyListRemovingItem(callback, callbacks);
if (newCallbacks.length === 0) {
delete fileWatcherCallbacks[filePath];
}
@ -306,7 +301,7 @@ namespace ts {
? undefined
: ts.getNormalizedAbsolutePath(relativeFileName, baseDirPath);
// Some applications save a working file via rename operations
if ((eventName === "change" || eventName === "rename") && hasProperty(fileWatcherCallbacks, fileName)) {
if ((eventName === "change" || eventName === "rename") && fileWatcherCallbacks[fileName]) {
for (const fileCallback of fileWatcherCallbacks[fileName]) {
fileCallback(fileName);
}

View File

@ -122,11 +122,11 @@ namespace ts {
const gutterSeparator = " ";
const resetEscapeSequence = "\u001b[0m";
const ellipsis = "...";
const categoryFormatMap: Map<string> = {
const categoryFormatMap = createMap<string>({
[DiagnosticCategory.Warning]: yellowForegroundEscapeSequence,
[DiagnosticCategory.Error]: redForegroundEscapeSequence,
[DiagnosticCategory.Message]: blueForegroundEscapeSequence,
};
});
function formatAndReset(text: string, formatStyle: string) {
return formatStyle + text + resetEscapeSequence;
@ -432,7 +432,7 @@ namespace ts {
}
// reset the cache of existing files
cachedExistingFiles = {};
cachedExistingFiles = createMap<boolean>();
const compileResult = compile(rootFileNames, compilerOptions, compilerHost);
@ -445,10 +445,9 @@ namespace ts {
}
function cachedFileExists(fileName: string): boolean {
if (hasProperty(cachedExistingFiles, fileName)) {
return cachedExistingFiles[fileName];
}
return cachedExistingFiles[fileName] = hostFileExists(fileName);
return fileName in cachedExistingFiles
? cachedExistingFiles[fileName]
: cachedExistingFiles[fileName] = hostFileExists(fileName);
}
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void) {
@ -676,7 +675,7 @@ namespace ts {
const usageColumn: string[] = []; // Things like "-d, --declaration" go in here.
const descriptionColumn: string[] = [];
const optionsDescriptionMap: Map<string[]> = {}; // Map between option.description and list of option.type if it is a kind
const optionsDescriptionMap = createMap<string[]>(); // Map between option.description and list of option.type if it is a kind
for (let i = 0; i < optsList.length; i++) {
const option = optsList[i];
@ -704,9 +703,10 @@ namespace ts {
description = getDiagnosticText(option.description);
const options: string[] = [];
const element = (<CommandLineOptionOfListType>option).element;
forEachKey(<Map<number | string>>element.type, key => {
const typeMap = <Map<number | string>>element.type;
for (const key in typeMap) {
options.push(`'${key}'`);
});
}
optionsDescriptionMap[description] = options;
}
else {
@ -786,7 +786,7 @@ namespace ts {
return;
function serializeCompilerOptions(options: CompilerOptions): Map<string | number | boolean> {
const result: Map<string | number | boolean> = {};
const result = createMap<string | number | boolean>();
const optionsNameMap = getOptionNameMap().optionNameMap;
for (const name in options) {
@ -812,9 +812,8 @@ namespace ts {
// Enum
const typeMap = <Map<number>>optionDefinition.type;
for (const key in typeMap) {
if (hasProperty(typeMap, key)) {
if (typeMap[key] === value)
result[name] = key;
if (typeMap[key] === value) {
result[name] = key;
}
}
}

View File

@ -1,9 +1,13 @@
namespace ts {
export interface Map<T> {
export interface MapLike<T> {
[index: string]: T;
}
export interface Map<T> extends MapLike<T> {
__mapBrand: any;
}
// branded string type used to store absolute, normalized and canonicalized paths
// arbitrary file name can be converted to Path via toPath function
export type Path = string & { __pathBrand: any };
@ -982,13 +986,19 @@ namespace ts {
multiLine?: boolean;
}
export type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression;
export type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression;
// @kind(SyntaxKind.PropertyAccessExpression)
export interface PropertyAccessExpression extends MemberExpression, Declaration {
expression: LeftHandSideExpression;
name: Identifier;
}
export type IdentifierOrPropertyAccess = Identifier | PropertyAccessExpression;
/** Brand for a PropertyAccessExpression which, like a QualifiedName, consists of a sequence of identifiers separated by dots. */
export interface PropertyAccessEntityNameExpression extends PropertyAccessExpression {
_propertyAccessExpressionLikeQualifiedNameBrand?: any;
expression: EntityNameExpression;
}
// @kind(SyntaxKind.ElementAccessExpression)
export interface ElementAccessExpression extends MemberExpression {
@ -1606,6 +1616,16 @@ namespace ts {
antecedent: FlowNode;
}
export type FlowType = Type | IncompleteType;
// Incomplete types occur during control flow analysis of loops. An IncompleteType
// is distinguished from a regular type by a flags value of zero. Incomplete type
// objects are internal to the getFlowTypeOfRefecence function and never escape it.
export interface IncompleteType {
flags: TypeFlags; // No flags set
type: Type; // The type marked incomplete
}
export interface AmdDependency {
path: string;
name: string;
@ -2021,7 +2041,7 @@ namespace ts {
writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
writeBaseConstructorTypeOfClass(node: ClassLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter): void;
isSymbolAccessible(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags): SymbolAccessibilityResult;
isEntityNameVisible(entityName: EntityName | Expression, enclosingDeclaration: Node): SymbolVisibilityResult;
isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult;
// Returns the constant value this property access resolves to, or 'undefined' for a non-constant
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number;
getReferencedValueDeclaration(reference: Identifier): Declaration;
@ -2030,7 +2050,7 @@ namespace ts {
moduleExportsSomeValue(moduleReferenceExpression: Expression): boolean;
isArgumentsLocalBinding(node: Identifier): boolean;
getExternalModuleFileFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration): SourceFile;
getTypeReferenceDirectivesForEntityName(name: EntityName | PropertyAccessExpression): string[];
getTypeReferenceDirectivesForEntityName(name: EntityNameOrEntityNameExpression): string[];
getTypeReferenceDirectivesForSymbol(symbol: Symbol, meaning?: SymbolFlags): string[];
}
@ -2138,6 +2158,7 @@ namespace ts {
/* @internal */ constEnumOnlyModule?: boolean; // True if module contains only const enums or other modules with only const enums
/* @internal */ isReferenced?: boolean; // True if the symbol is referenced elsewhere
/* @internal */ isReplaceableByMethod?: boolean; // Can this Javascript class property be replaced by a method symbol?
/* @internal */ isAssigned?: boolean; // True if the symbol is a parameter with assignments
}
/* @internal */
@ -2151,6 +2172,8 @@ namespace ts {
mapper?: TypeMapper; // Type mapper for instantiation alias
referenced?: boolean; // True if alias symbol has been referenced as a value
containingType?: UnionOrIntersectionType; // Containing union or intersection type for synthetic property
hasCommonType?: boolean; // True if constituents of synthetic property all have same type
isDiscriminantProperty?: boolean; // True if discriminant synthetic property
resolvedExports?: SymbolTable; // Resolved exports of module
exportsChecked?: boolean; // True if exports of external module have been checked
isDeclarationWithCollidingName?: boolean; // True if symbol is block scoped redeclaration
@ -2161,9 +2184,7 @@ namespace ts {
/* @internal */
export interface TransientSymbol extends Symbol, SymbolLinks { }
export interface SymbolTable {
[index: string]: Symbol;
}
export type SymbolTable = Map<Symbol>;
/** Represents a "prefix*suffix" pattern. */
/* @internal */
@ -2190,23 +2211,24 @@ namespace ts {
AsyncMethodWithSuper = 0x00000800, // An async method that reads a value from a member of 'super'.
AsyncMethodWithSuperBinding = 0x00001000, // An async method that assigns a value to a member of 'super'.
CaptureArguments = 0x00002000, // Lexical 'arguments' used in body (for async functions)
EnumValuesComputed = 0x00004000, // Values for enum members have been computed, and any errors have been reported for them.
LexicalModuleMergesWithClass = 0x00008000, // Instantiated lexical module declaration is merged with a previous class declaration.
LoopWithCapturedBlockScopedBinding = 0x00010000, // Loop that contains block scoped variable captured in closure
CapturedBlockScopedBinding = 0x00020000, // Block-scoped binding that is captured in some function
BlockScopedBindingInLoop = 0x00040000, // Block-scoped binding with declaration nested inside iteration statement
ClassWithBodyScopedClassBinding = 0x00080000, // Decorated class that contains a binding to itself inside of the class body.
BodyScopedClassBinding = 0x00100000, // Binding to a decorated class inside of the class's body.
NeedsLoopOutParameter = 0x00200000, // Block scoped binding whose value should be explicitly copied outside of the converted loop
EnumValuesComputed = 0x00004000, // Values for enum members have been computed, and any errors have been reported for them.
LexicalModuleMergesWithClass = 0x00008000, // Instantiated lexical module declaration is merged with a previous class declaration.
LoopWithCapturedBlockScopedBinding = 0x00010000, // Loop that contains block scoped variable captured in closure
CapturedBlockScopedBinding = 0x00020000, // Block-scoped binding that is captured in some function
BlockScopedBindingInLoop = 0x00040000, // Block-scoped binding with declaration nested inside iteration statement
ClassWithBodyScopedClassBinding = 0x00080000, // Decorated class that contains a binding to itself inside of the class body.
BodyScopedClassBinding = 0x00100000, // Binding to a decorated class inside of the class's body.
NeedsLoopOutParameter = 0x00200000, // Block scoped binding whose value should be explicitly copied outside of the converted loop
AssignmentsMarked = 0x00400000, // Parameter assignments have been marked
}
/* @internal */
export interface NodeLinks {
flags?: NodeCheckFlags; // Set of flags specific to Node
resolvedType?: Type; // Cached type of type node
resolvedSignature?: Signature; // Cached signature of signature node or call expression
resolvedSymbol?: Symbol; // Cached name resolution result
resolvedIndexInfo?: IndexInfo; // Cached indexing info resolution result
flags?: NodeCheckFlags; // Set of flags specific to Node
enumMemberValue?: number; // Constant value of enum member
isVisible?: boolean; // Is this node visible
hasReportedStatementInAmbientContext?: boolean; // Cache boolean if we report statements in ambient context
@ -2360,6 +2382,7 @@ namespace ts {
export interface TupleType extends ObjectType {
elementTypes: Type[]; // Element types
thisType?: Type; // This-type of tuple (only needed for tuples that are constraints of type parameters)
}
export interface UnionOrIntersectionType extends Type {
@ -2546,7 +2569,7 @@ namespace ts {
}
export type RootPaths = string[];
export type PathSubstitutions = Map<string[]>;
export type PathSubstitutions = MapLike<string[]>;
export type TsConfigOnlyOptions = RootPaths | PathSubstitutions;
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | TsConfigOnlyOptions;
@ -2706,7 +2729,7 @@ namespace ts {
fileNames: string[];
raw?: any;
errors: Diagnostic[];
wildcardDirectories?: Map<WatchDirectoryFlags>;
wildcardDirectories?: MapLike<WatchDirectoryFlags>;
}
export const enum WatchDirectoryFlags {
@ -2716,7 +2739,7 @@ namespace ts {
export interface ExpandResult {
fileNames: string[];
wildcardDirectories: Map<WatchDirectoryFlags>;
wildcardDirectories: MapLike<WatchDirectoryFlags>;
}
/* @internal */
@ -2901,6 +2924,7 @@ namespace ts {
directoryExists?(directoryName: string): boolean;
realpath?(path: string): string;
getCurrentDirectory?(): string;
getDirectories?(path: string): string[];
}
export interface ResolvedModule {

View File

@ -87,25 +87,6 @@ namespace ts {
return node.end - node.pos;
}
export function mapIsEqualTo<T>(map1: Map<T>, map2: Map<T>): boolean {
if (!map1 || !map2) {
return map1 === map2;
}
return containsAll(map1, map2) && containsAll(map2, map1);
}
function containsAll<T>(map: Map<T>, other: Map<T>): boolean {
for (const key in map) {
if (!hasProperty(map, key)) {
continue;
}
if (!hasProperty(other, key) || map[key] !== other[key]) {
return false;
}
}
return true;
}
export function arrayIsEqualTo<T>(array1: T[], array2: T[], equaler?: (a: T, b: T) => boolean): boolean {
if (!array1 || !array2) {
return array1 === array2;
@ -126,7 +107,7 @@ namespace ts {
}
export function hasResolvedModule(sourceFile: SourceFile, moduleNameText: string): boolean {
return sourceFile.resolvedModules && hasProperty(sourceFile.resolvedModules, moduleNameText);
return !!(sourceFile.resolvedModules && sourceFile.resolvedModules[moduleNameText]);
}
export function getResolvedModule(sourceFile: SourceFile, moduleNameText: string): ResolvedModule {
@ -135,7 +116,7 @@ namespace ts {
export function setResolvedModule(sourceFile: SourceFile, moduleNameText: string, resolvedModule: ResolvedModule): void {
if (!sourceFile.resolvedModules) {
sourceFile.resolvedModules = {};
sourceFile.resolvedModules = createMap<ResolvedModule>();
}
sourceFile.resolvedModules[moduleNameText] = resolvedModule;
@ -143,7 +124,7 @@ namespace ts {
export function setResolvedTypeReferenceDirective(sourceFile: SourceFile, typeReferenceDirectiveName: string, resolvedTypeReferenceDirective: ResolvedTypeReferenceDirective): void {
if (!sourceFile.resolvedTypeReferenceDirectiveNames) {
sourceFile.resolvedTypeReferenceDirectiveNames = {};
sourceFile.resolvedTypeReferenceDirectiveNames = createMap<ResolvedTypeReferenceDirective>();
}
sourceFile.resolvedTypeReferenceDirectiveNames[typeReferenceDirectiveName] = resolvedTypeReferenceDirective;
@ -166,7 +147,7 @@ namespace ts {
}
for (let i = 0; i < names.length; i++) {
const newResolution = newResolutions[i];
const oldResolution = oldResolutions && hasProperty(oldResolutions, names[i]) ? oldResolutions[names[i]] : undefined;
const oldResolution = oldResolutions && oldResolutions[names[i]];
const changed =
oldResolution
? !newResolution || !comparer(oldResolution, newResolution)
@ -1033,14 +1014,14 @@ namespace ts {
&& (<PropertyAccessExpression | ElementAccessExpression>node).expression.kind === SyntaxKind.SuperKeyword;
}
export function getEntityNameFromTypeNode(node: TypeNode): EntityName | Expression {
export function getEntityNameFromTypeNode(node: TypeNode): EntityNameOrEntityNameExpression {
if (node) {
switch (node.kind) {
case SyntaxKind.TypeReference:
return (<TypeReferenceNode>node).typeName;
case SyntaxKind.ExpressionWithTypeArguments:
return (<ExpressionWithTypeArguments>node).expression;
Debug.assert(isEntityNameExpression((<ExpressionWithTypeArguments>node).expression));
return <EntityNameExpression>(<ExpressionWithTypeArguments>node).expression;
case SyntaxKind.Identifier:
case SyntaxKind.QualifiedName:
return (<EntityName><Node>node);
@ -1569,6 +1550,7 @@ namespace ts {
case SyntaxKind.MethodSignature:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.NamespaceImport:
case SyntaxKind.NamespaceExportDeclaration:
case SyntaxKind.Parameter:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.PropertyDeclaration:
@ -1693,8 +1675,8 @@ namespace ts {
// import * as <symbol> from ...
// import { x as <symbol> } from ...
// export { x as <symbol> } from ...
// export = ...
// export default ...
// export = <EntityNameExpression>
// export default <EntityNameExpression>
export function isAliasSymbolDeclaration(node: Node): boolean {
return node.kind === SyntaxKind.ImportEqualsDeclaration ||
node.kind === SyntaxKind.NamespaceExportDeclaration ||
@ -1702,7 +1684,11 @@ namespace ts {
node.kind === SyntaxKind.NamespaceImport ||
node.kind === SyntaxKind.ImportSpecifier ||
node.kind === SyntaxKind.ExportSpecifier ||
node.kind === SyntaxKind.ExportAssignment && (<ExportAssignment>node).expression.kind === SyntaxKind.Identifier;
node.kind === SyntaxKind.ExportAssignment && exportAssignmentIsAlias(<ExportAssignment>node);
}
export function exportAssignmentIsAlias(node: ExportAssignment): boolean {
return isEntityNameExpression(node.expression);
}
export function getClassExtendsHeritageClauseElement(node: ClassLikeDeclaration | InterfaceDeclaration) {
@ -1965,7 +1951,7 @@ namespace ts {
export function createDiagnosticCollection(): DiagnosticCollection {
let nonFileDiagnostics: Diagnostic[] = [];
const fileDiagnostics: Map<Diagnostic[]> = {};
const fileDiagnostics = createMap<Diagnostic[]>();
let diagnosticsModified = false;
let modificationCount = 0;
@ -1983,12 +1969,11 @@ namespace ts {
}
function reattachFileDiagnostics(newFile: SourceFile): void {
if (!hasProperty(fileDiagnostics, newFile.fileName)) {
return;
}
for (const diagnostic of fileDiagnostics[newFile.fileName]) {
diagnostic.file = newFile;
const diagnostics = fileDiagnostics[newFile.fileName];
if (diagnostics) {
for (const diagnostic of diagnostics) {
diagnostic.file = newFile;
}
}
}
@ -2029,9 +2014,7 @@ namespace ts {
forEach(nonFileDiagnostics, pushDiagnostic);
for (const key in fileDiagnostics) {
if (hasProperty(fileDiagnostics, key)) {
forEach(fileDiagnostics[key], pushDiagnostic);
}
forEach(fileDiagnostics[key], pushDiagnostic);
}
return sortAndDeduplicateDiagnostics(allDiagnostics);
@ -2046,9 +2029,7 @@ namespace ts {
nonFileDiagnostics = sortAndDeduplicateDiagnostics(nonFileDiagnostics);
for (const key in fileDiagnostics) {
if (hasProperty(fileDiagnostics, key)) {
fileDiagnostics[key] = sortAndDeduplicateDiagnostics(fileDiagnostics[key]);
}
fileDiagnostics[key] = sortAndDeduplicateDiagnostics(fileDiagnostics[key]);
}
}
}
@ -2059,7 +2040,7 @@ namespace ts {
// the map below must be updated. Note that this regexp *does not* include the 'delete' character.
// There is no reason for this other than that JSON.stringify does not handle it either.
const escapedCharsRegExp = /[\\\"\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g;
const escapedCharsMap: Map<string> = {
const escapedCharsMap = createMap({
"\0": "\\0",
"\t": "\\t",
"\v": "\\v",
@ -2072,7 +2053,7 @@ namespace ts {
"\u2028": "\\u2028", // lineSeparator
"\u2029": "\\u2029", // paragraphSeparator
"\u0085": "\\u0085" // nextLine
};
});
/**
@ -2680,22 +2661,9 @@ namespace ts {
isClassLike(node.parent.parent);
}
// Returns false if this heritage clause element's expression contains something unsupported
// (i.e. not a name or dotted name).
export function isSupportedExpressionWithTypeArguments(node: ExpressionWithTypeArguments): boolean {
return isSupportedExpressionWithTypeArgumentsRest(node.expression);
}
function isSupportedExpressionWithTypeArgumentsRest(node: Expression): boolean {
if (node.kind === SyntaxKind.Identifier) {
return true;
}
else if (isPropertyAccessExpression(node)) {
return isSupportedExpressionWithTypeArgumentsRest(node.expression);
}
else {
return false;
}
export function isEntityNameExpression(node: Expression): node is EntityNameExpression {
return node.kind === SyntaxKind.Identifier ||
node.kind === SyntaxKind.PropertyAccessExpression && isEntityNameExpression((<PropertyAccessExpression>node).expression);
}
export function isRightSideOfQualifiedNameOrPropertyAccess(node: Node) {
@ -2805,7 +2773,7 @@ namespace ts {
}
function stringifyObject(value: any) {
return `{${reduceProperties(value, stringifyProperty, "")}}`;
return `{${reduceOwnProperties(value, stringifyProperty, "")}}`;
}
function stringifyProperty(memo: string, value: any, key: string) {

View File

@ -52,7 +52,7 @@ class CompilerBaselineRunner extends RunnerBase {
private makeUnitName(name: string, root: string) {
const path = ts.toPath(name, root, (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", (fileName) => Harness.Compiler.getCanonicalFileName(fileName));
return path.replace(pathStart, "/");
return pathStart ? path.replace(pathStart, "/") : path;
};
public checkTestCodeOutput(fileName: string) {
@ -291,8 +291,8 @@ class CompilerBaselineRunner extends RunnerBase {
const fullWalker = new TypeWriterWalker(program, /*fullTypeCheck*/ true);
const fullResults: ts.Map<TypeWriterResult[]> = {};
const pullResults: ts.Map<TypeWriterResult[]> = {};
const fullResults = ts.createMap<TypeWriterResult[]>();
const pullResults = ts.createMap<TypeWriterResult[]>();
for (const sourceFile of allFiles) {
fullResults[sourceFile.unitName] = fullWalker.getTypeAndSymbols(sourceFile.unitName);

View File

@ -95,14 +95,14 @@ namespace FourSlash {
export import IndentStyle = ts.IndentStyle;
const entityMap: ts.Map<string> = {
const entityMap = ts.createMap({
"&": "&amp;",
"\"": "&quot;",
"'": "&#39;",
"/": "&#47;",
"<": "&lt;",
">": "&gt;"
};
});
export function escapeXmlAttributeValue(s: string) {
return s.replace(/[&<>"'\/]/g, ch => entityMap[ch]);
@ -204,7 +204,7 @@ namespace FourSlash {
public formatCodeOptions: ts.FormatCodeOptions;
private inputFiles: ts.Map<string> = {}; // Map between inputFile's fileName and its content for easily looking up when resolving references
private inputFiles = ts.createMap<string>(); // Map between inputFile's fileName and its content for easily looking up when resolving references
// Add input file which has matched file name with the given reference-file path.
// This is necessary when resolveReference flag is specified
@ -249,6 +249,7 @@ namespace FourSlash {
if (compilationOptions.typeRoots) {
compilationOptions.typeRoots = compilationOptions.typeRoots.map(p => ts.getNormalizedAbsolutePath(p, this.basePath));
}
compilationOptions.skipDefaultLibCheck = true;
const languageServiceAdapter = this.getLanguageServiceAdapter(testType, this.cancellationToken, compilationOptions);
this.languageServiceAdapterHost = languageServiceAdapter.getHost();
@ -300,11 +301,11 @@ namespace FourSlash {
}
else {
// resolveReference file-option is not specified then do not resolve any files and include all inputFiles
ts.forEachKey(this.inputFiles, fileName => {
for (const fileName in this.inputFiles) {
if (!Harness.isDefaultLibraryFile(fileName)) {
this.languageServiceAdapterHost.addScript(fileName, this.inputFiles[fileName], /*isRootFile*/ true);
}
});
}
this.languageServiceAdapterHost.addScript(Harness.Compiler.defaultLibFileName,
Harness.Compiler.getDefaultLibrarySourceFile().text, /*isRootFile*/ false);
}
@ -376,7 +377,7 @@ namespace FourSlash {
public verifyErrorExistsBetweenMarkers(startMarkerName: string, endMarkerName: string, negative: boolean) {
const startMarker = this.getMarkerByName(startMarkerName);
const endMarker = this.getMarkerByName(endMarkerName);
const predicate = function (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
const predicate = function(errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
return ((errorMinChar === startPos) && (errorLimChar === endPos)) ? true : false;
};
@ -428,12 +429,12 @@ namespace FourSlash {
let predicate: (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) => boolean;
if (after) {
predicate = function (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
predicate = function(errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
return ((errorMinChar >= startPos) && (errorLimChar >= startPos)) ? true : false;
};
}
else {
predicate = function (errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
predicate = function(errorMinChar: number, errorLimChar: number, startPos: number, endPos: number) {
return ((errorMinChar <= startPos) && (errorLimChar <= startPos)) ? true : false;
};
}
@ -458,7 +459,7 @@ namespace FourSlash {
endPos = endMarker.position;
}
errors.forEach(function (error: ts.Diagnostic) {
errors.forEach(function(error: ts.Diagnostic) {
if (predicate(error.start, error.start + error.length, startPos, endPos)) {
exists = true;
}
@ -475,7 +476,7 @@ namespace FourSlash {
Harness.IO.log("Unexpected error(s) found. Error list is:");
}
errors.forEach(function (error: ts.Diagnostic) {
errors.forEach(function(error: ts.Diagnostic) {
Harness.IO.log(" minChar: " + error.start +
", limChar: " + (error.start + error.length) +
", message: " + ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine()) + "\n");
@ -593,9 +594,9 @@ namespace FourSlash {
public noItemsWithSameNameButDifferentKind(): void {
const completions = this.getCompletionListAtCaret();
const uniqueItems: ts.Map<string> = {};
const uniqueItems = ts.createMap<string>();
for (const item of completions.entries) {
if (!ts.hasProperty(uniqueItems, item.name)) {
if (!(item.name in uniqueItems)) {
uniqueItems[item.name] = item.kind;
}
else {
@ -773,7 +774,7 @@ namespace FourSlash {
}
public verifyRangesWithSameTextReferenceEachOther() {
ts.forEachValue(this.rangesByText(), ranges => this.verifyRangesReferenceEachOther(ranges));
ts.forEachProperty(this.rangesByText(), ranges => this.verifyRangesReferenceEachOther(ranges));
}
private verifyReferencesWorker(references: ts.ReferenceEntry[], fileName: string, start: number, end: number, isWriteAccess?: boolean, isDefinition?: boolean) {
@ -1348,14 +1349,7 @@ namespace FourSlash {
}
// Enters lines of text at the current caret position
public type(text: string) {
return this.typeHighFidelity(text);
}
// Enters lines of text at the current caret position, invoking
// language service APIs to mimic Visual Studio's behavior
// as much as possible
private typeHighFidelity(text: string) {
public type(text: string, highFidelity = false) {
let offset = this.currentCaretPosition;
const prevChar = " ";
const checkCadence = (text.length >> 2) + 1;
@ -1364,24 +1358,26 @@ namespace FourSlash {
// Make the edit
const ch = text.charAt(i);
this.languageServiceAdapterHost.editScript(this.activeFile.fileName, offset, offset, ch);
this.languageService.getBraceMatchingAtPosition(this.activeFile.fileName, offset);
if (highFidelity) {
this.languageService.getBraceMatchingAtPosition(this.activeFile.fileName, offset);
}
this.updateMarkersForEdit(this.activeFile.fileName, offset, offset, ch);
offset++;
if (ch === "(" || ch === ",") {
/* Signature help*/
this.languageService.getSignatureHelpItems(this.activeFile.fileName, offset);
}
else if (prevChar === " " && /A-Za-z_/.test(ch)) {
/* Completions */
this.languageService.getCompletionsAtPosition(this.activeFile.fileName, offset);
}
if (highFidelity) {
if (ch === "(" || ch === ",") {
/* Signature help*/
this.languageService.getSignatureHelpItems(this.activeFile.fileName, offset);
}
else if (prevChar === " " && /A-Za-z_/.test(ch)) {
/* Completions */
this.languageService.getCompletionsAtPosition(this.activeFile.fileName, offset);
}
if (i % checkCadence === 0) {
this.checkPostEditInvariants();
// this.languageService.getSyntacticDiagnostics(this.activeFile.fileName);
// this.languageService.getSemanticDiagnostics(this.activeFile.fileName);
if (i % checkCadence === 0) {
this.checkPostEditInvariants();
}
}
// Handle post-keystroke formatting
@ -1389,14 +1385,12 @@ namespace FourSlash {
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, offset, ch, this.formatCodeOptions);
if (edits.length) {
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
// this.checkPostEditInvariants();
}
}
}
// Move the caret to wherever we ended up
this.currentCaretPosition = offset;
this.fixCaretPosition();
this.checkPostEditInvariants();
}
@ -1415,7 +1409,6 @@ namespace FourSlash {
const edits = this.languageService.getFormattingEditsForRange(this.activeFile.fileName, start, offset, this.formatCodeOptions);
if (edits.length) {
offset += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
this.checkPostEditInvariants();
}
}
@ -1639,10 +1632,11 @@ namespace FourSlash {
}
public rangesByText(): ts.Map<Range[]> {
const result: ts.Map<Range[]> = {};
const result = ts.createMap<Range[]>();
for (const range of this.getRanges()) {
const text = this.rangeText(range);
(ts.getProperty(result, text) || (result[text] = [])).push(range);
const ranges = result[text] || (result[text] = []);
ranges.push(range);
}
return result;
}
@ -1897,7 +1891,7 @@ namespace FourSlash {
public verifyBraceCompletionAtPosition(negative: boolean, openingBrace: string) {
const openBraceMap: ts.Map<ts.CharacterCodes> = {
const openBraceMap = ts.createMap<ts.CharacterCodes>({
"(": ts.CharacterCodes.openParen,
"{": ts.CharacterCodes.openBrace,
"[": ts.CharacterCodes.openBracket,
@ -1905,7 +1899,7 @@ namespace FourSlash {
'"': ts.CharacterCodes.doubleQuote,
"`": ts.CharacterCodes.backtick,
"<": ts.CharacterCodes.lessThan
};
});
const charCode = openBraceMap[openingBrace];
@ -2269,40 +2263,12 @@ namespace FourSlash {
export function runFourSlashTestContent(basePath: string, testType: FourSlashTestType, content: string, fileName: string): void {
// Parse out the files and their metadata
const testData = parseTestData(basePath, content, fileName);
const state = new TestState(basePath, testType, testData);
let result = "";
const fourslashFile: Harness.Compiler.TestFile = {
unitName: Harness.Compiler.fourslashFileName,
content: undefined,
};
const testFile: Harness.Compiler.TestFile = {
unitName: fileName,
content: content
};
const host = Harness.Compiler.createCompilerHost(
[fourslashFile, testFile],
(fn, contents) => result = contents,
ts.ScriptTarget.Latest,
Harness.IO.useCaseSensitiveFileNames(),
Harness.IO.getCurrentDirectory());
const program = ts.createProgram([Harness.Compiler.fourslashFileName, fileName], { outFile: "fourslashTestOutput.js", noResolve: true, target: ts.ScriptTarget.ES3 }, host);
const sourceFile = host.getSourceFile(fileName, ts.ScriptTarget.ES3);
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);
if (diagnostics.length > 0) {
throw new Error(`Error compiling ${fileName}: ` +
diagnostics.map(e => ts.flattenDiagnosticMessageText(e.messageText, Harness.IO.newLine())).join("\r\n"));
const output = ts.transpileModule(content, { reportDiagnostics: true });
if (output.diagnostics.length > 0) {
throw new Error(`Syntax error in ${basePath}: ${output.diagnostics[0].messageText}`);
}
program.emit(sourceFile);
ts.Debug.assert(!!result);
runCode(result, state);
runCode(output.outputText, state);
}
function runCode(code: string, state: TestState): void {
@ -2395,13 +2361,14 @@ ${code}
// Comment line, check for global/file @options and record them
const match = optionRegex.exec(line.substr(2));
if (match) {
const fileMetadataNamesIndex = fileMetadataNames.indexOf(match[1]);
const [key, value] = match.slice(1);
const fileMetadataNamesIndex = fileMetadataNames.indexOf(key);
if (fileMetadataNamesIndex === -1) {
// Check if the match is already existed in the global options
if (globalOptions[match[1]] !== undefined) {
throw new Error("Global Option : '" + match[1] + "' is already existed");
if (globalOptions[key] !== undefined) {
throw new Error(`Global option '${key}' already exists`);
}
globalOptions[match[1]] = match[2];
globalOptions[key] = value;
}
else {
if (fileMetadataNamesIndex === fileMetadataNames.indexOf(metadataOptionNames.fileName)) {
@ -2416,12 +2383,12 @@ ${code}
resetLocalData();
}
currentFileName = basePath + "/" + match[2];
currentFileOptions[match[1]] = match[2];
currentFileName = basePath + "/" + value;
currentFileOptions[key] = value;
}
else {
// Add other fileMetadata flag
currentFileOptions[match[1]] = match[2];
currentFileOptions[key] = value;
}
}
}
@ -2509,7 +2476,7 @@ ${code}
}
const marker: Marker = {
fileName: fileName,
fileName,
position: location.position,
data: markerValue
};
@ -2526,7 +2493,7 @@ ${code}
function recordMarker(fileName: string, location: LocationInformation, name: string, markerMap: MarkerMap, markers: Marker[]): Marker {
const marker: Marker = {
fileName: fileName,
fileName,
position: location.position
};

View File

@ -750,7 +750,7 @@ namespace Harness {
export function readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[]) {
const fs = new Utils.VirtualFileSystem(path, useCaseSensitiveFileNames());
for (const file in listFiles(path)) {
for (const file of listFiles(path)) {
fs.addFile(file);
}
return ts.matchFiles(path, extension, exclude, include, useCaseSensitiveFileNames(), getCurrentDirectory(), path => {
@ -848,9 +848,9 @@ namespace Harness {
export const defaultLibFileName = "lib.d.ts";
export const es2015DefaultLibFileName = "lib.es2015.d.ts";
const libFileNameSourceFileMap: ts.Map<ts.SourceFile> = {
const libFileNameSourceFileMap= ts.createMap<ts.SourceFile>({
[defaultLibFileName]: createSourceFileAndAssertInvariants(defaultLibFileName, IO.readFile(libFolder + "lib.es5.d.ts"), /*languageVersion*/ ts.ScriptTarget.Latest)
};
});
export function getDefaultLibrarySourceFile(fileName = defaultLibFileName): ts.SourceFile {
if (!isDefaultLibraryFile(fileName)) {
@ -1005,13 +1005,13 @@ namespace Harness {
let optionsIndex: ts.Map<ts.CommandLineOption>;
function getCommandLineOption(name: string): ts.CommandLineOption {
if (!optionsIndex) {
optionsIndex = {};
optionsIndex = ts.createMap<ts.CommandLineOption>();
const optionDeclarations = harnessOptionDeclarations.concat(ts.optionDeclarations);
for (const option of optionDeclarations) {
optionsIndex[option.name.toLowerCase()] = option;
}
}
return ts.lookUp(optionsIndex, name.toLowerCase());
return optionsIndex[name.toLowerCase()];
}
export function setCompilerOptionsFromHarnessSetting(settings: Harness.TestCaseParser.CompilerSettings, options: ts.CompilerOptions & HarnessOptions): void {

View File

@ -123,7 +123,7 @@ namespace Harness.LanguageService {
}
export class LanguageServiceAdapterHost {
protected fileNameToScript: ts.Map<ScriptInfo> = {};
protected fileNameToScript = ts.createMap<ScriptInfo>();
constructor(protected cancellationToken = DefaultHostCancellationToken.Instance,
protected settings = ts.getDefaultCompilerOptions()) {
@ -135,7 +135,7 @@ namespace Harness.LanguageService {
public getFilenames(): string[] {
const fileNames: string[] = [];
ts.forEachValue(this.fileNameToScript, (scriptInfo) => {
ts.forEachProperty(this.fileNameToScript, (scriptInfo) => {
if (scriptInfo.isRootFile) {
// only include root files here
// usually it means that we won't include lib.d.ts in the list of root files so it won't mess the computation of compilation root dir.
@ -146,7 +146,7 @@ namespace Harness.LanguageService {
}
public getScriptInfo(fileName: string): ScriptInfo {
return ts.lookUp(this.fileNameToScript, fileName);
return this.fileNameToScript[fileName];
}
public addScript(fileName: string, content: string, isRootFile: boolean): void {
@ -235,7 +235,7 @@ namespace Harness.LanguageService {
this.getModuleResolutionsForFile = (fileName) => {
const scriptInfo = this.getScriptInfo(fileName);
const preprocessInfo = ts.preProcessFile(scriptInfo.content, /*readImportFiles*/ true);
const imports: ts.Map<string> = {};
const imports = ts.createMap<string>();
for (const module of preprocessInfo.importedFiles) {
const resolutionInfo = ts.resolveModuleName(module.fileName, fileName, compilerOptions, moduleResolutionHost);
if (resolutionInfo.resolvedModule) {
@ -248,7 +248,7 @@ namespace Harness.LanguageService {
const scriptInfo = this.getScriptInfo(fileName);
if (scriptInfo) {
const preprocessInfo = ts.preProcessFile(scriptInfo.content, /*readImportFiles*/ false);
const resolutions: ts.Map<ts.ResolvedTypeReferenceDirective> = {};
const resolutions = ts.createMap<ts.ResolvedTypeReferenceDirective>();
const settings = this.nativeHost.getCompilationSettings();
for (const typeReferenceDirective of preprocessInfo.typeReferenceDirectives) {
const resolutionInfo = ts.resolveTypeReferenceDirective(typeReferenceDirective.fileName, fileName, settings, moduleResolutionHost);

View File

@ -253,18 +253,15 @@ class ProjectRunner extends RunnerBase {
moduleResolution: ts.ModuleResolutionKind.Classic, // currently all tests use classic module resolution kind, this will change in the future
};
// Set the values specified using json
const optionNameMap: ts.Map<ts.CommandLineOption> = {};
ts.forEach(ts.optionDeclarations, option => {
optionNameMap[option.name] = option;
});
const optionNameMap = ts.arrayToMap(ts.optionDeclarations, option => option.name);
for (const name in testCase) {
if (name !== "mapRoot" && name !== "sourceRoot" && ts.hasProperty(optionNameMap, name)) {
if (name !== "mapRoot" && name !== "sourceRoot" && name in optionNameMap) {
const option = optionNameMap[name];
const optType = option.type;
let value = <any>testCase[name];
if (typeof optType !== "string") {
const key = value.toLowerCase();
if (ts.hasProperty(optType, key)) {
if (key in optType) {
value = optType[key];
}
}

View File

@ -7,16 +7,16 @@ namespace ts {
}
function createDefaultServerHost(fileMap: Map<File>): server.ServerHost {
const existingDirectories: Map<boolean> = {};
forEachValue(fileMap, v => {
let dir = getDirectoryPath(v.name);
const existingDirectories = createMap<boolean>();
for (const name in fileMap) {
let dir = getDirectoryPath(name);
let previous: string;
do {
existingDirectories[dir] = true;
previous = dir;
dir = getDirectoryPath(dir);
} while (dir !== previous);
});
}
return {
args: <string[]>[],
newLine: "\r\n",
@ -24,7 +24,7 @@ namespace ts {
write: (s: string) => {
},
readFile: (path: string, encoding?: string): string => {
return hasProperty(fileMap, path) && fileMap[path].content;
return path in fileMap ? fileMap[path].content : undefined;
},
writeFile: (path: string, data: string, writeByteOrderMark?: boolean) => {
throw new Error("NYI");
@ -33,10 +33,10 @@ namespace ts {
throw new Error("NYI");
},
fileExists: (path: string): boolean => {
return hasProperty(fileMap, path);
return path in fileMap;
},
directoryExists: (path: string): boolean => {
return hasProperty(existingDirectories, path);
return existingDirectories[path] || false;
},
createDirectory: (path: string) => {
},
@ -101,7 +101,7 @@ namespace ts {
content: `foo()`
};
const serverHost = createDefaultServerHost({ [root.name]: root, [imported.name]: imported });
const serverHost = createDefaultServerHost(createMap({ [root.name]: root, [imported.name]: imported }));
const { project, rootScriptInfo } = createProject(root.name, serverHost);
// ensure that imported file was found
@ -193,7 +193,7 @@ namespace ts {
content: `export var y = 1`
};
const fileMap: Map<File> = { [root.name]: root };
const fileMap = createMap({ [root.name]: root });
const serverHost = createDefaultServerHost(fileMap);
const originalFileExists = serverHost.fileExists;

View File

@ -10,7 +10,7 @@ namespace ts {
const map = arrayToMap(files, f => f.name);
if (hasDirectoryExists) {
const directories: Map<string> = {};
const directories = createMap<string>();
for (const f of files) {
let name = getDirectoryPath(f.name);
while (true) {
@ -25,19 +25,19 @@ namespace ts {
return {
readFile,
directoryExists: path => {
return hasProperty(directories, path);
return path in directories;
},
fileExists: path => {
assert.isTrue(hasProperty(directories, getDirectoryPath(path)), `'fileExists' '${path}' request in non-existing directory`);
return hasProperty(map, path);
assert.isTrue(getDirectoryPath(path) in directories, `'fileExists' '${path}' request in non-existing directory`);
return path in map;
}
};
}
else {
return { readFile, fileExists: path => hasProperty(map, path), };
return { readFile, fileExists: path => path in map, };
}
function readFile(path: string): string {
return hasProperty(map, path) ? map[path].content : undefined;
return path in map ? map[path].content : undefined;
}
}
@ -287,7 +287,7 @@ namespace ts {
const host: CompilerHost = {
getSourceFile: (fileName: string, languageVersion: ScriptTarget) => {
const path = normalizePath(combinePaths(currentDirectory, fileName));
return hasProperty(files, path) ? createSourceFile(fileName, files[path], languageVersion) : undefined;
return path in files ? createSourceFile(fileName, files[path], languageVersion) : undefined;
},
getDefaultLibFileName: () => "lib.d.ts",
writeFile: (fileName, content): void => { throw new Error("NotImplemented"); },
@ -298,7 +298,7 @@ namespace ts {
useCaseSensitiveFileNames: () => false,
fileExists: fileName => {
const path = normalizePath(combinePaths(currentDirectory, fileName));
return hasProperty(files, path);
return path in files;
},
readFile: (fileName): string => { throw new Error("NotImplemented"); }
};
@ -318,7 +318,7 @@ namespace ts {
}
it("should find all modules", () => {
const files: Map<string> = {
const files = createMap({
"/a/b/c/first/shared.ts": `
class A {}
export = A`,
@ -332,23 +332,23 @@ import Shared = require('../first/shared');
class C {}
export = C;
`
};
});
test(files, "/a/b/c/first/second", ["class_a.ts"], 3, ["../../../c/third/class_c.ts"]);
});
it("should find modules in node_modules", () => {
const files: Map<string> = {
const files = createMap({
"/parent/node_modules/mod/index.d.ts": "export var x",
"/parent/app/myapp.ts": `import {x} from "mod"`
};
});
test(files, "/parent/app", ["myapp.ts"], 2, []);
});
it("should find file referenced via absolute and relative names", () => {
const files: Map<string> = {
const files = createMap({
"/a/b/c.ts": `/// <reference path="b.ts"/>`,
"/a/b/b.ts": "var x"
};
});
test(files, "/a/b", ["c.ts", "/a/b/b.ts"], 2, []);
});
});
@ -358,11 +358,7 @@ export = C;
function test(files: Map<string>, options: CompilerOptions, currentDirectory: string, useCaseSensitiveFileNames: boolean, rootFiles: string[], diagnosticCodes: number[]): void {
const getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
if (!useCaseSensitiveFileNames) {
const f: Map<string> = {};
for (const fileName in files) {
f[getCanonicalFileName(fileName)] = files[fileName];
}
files = f;
files = reduceProperties(files, (files, file, fileName) => (files[getCanonicalFileName(fileName)] = file, files), createMap<string>());
}
const host: CompilerHost = {
@ -371,7 +367,7 @@ export = C;
return library;
}
const path = getCanonicalFileName(normalizePath(combinePaths(currentDirectory, fileName)));
return hasProperty(files, path) ? createSourceFile(fileName, files[path], languageVersion) : undefined;
return path in files ? createSourceFile(fileName, files[path], languageVersion) : undefined;
},
getDefaultLibFileName: () => "lib.d.ts",
writeFile: (fileName, content): void => { throw new Error("NotImplemented"); },
@ -382,7 +378,7 @@ export = C;
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
fileExists: fileName => {
const path = getCanonicalFileName(normalizePath(combinePaths(currentDirectory, fileName)));
return hasProperty(files, path);
return path in files;
},
readFile: (fileName): string => { throw new Error("NotImplemented"); }
};
@ -395,57 +391,57 @@ export = C;
}
it("should succeed when the same file is referenced using absolute and relative names", () => {
const files: Map<string> = {
const files = createMap({
"/a/b/c.ts": `/// <reference path="d.ts"/>`,
"/a/b/d.ts": "var x"
};
});
test(files, { module: ts.ModuleKind.AMD }, "/a/b", /*useCaseSensitiveFileNames*/ false, ["c.ts", "/a/b/d.ts"], []);
});
it("should fail when two files used in program differ only in casing (tripleslash references)", () => {
const files: Map<string> = {
const files = createMap({
"/a/b/c.ts": `/// <reference path="D.ts"/>`,
"/a/b/d.ts": "var x"
};
});
test(files, { module: ts.ModuleKind.AMD, forceConsistentCasingInFileNames: true }, "/a/b", /*useCaseSensitiveFileNames*/ false, ["c.ts", "d.ts"], [1149]);
});
it("should fail when two files used in program differ only in casing (imports)", () => {
const files: Map<string> = {
const files = createMap({
"/a/b/c.ts": `import {x} from "D"`,
"/a/b/d.ts": "export var x"
};
});
test(files, { module: ts.ModuleKind.AMD, forceConsistentCasingInFileNames: true }, "/a/b", /*useCaseSensitiveFileNames*/ false, ["c.ts", "d.ts"], [1149]);
});
it("should fail when two files used in program differ only in casing (imports, relative module names)", () => {
const files: Map<string> = {
const files = createMap({
"moduleA.ts": `import {x} from "./ModuleB"`,
"moduleB.ts": "export var x"
};
});
test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "", /*useCaseSensitiveFileNames*/ false, ["moduleA.ts", "moduleB.ts"], [1149]);
});
it("should fail when two files exist on disk that differs only in casing", () => {
const files: Map<string> = {
const files = createMap({
"/a/b/c.ts": `import {x} from "D"`,
"/a/b/D.ts": "export var x",
"/a/b/d.ts": "export var y"
};
});
test(files, { module: ts.ModuleKind.AMD }, "/a/b", /*useCaseSensitiveFileNames*/ true, ["c.ts", "d.ts"], [1149]);
});
it("should fail when module name in 'require' calls has inconsistent casing", () => {
const files: Map<string> = {
const files = createMap({
"moduleA.ts": `import a = require("./ModuleC")`,
"moduleB.ts": `import a = require("./moduleC")`,
"moduleC.ts": "export var x"
};
});
test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "", /*useCaseSensitiveFileNames*/ false, ["moduleA.ts", "moduleB.ts", "moduleC.ts"], [1149, 1149]);
});
it("should fail when module names in 'require' calls has inconsistent casing and current directory has uppercase chars", () => {
const files: Map<string> = {
const files = createMap({
"/a/B/c/moduleA.ts": `import a = require("./ModuleC")`,
"/a/B/c/moduleB.ts": `import a = require("./moduleC")`,
"/a/B/c/moduleC.ts": "export var x",
@ -453,11 +449,11 @@ export = C;
import a = require("./moduleA.ts");
import b = require("./moduleB.ts");
`
};
});
test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "/a/B/c", /*useCaseSensitiveFileNames*/ false, ["moduleD.ts"], [1149]);
});
it("should not fail when module names in 'require' calls has consistent casing and current directory has uppercase chars", () => {
const files: Map<string> = {
const files = createMap({
"/a/B/c/moduleA.ts": `import a = require("./moduleC")`,
"/a/B/c/moduleB.ts": `import a = require("./moduleC")`,
"/a/B/c/moduleC.ts": "export var x",
@ -465,7 +461,7 @@ import b = require("./moduleB.ts");
import a = require("./moduleA.ts");
import b = require("./moduleB.ts");
`
};
});
test(files, { module: ts.ModuleKind.CommonJS, forceConsistentCasingInFileNames: true }, "/a/B/c", /*useCaseSensitiveFileNames*/ false, ["moduleD.ts"], []);
});
});
@ -1023,7 +1019,7 @@ import b = require("./moduleB.ts");
const names = map(files, f => f.name);
const sourceFiles = arrayToMap(map(files, f => createSourceFile(f.name, f.content, ScriptTarget.ES6)), f => f.fileName);
const compilerHost: CompilerHost = {
fileExists : fileName => hasProperty(sourceFiles, fileName),
fileExists : fileName => fileName in sourceFiles,
getSourceFile: fileName => sourceFiles[fileName],
getDefaultLibFileName: () => "lib.d.ts",
writeFile(file, text) {
@ -1034,7 +1030,7 @@ import b = require("./moduleB.ts");
getCanonicalFileName: f => f.toLowerCase(),
getNewLine: () => "\r\n",
useCaseSensitiveFileNames: () => false,
readFile: fileName => hasProperty(sourceFiles, fileName) ? sourceFiles[fileName].text : undefined
readFile: fileName => fileName in sourceFiles ? sourceFiles[fileName].text : undefined
};
const program1 = createProgram(names, {}, compilerHost);
const diagnostics1 = program1.getFileProcessingDiagnostics().getDiagnostics();

View File

@ -95,13 +95,14 @@ namespace ts {
}
}
function createSourceFileWithText(fileName: string, sourceText: SourceText, target: ScriptTarget) {
const file = <SourceFileWithText>createSourceFile(fileName, sourceText.getFullText(), target);
file.sourceText = sourceText;
return file;
}
function createTestCompilerHost(texts: NamedSourceText[], target: ScriptTarget): CompilerHost {
const files: Map<SourceFileWithText> = {};
for (const t of texts) {
const file = <SourceFileWithText>createSourceFile(t.name, t.text.getFullText(), target);
file.sourceText = t.text;
files[t.name] = file;
}
const files = arrayToMap(texts, t => t.name, t => createSourceFileWithText(t.name, t.text, target));
return {
getSourceFile(fileName): SourceFile {
@ -128,10 +129,9 @@ namespace ts {
getNewLine(): string {
return sys ? sys.newLine : newLine;
},
fileExists: fileName => hasProperty(files, fileName),
fileExists: fileName => fileName in files,
readFile: fileName => {
const file = lookUp(files, fileName);
return file && file.text;
return fileName in files ? files[fileName].text : undefined;
}
};
}
@ -152,29 +152,29 @@ namespace ts {
return program;
}
function getSizeOfMap(map: Map<any>): number {
let size = 0;
for (const id in map) {
if (hasProperty(map, id)) {
size++;
function checkResolvedModule(expected: ResolvedModule, actual: ResolvedModule): boolean {
if (!expected === !actual) {
if (expected) {
assert.isTrue(expected.resolvedFileName === actual.resolvedFileName, `'resolvedFileName': expected '${expected.resolvedFileName}' to be equal to '${actual.resolvedFileName}'`);
assert.isTrue(expected.isExternalLibraryImport === actual.isExternalLibraryImport, `'isExternalLibraryImport': expected '${expected.isExternalLibraryImport}' to be equal to '${actual.isExternalLibraryImport}'`);
}
return true;
}
return size;
return false;
}
function checkResolvedModule(expected: ResolvedModule, actual: ResolvedModule): void {
assert.isTrue(actual !== undefined);
assert.isTrue(expected.resolvedFileName === actual.resolvedFileName, `'resolvedFileName': expected '${expected.resolvedFileName}' to be equal to '${actual.resolvedFileName}'`);
assert.isTrue(expected.isExternalLibraryImport === actual.isExternalLibraryImport, `'isExternalLibraryImport': expected '${expected.isExternalLibraryImport}' to be equal to '${actual.isExternalLibraryImport}'`);
function checkResolvedTypeDirective(expected: ResolvedTypeReferenceDirective, actual: ResolvedTypeReferenceDirective): boolean {
if (!expected === !actual) {
if (expected) {
assert.isTrue(expected.resolvedFileName === actual.resolvedFileName, `'resolvedFileName': expected '${expected.resolvedFileName}' to be equal to '${actual.resolvedFileName}'`);
assert.isTrue(expected.primary === actual.primary, `'primary': expected '${expected.primary}' to be equal to '${actual.primary}'`);
}
return true;
}
return false;
}
function checkResolvedTypeDirective(expected: ResolvedTypeReferenceDirective, actual: ResolvedTypeReferenceDirective): void {
assert.isTrue(actual !== undefined);
assert.isTrue(expected.resolvedFileName === actual.resolvedFileName, `'resolvedFileName': expected '${expected.resolvedFileName}' to be equal to '${actual.resolvedFileName}'`);
assert.isTrue(expected.primary === actual.primary, `'primary': expected '${expected.primary}' to be equal to '${actual.primary}'`);
}
function checkCache<T>(caption: string, program: Program, fileName: string, expectedContent: Map<T>, getCache: (f: SourceFile) => Map<T>, entryChecker: (expected: T, original: T) => void): void {
function checkCache<T>(caption: string, program: Program, fileName: string, expectedContent: Map<T>, getCache: (f: SourceFile) => Map<T>, entryChecker: (expected: T, original: T) => boolean): void {
const file = program.getSourceFile(fileName);
assert.isTrue(file !== undefined, `cannot find file ${fileName}`);
const cache = getCache(file);
@ -183,23 +183,7 @@ namespace ts {
}
else {
assert.isTrue(cache !== undefined, `expected ${caption} to be set`);
const actualCacheSize = getSizeOfMap(cache);
const expectedSize = getSizeOfMap(expectedContent);
assert.isTrue(actualCacheSize === expectedSize, `expected actual size: ${actualCacheSize} to be equal to ${expectedSize}`);
for (const id in expectedContent) {
if (hasProperty(expectedContent, id)) {
if (expectedContent[id]) {
const expected = expectedContent[id];
const actual = cache[id];
entryChecker(expected, actual);
}
}
else {
assert.isTrue(cache[id] === undefined);
}
}
assert.isTrue(equalOwnProperties(expectedContent, cache, entryChecker), `contents of ${caption} did not match the expected contents.`);
}
}
@ -313,6 +297,8 @@ namespace ts {
});
it("resolution cache follows imports", () => {
(<any>Error).stackTraceLimit = Infinity;
const files = [
{ name: "a.ts", text: SourceText.New("", "import {_} from 'b'", "var x = 1") },
{ name: "b.ts", text: SourceText.New("", "", "var y = 2") },
@ -320,7 +306,7 @@ namespace ts {
const options: CompilerOptions = { target };
const program_1 = newProgram(files, ["a.ts"], options);
checkResolvedModulesCache(program_1, "a.ts", { "b": { resolvedFileName: "b.ts" } });
checkResolvedModulesCache(program_1, "a.ts", createMap({ "b": { resolvedFileName: "b.ts" } }));
checkResolvedModulesCache(program_1, "b.ts", undefined);
const program_2 = updateProgram(program_1, ["a.ts"], options, files => {
@ -329,7 +315,7 @@ namespace ts {
assert.isTrue(program_1.structureIsReused);
// content of resolution cache should not change
checkResolvedModulesCache(program_1, "a.ts", { "b": { resolvedFileName: "b.ts" } });
checkResolvedModulesCache(program_1, "a.ts", createMap({ "b": { resolvedFileName: "b.ts" } }));
checkResolvedModulesCache(program_1, "b.ts", undefined);
// imports has changed - program is not reused
@ -346,7 +332,7 @@ namespace ts {
files[0].text = files[0].text.updateImportsAndExports(newImports);
});
assert.isTrue(!program_3.structureIsReused);
checkResolvedModulesCache(program_4, "a.ts", { "b": { resolvedFileName: "b.ts" }, "c": undefined });
checkResolvedModulesCache(program_4, "a.ts", createMap({ "b": { resolvedFileName: "b.ts" }, "c": undefined }));
});
it("resolved type directives cache follows type directives", () => {
@ -357,7 +343,7 @@ namespace ts {
const options: CompilerOptions = { target, typeRoots: ["/types"] };
const program_1 = newProgram(files, ["/a.ts"], options);
checkResolvedTypeDirectivesCache(program_1, "/a.ts", { "typedefs": { resolvedFileName: "/types/typedefs/index.d.ts", primary: true } });
checkResolvedTypeDirectivesCache(program_1, "/a.ts", createMap({ "typedefs": { resolvedFileName: "/types/typedefs/index.d.ts", primary: true } }));
checkResolvedTypeDirectivesCache(program_1, "/types/typedefs/index.d.ts", undefined);
const program_2 = updateProgram(program_1, ["/a.ts"], options, files => {
@ -366,7 +352,7 @@ namespace ts {
assert.isTrue(program_1.structureIsReused);
// content of resolution cache should not change
checkResolvedTypeDirectivesCache(program_1, "/a.ts", { "typedefs": { resolvedFileName: "/types/typedefs/index.d.ts", primary: true } });
checkResolvedTypeDirectivesCache(program_1, "/a.ts", createMap({ "typedefs": { resolvedFileName: "/types/typedefs/index.d.ts", primary: true } }));
checkResolvedTypeDirectivesCache(program_1, "/types/typedefs/index.d.ts", undefined);
// type reference directives has changed - program is not reused
@ -384,7 +370,7 @@ namespace ts {
files[0].text = files[0].text.updateReferences(newReferences);
});
assert.isTrue(!program_3.structureIsReused);
checkResolvedTypeDirectivesCache(program_1, "/a.ts", { "typedefs": { resolvedFileName: "/types/typedefs/index.d.ts", primary: true } });
checkResolvedTypeDirectivesCache(program_1, "/a.ts", createMap({ "typedefs": { resolvedFileName: "/types/typedefs/index.d.ts", primary: true } }));
});
});

View File

@ -106,7 +106,7 @@ namespace ts.server {
describe("onMessage", () => {
it("should not throw when commands are executed with invalid arguments", () => {
let i = 0;
for (name in CommandNames) {
for (const name in CommandNames) {
if (!Object.prototype.hasOwnProperty.call(CommandNames, name)) {
continue;
}
@ -362,13 +362,13 @@ namespace ts.server {
class InProcClient {
private server: InProcSession;
private seq = 0;
private callbacks: ts.Map<(resp: protocol.Response) => void> = {};
private eventHandlers: ts.Map<(args: any) => void> = {};
private callbacks = createMap<(resp: protocol.Response) => void>();
private eventHandlers = createMap<(args: any) => void>();
handle(msg: protocol.Message): void {
if (msg.type === "response") {
const response = <protocol.Response>msg;
if (this.callbacks[response.request_seq]) {
if (response.request_seq in this.callbacks) {
this.callbacks[response.request_seq](response);
delete this.callbacks[response.request_seq];
}
@ -380,7 +380,7 @@ namespace ts.server {
}
emit(name: string, args: any): void {
if (this.eventHandlers[name]) {
if (name in this.eventHandlers) {
this.eventHandlers[name](args);
}
}

View File

@ -68,20 +68,10 @@ namespace ts {
return entry;
}
function sizeOfMap(map: Map<any>): number {
let n = 0;
for (const name in map) {
if (hasProperty(map, name)) {
n++;
}
}
return n;
}
function checkMapKeys(caption: string, map: Map<any>, expectedKeys: string[]) {
assert.equal(sizeOfMap(map), expectedKeys.length, `${caption}: incorrect size of map`);
assert.equal(reduceProperties(map, count => count + 1, 0), expectedKeys.length, `${caption}: incorrect size of map`);
for (const name of expectedKeys) {
assert.isTrue(hasProperty(map, name), `${caption} is expected to contain ${name}, actual keys: ${getKeys(map)}`);
assert.isTrue(name in map, `${caption} is expected to contain ${name}, actual keys: ${Object.keys(map)}`);
}
}
@ -126,8 +116,8 @@ namespace ts {
private getCanonicalFileName: (s: string) => string;
private toPath: (f: string) => Path;
private callbackQueue: TimeOutCallback[] = [];
readonly watchedDirectories: Map<{ cb: DirectoryWatcherCallback, recursive: boolean }[]> = {};
readonly watchedFiles: Map<FileWatcherCallback[]> = {};
readonly watchedDirectories = createMap<{ cb: DirectoryWatcherCallback, recursive: boolean }[]>();
readonly watchedFiles = createMap<FileWatcherCallback[]>();
constructor(public useCaseSensitiveFileNames: boolean, private executingFilePath: string, private currentDirectory: string, fileOrFolderList: FileOrFolder[]) {
this.getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
@ -208,7 +198,7 @@ namespace ts {
watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher {
const path = this.toPath(directoryName);
const callbacks = lookUp(this.watchedDirectories, path) || (this.watchedDirectories[path] = []);
const callbacks = this.watchedDirectories[path] || (this.watchedDirectories[path] = []);
callbacks.push({ cb: callback, recursive });
return {
referenceCount: 0,
@ -229,7 +219,7 @@ namespace ts {
triggerDirectoryWatcherCallback(directoryName: string, fileName: string): void {
const path = this.toPath(directoryName);
const callbacks = lookUp(this.watchedDirectories, path);
const callbacks = this.watchedDirectories[path];
if (callbacks) {
for (const callback of callbacks) {
callback.cb(fileName);
@ -239,7 +229,7 @@ namespace ts {
triggerFileWatcherCallback(fileName: string, removed?: boolean): void {
const path = this.toPath(fileName);
const callbacks = lookUp(this.watchedFiles, path);
const callbacks = this.watchedFiles[path];
if (callbacks) {
for (const callback of callbacks) {
callback(path, removed);
@ -249,7 +239,7 @@ namespace ts {
watchFile(fileName: string, callback: FileWatcherCallback) {
const path = this.toPath(fileName);
const callbacks = lookUp(this.watchedFiles, path) || (this.watchedFiles[path] = []);
const callbacks = this.watchedFiles[path] || (this.watchedFiles[path] = []);
callbacks.push(callback);
return {
close: () => {
@ -594,7 +584,7 @@ namespace ts {
content: `{
"compilerOptions": {
"target": "es6"
},
},
"files": [ "main.ts" ]
}`
};
@ -621,7 +611,7 @@ namespace ts {
content: `{
"compilerOptions": {
"target": "es6"
},
},
"files": [ "main.ts" ]
}`
};

View File

@ -68,6 +68,10 @@ interface ArrayConstructor {
of<T>(...items: T[]): Array<T>;
}
interface DateConstructor {
new (value: Date): Date;
}
interface Function {
/**
* Returns the name of the function. Function names are read-only and can not be changed.
@ -343,6 +347,30 @@ interface ObjectConstructor {
defineProperty(o: any, propertyKey: PropertyKey, attributes: PropertyDescriptor): any;
}
interface ReadonlyArray<T> {
/**
* Returns the value of the first element in the array where predicate is true, and undefined
* otherwise.
* @param predicate find calls predicate once for each element of the array, in ascending
* order, until it finds one where predicate returns true. If such an element is found, find
* immediately returns that element value. Otherwise, find returns undefined.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
find(predicate: (value: T, index: number, obj: ReadonlyArray<T>) => boolean, thisArg?: any): T | undefined;
/**
* Returns the index of the first element in the array where predicate is true, and undefined
* otherwise.
* @param predicate find calls predicate once for each element of the array, in ascending
* order, until it finds one where predicate returns true. If such an element is found,
* findIndex immediately returns that element index. Otherwise, findIndex returns -1.
* @param thisArg If provided, it will be used as the this value for each invocation of
* predicate. If it is not provided, undefined is used instead.
*/
findIndex(predicate: (value: T) => boolean, thisArg?: any): number;
}
interface RegExp {
/**
* Returns a string indicating the flags of the regular expression in question. This field is read-only.

View File

@ -63,6 +63,26 @@ interface ArrayConstructor {
from<T>(iterable: Iterable<T>): Array<T>;
}
interface ReadonlyArray<T> {
/** Iterator */
[Symbol.iterator](): IterableIterator<T>;
/**
* Returns an array of key, value pairs for every entry in the array
*/
entries(): IterableIterator<[number, T]>;
/**
* Returns an list of keys in the array
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the array
*/
values(): IterableIterator<T>;
}
interface IArguments {
/** Iterator */
[Symbol.iterator](): IterableIterator<any>;

7
src/lib/es5.d.ts vendored
View File

@ -1006,7 +1006,12 @@ interface ReadonlyArray<T> {
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: T[]): T[];
concat(...items: T[][]): T[];
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: (T | T[])[]): T[];
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

View File

@ -21,7 +21,7 @@ namespace ts.server {
export class SessionClient implements LanguageService {
private sequence: number = 0;
private lineMaps: ts.Map<number[]> = {};
private lineMaps: ts.Map<number[]> = ts.createMap<number[]>();
private messages: string[] = [];
private lastRenameEntry: RenameEntry;
@ -37,7 +37,7 @@ namespace ts.server {
}
private getLineMap(fileName: string): number[] {
let lineMap = ts.lookUp(this.lineMaps, fileName);
let lineMap = this.lineMaps[fileName];
if (!lineMap) {
const scriptSnapshot = this.host.getScriptSnapshot(fileName);
lineMap = this.lineMaps[fileName] = ts.computeLineStarts(scriptSnapshot.getText(0, scriptSnapshot.getLength()));

View File

@ -130,15 +130,15 @@ namespace ts.server {
const path = toPath(containingFile, this.host.getCurrentDirectory(), this.getCanonicalFileName);
const currentResolutionsInFile = cache.get(path);
const newResolutions: Map<T> = {};
const newResolutions = createMap<T>();
const resolvedModules: R[] = [];
const compilerOptions = this.getCompilationSettings();
for (const name of names) {
// check if this is a duplicate entry in the list
let resolution = lookUp(newResolutions, name);
let resolution = newResolutions[name];
if (!resolution) {
const existingResolution = currentResolutionsInFile && ts.lookUp(currentResolutionsInFile, name);
const existingResolution = currentResolutionsInFile && currentResolutionsInFile[name];
if (moduleResolutionIsValid(existingResolution)) {
// ok, it is safe to use existing name resolution results
resolution = existingResolution;
@ -378,7 +378,7 @@ namespace ts.server {
export interface ProjectOptions {
// these fields can be present in the project file
files?: string[];
wildcardDirectories?: ts.Map<ts.WatchDirectoryFlags>;
wildcardDirectories?: ts.MapLike<ts.WatchDirectoryFlags>;
compilerOptions?: ts.CompilerOptions;
}
@ -391,7 +391,7 @@ namespace ts.server {
// Used to keep track of what directories are watched for this project
directoriesWatchedForTsconfig: string[] = [];
program: ts.Program;
filenameToSourceFile: ts.Map<ts.SourceFile> = {};
filenameToSourceFile = ts.createMap<ts.SourceFile>();
updateGraphSeq = 0;
/** Used for configured projects which may have multiple open roots */
openRefCount = 0;
@ -504,7 +504,7 @@ namespace ts.server {
return;
}
this.filenameToSourceFile = {};
this.filenameToSourceFile = createMap<SourceFile>();
const sourceFiles = this.program.getSourceFiles();
for (let i = 0, len = sourceFiles.length; i < len; i++) {
const normFilename = ts.normalizePath(sourceFiles[i].fileName);
@ -563,7 +563,7 @@ namespace ts.server {
}
let strBuilder = "";
ts.forEachValue(this.filenameToSourceFile,
ts.forEachProperty(this.filenameToSourceFile,
sourceFile => { strBuilder += sourceFile.fileName + "\n"; });
return strBuilder;
}
@ -613,7 +613,7 @@ namespace ts.server {
}
export class ProjectService {
filenameToScriptInfo: ts.Map<ScriptInfo> = {};
filenameToScriptInfo = ts.createMap<ScriptInfo>();
// open, non-configured root files
openFileRoots: ScriptInfo[] = [];
// projects built from openFileRoots
@ -625,12 +625,12 @@ namespace ts.server {
// open files that are roots of a configured project
openFileRootsConfigured: ScriptInfo[] = [];
// a path to directory watcher map that detects added tsconfig files
directoryWatchersForTsconfig: ts.Map<FileWatcher> = {};
directoryWatchersForTsconfig = ts.createMap<FileWatcher>();
// count of how many projects are using the directory watcher. If the
// number becomes 0 for a watcher, then we should close it.
directoryWatchersRefCount: ts.Map<number> = {};
directoryWatchersRefCount = ts.createMap<number>();
hostConfiguration: HostConfiguration;
timerForDetectingProjectFileListChanges: Map<any> = {};
timerForDetectingProjectFileListChanges = createMap<any>();
constructor(public host: ServerHost, public psLogger: Logger, public eventHandler?: ProjectServiceEventHandler) {
// ts.disableIncrementalParsing = true;
@ -857,7 +857,7 @@ namespace ts.server {
if (project.isConfiguredProject()) {
project.projectFileWatcher.close();
project.directoryWatcher.close();
forEachValue(project.directoriesWatchedForWildcards, watcher => { watcher.close(); });
forEachProperty(project.directoriesWatchedForWildcards, watcher => { watcher.close(); });
delete project.directoriesWatchedForWildcards;
this.configuredProjects = copyListRemovingItem(project, this.configuredProjects);
}
@ -1124,7 +1124,7 @@ namespace ts.server {
getScriptInfo(filename: string) {
filename = ts.normalizePath(filename);
return ts.lookUp(this.filenameToScriptInfo, filename);
return this.filenameToScriptInfo[filename];
}
/**
@ -1133,7 +1133,7 @@ namespace ts.server {
*/
openFile(fileName: string, openedByClient: boolean, fileContent?: string, scriptKind?: ScriptKind) {
fileName = ts.normalizePath(fileName);
let info = ts.lookUp(this.filenameToScriptInfo, fileName);
let info = this.filenameToScriptInfo[fileName];
if (!info) {
let content: string;
if (this.host.fileExists(fileName)) {
@ -1246,7 +1246,7 @@ namespace ts.server {
* @param filename is absolute pathname
*/
closeClientFile(filename: string) {
const info = ts.lookUp(this.filenameToScriptInfo, filename);
const info = this.filenameToScriptInfo[filename];
if (info) {
this.closeOpenFile(info);
info.isOpen = false;
@ -1255,14 +1255,14 @@ namespace ts.server {
}
getProjectForFile(filename: string) {
const scriptInfo = ts.lookUp(this.filenameToScriptInfo, filename);
const scriptInfo = this.filenameToScriptInfo[filename];
if (scriptInfo) {
return scriptInfo.defaultProject;
}
}
printProjectsForFile(filename: string) {
const scriptInfo = ts.lookUp(this.filenameToScriptInfo, filename);
const scriptInfo = this.filenameToScriptInfo[filename];
if (scriptInfo) {
this.psLogger.startGroup();
this.psLogger.info("Projects for " + filename);
@ -1419,7 +1419,7 @@ namespace ts.server {
/*recursive*/ true
);
project.directoriesWatchedForWildcards = reduceProperties(projectOptions.wildcardDirectories, (watchers, flag, directory) => {
project.directoriesWatchedForWildcards = reduceProperties(createMap(projectOptions.wildcardDirectories), (watchers, flag, directory) => {
if (comparePaths(configDirectoryPath, directory, ".", !this.host.useCaseSensitiveFileNames) !== Comparison.EqualTo) {
const recursive = (flag & WatchDirectoryFlags.Recursive) !== 0;
this.log(`Add ${ recursive ? "recursive " : ""}watcher for: ${directory}`);

View File

@ -1061,7 +1061,7 @@ namespace ts.server {
return { response, responseRequired: true };
}
private handlers: Map<(request: protocol.Request) => { response?: any, responseRequired?: boolean }> = {
private handlers = createMap<(request: protocol.Request) => { response?: any, responseRequired?: boolean }>({
[CommandNames.Exit]: () => {
this.exit();
return { responseRequired: false };
@ -1198,9 +1198,10 @@ namespace ts.server {
this.reloadProjects();
return { responseRequired: false };
}
};
});
public addProtocolHandler(command: string, handler: (request: protocol.Request) => { response?: any, responseRequired: boolean }) {
if (this.handlers[command]) {
if (command in this.handlers) {
throw new Error(`Protocol handler already exists for command "${command}"`);
}
this.handlers[command] = handler;

View File

@ -10,6 +10,8 @@
"types": []
},
"files": [
"../services/shims.ts",
"../services/utilities.ts",
"editorServices.ts",
"protocol.d.ts",
"session.ts"

View File

@ -47,7 +47,7 @@ namespace ts.JsTyping {
{ cachedTypingPaths: string[], newTypingNames: string[], filesToWatch: string[] } {
// A typing name to typing file path mapping
const inferredTypings: Map<string> = {};
const inferredTypings = createMap<string>();
if (!typingOptions || !typingOptions.enableAutoDiscovery) {
return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] };
@ -58,12 +58,7 @@ namespace ts.JsTyping {
if (!safeList) {
const result = readConfigFile(safeListPath, (path: string) => host.readFile(path));
if (result.config) {
safeList = result.config;
}
else {
safeList = {};
};
safeList = createMap<string>(result.config);
}
const filesToWatch: string[] = [];
@ -93,7 +88,7 @@ namespace ts.JsTyping {
// Add the cached typing locations for inferred typings that are already installed
for (const name in packageNameToTypingLocation) {
if (hasProperty(inferredTypings, name) && !inferredTypings[name]) {
if (name in inferredTypings && !inferredTypings[name]) {
inferredTypings[name] = packageNameToTypingLocation[name];
}
}
@ -124,7 +119,7 @@ namespace ts.JsTyping {
}
for (const typing of typingNames) {
if (!hasProperty(inferredTypings, typing)) {
if (!(typing in inferredTypings)) {
inferredTypings[typing] = undefined;
}
}
@ -139,16 +134,16 @@ namespace ts.JsTyping {
const jsonConfig: PackageJson = result.config;
filesToWatch.push(jsonPath);
if (jsonConfig.dependencies) {
mergeTypings(getKeys(jsonConfig.dependencies));
mergeTypings(getOwnKeys(jsonConfig.dependencies));
}
if (jsonConfig.devDependencies) {
mergeTypings(getKeys(jsonConfig.devDependencies));
mergeTypings(getOwnKeys(jsonConfig.devDependencies));
}
if (jsonConfig.optionalDependencies) {
mergeTypings(getKeys(jsonConfig.optionalDependencies));
mergeTypings(getOwnKeys(jsonConfig.optionalDependencies));
}
if (jsonConfig.peerDependencies) {
mergeTypings(getKeys(jsonConfig.peerDependencies));
mergeTypings(getOwnKeys(jsonConfig.peerDependencies));
}
}
}
@ -167,7 +162,7 @@ namespace ts.JsTyping {
mergeTypings(cleanedTypingNames);
}
else {
mergeTypings(filter(cleanedTypingNames, f => hasProperty(safeList, f)));
mergeTypings(filter(cleanedTypingNames, f => f in safeList));
}
const hasJsxFile = forEach(fileNames, f => scriptKindIs(f, /*LanguageServiceHost*/ undefined, ScriptKind.JSX));

View File

@ -15,7 +15,7 @@ namespace ts.NavigateTo {
const nameToDeclarations = sourceFile.getNamedDeclarations();
for (const name in nameToDeclarations) {
const declarations = getProperty(nameToDeclarations, name);
const declarations = nameToDeclarations[name];
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.

View File

@ -234,7 +234,7 @@ namespace ts.NavigationBar {
/** Merge declarations of the same kind. */
function mergeChildren(children: NavigationBarNode[]): void {
const nameToItems: Map<NavigationBarNode | NavigationBarNode[]> = {};
const nameToItems = createMap<NavigationBarNode | NavigationBarNode[]>();
filterMutate(children, child => {
const decl = <Declaration>child.node;
const name = decl.name && nodeText(decl.name);
@ -243,7 +243,7 @@ namespace ts.NavigationBar {
return true;
}
const itemsWithSameName = getProperty(nameToItems, name);
const itemsWithSameName = nameToItems[name];
if (!itemsWithSameName) {
nameToItems[name] = child;
return true;

View File

@ -113,7 +113,7 @@ namespace ts {
// we see the name of a module that is used everywhere, or the name of an overload). As
// such, we cache the information we compute about the candidate for the life of this
// pattern matcher so we don't have to compute it multiple times.
const stringToWordSpans: Map<TextSpan[]> = {};
const stringToWordSpans = createMap<TextSpan[]>();
pattern = pattern.trim();
@ -188,7 +188,7 @@ namespace ts {
}
function getWordSpans(word: string): TextSpan[] {
if (!hasProperty(stringToWordSpans, word)) {
if (!(word in stringToWordSpans)) {
stringToWordSpans[word] = breakIntoWordSpans(word);
}

View File

@ -975,7 +975,7 @@ namespace ts {
}
private computeNamedDeclarations(): Map<Declaration[]> {
const result: Map<Declaration[]> = {};
const result = createMap<Declaration[]>();
forEachChild(this, visit);
@ -990,7 +990,7 @@ namespace ts {
}
function getDeclarations(name: string) {
return getProperty(result, name) || (result[name] = []);
return result[name] || (result[name] = []);
}
function getDeclarationName(declaration: Declaration) {
@ -1886,7 +1886,7 @@ namespace ts {
};
}
// Cache host information about scrip Should be refreshed
// Cache host information about script should be refreshed
// at each language service public entry point, since we don't know when
// set of scripts handled by the host changes.
class HostCache {
@ -2025,7 +2025,7 @@ namespace ts {
fileName?: string;
reportDiagnostics?: boolean;
moduleName?: string;
renamedDependencies?: Map<string>;
renamedDependencies?: MapLike<string>;
}
export interface TranspileOutput {
@ -2042,7 +2042,7 @@ namespace ts {
function fixupCompilerOptions(options: CompilerOptions, diagnostics: Diagnostic[]): CompilerOptions {
// Lazily create this value to fix module loading errors.
commandLineOptionsStringToEnum = commandLineOptionsStringToEnum || <CommandLineOptionOfCustomType[]>filter(optionDeclarations, o =>
typeof o.type === "object" && !forEachValue(<Map<any>>o.type, v => typeof v !== "number"));
typeof o.type === "object" && !forEachProperty(o.type, v => typeof v !== "number"));
options = clone(options);
@ -2058,7 +2058,7 @@ namespace ts {
options[opt.name] = parseCustomTypeOption(opt, value, diagnostics);
}
else {
if (!forEachValue(opt.type, v => v === value)) {
if (!forEachProperty(opt.type, v => v === value)) {
// Supplied value isn't a valid enum value.
diagnostics.push(createCompilerDiagnosticForInvalidCustomType(opt));
}
@ -2117,7 +2117,9 @@ namespace ts {
sourceFile.moduleName = transpileOptions.moduleName;
}
sourceFile.renamedDependencies = transpileOptions.renamedDependencies;
if (transpileOptions.renamedDependencies) {
sourceFile.renamedDependencies = createMap(transpileOptions.renamedDependencies);
}
const newLine = getNewLineCharacter(options);
@ -2243,7 +2245,7 @@ namespace ts {
export function createDocumentRegistry(useCaseSensitiveFileNames?: boolean, currentDirectory = ""): DocumentRegistry {
// Maps from compiler setting target (ES3, ES5, etc.) to all the cached documents we have
// for those settings.
const buckets: Map<FileMap<DocumentRegistryEntry>> = {};
const buckets = createMap<FileMap<DocumentRegistryEntry>>();
const getCanonicalFileName = createGetCanonicalFileName(!!useCaseSensitiveFileNames);
function getKeyForCompilationSettings(settings: CompilerOptions): DocumentRegistryBucketKey {
@ -2251,7 +2253,7 @@ namespace ts {
}
function getBucketForCompilationSettings(key: DocumentRegistryBucketKey, createIfMissing: boolean): FileMap<DocumentRegistryEntry> {
let bucket = lookUp(buckets, key);
let bucket = buckets[key];
if (!bucket && createIfMissing) {
buckets[key] = bucket = createFileMap<DocumentRegistryEntry>();
}
@ -2260,7 +2262,7 @@ namespace ts {
function reportStats() {
const bucketInfoArray = Object.keys(buckets).filter(name => name && name.charAt(0) === "_").map(name => {
const entries = lookUp(buckets, name);
const entries = buckets[name];
const sourceFiles: { name: string; refCount: number; references: string[]; }[] = [];
entries.forEachValue((key, entry) => {
sourceFiles.push({
@ -3099,7 +3101,7 @@ namespace ts {
oldSettings.allowJs !== newSettings.allowJs ||
oldSettings.disableSizeLimit !== oldSettings.disableSizeLimit ||
oldSettings.baseUrl !== newSettings.baseUrl ||
!mapIsEqualTo(oldSettings.paths, newSettings.paths));
!equalOwnProperties(oldSettings.paths, newSettings.paths));
// Now create a new compiler
const compilerHost: CompilerHost = {
@ -3114,7 +3116,6 @@ namespace ts {
getCurrentDirectory: () => currentDirectory,
fileExists: (fileName): boolean => {
// stub missing host functionality
Debug.assert(!host.resolveModuleNames || !host.resolveTypeReferenceDirectives);
return hostCache.getOrCreateEntry(fileName) !== undefined;
},
readFile: (fileName): string => {
@ -4102,7 +4103,7 @@ namespace ts {
* do not occur at the current position and have not otherwise been typed.
*/
function filterNamedImportOrExportCompletionItems(exportsOfModule: Symbol[], namedImportsOrExports: ImportOrExportSpecifier[]): Symbol[] {
const existingImportsOrExports: Map<boolean> = {};
const existingImportsOrExports = createMap<boolean>();
for (const element of namedImportsOrExports) {
// If this is the current item we are editing right now, do not filter it out
@ -4114,11 +4115,11 @@ namespace ts {
existingImportsOrExports[name.text] = true;
}
if (isEmpty(existingImportsOrExports)) {
if (!someProperties(existingImportsOrExports)) {
return filter(exportsOfModule, e => e.name !== "default");
}
return filter(exportsOfModule, e => e.name !== "default" && !lookUp(existingImportsOrExports, e.name));
return filter(exportsOfModule, e => e.name !== "default" && !existingImportsOrExports[e.name]);
}
/**
@ -4132,7 +4133,7 @@ namespace ts {
return contextualMemberSymbols;
}
const existingMemberNames: Map<boolean> = {};
const existingMemberNames = createMap<boolean>();
for (const m of existingMembers) {
// Ignore omitted expressions for missing members
if (m.kind !== SyntaxKind.PropertyAssignment &&
@ -4165,7 +4166,7 @@ namespace ts {
existingMemberNames[existingName] = true;
}
return filter(contextualMemberSymbols, m => !lookUp(existingMemberNames, m.name));
return filter(contextualMemberSymbols, m => !existingMemberNames[m.name]);
}
/**
@ -4175,7 +4176,7 @@ namespace ts {
* do not occur at the current position and have not otherwise been typed.
*/
function filterJsxAttributes(symbols: Symbol[], attributes: NodeArray<JsxAttribute | JsxSpreadAttribute>): Symbol[] {
const seenNames: Map<boolean> = {};
const seenNames = createMap<boolean>();
for (const attr of attributes) {
// If this is the current item we are editing right now, do not filter it out
if (attr.getStart() <= position && position <= attr.getEnd()) {
@ -4187,7 +4188,7 @@ namespace ts {
}
}
return filter(symbols, a => !lookUp(seenNames, a.name));
return filter(symbols, a => !seenNames[a.name]);
}
}
@ -4317,13 +4318,13 @@ namespace ts {
function getCompletionEntriesFromSymbols(symbols: Symbol[], entries: CompletionEntry[], location: Node, performCharacterChecks: boolean): Map<string> {
const start = timestamp();
const uniqueNames: Map<string> = {};
const uniqueNames = createMap<string>();
if (symbols) {
for (const symbol of symbols) {
const entry = createCompletionEntry(symbol, location, performCharacterChecks);
if (entry) {
const id = escapeIdentifier(entry.name);
if (!lookUp(uniqueNames, id)) {
if (!uniqueNames[id]) {
entries.push(entry);
uniqueNames[id] = id;
}
@ -4830,7 +4831,14 @@ namespace ts {
}
if (symbolFlags & SymbolFlags.Alias) {
addNewLineIfDisplayPartsExist();
displayParts.push(keywordPart(SyntaxKind.ImportKeyword));
if (symbol.declarations[0].kind === SyntaxKind.NamespaceExportDeclaration) {
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
displayParts.push(spacePart());
displayParts.push(keywordPart(SyntaxKind.NamespaceKeyword));
}
else {
displayParts.push(keywordPart(SyntaxKind.ImportKeyword));
}
displayParts.push(spacePart());
addFullSymbolName(symbol);
ts.forEach(symbol.declarations, declaration => {
@ -5144,7 +5152,7 @@ namespace ts {
// Type reference directives
const typeReferenceDirective = findReferenceInPosition(sourceFile.typeReferenceDirectives, position);
if (typeReferenceDirective) {
const referenceFile = lookUp(program.getResolvedTypeReferenceDirectives(), typeReferenceDirective.fileName);
const referenceFile = program.getResolvedTypeReferenceDirectives()[typeReferenceDirective.fileName];
if (referenceFile && referenceFile.resolvedFileName) {
return [getDefinitionInfoForFileReference(typeReferenceDirective.fileName, referenceFile.resolvedFileName)];
}
@ -5311,12 +5319,12 @@ namespace ts {
return undefined;
}
const fileNameToDocumentHighlights: Map<DocumentHighlights> = {};
const fileNameToDocumentHighlights = createMap<DocumentHighlights>();
const result: DocumentHighlights[] = [];
for (const referencedSymbol of referencedSymbols) {
for (const referenceEntry of referencedSymbol.references) {
const fileName = referenceEntry.fileName;
let documentHighlights = getProperty(fileNameToDocumentHighlights, fileName);
let documentHighlights = fileNameToDocumentHighlights[fileName];
if (!documentHighlights) {
documentHighlights = { fileName, highlightSpans: [] };
@ -6061,7 +6069,7 @@ namespace ts {
const nameTable = getNameTable(sourceFile);
if (lookUp(nameTable, internedName) !== undefined) {
if (nameTable[internedName] !== undefined) {
result = result || [];
getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result, symbolToIndex);
}
@ -6706,7 +6714,7 @@ namespace ts {
// Add symbol of properties/methods of the same name in base classes and implemented interfaces definitions
if (rootSymbol.parent && rootSymbol.parent.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
getPropertySymbolsFromBaseTypes(rootSymbol.parent, rootSymbol.getName(), result, /*previousIterationSymbolsCache*/ {});
getPropertySymbolsFromBaseTypes(rootSymbol.parent, rootSymbol.getName(), result, /*previousIterationSymbolsCache*/ createMap<Symbol>());
}
});
@ -6738,7 +6746,7 @@ namespace ts {
// the function will add any found symbol of the property-name, then its sub-routine will call
// getPropertySymbolsFromBaseTypes again to walk up any base types to prevent revisiting already
// visited symbol, interface "C", the sub-routine will pass the current symbol as previousIterationSymbol.
if (hasProperty(previousIterationSymbolsCache, symbol.name)) {
if (symbol.name in previousIterationSymbolsCache) {
return;
}
@ -6827,7 +6835,7 @@ namespace ts {
// see if any is in the list
if (rootSymbol.parent && rootSymbol.parent.flags & (SymbolFlags.Class | SymbolFlags.Interface)) {
const result: Symbol[] = [];
getPropertySymbolsFromBaseTypes(rootSymbol.parent, rootSymbol.getName(), result, /*previousIterationSymbolsCache*/ {});
getPropertySymbolsFromBaseTypes(rootSymbol.parent, rootSymbol.getName(), result, /*previousIterationSymbolsCache*/ createMap<Symbol>());
return forEach(result, s => searchSymbols.indexOf(s) >= 0 ? s : undefined);
}
@ -8311,7 +8319,7 @@ namespace ts {
}
function initializeNameTable(sourceFile: SourceFile): void {
const nameTable: Map<number> = {};
const nameTable = createMap<number>();
walk(sourceFile);
sourceFile.nameTable = nameTable;

View File

@ -72,8 +72,13 @@ namespace ts {
directoryExists(directoryName: string): boolean;
}
/** Public interface of the the of a config service shim instance.*/
export interface CoreServicesShimHost extends Logger, ModuleResolutionHost {
/** Public interface of the core-services host instance used in managed side */
export interface CoreServicesShimHost extends Logger {
directoryExists(directoryName: string): boolean;
fileExists(fileName: string): boolean;
getCurrentDirectory(): string;
getDirectories(path: string): string;
/**
* Returns a JSON-encoded value of the type: string[]
*
@ -81,9 +86,14 @@ namespace ts {
* when enumerating the directory.
*/
readDirectory(rootDir: string, extension: string, basePaths?: string, excludeEx?: string, includeFileEx?: string, includeDirEx?: string, depth?: number): string;
useCaseSensitiveFileNames?(): boolean;
getCurrentDirectory(): string;
/**
* Read arbitary text files on disk, i.e. when resolution procedure needs the content of 'package.json' to determine location of bundled typings for node modules
*/
readFile(fileName: string): string;
realpath?(path: string): string;
trace(s: string): void;
useCaseSensitiveFileNames?(): boolean;
}
///
@ -240,6 +250,7 @@ namespace ts {
}
export interface CoreServicesShim extends Shim {
getAutomaticTypeDirectiveNames(compilerOptionsJson: string): string;
getPreProcessedFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
getTSConfigFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
getDefaultCompilationSettings(): string;
@ -300,9 +311,9 @@ namespace ts {
// 'in' does not have this effect.
if ("getModuleResolutionsForFile" in this.shimHost) {
this.resolveModuleNames = (moduleNames: string[], containingFile: string) => {
const resolutionsInFile = <Map<string>>JSON.parse(this.shimHost.getModuleResolutionsForFile(containingFile));
const resolutionsInFile = <MapLike<string>>JSON.parse(this.shimHost.getModuleResolutionsForFile(containingFile));
return map(moduleNames, name => {
const result = lookUp(resolutionsInFile, name);
const result = getProperty(resolutionsInFile, name);
return result ? { resolvedFileName: result } : undefined;
});
};
@ -312,8 +323,8 @@ namespace ts {
}
if ("getTypeReferenceDirectiveResolutionsForFile" in this.shimHost) {
this.resolveTypeReferenceDirectives = (typeDirectiveNames: string[], containingFile: string) => {
const typeDirectivesForFile = <Map<ResolvedTypeReferenceDirective>>JSON.parse(this.shimHost.getTypeReferenceDirectiveResolutionsForFile(containingFile));
return map(typeDirectiveNames, name => lookUp(typeDirectivesForFile, name));
const typeDirectivesForFile = <MapLike<ResolvedTypeReferenceDirective>>JSON.parse(this.shimHost.getTypeReferenceDirectiveResolutionsForFile(containingFile));
return map(typeDirectiveNames, name => getProperty(typeDirectivesForFile, name));
};
}
}
@ -492,6 +503,10 @@ namespace ts {
private readDirectoryFallback(rootDir: string, extension: string, exclude: string[]) {
return JSON.parse(this.shimHost.readDirectory(rootDir, extension, JSON.stringify(exclude)));
}
public getDirectories(path: string): string[] {
return JSON.parse(this.shimHost.getDirectories(path));
}
}
function simpleForwardCall(logger: Logger, actionDescription: string, action: () => any, logPerformance: boolean): any {
@ -1003,7 +1018,7 @@ namespace ts {
public getPreProcessedFileInfo(fileName: string, sourceTextSnapshot: IScriptSnapshot): string {
return this.forwardJSONCall(
"getPreProcessedFileInfo('" + fileName + "')",
`getPreProcessedFileInfo('${fileName}')`,
() => {
// for now treat files as JavaScript
const result = preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), /* readImportFiles */ true, /* detectJavaScriptImports */ true);
@ -1017,6 +1032,16 @@ namespace ts {
});
}
public getAutomaticTypeDirectiveNames(compilerOptionsJson: string): string {
return this.forwardJSONCall(
`getAutomaticTypeDirectiveNames('${compilerOptionsJson}')`,
() => {
const compilerOptions = <CompilerOptions>JSON.parse(compilerOptionsJson);
return getAutomaticTypeDirectiveNames(compilerOptions, this.host);
}
);
}
private convertFileReferences(refs: FileReference[]): IFileReference[] {
if (!refs) {
return undefined;

View File

@ -237,7 +237,7 @@ namespace ts.SignatureHelp {
const typeChecker = program.getTypeChecker();
for (const sourceFile of program.getSourceFiles()) {
const nameToDeclarations = sourceFile.getNamedDeclarations();
const declarations = getProperty(nameToDeclarations, name.text);
const declarations = nameToDeclarations[name.text];
if (declarations) {
for (const declaration of declarations) {

View File

@ -20,7 +20,7 @@ declare var path: any;
import * as ts from "typescript";
function watch(rootFileNames: string[], options: ts.CompilerOptions) {
const files: ts.Map<{ version: number }> = {};
const files: ts.MapLike<{ version: number }> = {};
// initialize the list of files
rootFileNames.forEach(fileName => {

View File

@ -0,0 +1,46 @@
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(13,1): error TS2322: Type 'A[]' is not assignable to type 'ReadonlyArray<B>'.
Types of property 'concat' are incompatible.
Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
Type 'A[]' is not assignable to type 'B[]'.
Type 'A' is not assignable to type 'B'.
Property 'b' is missing in type 'A'.
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error TS2322: Type 'C<A>' is not assignable to type 'ReadonlyArray<B>'.
Types of property 'concat' are incompatible.
Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
Type 'A[]' is not assignable to type 'B[]'.
Type 'A' is not assignable to type 'B'.
==== tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts (2 errors) ====
class A { a }
class B extends A { b }
class C<T> extends Array<T> { c }
declare var ara: A[];
declare var arb: B[];
declare var cra: C<A>;
declare var crb: C<B>;
declare var rra: ReadonlyArray<A>;
declare var rrb: ReadonlyArray<B>;
rra = ara;
rrb = arb; // OK, Array<B> is assignable to ReadonlyArray<A>
rra = arb;
rrb = ara; // error: 'A' is not assignable to 'B'
~~~
!!! error TS2322: Type 'A[]' is not assignable to type 'ReadonlyArray<B>'.
!!! error TS2322: Types of property 'concat' are incompatible.
!!! error TS2322: Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
!!! error TS2322: Type 'A' is not assignable to type 'B'.
!!! error TS2322: Property 'b' is missing in type 'A'.
rra = cra;
rra = crb; // OK, C<B> is assignable to ReadonlyArray<A>
rrb = crb;
rrb = cra; // error: 'A' is not assignable to 'B'
~~~
!!! error TS2322: Type 'C<A>' is not assignable to type 'ReadonlyArray<B>'.
!!! error TS2322: Types of property 'concat' are incompatible.
!!! error TS2322: Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
!!! error TS2322: Type 'A' is not assignable to type 'B'.

View File

@ -0,0 +1,54 @@
//// [arrayOfSubtypeIsAssignableToReadonlyArray.ts]
class A { a }
class B extends A { b }
class C<T> extends Array<T> { c }
declare var ara: A[];
declare var arb: B[];
declare var cra: C<A>;
declare var crb: C<B>;
declare var rra: ReadonlyArray<A>;
declare var rrb: ReadonlyArray<B>;
rra = ara;
rrb = arb; // OK, Array<B> is assignable to ReadonlyArray<A>
rra = arb;
rrb = ara; // error: 'A' is not assignable to 'B'
rra = cra;
rra = crb; // OK, C<B> is assignable to ReadonlyArray<A>
rrb = crb;
rrb = cra; // error: 'A' is not assignable to 'B'
//// [arrayOfSubtypeIsAssignableToReadonlyArray.js]
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var A = (function () {
function A() {
}
return A;
}());
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
}
return B;
}(A));
var C = (function (_super) {
__extends(C, _super);
function C() {
_super.apply(this, arguments);
}
return C;
}(Array));
rra = ara;
rrb = arb; // OK, Array<B> is assignable to ReadonlyArray<A>
rra = arb;
rrb = ara; // error: 'A' is not assignable to 'B'
rra = cra;
rra = crb; // OK, C<B> is assignable to ReadonlyArray<A>
rrb = crb;
rrb = cra; // error: 'A' is not assignable to 'B'

View File

@ -13,14 +13,14 @@ let C = class extends class extends class {
}
}
{
constructor(...args) {
super(...args);
constructor() {
super(...arguments);
this.b = 2;
}
}
{
constructor(...args) {
super(...args);
constructor() {
super(...arguments);
this.c = 3;
}
}

View File

@ -64,9 +64,9 @@ if (isNodeList(sourceObj)) {
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))
sourceObj.length;
>sourceObj.length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>sourceObj.length : Symbol(NodeList.length, Decl(controlFlowBinaryOrExpression.ts, 10, 27))
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))
>length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>length : Symbol(NodeList.length, Decl(controlFlowBinaryOrExpression.ts, 10, 27))
}
if (isHTMLCollection(sourceObj)) {
@ -74,9 +74,9 @@ if (isHTMLCollection(sourceObj)) {
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))
sourceObj.length;
>sourceObj.length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>sourceObj.length : Symbol(HTMLCollection.length, Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>sourceObj : Symbol(sourceObj, Decl(controlFlowBinaryOrExpression.ts, 23, 3))
>length : Symbol(length, Decl(controlFlowBinaryOrExpression.ts, 10, 27), Decl(controlFlowBinaryOrExpression.ts, 14, 33))
>length : Symbol(HTMLCollection.length, Decl(controlFlowBinaryOrExpression.ts, 14, 33))
}
if (isNodeList(sourceObj) || isHTMLCollection(sourceObj)) {

View File

@ -80,7 +80,7 @@ if (isNodeList(sourceObj)) {
sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList | HTMLCollection
>sourceObj : NodeList
>length : number
}
@ -91,7 +91,7 @@ if (isHTMLCollection(sourceObj)) {
sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList | HTMLCollection
>sourceObj : HTMLCollection
>length : number
}
@ -102,11 +102,11 @@ if (isNodeList(sourceObj) || isHTMLCollection(sourceObj)) {
>sourceObj : EventTargetLike
>isHTMLCollection(sourceObj) : boolean
>isHTMLCollection : (sourceObj: any) => sourceObj is HTMLCollection
>sourceObj : { a: string; }
>sourceObj : HTMLCollection | { a: string; }
sourceObj.length;
>sourceObj.length : number
>sourceObj : NodeList | HTMLCollection | ({ a: string; } & HTMLCollection)
>sourceObj : NodeList | HTMLCollection
>length : number
}

View File

@ -35,6 +35,22 @@ function b() {
}
x; // string
}
function c<T>(data: string | T): T {
if (typeof data === 'string') {
return JSON.parse(data);
}
else {
return data;
}
}
function d<T extends string>(data: string | T): never {
if (typeof data === 'string') {
throw new Error('will always happen');
}
else {
return data;
}
}
//// [controlFlowIfStatement.js]
@ -72,3 +88,19 @@ function b() {
}
x; // string
}
function c(data) {
if (typeof data === 'string') {
return JSON.parse(data);
}
else {
return data;
}
}
function d(data) {
if (typeof data === 'string') {
throw new Error('will always happen');
}
else {
return data;
}
}

View File

@ -71,4 +71,42 @@ function b() {
x; // string
>x : Symbol(x, Decl(controlFlowIfStatement.ts, 26, 7))
}
function c<T>(data: string | T): T {
>c : Symbol(c, Decl(controlFlowIfStatement.ts, 35, 1))
>T : Symbol(T, Decl(controlFlowIfStatement.ts, 36, 11))
>data : Symbol(data, Decl(controlFlowIfStatement.ts, 36, 14))
>T : Symbol(T, Decl(controlFlowIfStatement.ts, 36, 11))
>T : Symbol(T, Decl(controlFlowIfStatement.ts, 36, 11))
if (typeof data === 'string') {
>data : Symbol(data, Decl(controlFlowIfStatement.ts, 36, 14))
return JSON.parse(data);
>JSON.parse : Symbol(JSON.parse, Decl(lib.d.ts, --, --))
>JSON : Symbol(JSON, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>parse : Symbol(JSON.parse, Decl(lib.d.ts, --, --))
>data : Symbol(data, Decl(controlFlowIfStatement.ts, 36, 14))
}
else {
return data;
>data : Symbol(data, Decl(controlFlowIfStatement.ts, 36, 14))
}
}
function d<T extends string>(data: string | T): never {
>d : Symbol(d, Decl(controlFlowIfStatement.ts, 43, 1))
>T : Symbol(T, Decl(controlFlowIfStatement.ts, 44, 11))
>data : Symbol(data, Decl(controlFlowIfStatement.ts, 44, 29))
>T : Symbol(T, Decl(controlFlowIfStatement.ts, 44, 11))
if (typeof data === 'string') {
>data : Symbol(data, Decl(controlFlowIfStatement.ts, 44, 29))
throw new Error('will always happen');
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
}
else {
return data;
>data : Symbol(data, Decl(controlFlowIfStatement.ts, 44, 29))
}
}

View File

@ -90,4 +90,51 @@ function b() {
x; // string
>x : string
}
function c<T>(data: string | T): T {
>c : <T>(data: string | T) => T
>T : T
>data : string | T
>T : T
>T : T
if (typeof data === 'string') {
>typeof data === 'string' : boolean
>typeof data : string
>data : string | T
>'string' : "string"
return JSON.parse(data);
>JSON.parse(data) : any
>JSON.parse : (text: string, reviver?: (key: any, value: any) => any) => any
>JSON : JSON
>parse : (text: string, reviver?: (key: any, value: any) => any) => any
>data : string
}
else {
return data;
>data : T
}
}
function d<T extends string>(data: string | T): never {
>d : <T extends string>(data: string | T) => never
>T : T
>data : string | T
>T : T
if (typeof data === 'string') {
>typeof data === 'string' : boolean
>typeof data : string
>data : string | T
>'string' : "string"
throw new Error('will always happen');
>new Error('will always happen') : Error
>Error : ErrorConstructor
>'will always happen' : string
}
else {
return data;
>data : never
}
}

View File

@ -0,0 +1,184 @@
//// [controlFlowInstanceof.ts]
// Repros from #10167
function f1(s: Set<string> | Set<number>) {
s = new Set<number>();
s; // Set<number>
if (s instanceof Set) {
s; // Set<number>
}
s; // Set<number>
s.add(42);
}
function f2(s: Set<string> | Set<number>) {
s = new Set<number>();
s; // Set<number>
if (s instanceof Promise) {
s; // Set<number> & Promise<any>
}
s; // Set<number>
s.add(42);
}
function f3(s: Set<string> | Set<number>) {
s; // Set<string> | Set<number>
if (s instanceof Set) {
s; // Set<string> | Set<number>
}
else {
s; // never
}
}
function f4(s: Set<string> | Set<number>) {
s = new Set<number>();
s; // Set<number>
if (s instanceof Set) {
s; // Set<number>
}
else {
s; // never
}
}
// More tests
class A { a: string }
class B extends A { b: string }
class C extends A { c: string }
function foo(x: A | undefined) {
x; // A | undefined
if (x instanceof B || x instanceof C) {
x; // B | C
}
x; // A | undefined
if (x instanceof B && x instanceof C) {
x; // B & C
}
x; // A | undefined
if (!x) {
return;
}
x; // A
if (x instanceof B) {
x; // B
if (x instanceof C) {
x; // B & C
}
else {
x; // B
}
x; // B
}
else {
x; // A
}
x; // A
}
// X is neither assignable to Y nor a subtype of Y
// Y is assignable to X, but not a subtype of X
interface X {
x?: string;
}
class Y {
y: string;
}
function goo(x: X) {
x;
if (x instanceof Y) {
x.y;
}
x;
}
//// [controlFlowInstanceof.js]
// Repros from #10167
function f1(s) {
s = new Set();
s; // Set<number>
if (s instanceof Set) {
s; // Set<number>
}
s; // Set<number>
s.add(42);
}
function f2(s) {
s = new Set();
s; // Set<number>
if (s instanceof Promise) {
s; // Set<number> & Promise<any>
}
s; // Set<number>
s.add(42);
}
function f3(s) {
s; // Set<string> | Set<number>
if (s instanceof Set) {
s; // Set<string> | Set<number>
}
else {
s; // never
}
}
function f4(s) {
s = new Set();
s; // Set<number>
if (s instanceof Set) {
s; // Set<number>
}
else {
s; // never
}
}
// More tests
class A {
}
class B extends A {
}
class C extends A {
}
function foo(x) {
x; // A | undefined
if (x instanceof B || x instanceof C) {
x; // B | C
}
x; // A | undefined
if (x instanceof B && x instanceof C) {
x; // B & C
}
x; // A | undefined
if (!x) {
return;
}
x; // A
if (x instanceof B) {
x; // B
if (x instanceof C) {
x; // B & C
}
else {
x; // B
}
x; // B
}
else {
x; // A
}
x; // A
}
class Y {
}
function goo(x) {
x;
if (x instanceof Y) {
x.y;
}
x;
}

View File

@ -0,0 +1,232 @@
=== tests/cases/compiler/controlFlowInstanceof.ts ===
// Repros from #10167
function f1(s: Set<string> | Set<number>) {
>f1 : Symbol(f1, Decl(controlFlowInstanceof.ts, 0, 0))
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s = new Set<number>();
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s; // Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
if (s instanceof Set) {
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s; // Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
}
s; // Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
s.add(42);
>s.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 3, 12))
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
}
function f2(s: Set<string> | Set<number>) {
>f2 : Symbol(f2, Decl(controlFlowInstanceof.ts, 11, 1))
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s = new Set<number>();
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s; // Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
if (s instanceof Promise) {
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
s; // Set<number> & Promise<any>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
}
s; // Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
s.add(42);
>s.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 13, 12))
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
}
function f3(s: Set<string> | Set<number>) {
>f3 : Symbol(f3, Decl(controlFlowInstanceof.ts, 21, 1))
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s; // Set<string> | Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
if (s instanceof Set) {
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s; // Set<string> | Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
}
else {
s; // never
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 23, 12))
}
}
function f4(s: Set<string> | Set<number>) {
>f4 : Symbol(f4, Decl(controlFlowInstanceof.ts, 31, 1))
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s = new Set<number>();
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s; // Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
if (s instanceof Set) {
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
>Set : Symbol(Set, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
s; // Set<number>
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
}
else {
s; // never
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 33, 12))
}
}
// More tests
class A { a: string }
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
>a : Symbol(A.a, Decl(controlFlowInstanceof.ts, 46, 9))
class B extends A { b: string }
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
>b : Symbol(B.b, Decl(controlFlowInstanceof.ts, 47, 19))
class C extends A { c: string }
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
>c : Symbol(C.c, Decl(controlFlowInstanceof.ts, 48, 19))
function foo(x: A | undefined) {
>foo : Symbol(foo, Decl(controlFlowInstanceof.ts, 48, 31))
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
>A : Symbol(A, Decl(controlFlowInstanceof.ts, 42, 1))
x; // A | undefined
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
if (x instanceof B || x instanceof C) {
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
x; // B | C
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
}
x; // A | undefined
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
if (x instanceof B && x instanceof C) {
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
x; // B & C
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
}
x; // A | undefined
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
if (!x) {
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
return;
}
x; // A
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
if (x instanceof B) {
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
>B : Symbol(B, Decl(controlFlowInstanceof.ts, 46, 21))
x; // B
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
if (x instanceof C) {
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
>C : Symbol(C, Decl(controlFlowInstanceof.ts, 47, 31))
x; // B & C
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
}
else {
x; // B
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
}
x; // B
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
}
else {
x; // A
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
}
x; // A
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 50, 13))
}
// X is neither assignable to Y nor a subtype of Y
// Y is assignable to X, but not a subtype of X
interface X {
>X : Symbol(X, Decl(controlFlowInstanceof.ts, 78, 1))
x?: string;
>x : Symbol(X.x, Decl(controlFlowInstanceof.ts, 83, 13))
}
class Y {
>Y : Symbol(Y, Decl(controlFlowInstanceof.ts, 85, 1))
y: string;
>y : Symbol(Y.y, Decl(controlFlowInstanceof.ts, 87, 9))
}
function goo(x: X) {
>goo : Symbol(goo, Decl(controlFlowInstanceof.ts, 89, 1))
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 91, 13))
>X : Symbol(X, Decl(controlFlowInstanceof.ts, 78, 1))
x;
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 91, 13))
if (x instanceof Y) {
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 91, 13))
>Y : Symbol(Y, Decl(controlFlowInstanceof.ts, 85, 1))
x.y;
>x.y : Symbol(Y.y, Decl(controlFlowInstanceof.ts, 87, 9))
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 91, 13))
>y : Symbol(Y.y, Decl(controlFlowInstanceof.ts, 87, 9))
}
x;
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 91, 13))
}

View File

@ -0,0 +1,256 @@
=== tests/cases/compiler/controlFlowInstanceof.ts ===
// Repros from #10167
function f1(s: Set<string> | Set<number>) {
>f1 : (s: Set<string> | Set<number>) => void
>s : Set<string> | Set<number>
>Set : Set<T>
>Set : Set<T>
s = new Set<number>();
>s = new Set<number>() : Set<number>
>s : Set<string> | Set<number>
>new Set<number>() : Set<number>
>Set : SetConstructor
s; // Set<number>
>s : Set<number>
if (s instanceof Set) {
>s instanceof Set : boolean
>s : Set<number>
>Set : SetConstructor
s; // Set<number>
>s : Set<number>
}
s; // Set<number>
>s : Set<number>
s.add(42);
>s.add(42) : Set<number>
>s.add : (value: number) => Set<number>
>s : Set<number>
>add : (value: number) => Set<number>
>42 : number
}
function f2(s: Set<string> | Set<number>) {
>f2 : (s: Set<string> | Set<number>) => void
>s : Set<string> | Set<number>
>Set : Set<T>
>Set : Set<T>
s = new Set<number>();
>s = new Set<number>() : Set<number>
>s : Set<string> | Set<number>
>new Set<number>() : Set<number>
>Set : SetConstructor
s; // Set<number>
>s : Set<number>
if (s instanceof Promise) {
>s instanceof Promise : boolean
>s : Set<number>
>Promise : PromiseConstructor
s; // Set<number> & Promise<any>
>s : Set<number> & Promise<any>
}
s; // Set<number>
>s : Set<number>
s.add(42);
>s.add(42) : Set<number>
>s.add : (value: number) => Set<number>
>s : Set<number>
>add : (value: number) => Set<number>
>42 : number
}
function f3(s: Set<string> | Set<number>) {
>f3 : (s: Set<string> | Set<number>) => void
>s : Set<string> | Set<number>
>Set : Set<T>
>Set : Set<T>
s; // Set<string> | Set<number>
>s : Set<string> | Set<number>
if (s instanceof Set) {
>s instanceof Set : boolean
>s : Set<string> | Set<number>
>Set : SetConstructor
s; // Set<string> | Set<number>
>s : Set<string> | Set<number>
}
else {
s; // never
>s : never
}
}
function f4(s: Set<string> | Set<number>) {
>f4 : (s: Set<string> | Set<number>) => void
>s : Set<string> | Set<number>
>Set : Set<T>
>Set : Set<T>
s = new Set<number>();
>s = new Set<number>() : Set<number>
>s : Set<string> | Set<number>
>new Set<number>() : Set<number>
>Set : SetConstructor
s; // Set<number>
>s : Set<number>
if (s instanceof Set) {
>s instanceof Set : boolean
>s : Set<number>
>Set : SetConstructor
s; // Set<number>
>s : Set<number>
}
else {
s; // never
>s : never
}
}
// More tests
class A { a: string }
>A : A
>a : string
class B extends A { b: string }
>B : B
>A : A
>b : string
class C extends A { c: string }
>C : C
>A : A
>c : string
function foo(x: A | undefined) {
>foo : (x: A) => void
>x : A
>A : A
x; // A | undefined
>x : A
if (x instanceof B || x instanceof C) {
>x instanceof B || x instanceof C : boolean
>x instanceof B : boolean
>x : A
>B : typeof B
>x instanceof C : boolean
>x : A
>C : typeof C
x; // B | C
>x : B | C
}
x; // A | undefined
>x : A
if (x instanceof B && x instanceof C) {
>x instanceof B && x instanceof C : boolean
>x instanceof B : boolean
>x : A
>B : typeof B
>x instanceof C : boolean
>x : B
>C : typeof C
x; // B & C
>x : B & C
}
x; // A | undefined
>x : A
if (!x) {
>!x : boolean
>x : A
return;
}
x; // A
>x : A
if (x instanceof B) {
>x instanceof B : boolean
>x : A
>B : typeof B
x; // B
>x : B
if (x instanceof C) {
>x instanceof C : boolean
>x : B
>C : typeof C
x; // B & C
>x : B & C
}
else {
x; // B
>x : B
}
x; // B
>x : B
}
else {
x; // A
>x : A
}
x; // A
>x : A
}
// X is neither assignable to Y nor a subtype of Y
// Y is assignable to X, but not a subtype of X
interface X {
>X : X
x?: string;
>x : string
}
class Y {
>Y : Y
y: string;
>y : string
}
function goo(x: X) {
>goo : (x: X) => void
>x : X
>X : X
x;
>x : X
if (x instanceof Y) {
>x instanceof Y : boolean
>x : X
>Y : typeof Y
x.y;
>x.y : string
>x : Y
>y : string
}
x;
>x : X
}

View File

@ -0,0 +1,77 @@
tests/cases/compiler/discriminantPropertyCheck.ts(30,9): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/discriminantPropertyCheck.ts(66,9): error TS2532: Object is possibly 'undefined'.
==== tests/cases/compiler/discriminantPropertyCheck.ts (2 errors) ====
type Item = Item1 | Item2;
interface Base {
bar: boolean;
}
interface Item1 extends Base {
kind: "A";
foo: string | undefined;
baz: boolean;
qux: true;
}
interface Item2 extends Base {
kind: "B";
foo: string | undefined;
baz: boolean;
qux: false;
}
function goo1(x: Item) {
if (x.kind === "A" && x.foo !== undefined) {
x.foo.length;
}
}
function goo2(x: Item) {
if (x.foo !== undefined && x.kind === "A") {
x.foo.length; // Error, intervening discriminant guard
~~~~~
!!! error TS2532: Object is possibly 'undefined'.
}
}
function foo1(x: Item) {
if (x.bar && x.foo !== undefined) {
x.foo.length;
}
}
function foo2(x: Item) {
if (x.foo !== undefined && x.bar) {
x.foo.length;
}
}
function foo3(x: Item) {
if (x.baz && x.foo !== undefined) {
x.foo.length;
}
}
function foo4(x: Item) {
if (x.foo !== undefined && x.baz) {
x.foo.length;
}
}
function foo5(x: Item) {
if (x.qux && x.foo !== undefined) {
x.foo.length;
}
}
function foo6(x: Item) {
if (x.foo !== undefined && x.qux) {
x.foo.length; // Error, intervening discriminant guard
~~~~~
!!! error TS2532: Object is possibly 'undefined'.
}
}

View File

@ -0,0 +1,111 @@
//// [discriminantPropertyCheck.ts]
type Item = Item1 | Item2;
interface Base {
bar: boolean;
}
interface Item1 extends Base {
kind: "A";
foo: string | undefined;
baz: boolean;
qux: true;
}
interface Item2 extends Base {
kind: "B";
foo: string | undefined;
baz: boolean;
qux: false;
}
function goo1(x: Item) {
if (x.kind === "A" && x.foo !== undefined) {
x.foo.length;
}
}
function goo2(x: Item) {
if (x.foo !== undefined && x.kind === "A") {
x.foo.length; // Error, intervening discriminant guard
}
}
function foo1(x: Item) {
if (x.bar && x.foo !== undefined) {
x.foo.length;
}
}
function foo2(x: Item) {
if (x.foo !== undefined && x.bar) {
x.foo.length;
}
}
function foo3(x: Item) {
if (x.baz && x.foo !== undefined) {
x.foo.length;
}
}
function foo4(x: Item) {
if (x.foo !== undefined && x.baz) {
x.foo.length;
}
}
function foo5(x: Item) {
if (x.qux && x.foo !== undefined) {
x.foo.length;
}
}
function foo6(x: Item) {
if (x.foo !== undefined && x.qux) {
x.foo.length; // Error, intervening discriminant guard
}
}
//// [discriminantPropertyCheck.js]
function goo1(x) {
if (x.kind === "A" && x.foo !== undefined) {
x.foo.length;
}
}
function goo2(x) {
if (x.foo !== undefined && x.kind === "A") {
x.foo.length; // Error, intervening discriminant guard
}
}
function foo1(x) {
if (x.bar && x.foo !== undefined) {
x.foo.length;
}
}
function foo2(x) {
if (x.foo !== undefined && x.bar) {
x.foo.length;
}
}
function foo3(x) {
if (x.baz && x.foo !== undefined) {
x.foo.length;
}
}
function foo4(x) {
if (x.foo !== undefined && x.baz) {
x.foo.length;
}
}
function foo5(x) {
if (x.qux && x.foo !== undefined) {
x.foo.length;
}
}
function foo6(x) {
if (x.foo !== undefined && x.qux) {
x.foo.length; // Error, intervening discriminant guard
}
}

View File

@ -0,0 +1,44 @@
//// [discriminantsAndNullOrUndefined.ts]
// Repro from #10228
interface A { kind: 'A'; }
interface B { kind: 'B'; }
type C = A | B | undefined;
function never(_: never): never {
throw new Error();
}
function useA(_: A): void { }
function useB(_: B): void { }
declare var c: C;
if (c !== undefined) {
switch (c.kind) {
case 'A': useA(c); break;
case 'B': useB(c); break;
default: never(c);
}
}
//// [discriminantsAndNullOrUndefined.js]
// Repro from #10228
function never(_) {
throw new Error();
}
function useA(_) { }
function useB(_) { }
if (c !== undefined) {
switch (c.kind) {
case 'A':
useA(c);
break;
case 'B':
useB(c);
break;
default: never(c);
}
}

View File

@ -0,0 +1,61 @@
=== tests/cases/compiler/discriminantsAndNullOrUndefined.ts ===
// Repro from #10228
interface A { kind: 'A'; }
>A : Symbol(A, Decl(discriminantsAndNullOrUndefined.ts, 0, 0))
>kind : Symbol(A.kind, Decl(discriminantsAndNullOrUndefined.ts, 3, 13))
interface B { kind: 'B'; }
>B : Symbol(B, Decl(discriminantsAndNullOrUndefined.ts, 3, 26))
>kind : Symbol(B.kind, Decl(discriminantsAndNullOrUndefined.ts, 4, 13))
type C = A | B | undefined;
>C : Symbol(C, Decl(discriminantsAndNullOrUndefined.ts, 4, 26))
>A : Symbol(A, Decl(discriminantsAndNullOrUndefined.ts, 0, 0))
>B : Symbol(B, Decl(discriminantsAndNullOrUndefined.ts, 3, 26))
function never(_: never): never {
>never : Symbol(never, Decl(discriminantsAndNullOrUndefined.ts, 6, 27))
>_ : Symbol(_, Decl(discriminantsAndNullOrUndefined.ts, 8, 15))
throw new Error();
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
}
function useA(_: A): void { }
>useA : Symbol(useA, Decl(discriminantsAndNullOrUndefined.ts, 10, 1))
>_ : Symbol(_, Decl(discriminantsAndNullOrUndefined.ts, 12, 14))
>A : Symbol(A, Decl(discriminantsAndNullOrUndefined.ts, 0, 0))
function useB(_: B): void { }
>useB : Symbol(useB, Decl(discriminantsAndNullOrUndefined.ts, 12, 29))
>_ : Symbol(_, Decl(discriminantsAndNullOrUndefined.ts, 13, 14))
>B : Symbol(B, Decl(discriminantsAndNullOrUndefined.ts, 3, 26))
declare var c: C;
>c : Symbol(c, Decl(discriminantsAndNullOrUndefined.ts, 15, 11))
>C : Symbol(C, Decl(discriminantsAndNullOrUndefined.ts, 4, 26))
if (c !== undefined) {
>c : Symbol(c, Decl(discriminantsAndNullOrUndefined.ts, 15, 11))
>undefined : Symbol(undefined)
switch (c.kind) {
>c.kind : Symbol(kind, Decl(discriminantsAndNullOrUndefined.ts, 3, 13), Decl(discriminantsAndNullOrUndefined.ts, 4, 13))
>c : Symbol(c, Decl(discriminantsAndNullOrUndefined.ts, 15, 11))
>kind : Symbol(kind, Decl(discriminantsAndNullOrUndefined.ts, 3, 13), Decl(discriminantsAndNullOrUndefined.ts, 4, 13))
case 'A': useA(c); break;
>useA : Symbol(useA, Decl(discriminantsAndNullOrUndefined.ts, 10, 1))
>c : Symbol(c, Decl(discriminantsAndNullOrUndefined.ts, 15, 11))
case 'B': useB(c); break;
>useB : Symbol(useB, Decl(discriminantsAndNullOrUndefined.ts, 12, 29))
>c : Symbol(c, Decl(discriminantsAndNullOrUndefined.ts, 15, 11))
default: never(c);
>never : Symbol(never, Decl(discriminantsAndNullOrUndefined.ts, 6, 27))
>c : Symbol(c, Decl(discriminantsAndNullOrUndefined.ts, 15, 11))
}
}

View File

@ -0,0 +1,68 @@
=== tests/cases/compiler/discriminantsAndNullOrUndefined.ts ===
// Repro from #10228
interface A { kind: 'A'; }
>A : A
>kind : "A"
interface B { kind: 'B'; }
>B : B
>kind : "B"
type C = A | B | undefined;
>C : C
>A : A
>B : B
function never(_: never): never {
>never : (_: never) => never
>_ : never
throw new Error();
>new Error() : Error
>Error : ErrorConstructor
}
function useA(_: A): void { }
>useA : (_: A) => void
>_ : A
>A : A
function useB(_: B): void { }
>useB : (_: B) => void
>_ : B
>B : B
declare var c: C;
>c : C
>C : C
if (c !== undefined) {
>c !== undefined : boolean
>c : C
>undefined : undefined
switch (c.kind) {
>c.kind : "A" | "B"
>c : A | B
>kind : "A" | "B"
case 'A': useA(c); break;
>'A' : "A"
>useA(c) : void
>useA : (_: A) => void
>c : A
case 'B': useB(c); break;
>'B' : "B"
>useB(c) : void
>useB : (_: B) => void
>c : B
default: never(c);
>never(c) : never
>never : (_: never) => never
>c : never
}
}

View File

@ -0,0 +1,84 @@
//// [discriminantsAndPrimitives.ts]
// Repro from #10257 plus other tests
interface Foo {
kind: "foo";
name: string;
}
interface Bar {
kind: "bar";
length: string;
}
function f1(x: Foo | Bar | string) {
if (typeof x !== 'string') {
switch(x.kind) {
case 'foo':
x.name;
}
}
}
function f2(x: Foo | Bar | string | undefined) {
if (typeof x === "object") {
switch(x.kind) {
case 'foo':
x.name;
}
}
}
function f3(x: Foo | Bar | string | null) {
if (x && typeof x !== "string") {
switch(x.kind) {
case 'foo':
x.name;
}
}
}
function f4(x: Foo | Bar | string | number | null) {
if (x && typeof x === "object") {
switch(x.kind) {
case 'foo':
x.name;
}
}
}
//// [discriminantsAndPrimitives.js]
// Repro from #10257 plus other tests
function f1(x) {
if (typeof x !== 'string') {
switch (x.kind) {
case 'foo':
x.name;
}
}
}
function f2(x) {
if (typeof x === "object") {
switch (x.kind) {
case 'foo':
x.name;
}
}
}
function f3(x) {
if (x && typeof x !== "string") {
switch (x.kind) {
case 'foo':
x.name;
}
}
}
function f4(x) {
if (x && typeof x === "object") {
switch (x.kind) {
case 'foo':
x.name;
}
}
}

View File

@ -0,0 +1,117 @@
=== tests/cases/compiler/discriminantsAndPrimitives.ts ===
// Repro from #10257 plus other tests
interface Foo {
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
kind: "foo";
>kind : Symbol(Foo.kind, Decl(discriminantsAndPrimitives.ts, 3, 15))
name: string;
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
}
interface Bar {
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
kind: "bar";
>kind : Symbol(Bar.kind, Decl(discriminantsAndPrimitives.ts, 8, 15))
length: string;
>length : Symbol(Bar.length, Decl(discriminantsAndPrimitives.ts, 9, 16))
}
function f1(x: Foo | Bar | string) {
>f1 : Symbol(f1, Decl(discriminantsAndPrimitives.ts, 11, 1))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
if (typeof x !== 'string') {
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
switch(x.kind) {
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
case 'foo':
x.name;
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 13, 12))
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
}
}
}
function f2(x: Foo | Bar | string | undefined) {
>f2 : Symbol(f2, Decl(discriminantsAndPrimitives.ts, 20, 1))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
if (typeof x === "object") {
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
switch(x.kind) {
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
case 'foo':
x.name;
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 22, 12))
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
}
}
}
function f3(x: Foo | Bar | string | null) {
>f3 : Symbol(f3, Decl(discriminantsAndPrimitives.ts, 29, 1))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
if (x && typeof x !== "string") {
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
switch(x.kind) {
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
case 'foo':
x.name;
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 31, 12))
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
}
}
}
function f4(x: Foo | Bar | string | number | null) {
>f4 : Symbol(f4, Decl(discriminantsAndPrimitives.ts, 38, 1))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
>Foo : Symbol(Foo, Decl(discriminantsAndPrimitives.ts, 0, 0))
>Bar : Symbol(Bar, Decl(discriminantsAndPrimitives.ts, 6, 1))
if (x && typeof x === "object") {
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
switch(x.kind) {
>x.kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
>kind : Symbol(kind, Decl(discriminantsAndPrimitives.ts, 3, 15), Decl(discriminantsAndPrimitives.ts, 8, 15))
case 'foo':
x.name;
>x.name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
>x : Symbol(x, Decl(discriminantsAndPrimitives.ts, 40, 12))
>name : Symbol(Foo.name, Decl(discriminantsAndPrimitives.ts, 4, 16))
}
}
}

View File

@ -0,0 +1,141 @@
=== tests/cases/compiler/discriminantsAndPrimitives.ts ===
// Repro from #10257 plus other tests
interface Foo {
>Foo : Foo
kind: "foo";
>kind : "foo"
name: string;
>name : string
}
interface Bar {
>Bar : Bar
kind: "bar";
>kind : "bar"
length: string;
>length : string
}
function f1(x: Foo | Bar | string) {
>f1 : (x: string | Foo | Bar) => void
>x : string | Foo | Bar
>Foo : Foo
>Bar : Bar
if (typeof x !== 'string') {
>typeof x !== 'string' : boolean
>typeof x : string
>x : string | Foo | Bar
>'string' : "string"
switch(x.kind) {
>x.kind : "foo" | "bar"
>x : Foo | Bar
>kind : "foo" | "bar"
case 'foo':
>'foo' : "foo"
x.name;
>x.name : string
>x : Foo
>name : string
}
}
}
function f2(x: Foo | Bar | string | undefined) {
>f2 : (x: string | Foo | Bar | undefined) => void
>x : string | Foo | Bar | undefined
>Foo : Foo
>Bar : Bar
if (typeof x === "object") {
>typeof x === "object" : boolean
>typeof x : string
>x : string | Foo | Bar | undefined
>"object" : "object"
switch(x.kind) {
>x.kind : "foo" | "bar"
>x : Foo | Bar
>kind : "foo" | "bar"
case 'foo':
>'foo' : "foo"
x.name;
>x.name : string
>x : Foo
>name : string
}
}
}
function f3(x: Foo | Bar | string | null) {
>f3 : (x: string | Foo | Bar | null) => void
>x : string | Foo | Bar | null
>Foo : Foo
>Bar : Bar
>null : null
if (x && typeof x !== "string") {
>x && typeof x !== "string" : boolean | "" | null
>x : string | Foo | Bar | null
>typeof x !== "string" : boolean
>typeof x : string
>x : string | Foo | Bar
>"string" : "string"
switch(x.kind) {
>x.kind : "foo" | "bar"
>x : Foo | Bar
>kind : "foo" | "bar"
case 'foo':
>'foo' : "foo"
x.name;
>x.name : string
>x : Foo
>name : string
}
}
}
function f4(x: Foo | Bar | string | number | null) {
>f4 : (x: string | number | Foo | Bar | null) => void
>x : string | number | Foo | Bar | null
>Foo : Foo
>Bar : Bar
>null : null
if (x && typeof x === "object") {
>x && typeof x === "object" : boolean | "" | 0 | null
>x : string | number | Foo | Bar | null
>typeof x === "object" : boolean
>typeof x : string
>x : string | number | Foo | Bar
>"object" : "object"
switch(x.kind) {
>x.kind : "foo" | "bar"
>x : Foo | Bar
>kind : "foo" | "bar"
case 'foo':
>'foo' : "foo"
x.name;
>x.name : string
>x : Foo
>name : string
}
}
}

View File

@ -0,0 +1,59 @@
//// [discriminantsAndTypePredicates.ts]
// Repro from #10145
interface A { type: 'A' }
interface B { type: 'B' }
function isA(x: A | B): x is A { return x.type === 'A'; }
function isB(x: A | B): x is B { return x.type === 'B'; }
function foo1(x: A | B): any {
x; // A | B
if (isA(x)) {
return x; // A
}
x; // B
if (isB(x)) {
return x; // B
}
x; // never
}
function foo2(x: A | B): any {
x; // A | B
if (x.type === 'A') {
return x; // A
}
x; // B
if (x.type === 'B') {
return x; // B
}
x; // never
}
//// [discriminantsAndTypePredicates.js]
// Repro from #10145
function isA(x) { return x.type === 'A'; }
function isB(x) { return x.type === 'B'; }
function foo1(x) {
x; // A | B
if (isA(x)) {
return x; // A
}
x; // B
if (isB(x)) {
return x; // B
}
x; // never
}
function foo2(x) {
x; // A | B
if (x.type === 'A') {
return x; // A
}
x; // B
if (x.type === 'B') {
return x; // B
}
x; // never
}

View File

@ -0,0 +1,94 @@
=== tests/cases/compiler/discriminantsAndTypePredicates.ts ===
// Repro from #10145
interface A { type: 'A' }
>A : Symbol(A, Decl(discriminantsAndTypePredicates.ts, 0, 0))
>type : Symbol(A.type, Decl(discriminantsAndTypePredicates.ts, 2, 13))
interface B { type: 'B' }
>B : Symbol(B, Decl(discriminantsAndTypePredicates.ts, 2, 25))
>type : Symbol(B.type, Decl(discriminantsAndTypePredicates.ts, 3, 13))
function isA(x: A | B): x is A { return x.type === 'A'; }
>isA : Symbol(isA, Decl(discriminantsAndTypePredicates.ts, 3, 25))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 5, 13))
>A : Symbol(A, Decl(discriminantsAndTypePredicates.ts, 0, 0))
>B : Symbol(B, Decl(discriminantsAndTypePredicates.ts, 2, 25))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 5, 13))
>A : Symbol(A, Decl(discriminantsAndTypePredicates.ts, 0, 0))
>x.type : Symbol(type, Decl(discriminantsAndTypePredicates.ts, 2, 13), Decl(discriminantsAndTypePredicates.ts, 3, 13))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 5, 13))
>type : Symbol(type, Decl(discriminantsAndTypePredicates.ts, 2, 13), Decl(discriminantsAndTypePredicates.ts, 3, 13))
function isB(x: A | B): x is B { return x.type === 'B'; }
>isB : Symbol(isB, Decl(discriminantsAndTypePredicates.ts, 5, 57))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 6, 13))
>A : Symbol(A, Decl(discriminantsAndTypePredicates.ts, 0, 0))
>B : Symbol(B, Decl(discriminantsAndTypePredicates.ts, 2, 25))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 6, 13))
>B : Symbol(B, Decl(discriminantsAndTypePredicates.ts, 2, 25))
>x.type : Symbol(type, Decl(discriminantsAndTypePredicates.ts, 2, 13), Decl(discriminantsAndTypePredicates.ts, 3, 13))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 6, 13))
>type : Symbol(type, Decl(discriminantsAndTypePredicates.ts, 2, 13), Decl(discriminantsAndTypePredicates.ts, 3, 13))
function foo1(x: A | B): any {
>foo1 : Symbol(foo1, Decl(discriminantsAndTypePredicates.ts, 6, 57))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
>A : Symbol(A, Decl(discriminantsAndTypePredicates.ts, 0, 0))
>B : Symbol(B, Decl(discriminantsAndTypePredicates.ts, 2, 25))
x; // A | B
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
if (isA(x)) {
>isA : Symbol(isA, Decl(discriminantsAndTypePredicates.ts, 3, 25))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
return x; // A
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
}
x; // B
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
if (isB(x)) {
>isB : Symbol(isB, Decl(discriminantsAndTypePredicates.ts, 5, 57))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
return x; // B
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
}
x; // never
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 8, 14))
}
function foo2(x: A | B): any {
>foo2 : Symbol(foo2, Decl(discriminantsAndTypePredicates.ts, 18, 1))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
>A : Symbol(A, Decl(discriminantsAndTypePredicates.ts, 0, 0))
>B : Symbol(B, Decl(discriminantsAndTypePredicates.ts, 2, 25))
x; // A | B
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
if (x.type === 'A') {
>x.type : Symbol(type, Decl(discriminantsAndTypePredicates.ts, 2, 13), Decl(discriminantsAndTypePredicates.ts, 3, 13))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
>type : Symbol(type, Decl(discriminantsAndTypePredicates.ts, 2, 13), Decl(discriminantsAndTypePredicates.ts, 3, 13))
return x; // A
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
}
x; // B
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
if (x.type === 'B') {
>x.type : Symbol(B.type, Decl(discriminantsAndTypePredicates.ts, 3, 13))
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
>type : Symbol(B.type, Decl(discriminantsAndTypePredicates.ts, 3, 13))
return x; // B
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
}
x; // never
>x : Symbol(x, Decl(discriminantsAndTypePredicates.ts, 20, 14))
}

View File

@ -0,0 +1,104 @@
=== tests/cases/compiler/discriminantsAndTypePredicates.ts ===
// Repro from #10145
interface A { type: 'A' }
>A : A
>type : "A"
interface B { type: 'B' }
>B : B
>type : "B"
function isA(x: A | B): x is A { return x.type === 'A'; }
>isA : (x: A | B) => x is A
>x : A | B
>A : A
>B : B
>x : any
>A : A
>x.type === 'A' : boolean
>x.type : "A" | "B"
>x : A | B
>type : "A" | "B"
>'A' : "A"
function isB(x: A | B): x is B { return x.type === 'B'; }
>isB : (x: A | B) => x is B
>x : A | B
>A : A
>B : B
>x : any
>B : B
>x.type === 'B' : boolean
>x.type : "A" | "B"
>x : A | B
>type : "A" | "B"
>'B' : "B"
function foo1(x: A | B): any {
>foo1 : (x: A | B) => any
>x : A | B
>A : A
>B : B
x; // A | B
>x : A | B
if (isA(x)) {
>isA(x) : boolean
>isA : (x: A | B) => x is A
>x : A | B
return x; // A
>x : A
}
x; // B
>x : B
if (isB(x)) {
>isB(x) : boolean
>isB : (x: A | B) => x is B
>x : B
return x; // B
>x : B
}
x; // never
>x : never
}
function foo2(x: A | B): any {
>foo2 : (x: A | B) => any
>x : A | B
>A : A
>B : B
x; // A | B
>x : A | B
if (x.type === 'A') {
>x.type === 'A' : boolean
>x.type : "A" | "B"
>x : A | B
>type : "A" | "B"
>'A' : "A"
return x; // A
>x : A
}
x; // B
>x : B
if (x.type === 'B') {
>x.type === 'B' : boolean
>x.type : "B"
>x : B
>type : "B"
>'B' : "B"
return x; // B
>x : B
}
x; // never
>x : never
}

View File

@ -0,0 +1,13 @@
tests/cases/compiler/emitCapturingThisInTupleDestructuring1.ts(3,17): error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
tests/cases/compiler/emitCapturingThisInTupleDestructuring1.ts(3,29): error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
==== tests/cases/compiler/emitCapturingThisInTupleDestructuring1.ts (2 errors) ====
declare function wrapper(x: any);
wrapper((array: [any]) => {
[this.test, this.test1, this.test2] = array; // even though there is a compiler error, we should still emit lexical capture for "this"
~~~~~~~~~~
!!! error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
~~~~~~~~~~
!!! error TS2493: Tuple type '[any]' with length '1' cannot be assigned to tuple with length '3'.
});

View File

@ -0,0 +1,11 @@
//// [emitCapturingThisInTupleDestructuring1.ts]
declare function wrapper(x: any);
wrapper((array: [any]) => {
[this.test, this.test1, this.test2] = array; // even though there is a compiler error, we should still emit lexical capture for "this"
});
//// [emitCapturingThisInTupleDestructuring1.js]
var _this = this;
wrapper(function (array) {
_this.test = array[0], _this.test1 = array[1], _this.test2 = array[2]; // even though there is a compiler error, we should still emit lexical capture for "this"
});

View File

@ -0,0 +1,16 @@
tests/cases/compiler/emitCapturingThisInTupleDestructuring2.ts(8,39): error TS2493: Tuple type '[number, number]' with length '2' cannot be assigned to tuple with length '3'.
==== tests/cases/compiler/emitCapturingThisInTupleDestructuring2.ts (1 errors) ====
var array1: [number, number] = [1, 2];
class B {
test: number;
test1: any;
test2: any;
method() {
() => [this.test, this.test1, this.test2] = array1; // even though there is a compiler error, we should still emit lexical capture for "this"
~~~~~~~~~~
!!! error TS2493: Tuple type '[number, number]' with length '2' cannot be assigned to tuple with length '3'.
}
}

View File

@ -0,0 +1,23 @@
//// [emitCapturingThisInTupleDestructuring2.ts]
var array1: [number, number] = [1, 2];
class B {
test: number;
test1: any;
test2: any;
method() {
() => [this.test, this.test1, this.test2] = array1; // even though there is a compiler error, we should still emit lexical capture for "this"
}
}
//// [emitCapturingThisInTupleDestructuring2.js]
var array1 = [1, 2];
var B = (function () {
function B() {
}
B.prototype.method = function () {
var _this = this;
(function () { return (_this.test = array1[0], _this.test1 = array1[1], _this.test2 = array1[2], array1); }); // even though there is a compiler error, we should still emit lexical capture for "this"
};
return B;
}());

View File

@ -37,8 +37,8 @@ class D {
}
}
class E extends D {
constructor(...args) {
super(...args);
constructor() {
super(...arguments);
this.z = true;
}
}

View File

@ -0,0 +1,76 @@
//// [tests/cases/compiler/exportDefaultProperty.ts] ////
//// [declarations.d.ts]
// This test is just like exportEqualsProperty, but with `export default`.
declare namespace foo.bar {
export type X = number;
export const X: number;
}
declare module "foobar" {
export default foo.bar;
}
declare module "foobarx" {
export default foo.bar.X;
}
//// [a.ts]
namespace A {
export class B { constructor(b: number) {} }
export namespace B { export const b: number = 0; }
}
export default A.B;
//// [b.ts]
export default "foo".length;
//// [index.ts]
/// <reference path="declarations.d.ts" />
import fooBar from "foobar";
import X = fooBar.X;
import X2 from "foobarx";
const x: X = X;
const x2: X2 = X2;
import B from "./a";
const b: B = new B(B.b);
import fooLength from "./b";
fooLength + 1;
//// [a.js]
"use strict";
var A;
(function (A) {
var B = (function () {
function B(b) {
}
return B;
}());
A.B = B;
var B;
(function (B) {
B.b = 0;
})(B = A.B || (A.B = {}));
})(A || (A = {}));
exports.__esModule = true;
exports["default"] = A.B;
//// [b.js]
"use strict";
exports.__esModule = true;
exports["default"] = "foo".length;
//// [index.js]
"use strict";
/// <reference path="declarations.d.ts" />
var foobar_1 = require("foobar");
var X = foobar_1["default"].X;
var foobarx_1 = require("foobarx");
var x = X;
var x2 = foobarx_1["default"];
var a_1 = require("./a");
var b = new a_1["default"](a_1["default"].b);
var b_1 = require("./b");
b_1["default"] + 1;

View File

@ -0,0 +1,92 @@
=== tests/cases/compiler/index.ts ===
/// <reference path="declarations.d.ts" />
import fooBar from "foobar";
>fooBar : Symbol(fooBar, Decl(index.ts, 1, 6))
import X = fooBar.X;
>X : Symbol(X, Decl(index.ts, 1, 28))
>fooBar : Symbol(fooBar, Decl(index.ts, 1, 6))
>X : Symbol(fooBar.X, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
import X2 from "foobarx";
>X2 : Symbol(X2, Decl(index.ts, 3, 6))
const x: X = X;
>x : Symbol(x, Decl(index.ts, 4, 5))
>X : Symbol(X, Decl(index.ts, 1, 28))
>X : Symbol(X, Decl(index.ts, 1, 28))
const x2: X2 = X2;
>x2 : Symbol(x2, Decl(index.ts, 5, 5))
>X2 : Symbol(X2, Decl(index.ts, 3, 6))
>X2 : Symbol(X2, Decl(index.ts, 3, 6))
import B from "./a";
>B : Symbol(B, Decl(index.ts, 7, 6))
const b: B = new B(B.b);
>b : Symbol(b, Decl(index.ts, 8, 5))
>B : Symbol(B, Decl(index.ts, 7, 6))
>B : Symbol(B, Decl(index.ts, 7, 6))
>B.b : Symbol(B.b, Decl(a.ts, 2, 37))
>B : Symbol(B, Decl(index.ts, 7, 6))
>b : Symbol(B.b, Decl(a.ts, 2, 37))
import fooLength from "./b";
>fooLength : Symbol(fooLength, Decl(index.ts, 10, 6))
fooLength + 1;
>fooLength : Symbol(fooLength, Decl(index.ts, 10, 6))
=== tests/cases/compiler/declarations.d.ts ===
// This test is just like exportEqualsProperty, but with `export default`.
declare namespace foo.bar {
>foo : Symbol(foo, Decl(declarations.d.ts, 0, 0))
>bar : Symbol(bar, Decl(declarations.d.ts, 2, 22))
export type X = number;
>X : Symbol(X, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
export const X: number;
>X : Symbol(X, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
}
declare module "foobar" {
export default foo.bar;
>foo.bar : Symbol(default, Decl(declarations.d.ts, 2, 22))
>foo : Symbol(foo, Decl(declarations.d.ts, 0, 0))
>bar : Symbol(default, Decl(declarations.d.ts, 2, 22))
}
declare module "foobarx" {
export default foo.bar.X;
>foo.bar.X : Symbol(default, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
>foo.bar : Symbol(foo.bar, Decl(declarations.d.ts, 2, 22))
>foo : Symbol(foo, Decl(declarations.d.ts, 0, 0))
>bar : Symbol(foo.bar, Decl(declarations.d.ts, 2, 22))
>X : Symbol(default, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
}
=== tests/cases/compiler/a.ts ===
namespace A {
>A : Symbol(A, Decl(a.ts, 0, 0))
export class B { constructor(b: number) {} }
>B : Symbol(B, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
>b : Symbol(b, Decl(a.ts, 1, 33))
export namespace B { export const b: number = 0; }
>B : Symbol(B, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
>b : Symbol(b, Decl(a.ts, 2, 37))
}
export default A.B;
>A.B : Symbol(default, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
>A : Symbol(A, Decl(a.ts, 0, 0))
>B : Symbol(default, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
=== tests/cases/compiler/b.ts ===
export default "foo".length;
>"foo".length : Symbol(String.length, Decl(lib.d.ts, --, --))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))

View File

@ -0,0 +1,97 @@
=== tests/cases/compiler/index.ts ===
/// <reference path="declarations.d.ts" />
import fooBar from "foobar";
>fooBar : typeof fooBar
import X = fooBar.X;
>X : number
>fooBar : typeof fooBar
>X : number
import X2 from "foobarx";
>X2 : number
const x: X = X;
>x : number
>X : number
>X : number
const x2: X2 = X2;
>x2 : number
>X2 : number
>X2 : number
import B from "./a";
>B : typeof B
const b: B = new B(B.b);
>b : B
>B : B
>new B(B.b) : B
>B : typeof B
>B.b : number
>B : typeof B
>b : number
import fooLength from "./b";
>fooLength : number
fooLength + 1;
>fooLength + 1 : number
>fooLength : number
>1 : number
=== tests/cases/compiler/declarations.d.ts ===
// This test is just like exportEqualsProperty, but with `export default`.
declare namespace foo.bar {
>foo : typeof foo
>bar : typeof bar
export type X = number;
>X : number
export const X: number;
>X : number
}
declare module "foobar" {
export default foo.bar;
>foo.bar : typeof default
>foo : typeof foo
>bar : typeof default
}
declare module "foobarx" {
export default foo.bar.X;
>foo.bar.X : number
>foo.bar : typeof foo.bar
>foo : typeof foo
>bar : typeof foo.bar
>X : number
}
=== tests/cases/compiler/a.ts ===
namespace A {
>A : typeof A
export class B { constructor(b: number) {} }
>B : B
>b : number
export namespace B { export const b: number = 0; }
>B : typeof B
>b : number
>0 : number
}
export default A.B;
>A.B : typeof default
>A : typeof A
>B : typeof default
=== tests/cases/compiler/b.ts ===
export default "foo".length;
>"foo".length : number
>"foo" : string
>length : number

View File

@ -0,0 +1,72 @@
//// [tests/cases/compiler/exportEqualsProperty.ts] ////
//// [declarations.d.ts]
// This test is just like exportDefaultProperty, but with `export =`.
declare namespace foo.bar {
export type X = number;
export const X: number;
}
declare module "foobar" {
export = foo.bar;
}
declare module "foobarx" {
export = foo.bar.X;
}
//// [a.ts]
namespace A {
export class B { constructor(b: number) {} }
export namespace B { export const b: number = 0; }
}
export = A.B;
//// [b.ts]
export = "foo".length;
//// [index.ts]
/// <reference path="declarations.d.ts" />
import { X } from "foobar";
import X2 = require("foobarx");
const x: X = X;
const x2: X2 = X2;
import B = require("./a");
const b: B = new B(B.b);
import fooLength = require("./b");
fooLength + 1;
//// [a.js]
"use strict";
var A;
(function (A) {
var B = (function () {
function B(b) {
}
return B;
}());
A.B = B;
var B;
(function (B) {
B.b = 0;
})(B = A.B || (A.B = {}));
})(A || (A = {}));
module.exports = A.B;
//// [b.js]
"use strict";
module.exports = "foo".length;
//// [index.js]
"use strict";
/// <reference path="declarations.d.ts" />
var foobar_1 = require("foobar");
var X2 = require("foobarx");
var x = foobar_1.X;
var x2 = X2;
var B = require("./a");
var b = new B(B.b);
var fooLength = require("./b");
fooLength + 1;

View File

@ -0,0 +1,87 @@
=== tests/cases/compiler/index.ts ===
/// <reference path="declarations.d.ts" />
import { X } from "foobar";
>X : Symbol(X, Decl(index.ts, 1, 8))
import X2 = require("foobarx");
>X2 : Symbol(X2, Decl(index.ts, 1, 27))
const x: X = X;
>x : Symbol(x, Decl(index.ts, 3, 5))
>X : Symbol(X, Decl(index.ts, 1, 8))
>X : Symbol(X, Decl(index.ts, 1, 8))
const x2: X2 = X2;
>x2 : Symbol(x2, Decl(index.ts, 4, 5))
>X2 : Symbol(X2, Decl(index.ts, 1, 27))
>X2 : Symbol(X2, Decl(index.ts, 1, 27))
import B = require("./a");
>B : Symbol(B, Decl(index.ts, 4, 18))
const b: B = new B(B.b);
>b : Symbol(b, Decl(index.ts, 7, 5))
>B : Symbol(B, Decl(index.ts, 4, 18))
>B : Symbol(B, Decl(index.ts, 4, 18))
>B.b : Symbol(B.b, Decl(a.ts, 2, 37))
>B : Symbol(B, Decl(index.ts, 4, 18))
>b : Symbol(B.b, Decl(a.ts, 2, 37))
import fooLength = require("./b");
>fooLength : Symbol(fooLength, Decl(index.ts, 7, 24))
fooLength + 1;
>fooLength : Symbol(fooLength, Decl(index.ts, 7, 24))
=== tests/cases/compiler/declarations.d.ts ===
// This test is just like exportDefaultProperty, but with `export =`.
declare namespace foo.bar {
>foo : Symbol(foo, Decl(declarations.d.ts, 0, 0))
>bar : Symbol(bar, Decl(declarations.d.ts, 2, 22))
export type X = number;
>X : Symbol(X, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
export const X: number;
>X : Symbol(X, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
}
declare module "foobar" {
export = foo.bar;
>foo.bar : Symbol(foo.bar, Decl(declarations.d.ts, 2, 22))
>foo : Symbol(foo, Decl(declarations.d.ts, 0, 0))
>bar : Symbol(foo.bar, Decl(declarations.d.ts, 2, 22))
}
declare module "foobarx" {
export = foo.bar.X;
>foo.bar.X : Symbol(foo.bar.X, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
>foo.bar : Symbol(foo.bar, Decl(declarations.d.ts, 2, 22))
>foo : Symbol(foo, Decl(declarations.d.ts, 0, 0))
>bar : Symbol(foo.bar, Decl(declarations.d.ts, 2, 22))
>X : Symbol(foo.bar.X, Decl(declarations.d.ts, 2, 27), Decl(declarations.d.ts, 4, 16))
}
=== tests/cases/compiler/a.ts ===
namespace A {
>A : Symbol(A, Decl(a.ts, 0, 0))
export class B { constructor(b: number) {} }
>B : Symbol(B, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
>b : Symbol(b, Decl(a.ts, 1, 33))
export namespace B { export const b: number = 0; }
>B : Symbol(B, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
>b : Symbol(b, Decl(a.ts, 2, 37))
}
export = A.B;
>A.B : Symbol(A.B, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
>A : Symbol(A, Decl(a.ts, 0, 0))
>B : Symbol(A.B, Decl(a.ts, 0, 13), Decl(a.ts, 1, 48))
=== tests/cases/compiler/b.ts ===
export = "foo".length;
>"foo".length : Symbol(String.length, Decl(lib.d.ts, --, --))
>length : Symbol(String.length, Decl(lib.d.ts, --, --))

View File

@ -0,0 +1,92 @@
=== tests/cases/compiler/index.ts ===
/// <reference path="declarations.d.ts" />
import { X } from "foobar";
>X : number
import X2 = require("foobarx");
>X2 : number
const x: X = X;
>x : number
>X : number
>X : number
const x2: X2 = X2;
>x2 : number
>X2 : number
>X2 : number
import B = require("./a");
>B : typeof B
const b: B = new B(B.b);
>b : B
>B : B
>new B(B.b) : B
>B : typeof B
>B.b : number
>B : typeof B
>b : number
import fooLength = require("./b");
>fooLength : number
fooLength + 1;
>fooLength + 1 : number
>fooLength : number
>1 : number
=== tests/cases/compiler/declarations.d.ts ===
// This test is just like exportDefaultProperty, but with `export =`.
declare namespace foo.bar {
>foo : typeof foo
>bar : typeof bar
export type X = number;
>X : number
export const X: number;
>X : number
}
declare module "foobar" {
export = foo.bar;
>foo.bar : typeof foo.bar
>foo : typeof foo
>bar : typeof foo.bar
}
declare module "foobarx" {
export = foo.bar.X;
>foo.bar.X : number
>foo.bar : typeof foo.bar
>foo : typeof foo
>bar : typeof foo.bar
>X : number
}
=== tests/cases/compiler/a.ts ===
namespace A {
>A : typeof A
export class B { constructor(b: number) {} }
>B : B
>b : number
export namespace B { export const b: number = 0; }
>B : typeof B
>b : number
>0 : number
}
export = A.B;
>A.B : typeof A.B
>A : typeof A
>B : typeof A.B
=== tests/cases/compiler/b.ts ===
export = "foo".length;
>"foo".length : number
>"foo" : string
>length : number

View File

@ -0,0 +1,9 @@
//// [exportToString.ts]
const toString = 0;
export { toString };
//// [exportToString.js]
"use strict";
var toString = 0;
exports.toString = toString;

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/exportToString.ts ===
const toString = 0;
>toString : Symbol(toString, Decl(exportToString.ts, 0, 5))
export { toString };
>toString : Symbol(toString, Decl(exportToString.ts, 1, 8))

View File

@ -0,0 +1,8 @@
=== tests/cases/compiler/exportToString.ts ===
const toString = 0;
>toString : number
>0 : number
export { toString };
>toString : number

View File

@ -0,0 +1,65 @@
tests/cases/compiler/implicitConstParameters.ts(39,27): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/implicitConstParameters.ts(45,27): error TS2532: Object is possibly 'undefined'.
==== tests/cases/compiler/implicitConstParameters.ts (2 errors) ====
function doSomething(cb: () => void) {
cb();
}
function fn(x: number | string) {
if (typeof x === 'number') {
doSomething(() => x.toFixed());
}
}
function f1(x: string | undefined) {
if (!x) {
return;
}
doSomething(() => x.length);
}
function f2(x: string | undefined) {
if (x) {
doSomething(() => {
doSomething(() => x.length);
});
}
}
function f3(x: string | undefined) {
inner();
function inner() {
if (x) {
doSomething(() => x.length);
}
}
}
function f4(x: string | undefined) {
x = "abc"; // causes x to be considered non-const
if (x) {
doSomething(() => x.length);
~
!!! error TS2532: Object is possibly 'undefined'.
}
}
function f5(x: string | undefined) {
if (x) {
doSomething(() => x.length);
~
!!! error TS2532: Object is possibly 'undefined'.
}
x = "abc"; // causes x to be considered non-const
}
function f6(x: string | undefined) {
const y = x || "";
if (x) {
doSomething(() => y.length);
}
}

View File

@ -0,0 +1,106 @@
//// [implicitConstParameters.ts]
function doSomething(cb: () => void) {
cb();
}
function fn(x: number | string) {
if (typeof x === 'number') {
doSomething(() => x.toFixed());
}
}
function f1(x: string | undefined) {
if (!x) {
return;
}
doSomething(() => x.length);
}
function f2(x: string | undefined) {
if (x) {
doSomething(() => {
doSomething(() => x.length);
});
}
}
function f3(x: string | undefined) {
inner();
function inner() {
if (x) {
doSomething(() => x.length);
}
}
}
function f4(x: string | undefined) {
x = "abc"; // causes x to be considered non-const
if (x) {
doSomething(() => x.length);
}
}
function f5(x: string | undefined) {
if (x) {
doSomething(() => x.length);
}
x = "abc"; // causes x to be considered non-const
}
function f6(x: string | undefined) {
const y = x || "";
if (x) {
doSomething(() => y.length);
}
}
//// [implicitConstParameters.js]
function doSomething(cb) {
cb();
}
function fn(x) {
if (typeof x === 'number') {
doSomething(function () { return x.toFixed(); });
}
}
function f1(x) {
if (!x) {
return;
}
doSomething(function () { return x.length; });
}
function f2(x) {
if (x) {
doSomething(function () {
doSomething(function () { return x.length; });
});
}
}
function f3(x) {
inner();
function inner() {
if (x) {
doSomething(function () { return x.length; });
}
}
}
function f4(x) {
x = "abc"; // causes x to be considered non-const
if (x) {
doSomething(function () { return x.length; });
}
}
function f5(x) {
if (x) {
doSomething(function () { return x.length; });
}
x = "abc"; // causes x to be considered non-const
}
function f6(x) {
var y = x || "";
if (x) {
doSomething(function () { return y.length; });
}
}

View File

@ -0,0 +1,22 @@
//// [indexWithUndefinedAndNull.ts]
interface N {
[n: number]: string;
}
interface S {
[s: string]: number;
}
let n: N;
let s: S;
let str: string = n[undefined];
str = n[null];
let num: number = s[undefined];
num = s[null];
//// [indexWithUndefinedAndNull.js]
var n;
var s;
var str = n[undefined];
str = n[null];
var num = s[undefined];
num = s[null];

View File

@ -0,0 +1,39 @@
=== tests/cases/compiler/indexWithUndefinedAndNull.ts ===
interface N {
>N : Symbol(N, Decl(indexWithUndefinedAndNull.ts, 0, 0))
[n: number]: string;
>n : Symbol(n, Decl(indexWithUndefinedAndNull.ts, 1, 5))
}
interface S {
>S : Symbol(S, Decl(indexWithUndefinedAndNull.ts, 2, 1))
[s: string]: number;
>s : Symbol(s, Decl(indexWithUndefinedAndNull.ts, 4, 5))
}
let n: N;
>n : Symbol(n, Decl(indexWithUndefinedAndNull.ts, 6, 3))
>N : Symbol(N, Decl(indexWithUndefinedAndNull.ts, 0, 0))
let s: S;
>s : Symbol(s, Decl(indexWithUndefinedAndNull.ts, 7, 3))
>S : Symbol(S, Decl(indexWithUndefinedAndNull.ts, 2, 1))
let str: string = n[undefined];
>str : Symbol(str, Decl(indexWithUndefinedAndNull.ts, 8, 3))
>n : Symbol(n, Decl(indexWithUndefinedAndNull.ts, 6, 3))
>undefined : Symbol(undefined)
str = n[null];
>str : Symbol(str, Decl(indexWithUndefinedAndNull.ts, 8, 3))
>n : Symbol(n, Decl(indexWithUndefinedAndNull.ts, 6, 3))
let num: number = s[undefined];
>num : Symbol(num, Decl(indexWithUndefinedAndNull.ts, 10, 3))
>s : Symbol(s, Decl(indexWithUndefinedAndNull.ts, 7, 3))
>undefined : Symbol(undefined)
num = s[null];
>num : Symbol(num, Decl(indexWithUndefinedAndNull.ts, 10, 3))
>s : Symbol(s, Decl(indexWithUndefinedAndNull.ts, 7, 3))

View File

@ -0,0 +1,47 @@
=== tests/cases/compiler/indexWithUndefinedAndNull.ts ===
interface N {
>N : N
[n: number]: string;
>n : number
}
interface S {
>S : S
[s: string]: number;
>s : string
}
let n: N;
>n : N
>N : N
let s: S;
>s : S
>S : S
let str: string = n[undefined];
>str : string
>n[undefined] : string
>n : N
>undefined : undefined
str = n[null];
>str = n[null] : string
>str : string
>n[null] : string
>n : N
>null : null
let num: number = s[undefined];
>num : number
>s[undefined] : number
>s : S
>undefined : undefined
num = s[null];
>num = s[null] : number
>num : number
>s[null] : number
>s : S
>null : null

View File

@ -0,0 +1,40 @@
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(9,19): error TS2454: Variable 'n' is used before being assigned.
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(9,19): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(10,7): error TS2454: Variable 'n' is used before being assigned.
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(10,7): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(11,19): error TS2454: Variable 's' is used before being assigned.
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(11,19): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(12,7): error TS2454: Variable 's' is used before being assigned.
tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts(12,7): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.
==== tests/cases/compiler/indexWithUndefinedAndNullStrictNullChecks.ts (8 errors) ====
interface N {
[n: number]: string;
}
interface S {
[s: string]: number;
}
let n: N;
let s: S;
let str: string = n[undefined];
~
!!! error TS2454: Variable 'n' is used before being assigned.
~~~~~~~~~~~~
!!! error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.
str = n[null];
~
!!! error TS2454: Variable 'n' is used before being assigned.
~~~~~~~
!!! error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.
let num: number = s[undefined];
~
!!! error TS2454: Variable 's' is used before being assigned.
~~~~~~~~~~~~
!!! error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.
num = s[null];
~
!!! error TS2454: Variable 's' is used before being assigned.
~~~~~~~
!!! error TS2342: An index expression argument must be of type 'string', 'number', 'symbol', or 'any'.

View File

@ -0,0 +1,22 @@
//// [indexWithUndefinedAndNullStrictNullChecks.ts]
interface N {
[n: number]: string;
}
interface S {
[s: string]: number;
}
let n: N;
let s: S;
let str: string = n[undefined];
str = n[null];
let num: number = s[undefined];
num = s[null];
//// [indexWithUndefinedAndNullStrictNullChecks.js]
var n;
var s;
var str = n[undefined];
str = n[null];
var num = s[undefined];
num = s[null];

View File

@ -0,0 +1,172 @@
//// [instanceofWithStructurallyIdenticalTypes.ts]
// Repro from #7271
class C1 { item: string }
class C2 { item: string[] }
class C3 { item: string }
function foo1(x: C1 | C2 | C3): string {
if (x instanceof C1) {
return x.item;
}
else if (x instanceof C2) {
return x.item[0];
}
else if (x instanceof C3) {
return x.item;
}
return "error";
}
function isC1(c: C1 | C2 | C3): c is C1 { return c instanceof C1 }
function isC2(c: C1 | C2 | C3): c is C2 { return c instanceof C2 }
function isC3(c: C1 | C2 | C3): c is C3 { return c instanceof C3 }
function foo2(x: C1 | C2 | C3): string {
if (isC1(x)) {
return x.item;
}
else if (isC2(x)) {
return x.item[0];
}
else if (isC3(x)) {
return x.item;
}
return "error";
}
// More tests
class A { a: string }
class A1 extends A { }
class A2 { a: string }
class B extends A { b: string }
function goo(x: A) {
if (x instanceof A) {
x; // A
}
else {
x; // never
}
if (x instanceof A1) {
x; // A1
}
else {
x; // A
}
if (x instanceof A2) {
x; // A2
}
else {
x; // A
}
if (x instanceof B) {
x; // B
}
else {
x; // A
}
}
//// [instanceofWithStructurallyIdenticalTypes.js]
// Repro from #7271
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var C1 = (function () {
function C1() {
}
return C1;
}());
var C2 = (function () {
function C2() {
}
return C2;
}());
var C3 = (function () {
function C3() {
}
return C3;
}());
function foo1(x) {
if (x instanceof C1) {
return x.item;
}
else if (x instanceof C2) {
return x.item[0];
}
else if (x instanceof C3) {
return x.item;
}
return "error";
}
function isC1(c) { return c instanceof C1; }
function isC2(c) { return c instanceof C2; }
function isC3(c) { return c instanceof C3; }
function foo2(x) {
if (isC1(x)) {
return x.item;
}
else if (isC2(x)) {
return x.item[0];
}
else if (isC3(x)) {
return x.item;
}
return "error";
}
// More tests
var A = (function () {
function A() {
}
return A;
}());
var A1 = (function (_super) {
__extends(A1, _super);
function A1() {
_super.apply(this, arguments);
}
return A1;
}(A));
var A2 = (function () {
function A2() {
}
return A2;
}());
var B = (function (_super) {
__extends(B, _super);
function B() {
_super.apply(this, arguments);
}
return B;
}(A));
function goo(x) {
if (x instanceof A) {
x; // A
}
else {
x; // never
}
if (x instanceof A1) {
x; // A1
}
else {
x; // A
}
if (x instanceof A2) {
x; // A2
}
else {
x; // A
}
if (x instanceof B) {
x; // B
}
else {
x; // A
}
}

View File

@ -0,0 +1,192 @@
=== tests/cases/compiler/instanceofWithStructurallyIdenticalTypes.ts ===
// Repro from #7271
class C1 { item: string }
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
>item : Symbol(C1.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10))
class C2 { item: string[] }
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
>item : Symbol(C2.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 10))
class C3 { item: string }
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
>item : Symbol(C3.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 10))
function foo1(x: C1 | C2 | C3): string {
>foo1 : Symbol(foo1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 25))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 6, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
if (x instanceof C1) {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 6, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
return x.item;
>x.item : Symbol(C1.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 6, 14))
>item : Symbol(C1.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10))
}
else if (x instanceof C2) {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 6, 14))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
return x.item[0];
>x.item : Symbol(C2.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 10))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 6, 14))
>item : Symbol(C2.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 10))
}
else if (x instanceof C3) {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 6, 14))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
return x.item;
>x.item : Symbol(C3.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 10))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 6, 14))
>item : Symbol(C3.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 10))
}
return "error";
}
function isC1(c: C1 | C2 | C3): c is C1 { return c instanceof C1 }
>isC1 : Symbol(isC1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 17, 1))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 19, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 19, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 19, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
function isC2(c: C1 | C2 | C3): c is C2 { return c instanceof C2 }
>isC2 : Symbol(isC2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 19, 66))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 20, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 20, 14))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 20, 14))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
function isC3(c: C1 | C2 | C3): c is C3 { return c instanceof C3 }
>isC3 : Symbol(isC3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 20, 66))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 21, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 21, 14))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
>c : Symbol(c, Decl(instanceofWithStructurallyIdenticalTypes.ts, 21, 14))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
function foo2(x: C1 | C2 | C3): string {
>foo2 : Symbol(foo2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 21, 66))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
>C1 : Symbol(C1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 0, 0))
>C2 : Symbol(C2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 25))
>C3 : Symbol(C3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 27))
if (isC1(x)) {
>isC1 : Symbol(isC1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 17, 1))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
return x.item;
>x.item : Symbol(C1.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
>item : Symbol(C1.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 2, 10))
}
else if (isC2(x)) {
>isC2 : Symbol(isC2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 19, 66))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
return x.item[0];
>x.item : Symbol(C2.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 10))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
>item : Symbol(C2.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 3, 10))
}
else if (isC3(x)) {
>isC3 : Symbol(isC3, Decl(instanceofWithStructurallyIdenticalTypes.ts, 20, 66))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
return x.item;
>x.item : Symbol(C3.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 10))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 23, 14))
>item : Symbol(C3.item, Decl(instanceofWithStructurallyIdenticalTypes.ts, 4, 10))
}
return "error";
}
// More tests
class A { a: string }
>A : Symbol(A, Decl(instanceofWithStructurallyIdenticalTypes.ts, 34, 1))
>a : Symbol(A.a, Decl(instanceofWithStructurallyIdenticalTypes.ts, 38, 9))
class A1 extends A { }
>A1 : Symbol(A1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 38, 21))
>A : Symbol(A, Decl(instanceofWithStructurallyIdenticalTypes.ts, 34, 1))
class A2 { a: string }
>A2 : Symbol(A2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 39, 22))
>a : Symbol(A2.a, Decl(instanceofWithStructurallyIdenticalTypes.ts, 40, 10))
class B extends A { b: string }
>B : Symbol(B, Decl(instanceofWithStructurallyIdenticalTypes.ts, 40, 22))
>A : Symbol(A, Decl(instanceofWithStructurallyIdenticalTypes.ts, 34, 1))
>b : Symbol(B.b, Decl(instanceofWithStructurallyIdenticalTypes.ts, 41, 19))
function goo(x: A) {
>goo : Symbol(goo, Decl(instanceofWithStructurallyIdenticalTypes.ts, 41, 31))
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
>A : Symbol(A, Decl(instanceofWithStructurallyIdenticalTypes.ts, 34, 1))
if (x instanceof A) {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
>A : Symbol(A, Decl(instanceofWithStructurallyIdenticalTypes.ts, 34, 1))
x; // A
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
else {
x; // never
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
if (x instanceof A1) {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
>A1 : Symbol(A1, Decl(instanceofWithStructurallyIdenticalTypes.ts, 38, 21))
x; // A1
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
else {
x; // A
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
if (x instanceof A2) {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
>A2 : Symbol(A2, Decl(instanceofWithStructurallyIdenticalTypes.ts, 39, 22))
x; // A2
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
else {
x; // A
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
if (x instanceof B) {
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
>B : Symbol(B, Decl(instanceofWithStructurallyIdenticalTypes.ts, 40, 22))
x; // B
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
else {
x; // A
>x : Symbol(x, Decl(instanceofWithStructurallyIdenticalTypes.ts, 43, 13))
}
}

View File

@ -0,0 +1,211 @@
=== tests/cases/compiler/instanceofWithStructurallyIdenticalTypes.ts ===
// Repro from #7271
class C1 { item: string }
>C1 : C1
>item : string
class C2 { item: string[] }
>C2 : C2
>item : string[]
class C3 { item: string }
>C3 : C3
>item : string
function foo1(x: C1 | C2 | C3): string {
>foo1 : (x: C1 | C2 | C3) => string
>x : C1 | C2 | C3
>C1 : C1
>C2 : C2
>C3 : C3
if (x instanceof C1) {
>x instanceof C1 : boolean
>x : C1 | C2 | C3
>C1 : typeof C1
return x.item;
>x.item : string
>x : C1
>item : string
}
else if (x instanceof C2) {
>x instanceof C2 : boolean
>x : C2 | C3
>C2 : typeof C2
return x.item[0];
>x.item[0] : string
>x.item : string[]
>x : C2
>item : string[]
>0 : number
}
else if (x instanceof C3) {
>x instanceof C3 : boolean
>x : C3
>C3 : typeof C3
return x.item;
>x.item : string
>x : C3
>item : string
}
return "error";
>"error" : string
}
function isC1(c: C1 | C2 | C3): c is C1 { return c instanceof C1 }
>isC1 : (c: C1 | C2 | C3) => c is C1
>c : C1 | C2 | C3
>C1 : C1
>C2 : C2
>C3 : C3
>c : any
>C1 : C1
>c instanceof C1 : boolean
>c : C1 | C2 | C3
>C1 : typeof C1
function isC2(c: C1 | C2 | C3): c is C2 { return c instanceof C2 }
>isC2 : (c: C1 | C2 | C3) => c is C2
>c : C1 | C2 | C3
>C1 : C1
>C2 : C2
>C3 : C3
>c : any
>C2 : C2
>c instanceof C2 : boolean
>c : C1 | C2 | C3
>C2 : typeof C2
function isC3(c: C1 | C2 | C3): c is C3 { return c instanceof C3 }
>isC3 : (c: C1 | C2 | C3) => c is C3
>c : C1 | C2 | C3
>C1 : C1
>C2 : C2
>C3 : C3
>c : any
>C3 : C3
>c instanceof C3 : boolean
>c : C1 | C2 | C3
>C3 : typeof C3
function foo2(x: C1 | C2 | C3): string {
>foo2 : (x: C1 | C2 | C3) => string
>x : C1 | C2 | C3
>C1 : C1
>C2 : C2
>C3 : C3
if (isC1(x)) {
>isC1(x) : boolean
>isC1 : (c: C1 | C2 | C3) => c is C1
>x : C1 | C2 | C3
return x.item;
>x.item : string
>x : C1
>item : string
}
else if (isC2(x)) {
>isC2(x) : boolean
>isC2 : (c: C1 | C2 | C3) => c is C2
>x : C2 | C3
return x.item[0];
>x.item[0] : string
>x.item : string[]
>x : C2
>item : string[]
>0 : number
}
else if (isC3(x)) {
>isC3(x) : boolean
>isC3 : (c: C1 | C2 | C3) => c is C3
>x : C3
return x.item;
>x.item : string
>x : C3
>item : string
}
return "error";
>"error" : string
}
// More tests
class A { a: string }
>A : A
>a : string
class A1 extends A { }
>A1 : A1
>A : A
class A2 { a: string }
>A2 : A2
>a : string
class B extends A { b: string }
>B : B
>A : A
>b : string
function goo(x: A) {
>goo : (x: A) => void
>x : A
>A : A
if (x instanceof A) {
>x instanceof A : boolean
>x : A
>A : typeof A
x; // A
>x : A
}
else {
x; // never
>x : never
}
if (x instanceof A1) {
>x instanceof A1 : boolean
>x : A
>A1 : typeof A1
x; // A1
>x : A1
}
else {
x; // A
>x : A
}
if (x instanceof A2) {
>x instanceof A2 : boolean
>x : A
>A2 : typeof A2
x; // A2
>x : A2
}
else {
x; // A
>x : A
}
if (x instanceof B) {
>x instanceof B : boolean
>x : A
>B : typeof B
x; // B
>x : B
}
else {
x; // A
>x : A
}
}

View File

@ -1,5 +1,5 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory '/a/types'. ========",
"======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/a/types'. ========",
"Resolving with primary search path '/a/types'",
"File '/a/types/jquery/package.json' does not exist.",
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",

View File

@ -1,5 +1,5 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory '/a/types'. ========",
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
"Resolving with primary search path '/a/types'",
"File '/a/types/jquery/package.json' does not exist.",
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",

View File

@ -1,5 +1,5 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/b/__inferred type names__.ts', root directory 'types'. ========",
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory 'types'. ========",
"Resolving with primary search path 'types'",
"File 'types/jquery/package.json' does not exist.",
"File 'types/jquery/index.d.ts' exist - use it as a name resolution result.",

View File

@ -5,7 +5,7 @@
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
"File '/types/jquery/jquery.d.ts' exist - use it as a name resolution result.",
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/types'. ========",
"======== Resolving type reference directive 'jquery', containing file 'test/__inferred type names__.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'",
"Found 'package.json' at '/types/jquery/package.json'.",
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",

Some files were not shown because too many files have changed in this diff Show More