mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
merged with master
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
var x = {one: 1};
|
||||
var y: {[index:string]: any};
|
||||
|
||||
x = y;
|
||||
y = x;
|
||||
var x = { one: 1 };
|
||||
var y: { [index: string]: any };
|
||||
var z: { [index: number]: any };
|
||||
x = y; // Error
|
||||
y = x; // Ok because index signature type is any
|
||||
x = z; // Error
|
||||
z = x; // Ok because index signature type is any
|
||||
|
||||
12
tests/cases/compiler/classExtendsNull.ts
Normal file
12
tests/cases/compiler/classExtendsNull.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
class C extends null {
|
||||
constructor() {
|
||||
super();
|
||||
return Object.create(null);
|
||||
}
|
||||
}
|
||||
|
||||
class D extends null {
|
||||
constructor() {
|
||||
return Object.create(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// @noemithelpers: true
|
||||
// @experimentaldecorators: true
|
||||
// @emitdecoratormetadata: true
|
||||
// @target: es5
|
||||
|
||||
declare var decorator: any;
|
||||
|
||||
class MyClass {
|
||||
constructor(test: string, test2: number) {
|
||||
|
||||
}
|
||||
|
||||
@decorator
|
||||
doSomething() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,5 @@
|
||||
(...arg) => 103;
|
||||
(...arg:number [] = []) => 104;
|
||||
|
||||
// Non optional parameter following an optional one
|
||||
// Uninitialized parameter makes the initialized one required
|
||||
(arg1 = 1, arg2) => 1;
|
||||
5
tests/cases/compiler/inferringAnyFunctionType1.ts
Normal file
5
tests/cases/compiler/inferringAnyFunctionType1.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
function f<T extends { "0": (p1: number) => number }>(p: T): T {
|
||||
return p;
|
||||
}
|
||||
|
||||
var v = f([x => x]);
|
||||
5
tests/cases/compiler/inferringAnyFunctionType2.ts
Normal file
5
tests/cases/compiler/inferringAnyFunctionType2.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
function f<T extends [(p1: number) => number]>(p: T): T {
|
||||
return p;
|
||||
}
|
||||
|
||||
var v = f([x => x]);
|
||||
5
tests/cases/compiler/inferringAnyFunctionType3.ts
Normal file
5
tests/cases/compiler/inferringAnyFunctionType3.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
function f<T extends ((p1: number) => number)[]>(p: T): T {
|
||||
return p;
|
||||
}
|
||||
|
||||
var v = f([x => x]);
|
||||
5
tests/cases/compiler/inferringAnyFunctionType4.ts
Normal file
5
tests/cases/compiler/inferringAnyFunctionType4.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
function f<T extends (p1: number) => number>(p: T): T {
|
||||
return p;
|
||||
}
|
||||
|
||||
var v = f(x => x);
|
||||
5
tests/cases/compiler/inferringAnyFunctionType5.ts
Normal file
5
tests/cases/compiler/inferringAnyFunctionType5.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
function f<T extends { q: (p1: number) => number }>(p: T): T {
|
||||
return p;
|
||||
}
|
||||
|
||||
var v = f({ q: x => x });
|
||||
@@ -3,4 +3,4 @@
|
||||
// @target: es6
|
||||
|
||||
// @filename: file1.ts
|
||||
export var x;
|
||||
export var x = 1;
|
||||
23
tests/cases/compiler/jsxViaImport.tsx
Normal file
23
tests/cases/compiler/jsxViaImport.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
//@jsx: preserve
|
||||
//@module: commonjs
|
||||
|
||||
//@filename: component.d.ts
|
||||
declare module JSX {
|
||||
interface ElementAttributesProperty { props; }
|
||||
}
|
||||
declare module React {
|
||||
class Component<T, U> { }
|
||||
}
|
||||
declare module "BaseComponent" {
|
||||
var base: React.Component<any, {}>;
|
||||
export = base;
|
||||
}
|
||||
|
||||
//@filename: consumer.tsx
|
||||
/// <reference path="component.d.ts" />
|
||||
import BaseComponent = require('BaseComponent');
|
||||
class TestComponent extends React.Component<any, {}> {
|
||||
render() {
|
||||
return <BaseComponent />;
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,7 @@ class C1 {
|
||||
|
||||
public C1M5(C1M5A1:number,C1M5A2:number=0,C1M5A3?:number) { return C1M5A1 + C1M5A2; }
|
||||
|
||||
// Negative test
|
||||
// "Optional parameters may only be followed by other optional parameters"
|
||||
// Uninitialized parameter makes the initialized one required
|
||||
public C1M5(C1M5A1:number,C1M5A2:number=0,C1M5A3:number) { return C1M5A1 + C1M5A2; }
|
||||
}
|
||||
|
||||
|
||||
19
tests/cases/compiler/requiredInitializedParameter1.ts
Normal file
19
tests/cases/compiler/requiredInitializedParameter1.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
function f1(a, b = 0, c) { }
|
||||
function f2(a, b = 0, c = 0) { }
|
||||
function f3(a, b = 0, c?) { }
|
||||
function f4(a, b = 0, ...c) { }
|
||||
|
||||
f1(0, 1, 2);
|
||||
f2(0, 1, 2);
|
||||
f3(0, 1, 2);
|
||||
f4(0, 1, 2);
|
||||
|
||||
f1(0, 1);
|
||||
f2(0, 1);
|
||||
f3(0, 1);
|
||||
f4(0, 1);
|
||||
|
||||
f1(0);
|
||||
f2(0);
|
||||
f3(0);
|
||||
f4(0);
|
||||
7
tests/cases/compiler/requiredInitializedParameter2.ts
Normal file
7
tests/cases/compiler/requiredInitializedParameter2.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
interface I1 {
|
||||
method();
|
||||
}
|
||||
|
||||
class C1 implements I1 {
|
||||
method(a = 0, b) { }
|
||||
}
|
||||
8
tests/cases/compiler/requiredInitializedParameter3.ts
Normal file
8
tests/cases/compiler/requiredInitializedParameter3.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
//@declaration: true
|
||||
interface I1 {
|
||||
method();
|
||||
}
|
||||
|
||||
class C1 implements I1 {
|
||||
method(a = 0, b?) { }
|
||||
}
|
||||
4
tests/cases/compiler/requiredInitializedParameter4.ts
Normal file
4
tests/cases/compiler/requiredInitializedParameter4.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
//@declaration: true
|
||||
class C1 {
|
||||
method(a = 0, b) { }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
interface bar { }
|
||||
let bar: bar;
|
||||
@@ -0,0 +1,4 @@
|
||||
interface foo { }
|
||||
interface bar { }
|
||||
let bar: bar | foo;
|
||||
let foo: bar | foo;
|
||||
@@ -0,0 +1,8 @@
|
||||
declare module foo {
|
||||
|
||||
interface Bar {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
let foo: foo.Bar;
|
||||
@@ -0,0 +1,8 @@
|
||||
declare module "punycode" {
|
||||
interface ucs2 {
|
||||
decode(string: string): string;
|
||||
encode(codePoints: number[]): string;
|
||||
}
|
||||
|
||||
export let ucs2: ucs2;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C { }
|
||||
type baz = C;
|
||||
let baz: baz;
|
||||
@@ -0,0 +1,31 @@
|
||||
// @module: commonjs
|
||||
// @target: ES5
|
||||
|
||||
// @filename: m1.ts
|
||||
export default function Decl() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
export interface Decl {
|
||||
p1: number;
|
||||
p2: number;
|
||||
}
|
||||
|
||||
export namespace Decl {
|
||||
export var x = 10;
|
||||
export var y = 20;
|
||||
|
||||
interface I {
|
||||
}
|
||||
}
|
||||
|
||||
// @filename: m2.ts
|
||||
import Entity from "m1"
|
||||
|
||||
Entity();
|
||||
|
||||
var x: Entity;
|
||||
var y: Entity.I;
|
||||
|
||||
Entity.x;
|
||||
Entity.y;
|
||||
@@ -0,0 +1,26 @@
|
||||
// @module: commonjs
|
||||
// @target: ES5
|
||||
|
||||
// @filename: m1.ts
|
||||
export default class Decl {
|
||||
}
|
||||
|
||||
export interface Decl {
|
||||
p1: number;
|
||||
p2: number;
|
||||
}
|
||||
|
||||
export namespace Decl {
|
||||
interface I {
|
||||
}
|
||||
}
|
||||
|
||||
// @filename: m2.ts
|
||||
import Entity from "m1"
|
||||
|
||||
Entity();
|
||||
|
||||
var x: Entity;
|
||||
var y: Entity.I;
|
||||
var z = new Entity();
|
||||
var sum = z.p1 + z.p2
|
||||
@@ -0,0 +1,26 @@
|
||||
// @module: commonjs
|
||||
// @target: ES5
|
||||
|
||||
// @filename: m1.ts
|
||||
export default class Decl {
|
||||
}
|
||||
|
||||
interface Decl {
|
||||
p1: number;
|
||||
p2: number;
|
||||
}
|
||||
|
||||
namespace Decl {
|
||||
interface I {
|
||||
}
|
||||
}
|
||||
|
||||
// @filename: m2.ts
|
||||
import Entity from "m1"
|
||||
|
||||
Entity();
|
||||
|
||||
var x: Entity;
|
||||
var y: Entity.I;
|
||||
var z = new Entity();
|
||||
var sum = z.p1 + z.p2
|
||||
@@ -0,0 +1,15 @@
|
||||
// @module: commonjs
|
||||
// @target: ES5
|
||||
|
||||
export default function Foo() {
|
||||
}
|
||||
|
||||
namespace Foo {
|
||||
export var x;
|
||||
}
|
||||
|
||||
interface Foo {
|
||||
}
|
||||
|
||||
export interface Foo {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// @module: commonjs
|
||||
// @target: ES5
|
||||
|
||||
// @filename: m1.ts
|
||||
export default class foo {
|
||||
|
||||
}
|
||||
|
||||
export default function bar() {
|
||||
|
||||
}
|
||||
|
||||
var x = 10;
|
||||
export default x;
|
||||
|
||||
// @filename: m2.ts
|
||||
import Entity from "m1"
|
||||
|
||||
Entity();
|
||||
@@ -0,0 +1,16 @@
|
||||
// @module: commonjs
|
||||
// @target: ES5
|
||||
|
||||
// @filename: m1.ts
|
||||
export default function foo() {
|
||||
|
||||
}
|
||||
|
||||
export default function bar() {
|
||||
|
||||
}
|
||||
|
||||
// @filename: m2.ts
|
||||
import Entity from "m1"
|
||||
|
||||
Entity();
|
||||
@@ -1,5 +1,7 @@
|
||||
//@filename: file.tsx
|
||||
//@jsx: preserve
|
||||
//@sourceMap: true
|
||||
|
||||
declare module JSX {
|
||||
interface Element { }
|
||||
interface IntrinsicElements { }
|
||||
|
||||
17
tests/cases/conformance/jsx/tsxExternalModuleEmit2.tsx
Normal file
17
tests/cases/conformance/jsx/tsxExternalModuleEmit2.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
//@jsx: react
|
||||
//@module: commonjs
|
||||
|
||||
//@filename: modules.d.ts
|
||||
declare module 'mod' {
|
||||
var y: any;
|
||||
export default y;
|
||||
}
|
||||
|
||||
//@filename: app.tsx
|
||||
import Main from 'mod';
|
||||
declare var Foo, React;
|
||||
// Should see mod_1['default'] in emit here
|
||||
<Foo handler={Main}></Foo>;
|
||||
// Should see mod_1['default'] in emit here
|
||||
<Foo {...Main}></Foo>;
|
||||
|
||||
13
tests/cases/fourslash/server/projectInfo02.ts
Normal file
13
tests/cases/fourslash/server/projectInfo02.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/// <reference path="../fourslash.ts"/>
|
||||
|
||||
// @Filename: a.ts
|
||||
////export var test = "test String"
|
||||
|
||||
// @Filename: b.ts
|
||||
////export var test2 = "test String"
|
||||
|
||||
// @Filename: tsconfig.json
|
||||
////{ "files": ["a.ts", "b.ts"] }
|
||||
|
||||
goTo.file("a.ts")
|
||||
verify.ProjectInfo(["lib.d.ts", "a.ts", "b.ts"])
|
||||
@@ -2,58 +2,120 @@
|
||||
|
||||
module ts {
|
||||
describe("Transpile", () => {
|
||||
|
||||
function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, fileName?: string, moduleName?: string, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
|
||||
let diagnostics: Diagnostic[] = [];
|
||||
let result = transpile(input, compilerOptions, fileName || "file.ts", diagnostics, moduleName);
|
||||
|
||||
|
||||
interface TranspileTestSettings {
|
||||
options?: TranspileOptions;
|
||||
expectedOutput?: string;
|
||||
expectedDiagnosticCodes?: number[];
|
||||
}
|
||||
|
||||
function checkDiagnostics(diagnostics: Diagnostic[], expectedDiagnosticCodes?: number[]) {
|
||||
if(!expectedDiagnosticCodes) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < expectedDiagnosticCodes.length; i++) {
|
||||
assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`);
|
||||
}
|
||||
assert.equal(diagnostics.length, expectedDiagnosticCodes.length, "Resuting diagnostics count does not match expected");
|
||||
|
||||
if (expectedOutput !== undefined) {
|
||||
assert.equal(result, expectedOutput);
|
||||
}
|
||||
assert.equal(diagnostics.length, expectedDiagnosticCodes.length, "Resuting diagnostics count does not match expected");
|
||||
}
|
||||
|
||||
function test(input: string, testSettings: TranspileTestSettings): void {
|
||||
let diagnostics: Diagnostic[] = [];
|
||||
|
||||
let transpileOptions: TranspileOptions = testSettings.options || {};
|
||||
let transpileResult = transpile(input, transpileOptions.compilerOptions, transpileOptions.fileName, diagnostics, transpileOptions.moduleName);
|
||||
|
||||
transpileOptions.reportDiagnostics = true;
|
||||
let transpileModuleResult = transpileModule(input, transpileOptions);
|
||||
|
||||
checkDiagnostics(diagnostics, testSettings.expectedDiagnosticCodes);
|
||||
checkDiagnostics(transpileModuleResult.diagnostics, testSettings.expectedDiagnosticCodes);
|
||||
|
||||
if (testSettings.expectedOutput !== undefined) {
|
||||
assert.equal(transpileResult, testSettings.expectedOutput);
|
||||
assert.equal(transpileModuleResult.outputText, testSettings.expectedOutput);
|
||||
}
|
||||
|
||||
// check source maps
|
||||
if (!transpileOptions.compilerOptions) {
|
||||
transpileOptions.compilerOptions = {};
|
||||
}
|
||||
|
||||
if (!transpileOptions.fileName) {
|
||||
transpileOptions.fileName = "file.ts";
|
||||
}
|
||||
|
||||
transpileOptions.compilerOptions.sourceMap = true;
|
||||
let transpileModuleResultWithSourceMap = transpileModule(input, transpileOptions);
|
||||
assert.isTrue(transpileModuleResultWithSourceMap.sourceMapText !== undefined);
|
||||
|
||||
let expectedSourceMapFileName = removeFileExtension(transpileOptions.fileName) + ".js.map";
|
||||
let expectedSourceMappingUrlLine = `//# sourceMappingURL=${expectedSourceMapFileName}`;
|
||||
|
||||
if (testSettings.expectedOutput !== undefined) {
|
||||
assert.equal(transpileModuleResultWithSourceMap.outputText, testSettings.expectedOutput + expectedSourceMappingUrlLine);
|
||||
}
|
||||
else {
|
||||
// expected output is not set, just verify that output text has sourceMappingURL as a last line
|
||||
let output = transpileModuleResultWithSourceMap.outputText;
|
||||
assert.isTrue(output.length >= expectedSourceMappingUrlLine.length);
|
||||
if (output.length === expectedSourceMappingUrlLine.length) {
|
||||
assert.equal(output, expectedSourceMappingUrlLine);
|
||||
}
|
||||
else {
|
||||
let suffix = getNewLineCharacter(transpileOptions.compilerOptions) + expectedSourceMappingUrlLine
|
||||
assert.isTrue(output.indexOf(suffix, output.length - suffix.length) !== -1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
it("Generates correct compilerOptions diagnostics", () => {
|
||||
// Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher."
|
||||
runTest(`var x = 0;`, {}, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]);
|
||||
test(`var x = 0;`, { expectedDiagnosticCodes: [5047] });
|
||||
});
|
||||
|
||||
it("Generates no diagnostics with valid inputs", () => {
|
||||
// No errors
|
||||
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
||||
test(`var x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS } } });
|
||||
});
|
||||
|
||||
it("Generates no diagnostics for missing file references", () => {
|
||||
runTest(`/// <reference path="file2.ts" />
|
||||
var x = 0;`,
|
||||
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
||||
test(`/// <reference path="file2.ts" />
|
||||
var x = 0;`,
|
||||
{ options: { compilerOptions: { module: ModuleKind.CommonJS } } });
|
||||
});
|
||||
|
||||
it("Generates no diagnostics for missing module imports", () => {
|
||||
runTest(`import {a} from "module2";`,
|
||||
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined,/*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
||||
test(`import {a} from "module2";`,
|
||||
{ options: { compilerOptions: { module: ModuleKind.CommonJS } } });
|
||||
});
|
||||
|
||||
it("Generates expected syntactic diagnostics", () => {
|
||||
runTest(`a b`,
|
||||
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected
|
||||
test(`a b`,
|
||||
{ options: { compilerOptions: { module: ModuleKind.CommonJS } }, expectedDiagnosticCodes: [1005] }); /// 1005: ';' Expected
|
||||
});
|
||||
|
||||
it("Does not generate semantic diagnostics", () => {
|
||||
runTest(`var x: string = 0;`,
|
||||
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
||||
test(`var x: string = 0;`,
|
||||
{ options: { compilerOptions: { module: ModuleKind.CommonJS } } });
|
||||
});
|
||||
|
||||
it("Generates module output", () => {
|
||||
runTest(`var x = 0;`, { module: ModuleKind.AMD }, /*fileName*/ undefined, /*moduleName*/undefined, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
|
||||
test(`var x = 0;`,
|
||||
{
|
||||
options: { compilerOptions: { module: ModuleKind.AMD } },
|
||||
expectedOutput: `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`
|
||||
});
|
||||
});
|
||||
|
||||
it("Uses correct newLine character", () => {
|
||||
runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, /*fileName*/ undefined, /*moduleName*/undefined, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []);
|
||||
test(`var x = 0;`,
|
||||
{
|
||||
options: { compilerOptions: { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed } },
|
||||
expectedOutput: `var x = 0;\n`
|
||||
});
|
||||
});
|
||||
|
||||
it("Sets module name", () => {
|
||||
@@ -66,12 +128,15 @@ var x = 0;`,
|
||||
` }\n` +
|
||||
` }\n` +
|
||||
`});\n`;
|
||||
runTest("var x = 1;", { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, /*fileName*/ undefined, "NamedModule", output)
|
||||
test("var x = 1;",
|
||||
{
|
||||
options: { compilerOptions: { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, moduleName: "NamedModule" },
|
||||
expectedOutput: output
|
||||
})
|
||||
});
|
||||
|
||||
it("No extra errors for file without extension", () => {
|
||||
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, "file", /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/[]);
|
||||
test(`var x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS }, fileName: "file" } });
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user