Skip to content

Commit 4a973b9

Browse files
committed
Add parser support for imports
1 parent b50d7b7 commit 4a973b9

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

samples/import.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Import variants from https://www.typescriptlang.org/docs/handbook/modules.html#import
2+
3+
import { ZipCodeValidator } from "./ZipCodeValidator";
4+
5+
import { ZipCodeValidator as ZCV } from "./ZipCodeValidator";
6+
7+
import * as validator from "./ZipCodeValidator";
8+
9+
import "./my-module.js";
10+
11+
declare const hello: String;
12+
13+
declare module mod {
14+
import { NestedImport } from "./SomeModule";
15+
16+
export function f(x: Number): String;
17+
}

samples/import.ts.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
import scala.scalajs.js
3+
import js.annotation._
4+
import js.|
5+
6+
package `import` {
7+
8+
package mod {
9+
10+
@js.native
11+
@JSGlobal("mod")
12+
object Mod extends js.Object {
13+
def f(x: Number): String = js.native
14+
}
15+
16+
}
17+
18+
@js.native
19+
@JSGlobalScope
20+
object Import extends js.Object {
21+
val hello: String = js.native
22+
}
23+
24+
}

src/main/scala/org/scalajs/tools/tsimporter/Importer.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class Importer(val output: java.io.PrintWriter) {
116116
case FunctionDecl(IdentName(name), signature) =>
117117
processDefDecl(owner, name, signature)
118118

119+
case ImportDecl => // Ignore imports
120+
119121
case _ =>
120122
owner.members += new CommentSymbol("??? "+declaration)
121123
}

src/main/scala/org/scalajs/tools/tsimporter/Trees.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ object Trees {
8686

8787
case class FunctionDecl(name: Ident, signature: FunSignature) extends DeclTree
8888

89+
case object ImportDecl extends DeclTree
90+
8991
// Function signature
9092

9193
case class FunSignature(tparams: List[TypeParam], params: List[FunParam],

src/main/scala/org/scalajs/tools/tsimporter/parser/TSDefParser.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
4545

4646
lexical.delimiters ++= List(
4747
"{", "}", "(", ")", "[", "]", "<", ">",
48-
".", ";", ",", "?", ":", "=", "|",
48+
".", ";", ",", "?", ":", "=", "|", "*",
4949
// TypeScript-specific
5050
"...", "=>"
5151
)
@@ -83,6 +83,7 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
8383
ambientModuleDecl | ambientVarDecl | ambientFunctionDecl
8484
| ambientEnumDecl | ambientClassDecl | ambientInterfaceDecl
8585
| ambientConstDecl | ambientLetDecl | typeAliasDecl
86+
| importDecl
8687
)
8788

8889
lazy val ambientVarDecl: Parser[DeclTree] =
@@ -112,6 +113,13 @@ class TSDefParser extends StdTokenParsers with ImplicitConversions {
112113
lazy val typeAliasDecl: Parser[DeclTree] =
113114
"type" ~> typeName ~ tparams ~ ("=" ~> typeDesc) <~ opt(";") ^^ TypeAliasDecl
114115

116+
lazy val importDecl: Parser[DeclTree] =
117+
"import" ~> (
118+
"{" ~ identifier ~ opt(lexical.Identifier("as") ~ identifier) ~ "}" ~ lexical.Identifier("from") ~ stringLiteral
119+
| "*" ~ lexical.Identifier("as") ~ identifier ~ lexical.Identifier("from") ~ stringLiteral
120+
| stringLiteral
121+
) <~ ";" ^^^ ImportDecl
122+
115123
lazy val tparams = (
116124
"<" ~> rep1sep(typeParam, ",") <~ ">"
117125
| success(Nil)

0 commit comments

Comments
 (0)