Add error message if module is not specified and a file contains a module indicator

This commit is contained in:
Mohamed Hegazy
2016-05-17 17:14:51 -07:00
parent c62b6cb6fc
commit 53f6755907
11 changed files with 99 additions and 193 deletions

View File

@@ -2751,7 +2751,12 @@
"Resolving real path for '{0}', result '{1}'": {
"category": "Message",
"code": 6130
},
},
"Cannot compile modules using option '{0}' unless the '--module' flag is provided with a valid module type.": {
"category": "Error",
"code": 6131
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005

View File

@@ -2089,8 +2089,14 @@ namespace ts {
}
// Cannot specify module gen that isn't amd or system with --out
if (outFile && options.module && !(options.module === ModuleKind.AMD || options.module === ModuleKind.System)) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile"));
if (outFile) {
if (options.module && !(options.module === ModuleKind.AMD || options.module === ModuleKind.System)) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Only_amd_and_system_modules_are_supported_alongside_0, options.out ? "out" : "outFile"));
}
else if (options.module === undefined && firstExternalModuleSourceFile) {
const span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator);
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_modules_using_option_0_unless_the_module_flag_is_provided_with_a_valid_module_type, options.out ? "out" : "outFile"));
}
}
// there has to be common source directory if user specified --outdir || --sourceRoot

View File

@@ -0,0 +1,11 @@
tests/cases/compiler/a.ts(2,14): error TS6131: Cannot compile modules using option 'outFile' unless the '--module' flag is provided with a valid module type.
==== tests/cases/compiler/a.ts (1 errors) ====
export class A { } // module
~
!!! error TS6131: Cannot compile modules using option 'outFile' unless the '--module' flag is provided with a valid module type.
==== tests/cases/compiler/b.ts (0 errors) ====
var x = 0; // global

View File

@@ -0,0 +1,11 @@
//// [tests/cases/compiler/outModuleConcatUnspecifiedModuleKind.ts] ////
//// [a.ts]
export class A { } // module
//// [b.ts]
var x = 0; // global
//// [out.js]
var x = 0; // global

View File

@@ -0,0 +1,18 @@
/mod1.ts(2,17): error TS6131: Cannot compile modules using option 'out' unless the '--module' flag is provided with a valid module type.
==== /mod2.ts (0 errors) ====
import {foo} from "./mod1";
export const bar = foo();
==== /types/lib/index.d.ts (0 errors) ====
interface Lib { x }
==== /mod1.ts (1 errors) ====
export function foo(): Lib { return {x: 1} }
~~~
!!! error TS6131: Cannot compile modules using option 'out' unless the '--module' flag is provided with a valid module type.

View File

@@ -1,23 +0,0 @@
=== /mod2.ts ===
import {foo} from "./mod1";
>foo : Symbol(foo, Decl(mod2.ts, 1, 8))
export const bar = foo();
>bar : Symbol(bar, Decl(mod2.ts, 2, 12))
>foo : Symbol(foo, Decl(mod2.ts, 1, 8))
=== /types/lib/index.d.ts ===
interface Lib { x }
>Lib : Symbol(Lib, Decl(index.d.ts, 0, 0))
>x : Symbol(Lib.x, Decl(index.d.ts, 2, 15))
=== /mod1.ts ===
export function foo(): Lib { return {x: 1} }
>foo : Symbol(foo, Decl(mod1.ts, 0, 0))
>Lib : Symbol(Lib, Decl(index.d.ts, 0, 0))
>x : Symbol(x, Decl(mod1.ts, 1, 37))

View File

@@ -1,26 +0,0 @@
=== /mod2.ts ===
import {foo} from "./mod1";
>foo : () => Lib
export const bar = foo();
>bar : Lib
>foo() : Lib
>foo : () => Lib
=== /types/lib/index.d.ts ===
interface Lib { x }
>Lib : Lib
>x : any
=== /mod1.ts ===
export function foo(): Lib { return {x: 1} }
>foo : () => Lib
>Lib : Lib
>{x: 1} : { x: number; }
>x : number
>1 : number

View File

@@ -0,0 +1,37 @@
/main.ts(1,14): error TS6131: Cannot compile modules using option 'out' unless the '--module' flag is provided with a valid module type.
==== /mod2.ts (0 errors) ====
import { Cls } from "./main";
import "./mod1";
export const cls = Cls;
export const foo = new Cls().foo();
export const bar = Cls.bar();
==== /types/lib/index.d.ts (0 errors) ====
interface Lib { x }
==== /main.ts (1 errors) ====
export class Cls {
~~~
!!! error TS6131: Cannot compile modules using option 'out' unless the '--module' flag is provided with a valid module type.
x
}
==== /mod1.ts (0 errors) ====
/// <reference types="lib" />
import {Cls} from "./main";
Cls.prototype.foo = function() { return undefined; }
declare module "./main" {
interface Cls {
foo(): Lib;
}
namespace Cls {
function bar(): Lib;
}
}

View File

@@ -1,68 +0,0 @@
=== /mod2.ts ===
import { Cls } from "./main";
>Cls : Symbol(Cls, Decl(mod2.ts, 0, 8))
import "./mod1";
export const cls = Cls;
>cls : Symbol(cls, Decl(mod2.ts, 3, 12))
>Cls : Symbol(Cls, Decl(mod2.ts, 0, 8))
export const foo = new Cls().foo();
>foo : Symbol(foo, Decl(mod2.ts, 4, 12))
>new Cls().foo : Symbol(Cls.foo, Decl(mod1.ts, 6, 19))
>Cls : Symbol(Cls, Decl(mod2.ts, 0, 8))
>foo : Symbol(Cls.foo, Decl(mod1.ts, 6, 19))
export const bar = Cls.bar();
>bar : Symbol(bar, Decl(mod2.ts, 5, 12))
>Cls.bar : Symbol(Cls.bar, Decl(mod1.ts, 9, 19))
>Cls : Symbol(Cls, Decl(mod2.ts, 0, 8))
>bar : Symbol(Cls.bar, Decl(mod1.ts, 9, 19))
=== /types/lib/index.d.ts ===
interface Lib { x }
>Lib : Symbol(Lib, Decl(index.d.ts, 0, 0))
>x : Symbol(Lib.x, Decl(index.d.ts, 2, 15))
=== /main.ts ===
export class Cls {
>Cls : Symbol(Cls, Decl(main.ts, 0, 0), Decl(mod1.ts, 5, 25), Decl(mod1.ts, 8, 5))
x
>x : Symbol(Cls.x, Decl(main.ts, 0, 18))
}
=== /mod1.ts ===
/// <reference types="lib" />
import {Cls} from "./main";
>Cls : Symbol(Cls, Decl(mod1.ts, 2, 8))
Cls.prototype.foo = function() { return undefined; }
>Cls.prototype.foo : Symbol(Cls.foo, Decl(mod1.ts, 6, 19))
>Cls.prototype : Symbol(Cls.prototype)
>Cls : Symbol(Cls, Decl(mod1.ts, 2, 8))
>prototype : Symbol(Cls.prototype)
>foo : Symbol(Cls.foo, Decl(mod1.ts, 6, 19))
>undefined : Symbol(undefined)
declare module "./main" {
interface Cls {
>Cls : Symbol(Cls, Decl(main.ts, 0, 0), Decl(mod1.ts, 5, 25), Decl(mod1.ts, 8, 5))
foo(): Lib;
>foo : Symbol(Cls.foo, Decl(mod1.ts, 6, 19))
>Lib : Symbol(Lib, Decl(index.d.ts, 0, 0))
}
namespace Cls {
>Cls : Symbol(Cls, Decl(main.ts, 0, 0), Decl(mod1.ts, 5, 25), Decl(mod1.ts, 8, 5))
function bar(): Lib;
>bar : Symbol(bar, Decl(mod1.ts, 9, 19))
>Lib : Symbol(Lib, Decl(index.d.ts, 0, 0))
}
}

View File

@@ -1,73 +0,0 @@
=== /mod2.ts ===
import { Cls } from "./main";
>Cls : typeof Cls
import "./mod1";
export const cls = Cls;
>cls : typeof Cls
>Cls : typeof Cls
export const foo = new Cls().foo();
>foo : Lib
>new Cls().foo() : Lib
>new Cls().foo : () => Lib
>new Cls() : Cls
>Cls : typeof Cls
>foo : () => Lib
export const bar = Cls.bar();
>bar : Lib
>Cls.bar() : Lib
>Cls.bar : () => Lib
>Cls : typeof Cls
>bar : () => Lib
=== /types/lib/index.d.ts ===
interface Lib { x }
>Lib : Lib
>x : any
=== /main.ts ===
export class Cls {
>Cls : Cls
x
>x : any
}
=== /mod1.ts ===
/// <reference types="lib" />
import {Cls} from "./main";
>Cls : typeof Cls
Cls.prototype.foo = function() { return undefined; }
>Cls.prototype.foo = function() { return undefined; } : () => any
>Cls.prototype.foo : () => Lib
>Cls.prototype : Cls
>Cls : typeof Cls
>prototype : Cls
>foo : () => Lib
>function() { return undefined; } : () => any
>undefined : undefined
declare module "./main" {
interface Cls {
>Cls : Cls
foo(): Lib;
>foo : () => Lib
>Lib : Lib
}
namespace Cls {
>Cls : typeof Cls
function bar(): Lib;
>bar : () => Lib
>Lib : Lib
}
}

View File

@@ -0,0 +1,8 @@
// @target: ES5
// @outFile: out.js
// @Filename: a.ts
export class A { } // module
// @Filename: b.ts
var x = 0; // global