Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Merge branch 'master' into libReferenceDirective
  • Loading branch information
rbuckton committed May 30, 2017
commit 47ed6ccc39987402bda9fe1cd40ac6f72830d9d6
3 changes: 2 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace ts {
"es2017.object": "lib.es2017.object.d.ts",
"es2017.sharedmemory": "lib.es2017.sharedmemory.d.ts",
"es2017.string": "lib.es2017.string.d.ts",
"es2017.intl": "lib.es2017.intl.d.ts",
"esnext.asynciterable": "lib.esnext.asynciterable.d.ts",
});

Expand Down Expand Up @@ -1752,4 +1753,4 @@ namespace ts {
});
}
}
}
}
41 changes: 13 additions & 28 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,37 +1468,22 @@ namespace ts {
}
}

function processSourceFile(fileName: string, isDefaultLib: boolean, ignoreNoDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number) {
let diagnosticArgument: string[];
let diagnostic: DiagnosticMessage;
/** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */
function getSourceFileFromReference(referencingFile: SourceFile, ref: FileReference): SourceFile | undefined {
return getSourceFileFromReferenceWorker(resolveTripleslashReference(ref.fileName, referencingFile.fileName), fileName => filesByName.get(toPath(fileName, currentDirectory, getCanonicalFileName)));
}

function getSourceFileFromReferenceWorker(
fileName: string,
getSourceFile: (fileName: string) => SourceFile | undefined,
fail?: (diagnostic: DiagnosticMessage, ...argument: string[]) => void,
refFile?: SourceFile): SourceFile | undefined {

if (hasExtension(fileName)) {
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
if (fail) fail(Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1, fileName, "'" + supportedExtensions.join("', '") + "'");
return undefined;
}
else if (!findSourceFile(fileName, toPath(fileName, currentDirectory, getCanonicalFileName), isDefaultLib, ignoreNoDefaultLib, refFile, refPos, refEnd)) {
diagnostic = Diagnostics.File_0_not_found;
diagnosticArgument = [fileName];
}
else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) {
diagnostic = Diagnostics.A_file_cannot_have_a_reference_to_itself;
diagnosticArgument = [fileName];
}
}
else {
const nonTsFile: SourceFile = options.allowNonTsExtensions && findSourceFile(fileName, toPath(fileName, currentDirectory, getCanonicalFileName), isDefaultLib, ignoreNoDefaultLib, refFile, refPos, refEnd);
if (!nonTsFile) {
if (options.allowNonTsExtensions) {
diagnostic = Diagnostics.File_0_not_found;
diagnosticArgument = [fileName];
}
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, toPath(fileName + extension, currentDirectory, getCanonicalFileName), isDefaultLib, ignoreNoDefaultLib, refFile, refPos, refEnd))) {
diagnostic = Diagnostics.File_0_not_found;
fileName += ".ts";
diagnosticArgument = [fileName];
}
}
}

const sourceFile = getSourceFile(fileName);
if (fail) {
Expand Down Expand Up @@ -1526,9 +1511,9 @@ namespace ts {
}

/** This has side effects through `findSourceFile`. */
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number): void {
function processSourceFile(fileName: string, isDefaultLib: boolean, ignoreNoDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number): void {
getSourceFileFromReferenceWorker(fileName,
fileName => findSourceFile(fileName, toPath(fileName, currentDirectory, getCanonicalFileName), isDefaultLib, refFile, refPos, refEnd),
fileName => findSourceFile(fileName, toPath(fileName, currentDirectory, getCanonicalFileName), isDefaultLib, ignoreNoDefaultLib, refFile, refPos, refEnd),
(diagnostic, ...args) => {
fileProcessingDiagnostics.add(refFile !== undefined && refEnd !== undefined && refPos !== undefined
? createFileDiagnostic(refFile, refPos, refEnd - refPos, diagnostic, ...args)
Expand Down
32 changes: 15 additions & 17 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace ts {

export type ReferencePathMatchResult = NoDefaultLibDirective | ReferenceDirective | ReferenceDirectiveError;

export function getDeclarationOfKind(symbol: Symbol, kind: SyntaxKind): Declaration {
export function getDeclarationOfKind<T extends Declaration>(symbol: Symbol, kind: T["kind"]): T {
const declarations = symbol.declarations;
if (declarations) {
for (const declaration of declarations) {
Expand Down Expand Up @@ -1564,6 +1564,10 @@ namespace ts {
}

export function getJSDocs(node: Node): (JSDoc | JSDocTag)[] {
if (isJSDocTypedefTag(node)) {
return [node.parent];
}

let cache: (JSDoc | JSDocTag)[] = node.jsDocCache;
if (!cache) {
getJSDocsWorker(node);
Expand Down Expand Up @@ -1929,11 +1933,12 @@ namespace ts {
*
* | Group | Purpose |
* |:-----:|:--------------------------------------------------------------------------------|
* | 1 | Attribute name ('no-default-lib', 'path', 'types', or 'lib') |
* | 2 | Quote character (used by a backreference to balance the quoted attribute value) |
* | 3 | Attribute value |
* | 1 | Leading characters of the directive |
* | 2 | Attribute name ('no-default-lib', 'path', 'types', or 'lib') |
* | 3 | Quote character (used by a backreference to balance the quoted attribute value) |
* | 4 | Attribute value |
*/
export const fullTripleSlashReferenceRegEx = /^\/\/\/\s*<reference\s+(no-default-lib|path|types|lib)\s*=\s*('|")(.+?)\2.*?\/>/i;
export const fullTripleSlashReferenceRegEx = /^(\/\/\/\s*<reference\s+(no-default-lib|path|types|lib)\s*=\s*)('|")(.+?)\3.*?\/>/i;

const fullTripleSlashReferencePathOrAmdDependencyPathRegEx = /^\/\/\/\s*<(reference|amd-dependency)\s+path\s*=\s*('|")(.+?)\2.*?\/>/;

Expand All @@ -1952,21 +1957,14 @@ namespace ts {
kind: "error"
};
}
const kind = match[1] as "no-default-lib" | "path" | "types" | "lib";
const kind = match[2] as "no-default-lib" | "path" | "types" | "lib";
if (kind === "no-default-lib") {
return { kind };
}
const value = match[3];
const start = commentRange.pos;
const end = commentRange.end;
return {
fileReference: {
pos: start,
end: end,
fileName: value
},
kind
};
const fileName = match[4];
const pos = commentRange.pos + match[1].length + 1;
const end = pos + fileName.length;
return { fileReference: { pos, end, fileName }, kind };
}

export function isKeyword(token: SyntaxKind): boolean {
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/libReferenceDirective.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [libReferenceDirective.2.ts]
/// <reference lib="dom" />
Object.create(null);


//// [libReferenceDirective.2.js]
/// <reference lib="dom" />
Object.create(null);
7 changes: 7 additions & 0 deletions tests/baselines/reference/libReferenceDirective.2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/libReferenceDirective.2.ts ===
/// <reference lib="dom" />
Object.create(null);
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

9 changes: 9 additions & 0 deletions tests/baselines/reference/libReferenceDirective.2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/libReferenceDirective.2.ts ===
/// <reference lib="dom" />
Object.create(null);
>Object.create(null) : any
>Object.create : { (o: object): any; (o: object, properties: PropertyDescriptorMap & ThisType<any>): any; }
>Object : ObjectConstructor
>create : { (o: object): any; (o: object, properties: PropertyDescriptorMap & ThisType<any>): any; }
>null : null

12 changes: 6 additions & 6 deletions tests/baselines/reference/libReferenceDirectiveErrors.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/compiler/a.ts(1,1): error TS1084: Invalid 'reference' directive syntax.
tests/cases/compiler/b.ts(1,1): error TS1084: Invalid 'reference' directive syntax.
tests/cases/compiler/c.ts(1,1): error TS2711: Cannot find lib definition for 'es2015.foo'.
tests/cases/compiler/d.ts(1,1): error TS2712: Cannot find lib definition for 'es2015.collections'. Did you mean 'es2015.collection'?
tests/cases/compiler/e.ts(1,1): error TS2712: Cannot find lib definition for 'lib.es2015.d.ts'. Did you mean 'es2015'?
tests/cases/compiler/c.ts(1,21): error TS2711: Cannot find lib definition for 'es2015.foo'.
tests/cases/compiler/d.ts(1,21): error TS2712: Cannot find lib definition for 'es2015.collections'. Did you mean 'es2015.collection'?
tests/cases/compiler/e.ts(1,21): error TS2712: Cannot find lib definition for 'lib.es2015.d.ts'. Did you mean 'es2015'?


==== tests/cases/compiler/a.ts (1 errors) ====
Expand All @@ -15,13 +15,13 @@ tests/cases/compiler/e.ts(1,1): error TS2712: Cannot find lib definition for 'li
!!! error TS1084: Invalid 'reference' directive syntax.
==== tests/cases/compiler/c.ts (1 errors) ====
/// <reference lib="es2015.foo" />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
!!! error TS2711: Cannot find lib definition for 'es2015.foo'.
==== tests/cases/compiler/d.ts (1 errors) ====
/// <reference lib="es2015.collections" />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~
!!! error TS2712: Cannot find lib definition for 'es2015.collections'. Did you mean 'es2015.collection'?
==== tests/cases/compiler/e.ts (1 errors) ====
/// <reference lib="lib.es2015.d.ts" />
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~
!!! error TS2712: Cannot find lib definition for 'lib.es2015.d.ts'. Did you mean 'es2015'?
3 changes: 3 additions & 0 deletions tests/cases/compiler/libReferenceDirective.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @target: es5
/// <reference lib="dom" />
Object.create(null);
You are viewing a condensed version of this merge commit. You can view the full changes here.