Skip to content

Commit ce1982d

Browse files
authored
Merge pull request #43 from sbrunk/ignore-imports
Add parser support for imports
2 parents b50d7b7 + fd82e9c commit ce1982d

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-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 Single from "module.js";
4+
import { Single } from "module.js";
5+
import { Single as Renamed } from "module.js";
6+
import { First, Second } from "module.js";
7+
import { First as F, Second } from "module.js";
8+
import * as validator from "module.js";
9+
import "./my-module.js";
10+
11+
declare const hello: String;
12+
13+
declare module mod {
14+
import { NestedImport } from "module.js";
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: 14 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,18 @@ 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" ~> opt(
118+
(
119+
identifier
120+
| "{" ~ importIdentifierSeq ~ "}"
121+
| "*" ~ lexical.Identifier("as") ~ identifier
122+
) ~ lexical.Identifier("from")
123+
) ~ stringLiteral <~ ";" ^^^ ImportDecl
124+
125+
lazy val importIdentifierSeq =
126+
rep1sep(identifier ~ opt(lexical.Identifier("as") ~ identifier), ",")
127+
115128
lazy val tparams = (
116129
"<" ~> rep1sep(typeParam, ",") <~ ">"
117130
| success(Nil)

0 commit comments

Comments
 (0)