-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Support .d.ts files #2746
Merged
Merged
Support .d.ts files #2746
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
|
||
interface DirectiveInfo { | ||
path: string; | ||
start: number; | ||
end: number; | ||
} | ||
|
||
/** Remap the module name based on any supplied type directives passed. */ | ||
export function getMappedModuleName( | ||
moduleName: string, | ||
containingFile: string, | ||
typeDirectives?: Record<string, string> | ||
): string { | ||
if (containingFile.endsWith(".d.ts") && !moduleName.endsWith(".d.ts")) { | ||
moduleName = `${moduleName}.d.ts`; | ||
} | ||
if (!typeDirectives) { | ||
return moduleName; | ||
} | ||
if (moduleName in typeDirectives) { | ||
return typeDirectives[moduleName]; | ||
} | ||
return moduleName; | ||
} | ||
|
||
/** Matches directives that look something like this and parses out the value | ||
* of the directive: | ||
* | ||
* // @deno-types="./foo.d.ts" | ||
* | ||
* [See Diagram](http://bit.ly/31nZPCF) | ||
*/ | ||
const typeDirectiveRegEx = /@deno-types\s*=\s*(["'])((?:(?=(\\?))\3.)*?)\1/gi; | ||
|
||
/** Matches `import` or `export from` statements and parses out the value of the | ||
* module specifier in the second capture group: | ||
* | ||
* import * as foo from "./foo.js" | ||
* export { a, b, c } from "./bar.js" | ||
* | ||
* [See Diagram](http://bit.ly/2GSkJlF) | ||
*/ | ||
const importExportRegEx = /(?:import|export)\s+[\s\S]*?from\s+(["'])((?:(?=(\\?))\3.)*?)\1/; | ||
|
||
/** Parses out any Deno type directives that are part of the source code, or | ||
* returns `undefined` if there are not any. | ||
*/ | ||
export function parseTypeDirectives( | ||
sourceCode: string | undefined | ||
): Record<string, string> | undefined { | ||
if (!sourceCode) { | ||
return; | ||
} | ||
|
||
// collect all the directives in the file and their start and end positions | ||
const directives: DirectiveInfo[] = []; | ||
let maybeMatch: RegExpExecArray | null = null; | ||
while ((maybeMatch = typeDirectiveRegEx.exec(sourceCode))) { | ||
const [matchString, , path] = maybeMatch; | ||
const { index: start } = maybeMatch; | ||
directives.push({ | ||
path, | ||
start, | ||
end: start + matchString.length | ||
}); | ||
} | ||
if (!directives.length) { | ||
return; | ||
} | ||
|
||
// work from the last directive backwards for the next `import`/`export` | ||
// statement | ||
directives.reverse(); | ||
const directiveRecords: Record<string, string> = {}; | ||
for (const { path, start, end } of directives) { | ||
const searchString = sourceCode.substring(end); | ||
const maybeMatch = importExportRegEx.exec(searchString); | ||
if (maybeMatch) { | ||
const [, , fromPath] = maybeMatch; | ||
directiveRecords[fromPath] = path; | ||
} | ||
sourceCode = sourceCode.substring(0, start); | ||
} | ||
|
||
return directiveRecords; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
args: run --reload tests/error_type_definitions.ts | ||
check_stderr: true | ||
exit_code: 1 | ||
output: tests/error_type_definitions.ts.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// @deno-types="./type_definitions/bar.d.ts" | ||
import { Bar } from "./type_definitions/bar.js"; | ||
|
||
const bar = new Bar(); | ||
console.log(bar); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[WILDCARD]error: Uncaught TypeError: Automatic type resolution not supported | ||
[WILDCARD]js/compiler.ts:[WILDCARD] | ||
at fileExists (js/compiler.ts:[WILDCARD]) | ||
[WILDCARD] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
args: run --reload tests/type_definitions.ts | ||
output: tests/type_definitions.ts.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @deno-types="./type_definitions/foo.d.ts" | ||
import { foo } from "./type_definitions/foo.js"; | ||
|
||
console.log(foo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[WILDCARD]foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/// <reference types="baz" /> | ||
|
||
declare namespace bar { | ||
export class Bar { | ||
baz: string; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/** An exported value. */ | ||
export const foo: string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const foo = "foo"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could maybe intercept and remove a triple slash reference...
I'm still not sure about this syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we used something that conflicted with TypeScript, we could remove it for Deno, but we would want it to be a noop everywhere else, otherwise we would have Deno only TypeScript.
Even for Deno, if it overlapped TypeScript syntax, we would need a way to disambiguate it, which I don't think would be easy, so we could use a triple-slash directive, but it would have to be something TypeScript currently ignores, so the following would be out:
So something like this "could" work and would be terse enough:
/// <reference deno="..." />
The biggest "challenge" I see is that these are like include statements, and they effect the file they are in. What we need to support in Deno is something where certain statements are impacted, so they are external and their position matters. Because there is such a dramatic behaviour change, it maybe confusing.