From 95421411478259687c9ae4f8b879b53b2ebee627 Mon Sep 17 00:00:00 2001 From: vigneshshanmugam Date: Fri, 28 Jul 2023 17:45:23 -0700 Subject: [PATCH] handle local import specifier --- .../transform-journey.test.ts.snap | 32 +++++++++++++--- __tests__/push/transform-journey.test.ts | 22 ++++++++++- src/push/transform-journey.ts | 37 ++++++++++++++++--- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/__tests__/push/__snapshots__/transform-journey.test.ts.snap b/__tests__/push/__snapshots__/transform-journey.test.ts.snap index 1370913e..0ad01851 100644 --- a/__tests__/push/__snapshots__/transform-journey.test.ts.snap +++ b/__tests__/push/__snapshots__/transform-journey.test.ts.snap @@ -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', () => {}); @@ -16,7 +36,7 @@ createJourney('j2');" `; exports[`TransformJourneyPlugin dynamic journeys 2`] = ` -"import { journey, step, monitor } from '../../'; +"import { journey, step, monitor } from '@elastic/synthetics'; @@ -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', () => {}); @@ -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' }); }); @@ -55,7 +75,7 @@ function util() {}" `; exports[`TransformJourneyPlugin static journeys 2`] = ` -"import { journey, step, monitor } from '../../'; +"import { journey, step, monitor } from '@elastic/synthetics'; @@ -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' }); }); diff --git a/__tests__/push/transform-journey.test.ts b/__tests__/push/transform-journey.test.ts index f4e31448..c2ebfc44 100644 --- a/__tests__/push/transform-journey.test.ts +++ b/__tests__/push/transform-journey.test.ts @@ -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' }); }); @@ -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', () => {}); @@ -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(); + }); }); diff --git a/src/push/transform-journey.ts b/src/push/transform-journey.ts index 9fde25ff..eba40a44 100644 --- a/src/push/transform-journey.ts +++ b/src/push/transform-journey.ts @@ -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;