From a4e1d7d2e750831a1891b9267c8585a3e90083af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 11 Sep 2019 12:47:34 +0200 Subject: [PATCH] fix: type directives import (#2910) --- js/type_directives.ts | 8 ++++---- tests/type_definitions.ts | 2 ++ tests/type_definitions/fizz.d.ts | 2 ++ tests/type_definitions/fizz.js | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 tests/type_definitions/fizz.d.ts create mode 100644 tests/type_definitions/fizz.js diff --git a/js/type_directives.ts b/js/type_directives.ts index 3e903a80b5..4817738c79 100644 --- a/js/type_directives.ts +++ b/js/type_directives.ts @@ -1,5 +1,4 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - interface DirectiveInfo { path: string; start: number; @@ -33,15 +32,16 @@ export function getMappedModuleName( */ const typeDirectiveRegEx = /@deno-types\s*=\s*(["'])((?:(?=(\\?))\3.)*?)\1/gi; -/** Matches `import` or `export from` statements and parses out the value of the +/** Matches `import`, `import from` or `export from` statements and parses out the value of the * module specifier in the second capture group: * + * import "./foo.js" * import * as foo from "./foo.js" * export { a, b, c } from "./bar.js" * - * [See Diagram](http://bit.ly/2GSkJlF) + * [See Diagram](https://bit.ly/2lK0izL) */ -const importExportRegEx = /(?:import|export)\s+[\s\S]*?from\s+(["'])((?:(?=(\\?))\3.)*?)\1/; +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. diff --git a/tests/type_definitions.ts b/tests/type_definitions.ts index 3725011665..8e85df2b9d 100644 --- a/tests/type_definitions.ts +++ b/tests/type_definitions.ts @@ -1,4 +1,6 @@ // @deno-types="./type_definitions/foo.d.ts" import { foo } from "./type_definitions/foo.js"; +// @deno-types="./type_definitions/fizz.d.ts" +import "./type_definitions/fizz.js"; console.log(foo); diff --git a/tests/type_definitions/fizz.d.ts b/tests/type_definitions/fizz.d.ts new file mode 100644 index 0000000000..4b087efb7a --- /dev/null +++ b/tests/type_definitions/fizz.d.ts @@ -0,0 +1,2 @@ +/** An exported value. */ +export const fizz: string; diff --git a/tests/type_definitions/fizz.js b/tests/type_definitions/fizz.js new file mode 100644 index 0000000000..5141a4ddf9 --- /dev/null +++ b/tests/type_definitions/fizz.js @@ -0,0 +1 @@ +export const fizz = "fizz";