@@ -25,7 +25,7 @@ func TestFormat(t *testing.T) {
2525 }
2626}
2727
28- var importTests = []struct {
28+ var goImportTests = []struct {
2929 input string
3030 want string
3131}{
@@ -51,15 +51,15 @@ var a = make(map[string]abc.Imp
5151func f(q []foo.Temp) {}` },
5252}
5353
54- func TestExtractImports (t * testing.T ) {
55- for _ , tc := range importTests {
54+ func TestExtractGoImports (t * testing.T ) {
55+ for _ , tc := range goImportTests {
5656 if got := gen .ExtractGoImports (tc .input ); got != tc .want {
5757 t .Errorf ("ExtractImports(%q) = %q, want: %q" , tc .input , got , tc .want )
5858 }
5959 }
6060}
6161
62- func BenchmarkExtractImports (b * testing.B ) {
62+ func BenchmarkExtractGoImports (b * testing.B ) {
6363 const input = `package a
6464
6565func TestExtractImports(t *"testing"".T) {
@@ -74,3 +74,123 @@ func TestExtractImports(t *"testing"".T) {
7474 }
7575 b .SetBytes (int64 (len (input ))) // ~44 MB/s
7676}
77+
78+ var tsImportTests = []struct {
79+ name string
80+ input string
81+ want string
82+ }{
83+ {
84+ name : "single import" ,
85+ input : `
86+ const foo = "./foo".Bar;` ,
87+ want : `
88+ import {Bar} from "./foo";
89+
90+ const foo = Bar;` ,
91+ },
92+ {
93+ name : "skipping comments" ,
94+ input : `// abc
95+ // def
96+ const foo = "./foo".Bar;` ,
97+ want : `// abc
98+ // def
99+ import {Bar} from "./foo";
100+
101+ const foo = Bar;` ,
102+ },
103+ {
104+ name : "pre-existing imports" ,
105+ input : `// abc
106+ // def
107+ import {Baz} from "./foo2";
108+ const foo = "./foo".Bar;` ,
109+ want : `// abc
110+ // def
111+ import {Bar} from "./foo";
112+ import {Baz} from "./foo2";
113+ const foo = Bar;` ,
114+ },
115+ {
116+ name : "multiple imports from same module" ,
117+ input : `const x = "./foo".Bar;
118+ const y = "./foo".Baz;` ,
119+ want : `import {Bar, Baz} from "./foo";
120+
121+ const x = Bar;
122+ const y = Baz;` ,
123+ },
124+ {
125+ name : "imports from different modules" ,
126+ input : `const a = "./foo".Bar;
127+ const b = "./bar".Qux;` ,
128+ want : `import {Qux} from "./bar";
129+ import {Bar} from "./foo";
130+
131+ const a = Bar;
132+ const b = Qux;` ,
133+ },
134+ {
135+ name : "nested module paths" ,
136+ input : `const a = "./deep/nested/path".Component;` ,
137+ want : `import {Component} from "./deep/nested/path";
138+
139+ const a = Component;` ,
140+ },
141+ {
142+ name : "no imports" ,
143+ input : `const a = 5; function test() { return true; }` ,
144+ want : `const a = 5; function test() { return true; }` ,
145+ },
146+ {
147+ name : "function call with imported symbol" ,
148+ input : `function process() { return "./utils".formatData(data); }` ,
149+ want : `import {formatData} from "./utils";
150+
151+ function process() { return formatData(data); }` ,
152+ },
153+ {
154+ name : "import in a complex expression" ,
155+ input : `const handler = (event) => {
156+ "./events".EventBus.publish(new "./models".Event());
157+ };` ,
158+ want : `import {EventBus} from "./events";
159+ import {Event} from "./models";
160+
161+ const handler = (event) => {
162+ EventBus.publish(new Event());
163+ };` ,
164+ },
165+ }
166+
167+ func TestExtractTsImports (t * testing.T ) {
168+ for _ , tc := range tsImportTests {
169+ t .Run (tc .name , func (t * testing.T ) {
170+ got := gen .ExtractTsImports (tc .input )
171+ if got != tc .want {
172+ t .Errorf ("ExtractTsImports() =\n %s\n want:\n %s" , got , tc .want )
173+ }
174+ })
175+ }
176+ }
177+
178+ func BenchmarkExtractTsImports (b * testing.B ) {
179+ const input = `const Component = "./components".Component;
180+ const { useState, useEffect } = "./react".React;
181+ function App() {
182+ const [data, setData] = useState(null);
183+ const formatter = new "./utils".Formatter();
184+
185+ useEffect(() => {
186+ "./api".fetchData().then(setData);
187+ }, []);
188+
189+ return formatter.format(data);
190+ }`
191+
192+ for i := 0 ; i < b .N ; i ++ {
193+ gen .ExtractTsImports (input )
194+ }
195+ b .SetBytes (int64 (len (input )))
196+ }
0 commit comments