Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package scopes #4913

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ecf7c1d
Some types - I'll get back to this
weswigham Sep 18, 2015
c232afc
trashy initial prototype - ironing out issues
weswigham Sep 19, 2015
b2cd788
Remove debug stuff, working package scopes
weswigham Sep 21, 2015
02166e2
merge with master
weswigham Sep 21, 2015
cb6e430
Merge branch 'master' into package-scopes
weswigham Oct 5, 2015
3bfd92a
Fix issues remaining form merge, lint
weswigham Oct 5, 2015
314cfc1
update tests
weswigham Oct 5, 2015
c89ef0b
another test, update unit tests to use equals, lint
weswigham Oct 6, 2015
ffb38da
another tests and lint
weswigham Oct 6, 2015
ab269f6
Merge branch 'master' into package-scopes
weswigham Oct 9, 2015
1e32030
Merge branch 'master' into package-scopes
weswigham Nov 10, 2015
ae158ad
cleaning up with changes form pr
weswigham Nov 10, 2015
3e58162
finish cleaning up pr lints
weswigham Nov 10, 2015
f86c28d
pull out export
weswigham Nov 10, 2015
4224586
Update with work items from PR
weswigham Nov 11, 2015
149a1d9
Merge branch 'master' into package-scopes
weswigham Nov 12, 2015
22c6377
Merge branch 'master' into package-scopes
weswigham Nov 13, 2015
312d97c
use relative paths for package names
weswigham Nov 13, 2015
b1ea98c
use ts.
weswigham Nov 13, 2015
d32cedb
move convertToRelativePath into core so the generateDiagnostics scrip…
weswigham Nov 14, 2015
340702e
Merge branch 'master' into package-scopes
weswigham Nov 17, 2015
41c21b5
fix lint
weswigham Nov 17, 2015
fd33df3
Merge branch 'master' into package-scopes
weswigham Nov 17, 2015
733db05
Merge branch 'master' into package-scopes
weswigham Nov 30, 2015
1163d38
accept baselines
weswigham Nov 30, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace ts {
const emitResolver = createResolver();

const undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined");
undefinedSymbol.declarations = [];
const argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments");

const checker: TypeChecker = {
Expand Down Expand Up @@ -516,6 +517,12 @@ namespace ts {
}
switch (location.kind) {
case SyntaxKind.SourceFile:
if ((<SourceFile>location).package) {
if (hasProperty((<SourceFile>location).package.symbols, name)) {
result = (<SourceFile>location).package.symbols[name];
break loop;
}
}
if (!isExternalOrCommonJsModule(<SourceFile>location)) break;
case SyntaxKind.ModuleDeclaration:
const moduleExports = getSymbolOfNode(location).exports;
Expand Down Expand Up @@ -1040,7 +1047,8 @@ namespace ts {

const isRelative = isExternalModuleNameRelative(moduleName);
if (!isRelative) {
const symbol = getSymbol(globals, "\"" + moduleName + "\"", SymbolFlags.ValueModule);
const file = getSourceFileOfNode(location);
const symbol = getSymbol(file.package ? file.package.symbols : globals, "\"" + moduleName + "\"", SymbolFlags.ValueModule);
if (symbol) {
return symbol;
}
Expand All @@ -1052,6 +1060,12 @@ namespace ts {
if (sourceFile.symbol) {
return sourceFile.symbol;
}
else if (sourceFile.package && !isRelative) {
const symbol = getSymbol(sourceFile.package.symbols, "\"" + moduleName + "\"", SymbolFlags.ValueModule);
if (symbol) {
return symbol;
}
}
error(moduleReferenceLiteral, Diagnostics.File_0_is_not_a_module, sourceFile.fileName);
return;
}
Expand Down Expand Up @@ -1248,6 +1262,11 @@ namespace ts {
}
switch (location.kind) {
case SyntaxKind.SourceFile:
if ((<SourceFile>location).package) {
if (result = callback((<SourceFile>location).package.symbols)) {
return result;
}
}
if (!isExternalOrCommonJsModule(<SourceFile>location)) {
break;
}
Expand Down Expand Up @@ -14478,6 +14497,9 @@ namespace ts {

switch (location.kind) {
case SyntaxKind.SourceFile:
if ((<SourceFile>location).package) {
copySymbols((<SourceFile>location).package.symbols, meaning);
}
if (!isExternalOrCommonJsModule(<SourceFile>location)) {
break;
}
Expand Down Expand Up @@ -15156,6 +15178,10 @@ namespace ts {
return hasProperty(globals, name);
}

function hasPackageInternalName(file: SourceFile, name: string): boolean {
return file.package && hasProperty(file.package.symbols, name);
}

function getReferencedValueSymbol(reference: Identifier): Symbol {
return getNodeLinks(reference).resolvedSymbol ||
resolveName(reference, reference.text, SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias,
Expand Down Expand Up @@ -15190,6 +15216,7 @@ namespace ts {
isNestedRedeclaration,
isValueAliasDeclaration,
hasGlobalName,
hasPackageInternalName,
isReferencedAliasDeclaration,
getNodeCheckFlags,
isTopLevelValueImportEqualsWithEntityName,
Expand Down Expand Up @@ -15225,10 +15252,25 @@ namespace ts {
bindSourceFile(file, compilerOptions);
});

// Initialize global symbol table
const packages: Map<PackageDescriptor> = {};

// Initialize package/global symbol table(s)
forEach(host.getSourceFiles(), file => {
if (!isExternalOrCommonJsModule(file)) {
mergeSymbolTable(globals, file.locals);
if (file.package) {
// Dedupe/merge packages
const id = file.package.packagePath;
if (!packages[id]) {
packages[id] = file.package;
}
else {
file.package = packages[id];
}
mergeSymbolTable(file.package.symbols, file.locals);
}
else {
mergeSymbolTable(globals, file.locals);
}
}
});

Expand Down Expand Up @@ -15283,6 +15325,11 @@ namespace ts {
}

anyArrayType = createArrayType(anyType);

// Once all packages are merged and global scope is setup, merge global scope into each package
forEachValue(packages, package => {
mergeSymbolTable(package.symbols, globals);
});
}

function createInstantiatedPromiseLikeType(): ObjectType {
Expand Down
21 changes: 21 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ namespace ts {
return <Path>getCanonicalFileName(nonCanonicalizedPath);
}

export function convertToRelativePath(absoluteOrRelativePath: string, basePath: string, getCanonicalFileName: (path: string) => string): string {
return !isRootedDiskPath(absoluteOrRelativePath)
? absoluteOrRelativePath
: getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
}

export const enum Comparison {
LessThan = -1,
EqualTo = 0,
Expand Down Expand Up @@ -624,6 +630,21 @@ namespace ts {
}
}

/**
* Given a path to a file within a module, returns a path uniqely identifying that module
*/
export function moduleFilePathToIdentifyingPath(path: Path, currentDirectory: string, getCanonicalFileName: (filename: string) => string): string {
const components = normalizedPathComponents(normalizeSlashes(path), getRootLength(path));
let part: string;
let lastPart: string;
while (lastPart = part, part = components.pop()) {
if (part === "node_modules") {
return convertToRelativePath(getNormalizedPathFromPathComponents([...components, lastPart]), currentDirectory, getCanonicalFileName);
}
}
return convertToRelativePath(path, currentDirectory, getCanonicalFileName);
}

function getNormalizedPathComponentsOfUrl(url: string) {
// Get root length of http://www.website.com/folder1/foler2/
// In this example the root is: http://www.website.com/
Expand Down
7 changes: 5 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@
"category": "Error",
"code": 2653
},
"Exported external package typings file cannot contain tripleslash references. Please contact the package author to update the package definition.": {
"Exported external package typings file cannot contain script file tripleslash references. Please contact the package author to update the package definition.": {
"category": "Error",
"code": 2654
},
Expand Down Expand Up @@ -2277,7 +2277,10 @@
"category": "Error",
"code": 6063
},

"File '{0}' was referenced by a package '{1}' but is already included by package '{2}'.": {
"category": "Error",
"code": 6064
},
"Enables experimental support for ES7 decorators.": {
"category": "Message",
"code": 6065
Expand Down
1 change: 1 addition & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi

function isUniqueName(name: string): boolean {
return !resolver.hasGlobalName(name) &&
!resolver.hasPackageInternalName(currentSourceFile, name) &&
!hasProperty(currentFileIdentifiers, name) &&
!hasProperty(generatedNameSet, name);
}
Expand Down
Loading