Skip to content

Commit 8c08ae4

Browse files
committed
Always output “wrapped” (destructured) import or export member lists multiline, similar to the output for objects; update tests to correspond with this new expectation. Fixes multiline import/export
1 parent 10a6a4c commit 8c08ae4

File tree

2 files changed

+135
-62
lines changed

2 files changed

+135
-62
lines changed

src/nodes.coffee

+9-10
Original file line numberDiff line numberDiff line change
@@ -1277,21 +1277,20 @@ exports.ModuleList = class ModuleList extends Base
12771277
code
12781278

12791279
compileIdentifiers: (o, identifiers, wrapped) ->
1280-
code = []
12811280
compiledList = (identifier.compileToFragments o, LEVEL_LIST for identifier in identifiers)
1281+
code = []
1282+
1283+
code.push @makeCode("{\n#{o.indent}") if wrapped
12821284

12831285
for fragments, index in compiledList
1284-
code.push @makeCode(', ') if index
1286+
if index
1287+
if wrapped
1288+
code.push @makeCode(",\n#{o.indent}")
1289+
else
1290+
code.push @makeCode(', ')
12851291
code.push fragments...
12861292

1287-
if wrapped
1288-
if "\n" in fragmentsToText code
1289-
code.unshift @makeCode("{\n#{o.indent}")
1290-
code.push @makeCode("\n#{@tab}}")
1291-
else
1292-
code.unshift @makeCode("{ ")
1293-
code.push @makeCode(" }")
1294-
1293+
code.push @makeCode("\n}") if wrapped
12951294
code
12961295

12971296
exports.ModuleIdentifier = class ModuleIdentifier extends Base

test/modules.coffee

+126-52
Original file line numberDiff line numberDiff line change
@@ -52,96 +52,158 @@ test "import an entire module for side effects only, without importing any bindi
5252
eq toJS(input), output
5353

5454
test "import default member from module, adding the member to the current scope", ->
55-
input = "import foo from 'lib'\nfoo.fooMethod()"
56-
output = "import foo from 'lib';\n\nfoo.fooMethod();"
55+
input = """
56+
import foo from 'lib'
57+
foo.fooMethod()"""
58+
output = """
59+
import foo from 'lib';
60+
61+
foo.fooMethod();"""
5762
eq toJS(input), output
5863

5964
test "import an entire module's contents as an alias, adding the alias to the current scope", ->
60-
input = "import * as foo from 'lib'\nfoo.fooMethod()"
61-
output = "import * as foo from 'lib';\n\nfoo.fooMethod();"
65+
input = """
66+
import * as foo from 'lib'
67+
foo.fooMethod()"""
68+
output = """
69+
import * as foo from 'lib';
70+
71+
foo.fooMethod();"""
6272
eq toJS(input), output
6373

6474
test "import a single member of a module, adding the member to the current scope", ->
65-
input = "import { foo } from 'lib'\nfoo.fooMethod()"
66-
output = "import { foo } from 'lib';\n\nfoo.fooMethod();"
75+
input = """
76+
import { foo } from 'lib'
77+
foo.fooMethod()"""
78+
output = """
79+
import {
80+
foo
81+
} from 'lib';
82+
83+
foo.fooMethod();"""
6784
eq toJS(input), output
6885

6986
test "import a single member of a module as an alias, adding the alias to the current scope", ->
70-
input = "import { foo as bar } from 'lib'\nbar.barMethod()"
71-
output = "import { foo as bar } from 'lib';\n\nbar.barMethod();"
87+
input = """
88+
import { foo as bar } from 'lib'
89+
bar.barMethod()"""
90+
output = """
91+
import {
92+
foo as bar
93+
} from 'lib';
94+
95+
bar.barMethod();"""
7296
eq toJS(input), output
7397

7498
test "import a multiple members of a module, adding the members to the current scope", ->
75-
input = "import { foo, bar } from 'lib'\nfoo.fooMethod()\nbar.barMethod()"
76-
output = "import { foo, bar } from 'lib';\n\nfoo.fooMethod();\n\nbar.barMethod();"
99+
input = """
100+
import { foo, bar } from 'lib'
101+
foo.fooMethod()
102+
bar.barMethod()"""
103+
output = """
104+
import {
105+
foo,
106+
bar
107+
} from 'lib';
108+
109+
foo.fooMethod();
110+
111+
bar.barMethod();"""
77112
eq toJS(input), output
78113

79114
test "import a multiple members of a module where some are aliased, adding the members or aliases to the current scope", ->
80-
input = "import { foo, bar as baz } from 'lib'\nfoo.fooMethod()\nbaz.bazMethod()"
81-
output = "import { foo, bar as baz } from 'lib';\n\nfoo.fooMethod();\n\nbaz.bazMethod();"
115+
input = """
116+
import { foo, bar as baz } from 'lib'
117+
foo.fooMethod()
118+
baz.bazMethod()"""
119+
output = """
120+
import {
121+
foo,
122+
bar as baz
123+
} from 'lib';
124+
125+
foo.fooMethod();
126+
127+
baz.bazMethod();"""
82128
eq toJS(input), output
83129

84130
test "import default member and other members of a module, adding the members to the current scope", ->
85-
input = "import foo, { bar, baz as qux } from 'lib'\nfoo.fooMethod()\nbar.barMethod()\nqux.quxMethod()"
86-
output = "import foo, { bar, baz as qux } from 'lib';\n\nfoo.fooMethod();\n\nbar.barMethod();\n\nqux.quxMethod();"
131+
input = """
132+
import foo, { bar, baz as qux } from 'lib'
133+
foo.fooMethod()
134+
bar.barMethod()
135+
qux.quxMethod()"""
136+
output = """
137+
import foo, {
138+
bar,
139+
baz as qux
140+
} from 'lib';
141+
142+
foo.fooMethod();
143+
144+
bar.barMethod();
145+
146+
qux.quxMethod();"""
87147
eq toJS(input), output
88148

89149
test "import default member from a module as well as the entire module's contents as an alias, adding the member and alias to the current scope", ->
90-
input = "import foo, * as bar from 'lib'\nfoo.fooMethod()\nbar.barMethod()"
91-
output = "import foo, * as bar from 'lib';\n\nfoo.fooMethod();\n\nbar.barMethod();"
150+
input = """
151+
import foo, * as bar from 'lib'
152+
foo.fooMethod()
153+
bar.barMethod()"""
154+
output = """
155+
import foo, * as bar from 'lib';
156+
157+
foo.fooMethod();
158+
159+
bar.barMethod();"""
92160
eq toJS(input), output
93161

94162
test "multiline simple import", ->
95-
input = """import {
96-
foo,
97-
bar as baz
98-
} from 'lib'"""
99-
output = "import { foo, bar as baz } from 'lib';"
100-
163+
input = """
164+
import {
165+
foo,
166+
bar as baz
167+
} from 'lib'"""
168+
output = """
169+
import {
170+
foo,
171+
bar as baz
172+
} from 'lib';"""
101173
eq toJS(input), output
102174

103175
test "multiline complex import", ->
104-
input = """import foo, {
176+
input = """
177+
import foo, {
105178
bar,
106179
baz as qux
107180
} from 'lib'"""
108-
output = "import foo, { bar, baz as qux } from 'lib';"
109-
181+
output = """
182+
import foo, {
183+
bar,
184+
baz as qux
185+
} from 'lib';"""
110186
eq toJS(input), output
111187

112-
# test "multiline simple import", ->
113-
# input = """import {
114-
# foo,
115-
# bar as baz
116-
# } from 'lib'"""
117-
# output = """import {
118-
# foo,
119-
# bar as baz
120-
# } from 'lib';"""
121-
# eq toJS(input), output
122-
123-
# test "multiline complex import", ->
124-
# input = """import foo, {
125-
# bar,
126-
# baz as qux
127-
# } from 'lib'"""
128-
# output = """import foo, {
129-
# bar,
130-
# baz as qux
131-
# } from 'lib';"""
132-
# eq toJS(input), output
133-
134188

135189
# Export statements
136190

137191
test "export named members within an object", ->
138192
input = "export { foo, bar }"
139-
output = "export { foo, bar };"
193+
output = """
194+
export {
195+
foo,
196+
bar
197+
};"""
140198
eq toJS(input), output
141199

142200
test "export named members as aliases, within an object", ->
143201
input = "export { foo as bar, baz as qux }"
144-
output = "export { foo as bar, baz as qux };"
202+
output = """
203+
export {
204+
foo as bar,
205+
baz as qux
206+
};"""
145207
eq toJS(input), output
146208

147209
test "export default expression", ->
@@ -166,7 +228,11 @@ test "export default multiline function", ->
166228

167229
test "export default named member, within an object", ->
168230
input = "export { foo as default, bar }"
169-
output = "export { foo as default, bar };"
231+
output = """
232+
export {
233+
foo as default,
234+
bar
235+
};"""
170236
eq toJS(input), output
171237

172238

@@ -179,10 +245,18 @@ test "export an entire module's contents", ->
179245

180246
test "export members imported from another module", ->
181247
input = "export { foo, bar } from 'lib'"
182-
output = "export { foo, bar } from 'lib';"
248+
output = """
249+
export {
250+
foo,
251+
bar
252+
} from 'lib';"""
183253
eq toJS(input), output
184254

185255
test "export as aliases members imported from another module", ->
186256
input = "export { foo as bar, baz as qux } from 'lib'"
187-
output = "export { foo as bar, baz as qux } from 'lib';"
257+
output = """
258+
export {
259+
foo as bar,
260+
baz as qux
261+
} from 'lib';"""
188262
eq toJS(input), output

0 commit comments

Comments
 (0)