forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes denoland#1432
- Loading branch information
Showing
9 changed files
with
162 additions
and
20 deletions.
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 @@ | ||
export const bar: 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 bar = "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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// @deno-types="./foo.d.ts" | ||
import * as foo from "./foo.js"; | ||
|
||
// @deno-types="./bar.d.ts" | ||
import * as bar from "./bar.js"; | ||
|
||
console.log(foo.foo, bar.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,10 @@ | ||
// @deno-types="https://unpkg.com/dayjs@1.8.15/index.d.ts" | ||
import dayjs from "https://unpkg.com/dayjs@1.8.15/esm/index.js"; | ||
|
||
const date = dayjs(); | ||
console.log(date.second()); | ||
|
||
// @deno-types="https://unpkg.com/@types/lodash@4.14.136/index.d.ts" | ||
import * as _ from "https://unpkg.com/lodash-es@4.17.15/lodash.js"; | ||
|
||
console.log(_.add(1, 2)); |