Merge pull request #8945 from Microsoft/shorthand_ambient_module

Support shorthand ambient module declarations
This commit is contained in:
Andy
2016-06-10 10:09:57 -07:00
committed by GitHub
35 changed files with 436 additions and 28 deletions
+3 -2
View File
@@ -53,7 +53,8 @@ namespace ts {
return state;
}
else if (node.kind === SyntaxKind.ModuleDeclaration) {
return getModuleInstanceState((<ModuleDeclaration>node).body);
const body = (<ModuleDeclaration>node).body;
return body ? getModuleInstanceState(body) : ModuleInstanceState.Instantiated;
}
else {
return ModuleInstanceState.Instantiated;
@@ -1258,7 +1259,7 @@ namespace ts {
function hasExportDeclarations(node: ModuleDeclaration | SourceFile): boolean {
const body = node.kind === SyntaxKind.SourceFile ? node : (<ModuleDeclaration>node).body;
if (body.kind === SyntaxKind.SourceFile || body.kind === SyntaxKind.ModuleBlock) {
if (body && (body.kind === SyntaxKind.SourceFile || body.kind === SyntaxKind.ModuleBlock)) {
for (const stat of (<Block>body).statements) {
if (stat.kind === SyntaxKind.ExportDeclaration || stat.kind === SyntaxKind.ExportAssignment) {
return true;
+25 -6
View File
@@ -992,7 +992,9 @@ namespace ts {
const moduleSymbol = resolveExternalModuleName(node, (<ImportDeclaration>node.parent).moduleSpecifier);
if (moduleSymbol) {
const exportDefaultSymbol = moduleSymbol.exports["export="] ?
const exportDefaultSymbol = isShorthandAmbientModule(moduleSymbol.valueDeclaration) ?
moduleSymbol :
moduleSymbol.exports["export="] ?
getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") :
resolveSymbol(moduleSymbol.exports["default"]);
@@ -1066,6 +1068,10 @@ namespace ts {
if (targetSymbol) {
const name = specifier.propertyName || specifier.name;
if (name.text) {
if (isShorthandAmbientModule(moduleSymbol.valueDeclaration)) {
return moduleSymbol;
}
let symbolFromVariable: Symbol;
// First check if module was specified with "export=". If so, get the member from the resolved type
if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) {
@@ -3234,9 +3240,14 @@ namespace ts {
function getTypeOfFuncClassEnumModule(symbol: Symbol): Type {
const links = getSymbolLinks(symbol);
if (!links.type) {
const type = createObjectType(TypeFlags.Anonymous, symbol);
links.type = strictNullChecks && symbol.flags & SymbolFlags.Optional ?
addTypeKind(type, TypeFlags.Undefined) : type;
if (symbol.valueDeclaration.kind === SyntaxKind.ModuleDeclaration && isShorthandAmbientModule(<ModuleDeclaration>symbol.valueDeclaration)) {
links.type = anyType;
}
else {
const type = createObjectType(TypeFlags.Anonymous, symbol);
links.type = strictNullChecks && symbol.flags & SymbolFlags.Optional ?
addTypeKind(type, TypeFlags.Undefined) : type;
}
}
return links.type;
}
@@ -16052,7 +16063,7 @@ namespace ts {
// - augmentation for a global scope is always applied
// - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module).
const checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & SymbolFlags.Merged);
if (checkBody) {
if (checkBody && node.body) {
// body of ambient external module is always a module block
for (const statement of (<ModuleBlock>node.body).statements) {
checkModuleAugmentationElement(statement, isGlobalAugmentation);
@@ -16079,7 +16090,15 @@ namespace ts {
}
}
}
checkSourceElement(node.body);
if (compilerOptions.noImplicitAny && !node.body) {
// Ambient shorthand module is an implicit any
reportImplicitAnyError(node, anyType);
}
if (node.body) {
checkSourceElement(node.body);
}
}
function checkModuleAugmentationElement(node: Node, isGlobalAugmentation: boolean): void {
+15 -10
View File
@@ -853,21 +853,26 @@ namespace ts {
writeTextOfNode(currentText, node.name);
}
}
while (node.body.kind !== SyntaxKind.ModuleBlock) {
while (node.body && node.body.kind !== SyntaxKind.ModuleBlock) {
node = <ModuleDeclaration>node.body;
write(".");
writeTextOfNode(currentText, node.name);
}
const prevEnclosingDeclaration = enclosingDeclaration;
enclosingDeclaration = node;
write(" {");
writeLine();
increaseIndent();
emitLines((<ModuleBlock>node.body).statements);
decreaseIndent();
write("}");
writeLine();
enclosingDeclaration = prevEnclosingDeclaration;
if (node.body) {
enclosingDeclaration = node;
write(" {");
writeLine();
increaseIndent();
emitLines((<ModuleBlock>node.body).statements);
decreaseIndent();
write("}");
writeLine();
enclosingDeclaration = prevEnclosingDeclaration;
}
else {
write(";");
}
}
function writeTypeAliasDeclaration(node: TypeAliasDeclaration) {
+2 -1
View File
@@ -6300,7 +6300,7 @@ const _super = (function (geti, seti) {
}
function getInnerMostModuleDeclarationFromDottedModule(moduleDeclaration: ModuleDeclaration): ModuleDeclaration {
if (moduleDeclaration.body.kind === SyntaxKind.ModuleDeclaration) {
if (moduleDeclaration.body && moduleDeclaration.body.kind === SyntaxKind.ModuleDeclaration) {
const recursiveInnerModule = getInnerMostModuleDeclarationFromDottedModule(<ModuleDeclaration>moduleDeclaration.body);
return recursiveInnerModule || <ModuleDeclaration>moduleDeclaration.body;
}
@@ -6349,6 +6349,7 @@ const _super = (function (geti, seti) {
write(getGeneratedNameForNode(node));
emitEnd(node.name);
write(") ");
Debug.assert(node.body !== undefined); // node.body must exist, as this is a non-ambient module
if (node.body.kind === SyntaxKind.ModuleBlock) {
const saveConvertedLoopState = convertedLoopState;
const saveTempFlags = tempFlags;
+8 -1
View File
@@ -5337,7 +5337,14 @@ namespace ts {
else {
node.name = parseLiteralNode(/*internName*/ true);
}
node.body = parseModuleBlock();
if (token === SyntaxKind.OpenBraceToken) {
node.body = parseModuleBlock();
}
else {
parseSemicolon();
}
return finishNode(node);
}
+6 -3
View File
@@ -1775,9 +1775,12 @@ namespace ts {
// The StringLiteral must specify a top - level external module name.
// Relative external module names are not permitted
// NOTE: body of ambient module is always a module block
for (const statement of (<ModuleBlock>(<ModuleDeclaration>node).body).statements) {
collectModuleReferences(statement, /*inAmbientModule*/ true);
// NOTE: body of ambient module is always a module block, if it exists
const body = <ModuleBlock>(<ModuleDeclaration>node).body;
if (body) {
for (const statement of body.statements) {
collectModuleReferences(statement, /*inAmbientModule*/ true);
}
}
}
}
+1 -1
View File
@@ -1301,7 +1301,7 @@ namespace ts {
// @kind(SyntaxKind.ModuleDeclaration)
export interface ModuleDeclaration extends DeclarationStatement {
name: Identifier | LiteralExpression;
body: ModuleBlock | ModuleDeclaration;
body?: ModuleBlock | ModuleDeclaration;
}
// @kind(SyntaxKind.ModuleBlock)
+5
View File
@@ -372,6 +372,11 @@ namespace ts {
((<ModuleDeclaration>node).name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(<ModuleDeclaration>node));
}
export function isShorthandAmbientModule(node: Node): boolean {
// The only kind of module that can be missing a body is a shorthand ambient module.
return node.kind === SyntaxKind.ModuleDeclaration && (!(<ModuleDeclaration>node).body);
}
export function isBlockScopedContainerTopLevel(node: Node): boolean {
return node.kind === SyntaxKind.SourceFile ||
node.kind === SyntaxKind.ModuleDeclaration ||
+7 -3
View File
@@ -188,7 +188,10 @@ namespace ts.NavigationBar {
case SyntaxKind.ModuleDeclaration:
let moduleDeclaration = <ModuleDeclaration>node;
topLevelNodes.push(node);
addTopLevelNodes((<Block>getInnermostModule(moduleDeclaration).body).statements, topLevelNodes);
const inner = getInnermostModule(moduleDeclaration);
if (inner.body) {
addTopLevelNodes((<Block>inner.body).statements, topLevelNodes);
}
break;
case SyntaxKind.FunctionDeclaration:
@@ -453,7 +456,8 @@ namespace ts.NavigationBar {
function createModuleItem(node: ModuleDeclaration): NavigationBarItem {
const moduleName = getModuleName(node);
const childItems = getItemsWorker(getChildNodes((<Block>getInnermostModule(node).body).statements), createChildItem);
const body = <Block>getInnermostModule(node).body;
const childItems = body ? getItemsWorker(getChildNodes(body.statements), createChildItem) : [];
return getNavigationBarItem(moduleName,
ts.ScriptElementKind.moduleElement,
@@ -611,7 +615,7 @@ namespace ts.NavigationBar {
}
function getInnermostModule(node: ModuleDeclaration): ModuleDeclaration {
while (node.body.kind === SyntaxKind.ModuleDeclaration) {
while (node.body && node.body.kind === SyntaxKind.ModuleDeclaration) {
node = <ModuleDeclaration>node.body;
}
+1 -1
View File
@@ -414,7 +414,7 @@ namespace ts {
}
// If this is left side of dotted module declaration, there is no doc comments associated with this node
if (declaration.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>declaration).body.kind === SyntaxKind.ModuleDeclaration) {
if (declaration.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>declaration).body && (<ModuleDeclaration>declaration).body.kind === SyntaxKind.ModuleDeclaration) {
return;
}
@@ -0,0 +1,22 @@
//// [tests/cases/conformance/ambient/ambientShorthand.ts] ////
//// [declarations.d.ts]
declare module "jquery"
// Semicolon is optional
declare module "fs";
//// [user.ts]
///<reference path="declarations.d.ts"/>
import foo, {bar} from "jquery";
import * as baz from "fs";
import boom = require("jquery");
foo(bar, baz, boom);
//// [user.js]
"use strict";
///<reference path="declarations.d.ts"/>
var jquery_1 = require("jquery");
var baz = require("fs");
var boom = require("jquery");
jquery_1["default"](jquery_1.bar, baz, boom);
@@ -0,0 +1,24 @@
=== tests/cases/conformance/ambient/user.ts ===
///<reference path="declarations.d.ts"/>
import foo, {bar} from "jquery";
>foo : Symbol(foo, Decl(user.ts, 1, 6))
>bar : Symbol(bar, Decl(user.ts, 1, 13))
import * as baz from "fs";
>baz : Symbol(baz, Decl(user.ts, 2, 6))
import boom = require("jquery");
>boom : Symbol(boom, Decl(user.ts, 2, 26))
foo(bar, baz, boom);
>foo : Symbol(foo, Decl(user.ts, 1, 6))
>bar : Symbol(bar, Decl(user.ts, 1, 13))
>baz : Symbol(baz, Decl(user.ts, 2, 6))
>boom : Symbol(boom, Decl(user.ts, 2, 26))
=== tests/cases/conformance/ambient/declarations.d.ts ===
declare module "jquery"
No type information for this code.// Semicolon is optional
No type information for this code.declare module "fs";
No type information for this code.
No type information for this code.
@@ -0,0 +1,25 @@
=== tests/cases/conformance/ambient/user.ts ===
///<reference path="declarations.d.ts"/>
import foo, {bar} from "jquery";
>foo : any
>bar : any
import * as baz from "fs";
>baz : any
import boom = require("jquery");
>boom : any
foo(bar, baz, boom);
>foo(bar, baz, boom) : any
>foo : any
>bar : any
>baz : any
>boom : any
=== tests/cases/conformance/ambient/declarations.d.ts ===
declare module "jquery"
No type information for this code.// Semicolon is optional
No type information for this code.declare module "fs";
No type information for this code.
No type information for this code.
@@ -0,0 +1,9 @@
//// [ambientShorthand_declarationEmit.ts]
declare module "foo";
//// [ambientShorthand_declarationEmit.js]
//// [ambientShorthand_declarationEmit.d.ts]
declare module "foo";
@@ -0,0 +1,4 @@
=== tests/cases/conformance/ambient/ambientShorthand_declarationEmit.ts ===
declare module "foo";
No type information for this code.
No type information for this code.
@@ -0,0 +1,4 @@
=== tests/cases/conformance/ambient/ambientShorthand_declarationEmit.ts ===
declare module "foo";
No type information for this code.
No type information for this code.
@@ -0,0 +1,16 @@
//// [tests/cases/conformance/ambient/ambientShorthand_duplicate.ts] ////
//// [declarations1.d.ts]
declare module "foo";
//// [declarations2.d.ts]
declare module "foo";
//// [user.ts]
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo from "foo";
//// [user.js]
"use strict";
@@ -0,0 +1,10 @@
=== tests/cases/conformance/ambient/user.ts ===
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo from "foo";
>foo : Symbol(foo, Decl(user.ts, 2, 6))
=== tests/cases/conformance/ambient/declarations1.d.ts ===
declare module "foo";
No type information for this code.
No type information for this code.
@@ -0,0 +1,10 @@
=== tests/cases/conformance/ambient/user.ts ===
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo from "foo";
>foo : any
=== tests/cases/conformance/ambient/declarations1.d.ts ===
declare module "foo";
No type information for this code.
No type information for this code.
@@ -0,0 +1,8 @@
tests/cases/conformance/ambient/ambientShorthand_isImplicitAny.ts(1,16): error TS7005: Variable '"jquery"' implicitly has an 'any' type.
==== tests/cases/conformance/ambient/ambientShorthand_isImplicitAny.ts (1 errors) ====
declare module "jquery";
~~~~~~~~
!!! error TS7005: Variable '"jquery"' implicitly has an 'any' type.
@@ -0,0 +1,5 @@
//// [ambientShorthand_isImplicitAny.ts]
declare module "jquery";
//// [ambientShorthand_isImplicitAny.js]
@@ -0,0 +1,18 @@
//// [tests/cases/conformance/ambient/ambientShorthand_merging.ts] ////
//// [declarations1.d.ts]
declare module "foo";
//// [declarations2.d.ts]
declare module "foo" {
export const bar: number;
}
//// [user.ts]
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo, {bar} from "foo";
//// [user.js]
"use strict";
@@ -0,0 +1,11 @@
=== tests/cases/conformance/ambient/user.ts ===
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo, {bar} from "foo";
>foo : Symbol(foo, Decl(user.ts, 2, 6))
>bar : Symbol(bar, Decl(user.ts, 2, 13))
=== tests/cases/conformance/ambient/declarations1.d.ts ===
declare module "foo";
No type information for this code.
No type information for this code.
@@ -0,0 +1,11 @@
=== tests/cases/conformance/ambient/user.ts ===
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo, {bar} from "foo";
>foo : any
>bar : any
=== tests/cases/conformance/ambient/declarations1.d.ts ===
declare module "foo";
No type information for this code.
No type information for this code.
@@ -0,0 +1,30 @@
//// [tests/cases/conformance/ambient/ambientShorthand_reExport.ts] ////
//// [declarations.d.ts]
declare module "jquery";
//// [reExportX.ts]
export {x} from "jquery";
//// [reExportAll.ts]
export * from "jquery";
//// [reExportUser.ts]
import {x} from "./reExportX";
import * as $ from "./reExportAll";
// '$' is not callable, it is an object.
x($);
//// [reExportX.js]
"use strict";
var jquery_1 = require("jquery");
exports.x = jquery_1.x;
//// [reExportAll.js]
"use strict";
//// [reExportUser.js]
"use strict";
var reExportX_1 = require("./reExportX");
var $ = require("./reExportAll");
// '$' is not callable, it is an object.
reExportX_1.x($);
@@ -0,0 +1,22 @@
=== tests/cases/conformance/ambient/declarations.d.ts ===
declare module "jquery";
No type information for this code.
No type information for this code.=== tests/cases/conformance/ambient/reExportX.ts ===
export {x} from "jquery";
>x : Symbol(x, Decl(reExportX.ts, 0, 8))
=== tests/cases/conformance/ambient/reExportAll.ts ===
export * from "jquery";
No type information for this code.
No type information for this code.=== tests/cases/conformance/ambient/reExportUser.ts ===
import {x} from "./reExportX";
>x : Symbol(x, Decl(reExportUser.ts, 0, 8))
import * as $ from "./reExportAll";
>$ : Symbol($, Decl(reExportUser.ts, 1, 6))
// '$' is not callable, it is an object.
x($);
>x : Symbol(x, Decl(reExportUser.ts, 0, 8))
>$ : Symbol($, Decl(reExportUser.ts, 1, 6))
@@ -0,0 +1,23 @@
=== tests/cases/conformance/ambient/declarations.d.ts ===
declare module "jquery";
No type information for this code.
No type information for this code.=== tests/cases/conformance/ambient/reExportX.ts ===
export {x} from "jquery";
>x : any
=== tests/cases/conformance/ambient/reExportAll.ts ===
export * from "jquery";
No type information for this code.
No type information for this code.=== tests/cases/conformance/ambient/reExportUser.ts ===
import {x} from "./reExportX";
>x : any
import * as $ from "./reExportAll";
>$ : typeof $
// '$' is not callable, it is an object.
x($);
>x($) : any
>x : any
>$ : typeof $
@@ -0,0 +1,11 @@
// @Filename: declarations.d.ts
declare module "jquery"
// Semicolon is optional
declare module "fs";
// @Filename: user.ts
///<reference path="declarations.d.ts"/>
import foo, {bar} from "jquery";
import * as baz from "fs";
import boom = require("jquery");
foo(bar, baz, boom);
@@ -0,0 +1,2 @@
// @declaration: true
declare module "foo";
@@ -0,0 +1,10 @@
// @Filename: declarations1.d.ts
declare module "foo";
// @Filename: declarations2.d.ts
declare module "foo";
// @Filename: user.ts
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo from "foo";
@@ -0,0 +1,2 @@
// @noImplicitAny: true
declare module "jquery";
@@ -0,0 +1,12 @@
// @Filename: declarations1.d.ts
declare module "foo";
// @Filename: declarations2.d.ts
declare module "foo" {
export const bar: number;
}
// @Filename: user.ts
///<reference path="declarations1.d.ts" />
///<reference path="declarations1.d.ts" />
import foo, {bar} from "foo";
@@ -0,0 +1,14 @@
// @Filename: declarations.d.ts
declare module "jquery";
// @Filename: reExportX.ts
export {x} from "jquery";
// @Filename: reExportAll.ts
export * from "jquery";
// @Filename: reExportUser.ts
import {x} from "./reExportX";
import * as $ from "./reExportAll";
// '$' is not callable, it is an object.
x($);
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />
// @Filename: declarations.d.ts
////declare module "jquery";
// @Filename: user.ts
////import {[|x|]} from "jquery";
// @Filename: user2.ts
////import {[|x|]} from "jquery";
let ranges = test.ranges();
for (let range of ranges) {
goTo.file(range.fileName);
goTo.position(range.start);
verify.referencesCountIs(ranges.length);
for (let expectedRange of ranges) {
verify.referencesAtPositionContains(expectedRange);
}
}
@@ -0,0 +1,39 @@
/// <reference path='fourslash.ts' />
// @Filename: declarations.d.ts
/////*module*/declare module "jquery"
// @Filename: user.ts
///////<reference path="declarations.d.ts"/>
////import /*importFoo*/foo, {bar} from "jquery";
////import /*importBaz*/* as /*idBaz*/baz from "jquery";
/////*importBang*/import /*idBang*/bang = require("jquery");
////foo/*useFoo*/(bar/*useBar*/, baz/*useBaz*/, bang/*useBang*/);
goTo.marker("useFoo");
verify.quickInfoIs("import foo");
goTo.definition();
verify.caretAtMarker("importFoo");
goTo.definition();
verify.caretAtMarker("module");
goTo.marker("useBar");
verify.quickInfoIs("import bar");
goTo.definition();
verify.caretAtMarker("module");
goTo.marker("useBaz");
verify.quickInfoIs("import baz");
goTo.definition();
verify.caretAtMarker("importBaz");
goTo.marker("idBaz");
goTo.definition();
verify.caretAtMarker("module");
goTo.marker("useBang");
verify.quickInfoIs("import bang = require(\"jquery\")");
goTo.definition();
verify.caretAtMarker("importBang");
goTo.marker("idBang");
goTo.definition();
verify.caretAtMarker("module");