Skip to content

Commit

Permalink
handle local import specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed Sep 8, 2023
1 parent 8a7cefe commit 1097ddf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
32 changes: 26 additions & 6 deletions __tests__/push/__snapshots__/transform-journey.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TransformJourneyPlugin alias import specifier 1`] = `
"import { journey as x } from '@elastic/synthetics';
x('j1', () => {});"
`;

exports[`TransformJourneyPlugin alias import specifier 2`] = `
"import { journey as x } from '@elastic/synthetics';
x('j2', () => {});"
`;

exports[`TransformJourneyPlugin alias import specifier 3`] = `
"import { journey as x } from '@elastic/synthetics';
x('j1', () => {});
x('j2', () => {});"
`;

exports[`TransformJourneyPlugin dynamic journeys 1`] = `
"import { journey, step, monitor } from '../../';
"import { journey, step, monitor } from '@elastic/synthetics';
journey('j1', () => {});
Expand All @@ -16,7 +36,7 @@ createJourney('j2');"
`;

exports[`TransformJourneyPlugin dynamic journeys 2`] = `
"import { journey, step, monitor } from '../../';
"import { journey, step, monitor } from '@elastic/synthetics';
Expand All @@ -31,7 +51,7 @@ createJourney('j2');"
`;

exports[`TransformJourneyPlugin dynamic journeys 3`] = `
"import { journey, step, monitor } from '../../';
"import { journey, step, monitor } from '@elastic/synthetics';
journey('j1', () => {});
Expand All @@ -46,7 +66,7 @@ createJourney('j2');"
`;

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

exports[`TransformJourneyPlugin static journeys 2`] = `
"import { journey, step, monitor } from '../../';
"import { journey, step, monitor } from '@elastic/synthetics';
Expand All @@ -66,7 +86,7 @@ journey('j2', () => {});"
`;

exports[`TransformJourneyPlugin static journeys 3`] = `
"import { journey, step, monitor } from '../../';
"import { journey, step, monitor } from '@elastic/synthetics';
journey('j1', () => {
monitor.use({ id: 'duplicate id' });
});
Expand Down
22 changes: 20 additions & 2 deletions __tests__/push/transform-journey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('TransformJourneyPlugin', () => {
it('static journeys', async () => {
await writeFile(
journeyFile,
`import {journey, step, monitor} from '../../';
`import {journey, step, monitor} from '@elastic/synthetics';
journey('j1', () => {
monitor.use({ id: 'duplicate id' });
});
Expand All @@ -62,7 +62,7 @@ journey('j2', () => {});`
it('dynamic journeys', async () => {
await writeFile(
journeyFile,
`import {journey, step, monitor} from '../../';
`import {journey, step, monitor} from '@elastic/synthetics';
journey('j1', () => {});
Expand All @@ -84,4 +84,22 @@ createJourney('j2');
const res3 = await transform(journeyFile, '');
expect(res3?.code).toMatchSnapshot();
});

it('alias import specifier', async () => {
await writeFile(
journeyFile,
`import {journey as x} from '@elastic/synthetics';
x('j1', () => {});
x('j2', ()=>{});
`
);

const res1 = await transform(journeyFile, 'j1');
expect(res1?.code).toMatchSnapshot();
const res2 = await transform(journeyFile, 'j2');
expect(res2?.code).toMatchSnapshot();
const res3 = await transform(journeyFile, '');
expect(res3?.code).toMatchSnapshot();
});
});
37 changes: 32 additions & 5 deletions src/push/transform-journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,50 @@ type JourneyPluginOptions = {
name: string;
};

const SYNTHETICS_IMPORT = '@elastic/synthetics';

export function JourneyTransformPlugin(
{ types: t }: { types: typeof types },
opts: JourneyPluginOptions
): PluginObj {
// TODO: Perform a Program level visit to check if import/require declarations are tampered
// If so, dont perform any transformation
// Ex: import * as synthetics from '@elastic/synthetics'
// synthetics.journey('name', () => {})
// This handles when the import specifier is renamed
// Ex: import { journey as journeyAlias } from '@elastic/synthetics'
let importSpecifierName = 'journey';

return {
name: 'transform-journeys',
visitor: {
Program(path) {
path.traverse({
ImportDeclaration(path) {
const { source } = path.node;
if (
t.isStringLiteral(source) &&
source.value === SYNTHETICS_IMPORT
) {
const specifiers = path.node.specifiers;
for (const specifier of specifiers) {
if (t.isImportSpecifier(specifier)) {
const { imported, local } = specifier;
if (t.isIdentifier(imported) && imported.name === 'journey') {
importSpecifierName = local.name;
}
}
}
}
},
});
},
CallExpression(path) {
if (!path.parentPath.isExpressionStatement()) {
return;
}
const { callee } = path.node;
if (t.isIdentifier(callee) && callee.name === 'journey') {
if (
t.isIdentifier(callee) &&
importSpecifierName &&
callee.name === importSpecifierName
) {
const args = path.node.arguments;
if (!t.isStringLiteral(args[0])) {
return;
Expand Down

0 comments on commit 1097ddf

Please sign in to comment.