From 12df40ed8c1101c5c4053a1fe63c06fcd2809bc7 Mon Sep 17 00:00:00 2001 From: Wenfu <115598864+wenfw@users.noreply.github.com> Date: Sat, 23 Nov 2024 03:31:31 +0800 Subject: [PATCH] fix: support multiple imports of one module with multiple lines (#30314) e.g. ``` import { a, b, c, } from 'x'; ``` chore: remove unnecessary deep equal Co-authored-by: Bill Glesias --- cli/CHANGELOG.md | 9 +++ .../cypress/component/fixtures/kitchenSink.ts | 6 ++ .../cypress/component/importSyntax.cy.ts | 65 ++++++++++++++++++- npm/vite-plugin-cypress-esm/src/index.ts | 2 +- 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index e3fa2e371e28..6747c81aa385 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,4 +1,13 @@ + +## 13.16.1 + +_Released 11/26/2024 (PENDING)_ + +**Bugfixes:** + +- Support multiple imports of one module with multiple lines. Addressed in [#30314](https://github.com/cypress-io/cypress/pull/30314). + ## 13.16.0 _Released 11/19/2024_ diff --git a/npm/vite-plugin-cypress-esm/cypress/component/fixtures/kitchenSink.ts b/npm/vite-plugin-cypress-esm/cypress/component/fixtures/kitchenSink.ts index efbef2a22486..6ab57dd3ec1b 100644 --- a/npm/vite-plugin-cypress-esm/cypress/component/fixtures/kitchenSink.ts +++ b/npm/vite-plugin-cypress-esm/cypress/component/fixtures/kitchenSink.ts @@ -2,6 +2,12 @@ export const export1 = 'export1' export const export2 = 'export2' +export const export3 = 'export3' + +export const export4 = 'export4' + +export const export5 = 'export5' + // @ts-expect-error window.sideEffect = 'Side Effect' diff --git a/npm/vite-plugin-cypress-esm/cypress/component/importSyntax.cy.ts b/npm/vite-plugin-cypress-esm/cypress/component/importSyntax.cy.ts index 2248d3eed52a..ee771102568b 100644 --- a/npm/vite-plugin-cypress-esm/cypress/component/importSyntax.cy.ts +++ b/npm/vite-plugin-cypress-esm/cypress/component/importSyntax.cy.ts @@ -9,6 +9,17 @@ import { default as alias } from './fixtures/kitchenSink' import defaultExport2, { export2 } from './fixtures/kitchenSink' import defaultExport3, * as name2 from './fixtures/kitchenSink' import { export1 as e1, export2 as e2 } from './fixtures/kitchenSink' +import { + export3, + export4, +} from './fixtures/kitchenSink' +import { + export3 as alias3, + export4 as alias4, +} from './fixtures/kitchenSink' +import defaultExport4, { + export5, +} from './fixtures/kitchenSink' import './fixtures/kitchenSink' // Examples for all syntax @@ -19,6 +30,9 @@ describe('supports every combination of import syntax in a single file', () => { expect(defaultExport1).to.deep.eq({ export1: 'export1', export2: 'export2', + export3: 'export3', + export4: 'export4', + export5: 'export5', default: { export1: 'export1', export2: 'export2', @@ -30,6 +44,9 @@ describe('supports every combination of import syntax in a single file', () => { expect(name1).to.deep.eq({ export1: 'export1', export2: 'export2', + export3: 'export3', + export4: 'export4', + export5: 'export5', default: { export1: 'export1', export2: 'export2', @@ -38,7 +55,7 @@ describe('supports every combination of import syntax in a single file', () => { }) it('Import { export1 } from "./kitchenSink"', () => { - expect(export1).to.deep.eq(export1) + expect(export1).to.eq('export1') }) it('Import { export1 as alias1 } from "./kitchenSink"', () => { @@ -56,19 +73,25 @@ describe('supports every combination of import syntax in a single file', () => { expect(defaultExport2).to.deep.eq({ export1: 'export1', export2: 'export2', + export3: 'export3', + export4: 'export4', + export5: 'export5', default: { export1: 'export1', export2: 'export2', }, }) - expect(export2).to.eq(export2) + expect(export2).to.eq('export2') }) it('Import defaultExport3, * as name2 from "./kitchenSink"', () => { expect(defaultExport3).to.deep.eq({ export1: 'export1', export2: 'export2', + export3: 'export3', + export4: 'export4', + export5: 'export5', default: { export1: 'export1', export2: 'export2', @@ -80,6 +103,9 @@ describe('supports every combination of import syntax in a single file', () => { expect(name2).to.deep.eq({ export1: 'export1', export2: 'export2', + export3: 'export3', + export4: 'export4', + export5: 'export5', default: { export1: 'export1', export2: 'export2', @@ -92,6 +118,41 @@ describe('supports every combination of import syntax in a single file', () => { expect(e2).to.deep.eq(export2) }) + it(`import { + export3, + export4, +} from './fixtures/kitchenSink'`, () => { + expect(export3).to.deep.eq('export3') + expect(export4).to.deep.eq('export4') + }) + + it(`import { + export3 as alias3, + export4 as alias4, +} from './fixtures/kitchenSink'`, () => { + expect(alias3).to.deep.eq(export3) + expect(alias4).to.deep.eq(export4) + }) + + it(`import defaultExport4, { + export5, +} from './fixtures/kitchenSink'`, () => { + console.log(defaultExport4) + expect(defaultExport4).to.deep.eq({ + export1: 'export1', + export2: 'export2', + export3: 'export3', + export4: 'export4', + export5: 'export5', + default: { + export1: 'export1', + export2: 'export2', + }, + }) + + expect(export5).to.eq('export5') + }) + it('Import "./kitchenSink"', () => { // @ts-expect-error expect(window.sideEffect).to.eq('Side Effect') diff --git a/npm/vite-plugin-cypress-esm/src/index.ts b/npm/vite-plugin-cypress-esm/src/index.ts index e9416fb112a0..f67993af4549 100644 --- a/npm/vite-plugin-cypress-esm/src/index.ts +++ b/npm/vite-plugin-cypress-esm/src/index.ts @@ -120,7 +120,7 @@ export const CypressEsm = (options?: CypressEsmOptions): Plugin => { // Ensure import comes at start of line *or* is prefixed by a space so we don't capture things like // `Refresh.__hmr_import('') - const importRegex = /(?<=^|\s)import (.+?) from ['"](.*?)['"]/g + const importRegex = /(?<=^|\s)import ([^;'"]+?) from ['"](.*?)['"]/g return code.replace( importRegex,