Skip to content

Commit 44015fe

Browse files
committed
fix: reduce likelihood of name conflict, test on ts parser
1 parent ab6cf84 commit 44015fe

File tree

2 files changed

+110
-72
lines changed

2 files changed

+110
-72
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ module.exports = function addImports(root, _statements) {
3030
? _id => {
3131
let id = _id
3232
let count = 1
33-
while (babelScope.getBinding(id)) id = `${_id}${count++}`
33+
while (
34+
babelScope.getBinding(id) ||
35+
astTypeScope.lookup(id) ||
36+
astTypeScope.lookupType(id)
37+
)
38+
id = `${_id}${count++}`
3439
return id
3540
}
3641
: _id => {

test/index.js

Lines changed: 104 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const prettier = require('prettier')
55

66
const addImports = require('..')
77

8-
for (const parser of ['babylon']) {
8+
for (const parser of ['babylon', 'ts']) {
99
describe(`with parser: ${parser}`, function() {
1010
const j = jscodeshift.withParser(parser)
1111
const { statement } = j.template
@@ -241,52 +241,54 @@ for (const parser of ['babylon']) {
241241
`,
242242
})
243243
})
244-
it(`leaves existing non-default import type specifiers with aliases untouched`, function() {
245-
testCase({
246-
code: `
244+
if (parser !== 'ts') {
245+
it(`leaves existing non-default import type specifiers with aliases untouched`, function() {
246+
testCase({
247+
code: `
247248
import {foo as bar} from 'baz'
248249
import type {foo as qlob} from 'baz'
249250
`,
250-
add: `import type {foo as qux} from 'baz'`,
251-
expectedCode: `
251+
add: `import type {foo as qux} from 'baz'`,
252+
expectedCode: `
252253
import {foo as bar} from 'baz'
253254
import type {foo as qlob} from 'baz'
254255
`,
256+
})
255257
})
256-
})
257-
it(`adds missing non-default import type specifiers with aliases`, function() {
258-
testCase({
259-
code: `
258+
it(`adds missing non-default import type specifiers with aliases`, function() {
259+
testCase({
260+
code: `
260261
import {foo as bar} from 'baz'
261262
import type {glab as qlob} from 'baz'
262263
`,
263-
add: `import type {foo as qux} from 'baz'`,
264-
expectedCode: `
264+
add: `import type {foo as qux} from 'baz'`,
265+
expectedCode: `
265266
import {foo as bar} from 'baz'
266267
import type { glab as qlob, foo as qux } from 'baz'
267268
`,
269+
})
268270
})
269-
})
270-
it(`adds missing non-default import type specifiers with aliases case 2`, function() {
271-
testCase({
272-
code: `
271+
it(`adds missing non-default import type specifiers with aliases case 2`, function() {
272+
testCase({
273+
code: `
273274
import {foo as bar} from 'baz'
274275
import type { glab as qlob } from "qlob"
275276
`,
276-
add: `import type {foo as qux} from 'baz'`,
277-
expectedCode: `
277+
add: `import type {foo as qux} from 'baz'`,
278+
expectedCode: `
278279
import { foo as bar, type foo as qux } from 'baz'
279280
import type { glab as qlob } from "qlob"
280281
`,
282+
})
281283
})
282-
})
283-
it(`converts import type {} to import {type} if necessary`, function() {
284-
testCase({
285-
code: `import type {foo as bar} from 'baz'`,
286-
add: `import {foo as qux} from 'baz'`,
287-
expectedCode: `import { type foo as bar, foo as qux } from 'baz'`,
284+
it(`converts import type {} to import {type} if necessary`, function() {
285+
testCase({
286+
code: `import type {foo as bar} from 'baz'`,
287+
add: `import {foo as qux} from 'baz'`,
288+
expectedCode: `import { type foo as bar, foo as qux } from 'baz'`,
289+
})
288290
})
289-
})
291+
}
290292
it(`leaves existing non-default import specifiers without aliases untouched`, function() {
291293
testCase({
292294
code: `import {foo} from 'baz'`,
@@ -409,51 +411,53 @@ for (const parser of ['babylon']) {
409411
`,
410412
})
411413
})
412-
it(`avoids name conflicts with import type`, function() {
413-
testCase({
414-
code: `
414+
if (parser !== 'ts') {
415+
it(`avoids name conflicts with import type`, function() {
416+
testCase({
417+
code: `
415418
// @flow
416419
import type foo from 'foo'
417420
`,
418-
add: `import type foo from 'bar'`,
419-
expectedCode: `
421+
add: `import type foo from 'bar'`,
422+
expectedCode: `
420423
// @flow
421424
import type foo from 'foo'
422425
import type foo1 from 'bar'
423426
`,
424-
expectedReturn: { foo: 'foo1' },
427+
expectedReturn: { foo: 'foo1' },
428+
})
425429
})
426-
})
427-
it(`avoids name conflicts with import type {}`, function() {
428-
testCase({
429-
code: `
430+
it(`avoids name conflicts with import type {}`, function() {
431+
testCase({
432+
code: `
430433
// @flow
431434
import type {foo} from 'foo'
432435
`,
433-
add: `import type {foo} from 'bar'`,
434-
expectedCode: `
436+
add: `import type {foo} from 'bar'`,
437+
expectedCode: `
435438
// @flow
436439
import type {foo} from 'foo'
437440
import type {foo as foo1} from 'bar'
438441
`,
439-
expectedReturn: { foo: 'foo1' },
442+
expectedReturn: { foo: 'foo1' },
443+
})
440444
})
441-
})
442-
it(`avoids name conflicts with import {type}`, function() {
443-
testCase({
444-
code: `
445+
it(`avoids name conflicts with import {type}`, function() {
446+
testCase({
447+
code: `
445448
// @flow
446449
import {type foo} from 'foo'
447450
`,
448-
add: `import {type foo} from 'bar'`,
449-
expectedCode: `
451+
add: `import {type foo} from 'bar'`,
452+
expectedCode: `
450453
// @flow
451454
import {type foo} from 'foo'
452455
import {type foo as foo1} from 'bar'
453456
`,
454-
expectedReturn: { foo: 'foo1' },
457+
expectedReturn: { foo: 'foo1' },
458+
})
455459
})
456-
})
460+
}
457461
it(`doesn't break leading comments`, function() {
458462
testCase({
459463
code: `
@@ -474,18 +478,18 @@ for (const parser of ['babylon']) {
474478
testCase({
475479
code: `
476480
// @flow
477-
import {foo, type bar} from 'foo'
481+
import {foo, bar} from 'foo'
478482
import baz from 'baz'
479483
`,
480484
add: [
481-
`import type {bar, baz} from 'foo'`,
482-
`import blah, {type qux} from 'qux'`,
485+
`import {bar, baz} from 'foo'`,
486+
`import blah, {qux} from 'qux'`,
483487
],
484488
expectedCode: `
485489
// @flow
486-
import {foo, type bar, type baz as baz1} from 'foo'
490+
import {foo, bar, baz as baz1} from 'foo'
487491
import baz from 'baz'
488-
import blah, {type qux} from 'qux'
492+
import blah, {qux} from 'qux'
489493
`,
490494
expectedReturn: {
491495
bar: 'bar',
@@ -495,49 +499,78 @@ for (const parser of ['babylon']) {
495499
},
496500
})
497501
})
498-
describe(`bugs`, function() {
499-
it(`import type { foo, type bar }`, function() {
502+
if (parser !== 'ts') {
503+
it(`multiple statements and specifiers with types`, function() {
500504
testCase({
501505
code: `
506+
// @flow
507+
import {foo, type bar} from 'foo'
508+
import baz from 'baz'
509+
`,
510+
add: [
511+
`import type {bar, baz} from 'foo'`,
512+
`import blah, {type qux} from 'qux'`,
513+
],
514+
expectedCode: `
515+
// @flow
516+
import {foo, type bar, type baz as baz1} from 'foo'
517+
import baz from 'baz'
518+
import blah, {type qux} from 'qux'
519+
`,
520+
expectedReturn: {
521+
bar: 'bar',
522+
baz: 'baz1',
523+
blah: 'blah',
524+
qux: 'qux',
525+
},
526+
})
527+
})
528+
}
529+
describe(`bugs`, function() {
530+
if (parser !== 'ts') {
531+
it(`import type { foo, type bar }`, function() {
532+
testCase({
533+
code: `
502534
// @flow
503535
import type {foo} from 'foo'
504536
`,
505-
add: `import {type bar} from 'foo'`,
506-
expectedCode: `
537+
add: `import {type bar} from 'foo'`,
538+
expectedCode: `
507539
// @flow
508540
import type {foo, bar} from 'foo'
509541
`,
510-
expectedReturn: { bar: 'bar' },
542+
expectedReturn: { bar: 'bar' },
543+
})
511544
})
512-
})
513-
it(`import typeof { foo, type bar }`, function() {
514-
testCase({
515-
code: `
545+
it(`import typeof { foo, type bar }`, function() {
546+
testCase({
547+
code: `
516548
// @flow
517549
import typeof {foo} from 'foo'
518550
`,
519-
add: `import {type bar} from 'foo'`,
520-
expectedCode: `
551+
add: `import {type bar} from 'foo'`,
552+
expectedCode: `
521553
// @flow
522554
import {typeof foo, type bar} from 'foo'
523555
`,
524-
expectedReturn: { bar: 'bar' },
556+
expectedReturn: { bar: 'bar' },
557+
})
525558
})
526-
})
527-
it(`import type { foo, typeof bar }`, function() {
528-
testCase({
529-
code: `
559+
it(`import type { foo, typeof bar }`, function() {
560+
testCase({
561+
code: `
530562
// @flow
531563
import type {foo} from 'foo'
532564
`,
533-
add: `import typeof {bar} from 'foo'`,
534-
expectedCode: `
565+
add: `import typeof {bar} from 'foo'`,
566+
expectedCode: `
535567
// @flow
536568
import {type foo, typeof bar} from 'foo'
537569
`,
538-
expectedReturn: { bar: 'bar' },
570+
expectedReturn: { bar: 'bar' },
571+
})
539572
})
540-
})
573+
}
541574
})
542575
})
543576
})

0 commit comments

Comments
 (0)