Skip to content

Commit 9542141

Browse files
handle local import specifier
1 parent e770892 commit 9542141

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

__tests__/push/__snapshots__/transform-journey.test.ts.snap

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`TransformJourneyPlugin alias import specifier 1`] = `
4+
"import { journey as x } from '@elastic/synthetics';
5+
6+
x('j1', () => {});"
7+
`;
8+
9+
exports[`TransformJourneyPlugin alias import specifier 2`] = `
10+
"import { journey as x } from '@elastic/synthetics';
11+
12+
13+
x('j2', () => {});"
14+
`;
15+
16+
exports[`TransformJourneyPlugin alias import specifier 3`] = `
17+
"import { journey as x } from '@elastic/synthetics';
18+
19+
x('j1', () => {});
20+
x('j2', () => {});"
21+
`;
22+
323
exports[`TransformJourneyPlugin dynamic journeys 1`] = `
4-
"import { journey, step, monitor } from '../../';
24+
"import { journey, step, monitor } from '@elastic/synthetics';
525
626
journey('j1', () => {});
727
@@ -16,7 +36,7 @@ createJourney('j2');"
1636
`;
1737

1838
exports[`TransformJourneyPlugin dynamic journeys 2`] = `
19-
"import { journey, step, monitor } from '../../';
39+
"import { journey, step, monitor } from '@elastic/synthetics';
2040
2141
2242
@@ -31,7 +51,7 @@ createJourney('j2');"
3151
`;
3252

3353
exports[`TransformJourneyPlugin dynamic journeys 3`] = `
34-
"import { journey, step, monitor } from '../../';
54+
"import { journey, step, monitor } from '@elastic/synthetics';
3555
3656
journey('j1', () => {});
3757
@@ -46,7 +66,7 @@ createJourney('j2');"
4666
`;
4767

4868
exports[`TransformJourneyPlugin static journeys 1`] = `
49-
"import { journey, step, monitor } from '../../';
69+
"import { journey, step, monitor } from '@elastic/synthetics';
5070
journey('j1', () => {
5171
monitor.use({ id: 'duplicate id' });
5272
});
@@ -55,7 +75,7 @@ function util() {}"
5575
`;
5676

5777
exports[`TransformJourneyPlugin static journeys 2`] = `
58-
"import { journey, step, monitor } from '../../';
78+
"import { journey, step, monitor } from '@elastic/synthetics';
5979
6080
6181
@@ -66,7 +86,7 @@ journey('j2', () => {});"
6686
`;
6787

6888
exports[`TransformJourneyPlugin static journeys 3`] = `
69-
"import { journey, step, monitor } from '../../';
89+
"import { journey, step, monitor } from '@elastic/synthetics';
7090
journey('j1', () => {
7191
monitor.use({ id: 'duplicate id' });
7292
});

__tests__/push/transform-journey.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('TransformJourneyPlugin', () => {
4141
it('static journeys', async () => {
4242
await writeFile(
4343
journeyFile,
44-
`import {journey, step, monitor} from '../../';
44+
`import {journey, step, monitor} from '@elastic/synthetics';
4545
journey('j1', () => {
4646
monitor.use({ id: 'duplicate id' });
4747
});
@@ -62,7 +62,7 @@ journey('j2', () => {});`
6262
it('dynamic journeys', async () => {
6363
await writeFile(
6464
journeyFile,
65-
`import {journey, step, monitor} from '../../';
65+
`import {journey, step, monitor} from '@elastic/synthetics';
6666
6767
journey('j1', () => {});
6868
@@ -84,4 +84,22 @@ createJourney('j2');
8484
const res3 = await transform(journeyFile, '');
8585
expect(res3?.code).toMatchSnapshot();
8686
});
87+
88+
it('alias import specifier', async () => {
89+
await writeFile(
90+
journeyFile,
91+
`import {journey as x} from '@elastic/synthetics';
92+
93+
x('j1', () => {});
94+
x('j2', ()=>{});
95+
`
96+
);
97+
98+
const res1 = await transform(journeyFile, 'j1');
99+
expect(res1?.code).toMatchSnapshot();
100+
const res2 = await transform(journeyFile, 'j2');
101+
expect(res2?.code).toMatchSnapshot();
102+
const res3 = await transform(journeyFile, '');
103+
expect(res3?.code).toMatchSnapshot();
104+
});
87105
});

src/push/transform-journey.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,50 @@ type JourneyPluginOptions = {
3030
name: string;
3131
};
3232

33+
const SYNTHETICS_IMPORT = '@elastic/synthetics';
34+
3335
export function JourneyTransformPlugin(
3436
{ types: t }: { types: typeof types },
3537
opts: JourneyPluginOptions
3638
): PluginObj {
37-
// TODO: Perform a Program level visit to check if import/require declarations are tampered
38-
// If so, dont perform any transformation
39-
// Ex: import * as synthetics from '@elastic/synthetics'
40-
// synthetics.journey('name', () => {})
39+
// This handles when the import specifier is renamed
40+
// Ex: import { journey as journeyAlias } from '@elastic/synthetics'
41+
let importSpecifierName = 'journey';
42+
4143
return {
4244
name: 'transform-journeys',
4345
visitor: {
46+
Program(path) {
47+
path.traverse({
48+
ImportDeclaration(path) {
49+
const { source } = path.node;
50+
if (
51+
t.isStringLiteral(source) &&
52+
source.value === SYNTHETICS_IMPORT
53+
) {
54+
const specifiers = path.node.specifiers;
55+
for (const specifier of specifiers) {
56+
if (t.isImportSpecifier(specifier)) {
57+
const { imported, local } = specifier;
58+
if (t.isIdentifier(imported) && imported.name === 'journey') {
59+
importSpecifierName = local.name;
60+
}
61+
}
62+
}
63+
}
64+
},
65+
});
66+
},
4467
CallExpression(path) {
4568
if (!path.parentPath.isExpressionStatement()) {
4669
return;
4770
}
4871
const { callee } = path.node;
49-
if (t.isIdentifier(callee) && callee.name === 'journey') {
72+
if (
73+
t.isIdentifier(callee) &&
74+
importSpecifierName &&
75+
callee.name === importSpecifierName
76+
) {
5077
const args = path.node.arguments;
5178
if (!t.isStringLiteral(args[0])) {
5279
return;

0 commit comments

Comments
 (0)