|
| 1 | +const path = require('path'); |
| 2 | +const { transformSync } = require('@babel/core'); |
| 3 | +const { rewriteAlias } = require('./import-alias'); |
| 4 | + |
| 5 | +const ROOT = path.resolve(__dirname, '../../..'); |
| 6 | + |
| 7 | +describe('import-alias babel plugin', () => { |
| 8 | + function transform(code, filePath) { |
| 9 | + const result = transformSync(code, { |
| 10 | + filename: path.join(ROOT, filePath), |
| 11 | + plugins: [require.resolve('./import-alias')], |
| 12 | + parserOpts: { plugins: ['typescript'] }, |
| 13 | + configFile: false, |
| 14 | + babelrc: false, |
| 15 | + }); |
| 16 | + return result.code; |
| 17 | + } |
| 18 | + |
| 19 | + describe('rewriteAlias', () => { |
| 20 | + it('rewrites ~/shared/ to a relative path', () => { |
| 21 | + const filename = path.join(ROOT, 'app/scripts/migrations/183.ts'); |
| 22 | + const result = rewriteAlias('~/shared/constants/network', filename); |
| 23 | + expect(result).toBe('../../../shared/constants/network'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('rewrites ~/ui/ to a relative path', () => { |
| 27 | + const filename = path.join( |
| 28 | + ROOT, |
| 29 | + 'ui/components/multichain/activity-v2/hooks.ts', |
| 30 | + ); |
| 31 | + const result = rewriteAlias('~/ui/hooks/useI18nContext', filename); |
| 32 | + expect(result).toBe('../../../hooks/useI18nContext'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns null for non-alias imports', () => { |
| 36 | + const filename = path.join(ROOT, 'ui/components/foo.ts'); |
| 37 | + expect(rewriteAlias('./helpers', filename)).toBeNull(); |
| 38 | + expect(rewriteAlias('react', filename)).toBeNull(); |
| 39 | + expect(rewriteAlias('@metamask/utils', filename)).toBeNull(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('does not match partial prefix like ~/shared-extra', () => { |
| 43 | + const filename = path.join(ROOT, 'ui/components/foo.ts'); |
| 44 | + expect(rewriteAlias('~/shared-extra/foo', filename)).toBeNull(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('handles bare alias without subpath', () => { |
| 48 | + const filename = path.join(ROOT, 'app/scripts/background.js'); |
| 49 | + const result = rewriteAlias('~/shared', filename); |
| 50 | + expect(result).toBe('../../shared'); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('babel transform: import declarations', () => { |
| 55 | + it('rewrites named import from ~/shared/', () => { |
| 56 | + const code = `import { CHAIN_IDS } from '~/shared/constants/network';`; |
| 57 | + const output = transform( |
| 58 | + code, |
| 59 | + 'ui/components/multichain/activity-v2/hooks.ts', |
| 60 | + ); |
| 61 | + expect(output).toContain( |
| 62 | + `from "../../../../shared/constants/network"`, |
| 63 | + ); |
| 64 | + expect(output).not.toContain('~/'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('rewrites default import from ~/ui/', () => { |
| 68 | + const code = `import AssetPage from '~/ui/pages/asset/components/asset-page';`; |
| 69 | + const output = transform(code, 'ui/components/multichain/foo.ts'); |
| 70 | + expect(output).toContain(`from "../../pages/asset/components/asset-page"`); |
| 71 | + expect(output).not.toContain('~/'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('rewrites type import from ~/shared/', () => { |
| 75 | + const code = `import type { Token } from '~/shared/lib/multichain/types';`; |
| 76 | + const output = transform( |
| 77 | + code, |
| 78 | + 'ui/components/multichain/activity-v2/hooks.ts', |
| 79 | + ); |
| 80 | + expect(output).toContain(`"../../../../shared/lib/multichain/types"`); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not touch non-alias imports', () => { |
| 84 | + const code = [ |
| 85 | + `import React from 'react';`, |
| 86 | + `import { Box } from '@metamask/design-system-react';`, |
| 87 | + `import { foo } from './helpers';`, |
| 88 | + `import { bar } from '../utils';`, |
| 89 | + ].join('\n'); |
| 90 | + const output = transform(code, 'ui/components/foo.ts'); |
| 91 | + expect(output).toContain(`react`); |
| 92 | + expect(output).toContain(`@metamask/design-system-react`); |
| 93 | + expect(output).toContain(`./helpers`); |
| 94 | + expect(output).toContain(`../utils`); |
| 95 | + expect(output).not.toContain('~/'); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('babel transform: export declarations', () => { |
| 100 | + it('rewrites export { X } from ~/shared/', () => { |
| 101 | + const code = `export { CHAIN_IDS } from '~/shared/constants/network';`; |
| 102 | + const output = transform(code, 'app/scripts/lib/util.ts'); |
| 103 | + expect(output).toContain(`from "../../../shared/constants/network"`); |
| 104 | + }); |
| 105 | + |
| 106 | + it('rewrites export * from ~/ui/', () => { |
| 107 | + const code = `export * from '~/ui/selectors';`; |
| 108 | + const output = transform(code, 'ui/components/foo.ts'); |
| 109 | + expect(output).toContain(`from "../selectors"`); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('babel transform: require calls', () => { |
| 114 | + it('rewrites require(~/shared/)', () => { |
| 115 | + const code = `const { foo } = require('~/shared/lib/sentry');`; |
| 116 | + const output = transform(code, 'app/scripts/migrations/183.ts'); |
| 117 | + expect(output).toContain(`require("../../../shared/lib/sentry")`); |
| 118 | + }); |
| 119 | + |
| 120 | + it('does not touch non-alias require calls', () => { |
| 121 | + const code = `const path = require('path');`; |
| 122 | + const output = transform(code, 'app/scripts/lib/util.ts'); |
| 123 | + expect(output).toContain(`require('path')`); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments