Skip to content

Commit af4580b

Browse files
committed
Merge pull request microsoft#7675 from Microsoft/transforms-fixTypeErrors
Transforms fix type errors
2 parents 63cf58b + b60cf99 commit af4580b

24 files changed

Lines changed: 580 additions & 193 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tests/baselines/rwc/*
1313
tests/baselines/test262/*
1414
tests/baselines/reference/projectOutput/*
1515
tests/baselines/local/projectOutput/*
16+
tests/baselines/reference/testresults.tap
1617
tests/services/baselines/prototyping/local/*
1718
tests/services/browser/typescriptServices.js
1819
scripts/configureNightly.js

Jakefile.js

Lines changed: 214 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var os = require("os");
55
var path = require("path");
66
var child_process = require("child_process");
77
var Linter = require("tslint");
8+
var readline = require("readline");
89

910
// Variables
1011
var compilerDirectory = "src/compiler/";
@@ -673,9 +674,14 @@ function cleanTestDirs() {
673674
}
674675

675676
// used to pass data from jake command line directly to run.js
676-
function writeTestConfigFile(tests, light, testConfigFile) {
677+
function writeTestConfigFile(testConfigFile, tests, light, stackTraceLimit) {
677678
console.log('Running test(s): ' + tests);
678-
var testConfigContents = JSON.stringify({ test: [tests], light: light });
679+
var testConfig = { test: [tests], light: light };
680+
if (/^(\d+|full)$/.test(stackTraceLimit)) {
681+
testConfig.stackTraceLimit = stackTraceLimit;
682+
}
683+
684+
var testConfigContents = JSON.stringify(testConfig);
679685
fs.writeFileSync('test.config', testConfigContents);
680686
}
681687

@@ -685,6 +691,124 @@ function deleteTemporaryProjectOutput() {
685691
}
686692
}
687693

694+
function runTestsAndWriteOutput(file) {
695+
cleanTestDirs();
696+
var tests = process.env.test || process.env.tests || process.env.t;
697+
var light = process.env.light || false;
698+
var testConfigFile = 'test.config';
699+
if (fs.existsSync(testConfigFile)) {
700+
fs.unlinkSync(testConfigFile);
701+
}
702+
703+
if (tests || light) {
704+
writeTestConfigFile(testConfigFile, tests, light, 10);
705+
}
706+
707+
if (tests && tests.toLocaleLowerCase() === "rwc") {
708+
testTimeout = 100000;
709+
}
710+
711+
var args = [];
712+
args.push("-R", "TAP");
713+
args.push("--no-colors");
714+
args.push("-t", testTimeout);
715+
if (tests) {
716+
args.push("-g", '"' + tests + '"');
717+
}
718+
args.push(run);
719+
720+
var cmd = "mocha " + args.join(" ");
721+
console.log(cmd);
722+
var ex = jake.createExec([cmd], { windowsVerbatimArguments: true });
723+
var out = fs.createWriteStream(file);
724+
var tapRange = /^(\d+)\.\.(\d+)(?:$|\r\n?|\n)/;
725+
var tapOk = /^ok\s/;
726+
var tapNotOk = /^not\sok/;
727+
var tapComment = /^#/;
728+
var typeError = /^\s+TypeError:/;
729+
var progress = new ProgressBar("Running tests...");
730+
var expectedTestCount = 0;
731+
var testCount = 0;
732+
var failureCount = 0;
733+
var successCount = 0;
734+
var comments = [];
735+
var typeErrorCount = 0;
736+
737+
ex.addListener("stdout", function (output) {
738+
var m = tapRange.exec(output);
739+
if (m) {
740+
expectedTestCount = parseInt(m[2]);
741+
return;
742+
}
743+
744+
out.write(output);
745+
746+
if (tapOk.test(output)) {
747+
successCount++;
748+
}
749+
else if (tapNotOk.test(output)) {
750+
failureCount++;
751+
}
752+
else {
753+
if (tapComment.test(output)) {
754+
comments.push(output.toString().trim());
755+
}
756+
else if (typeError.test(output)) {
757+
typeErrorCount++;
758+
}
759+
return;
760+
}
761+
762+
testCount++;
763+
764+
var percentComplete = testCount * 100 / expectedTestCount;
765+
updateProgress(percentComplete);
766+
});
767+
768+
function updateProgress(percentComplete) {
769+
progress.update(percentComplete,
770+
/*foregroundColor*/ failureCount > 0
771+
? "red"
772+
: successCount === expectedTestCount
773+
? "green"
774+
: "cyan",
775+
/*backgroundColor*/ "gray"
776+
);
777+
}
778+
779+
ex.addListener("stderr", function (output) {
780+
progress.hide();
781+
process.stderr.write(output.toString().trim() + os.EOL);
782+
progress.show();
783+
});
784+
ex.addListener("cmdEnd", function () {
785+
if (progress.visible) {
786+
updateProgress(100);
787+
process.stdout.write("done." + os.EOL);
788+
}
789+
790+
console.log(comments.join(os.EOL));
791+
deleteTemporaryProjectOutput();
792+
complete();
793+
});
794+
ex.addListener("error", function (e, status) {
795+
if (progress.visible) {
796+
updateProgress(100);
797+
process.stdout.write("done." + os.EOL);
798+
}
799+
800+
console.log(comments.join(os.EOL));
801+
802+
if (typeErrorCount) {
803+
console.log("# type errors: %s", typeErrorCount);
804+
}
805+
806+
deleteTemporaryProjectOutput();
807+
fail("Process exited with code " + status);
808+
});
809+
ex.run();
810+
}
811+
688812
function runConsoleTests(defaultReporter, defaultSubsets) {
689813
cleanTestDirs();
690814
var debug = process.env.debug || process.env.d;
@@ -696,7 +820,7 @@ function runConsoleTests(defaultReporter, defaultSubsets) {
696820
}
697821

698822
if (tests || light) {
699-
writeTestConfigFile(tests, light, testConfigFile);
823+
writeTestConfigFile(testConfigFile, tests, light);
700824
}
701825

702826
if (tests && tests.toLocaleLowerCase() === "rwc") {
@@ -749,6 +873,10 @@ task("runtests", ["build-rules", "tests", builtLocalDirectory], function() {
749873
runConsoleTests('mocha-fivemat-progress-reporter', []);
750874
}, {async: true});
751875

876+
task("runtests-file", ["build-rules", "tests", builtLocalDirectory], function () {
877+
runTestsAndWriteOutput("tests/baselines/local/testresults.tap");
878+
}, { async: true });
879+
752880
desc("Generates code coverage data via instanbul");
753881
task("generate-code-coverage", ["tests", builtLocalDirectory], function () {
754882
var cmd = 'istanbul cover node_modules/mocha/bin/_mocha -- -R min -t ' + testTimeout + ' ' + run;
@@ -780,7 +908,7 @@ task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFi
780908
fs.unlinkSync(testConfigFile);
781909
}
782910
if(tests || light) {
783-
writeTestConfigFile(tests, light, testConfigFile);
911+
writeTestConfigFile(testConfigFile, tests, light);
784912
}
785913

786914
tests = tests ? tests : '';
@@ -1031,3 +1159,85 @@ task("lint-server", ["build-rules"], function() {
10311159
lintWatchFile(lintTargets[i]);
10321160
}
10331161
});
1162+
1163+
function ProgressBar(title) {
1164+
this.title = title;
1165+
}
1166+
ProgressBar.prototype = {
1167+
progressChars: ["\u0020", "\u2591", "\u2592", "\u2593", "\u2588"],
1168+
colors: {
1169+
foreground: {
1170+
black: "\u001b[90m",
1171+
red: "\u001b[91m",
1172+
green: "\u001b[92m",
1173+
yellow: "\u001b[93m",
1174+
blue: "\u001b[94m",
1175+
magenta: "\u001b[95m",
1176+
cyan: "\u001b[96m",
1177+
white: "\u001b[97m",
1178+
gray: "\u001b[37m"
1179+
},
1180+
background: {
1181+
black: "\u001b[40m",
1182+
red: "\u001b[41m",
1183+
green: "\u001b[42m",
1184+
yellow: "\u001b[43m",
1185+
blue: "\u001b[44m",
1186+
magenta: "\u001b[45m",
1187+
cyan: "\u001b[46m",
1188+
white: "\u001b[47m",
1189+
gray: "\u001b[100m"
1190+
},
1191+
reset: "\u001b[0m"
1192+
},
1193+
update: function (percentComplete, foregroundColor, backgroundColor) {
1194+
var progress = "";
1195+
for (var i = 0; i < 100; i += 4) {
1196+
progress += this.progressChars[Math.floor(Math.max(0, Math.min(4, percentComplete - i)))];
1197+
}
1198+
1199+
foregroundColor = foregroundColor && this.colors.foreground[foregroundColor];
1200+
backgroundColor = backgroundColor && this.colors.background[backgroundColor];
1201+
if (foregroundColor || backgroundColor) {
1202+
if (foregroundColor) {
1203+
progress = foregroundColor + progress;
1204+
}
1205+
if (backgroundColor) {
1206+
progress = backgroundColor + progress;
1207+
}
1208+
1209+
progress += this.colors.reset;
1210+
}
1211+
1212+
if (this._lastProgress !== progress || !this.visible) {
1213+
this._print(progress);
1214+
}
1215+
},
1216+
hide: function () {
1217+
if (this.visible) {
1218+
this._savedProgress = this._lastProgress;
1219+
this.clear();
1220+
}
1221+
},
1222+
show: function () {
1223+
if (this._savedProgress && !this.visible) {
1224+
this._print(this._savedProgress);
1225+
this._savedProgress = undefined;
1226+
}
1227+
},
1228+
clear: function () {
1229+
if (this._lastProgress) {
1230+
readline.moveCursor(process.stdout, -process.stdout.columns, 0);
1231+
readline.clearLine(process.stdout, 1);
1232+
this._lastProgress = undefined;
1233+
this.visible = false;
1234+
}
1235+
},
1236+
_print: function (progress) {
1237+
readline.moveCursor(process.stdout, -process.stdout.columns, 0);
1238+
process.stdout.write(this.title ? progress + " " + this.title : progress);
1239+
readline.clearLine(process.stdout, 1);
1240+
this._lastProgress = progress;
1241+
this.visible = true;
1242+
}
1243+
};

src/compiler/core.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,12 +1079,10 @@ namespace ts {
10791079
declare var process: any;
10801080
declare var require: any;
10811081

1082-
const currentAssertionLevel = getDevelopmentMode() === "development"
1083-
? AssertionLevel.Normal
1084-
: AssertionLevel.None;
1082+
let currentAssertionLevel: AssertionLevel;
10851083

10861084
export function shouldAssert(level: AssertionLevel): boolean {
1087-
return currentAssertionLevel >= level;
1085+
return getCurrentAssertionLevel() >= level;
10881086
}
10891087

10901088
export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string): void {
@@ -1102,15 +1100,21 @@ namespace ts {
11021100
Debug.assert(/*expression*/ false, message);
11031101
}
11041102

1105-
function getDevelopmentMode() {
1106-
return typeof require !== "undefined"
1107-
&& typeof process !== "undefined"
1108-
&& !process.browser
1109-
&& process.nextTick
1110-
&& process.env
1111-
&& process.env.NODE_ENV
1112-
? String(process.env.NODE_ENV).toLowerCase()
1113-
: undefined;
1103+
function getCurrentAssertionLevel() {
1104+
if (currentAssertionLevel !== undefined) {
1105+
return currentAssertionLevel;
1106+
}
1107+
1108+
const developmentMode = sys && /^development$/i.test(sys.getEnvironmentVariable("NODE_ENV"));
1109+
if (developmentMode === undefined) {
1110+
return AssertionLevel.None;
1111+
}
1112+
1113+
currentAssertionLevel = developmentMode
1114+
? AssertionLevel.Normal
1115+
: AssertionLevel.None;
1116+
1117+
return currentAssertionLevel;
11141118
}
11151119
}
11161120

src/compiler/factory.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ namespace ts {
114114

115115
// Literals
116116

117+
export function createLiteral(textSource: StringLiteral | Identifier, location?: TextRange): StringLiteral;
117118
export function createLiteral(value: string, location?: TextRange): StringLiteral;
118119
export function createLiteral(value: number, location?: TextRange): LiteralExpression;
119120
export function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression;
120-
export function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression {
121+
export function createLiteral(value: string | number | boolean | StringLiteral | Identifier, location?: TextRange): PrimaryExpression {
121122
if (typeof value === "number") {
122123
const node = <LiteralExpression>createNode(SyntaxKind.NumericLiteral, location);
123124
node.text = value.toString();
@@ -126,9 +127,15 @@ namespace ts {
126127
else if (typeof value === "boolean") {
127128
return <PrimaryExpression>createNode(value ? SyntaxKind.TrueKeyword : SyntaxKind.FalseKeyword, location);
128129
}
130+
else if (typeof value === "string") {
131+
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location);
132+
node.text = value;
133+
return node;
134+
}
129135
else {
130136
const node = <StringLiteral>createNode(SyntaxKind.StringLiteral, location);
131-
node.text = String(value);
137+
node.textSourceNode = value;
138+
node.text = value.text;
132139
return node;
133140
}
134141
}
@@ -138,6 +145,7 @@ namespace ts {
138145
export function createIdentifier(text: string, location?: TextRange): Identifier {
139146
const node = <Identifier>createNode(SyntaxKind.Identifier, location);
140147
node.text = escapeIdentifier(text);
148+
node.originalKeywordKind = stringToToken(text);
141149
return node;
142150
}
143151

@@ -423,11 +431,11 @@ namespace ts {
423431
return block;
424432
}
425433

426-
export function createVariableStatement(modifiers: Modifier[], declarationList: VariableDeclarationList, location?: TextRange): VariableStatement {
434+
export function createVariableStatement(modifiers: Modifier[], declarationList: VariableDeclarationList | VariableDeclaration[], location?: TextRange): VariableStatement {
427435
const node = <VariableStatement>createNode(SyntaxKind.VariableStatement, location);
428436
node.decorators = undefined;
429437
node.modifiers = modifiers ? createNodeArray(modifiers) : undefined;
430-
node.declarationList = declarationList;
438+
node.declarationList = isArray(declarationList) ? createVariableDeclarationList(declarationList) : declarationList;
431439
return node;
432440
}
433441

0 commit comments

Comments
 (0)