From 7a597b7d052b333db08089bad03d5ec5e4c85542 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 20 Sep 2022 10:27:07 +0800 Subject: [PATCH 1/4] test: add failing test --- test/exports.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/exports.test.ts b/test/exports.test.ts index a073320..94dbd09 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -130,6 +130,20 @@ describe('findExports', () => { const matches = findExports(code) expect(matches[0].names).toEqual(['foo', 'bar']) }) + + // https://github.com/nuxt/framework/issues/7658 + it('works the same with or without comment', () => { + const code1 = ` + export default function useMain() {}` + const matches1 = findExports(code1) + const code2 = ` +export default function useMain() {} +// export default function useMain() {}` + const matches2 = findExports(code2) + expect(matches1).toHaveLength(1) + expect(matches1[0].name).toEqual('default') + expect(matches2).toEqual(matches1) + }) }) describe('fineExportNames', () => { From ecfef014cc8c547bb8d2cbea85c2b943df8176bf Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 20 Sep 2022 10:35:02 +0800 Subject: [PATCH 2/4] fix(findExports): correctly filter out exports --- src/analyze.ts | 18 ++++++++++-------- test/exports.test.ts | 11 ++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index e27417b..e42ad6b 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -135,15 +135,14 @@ export function findExports (code: string): ESMExport[] { return [] } - return exports.filter((exp, index, exports) => { + return exports // Filter false positive export matches - if (exportLocations && !_isExportStatement(exportLocations, exp)) { - return false - } + .filter(exp => !exportLocations || _isExportStatement(exportLocations, exp)) // Prevent multiple exports of same function, only keep latest iteration of signatures - const nextExport = exports[index + 1] - return !nextExport || exp.type !== nextExport.type || !exp.name || exp.name !== nextExport.name - }) + .filter((exp, index, exports) => { + const nextExport = exports[index + 1] + return !nextExport || exp.type !== nextExport.type || !exp.name || exp.name !== nextExport.name + }) } export function findExportNames (code: string): string[] { @@ -179,7 +178,10 @@ interface TokenLocation { } function _isExportStatement (exportsLocation: TokenLocation[], exp: ESMExport) { - return exportsLocation.some(location => exp.start <= location.start && exp.end >= location.end) + return exportsLocation.some(location => + (exp.start <= location.start && location.start <= exp.end) || + (exp.start <= location.end && location.end <= exp.end) + ) } function _tryGetExportLocations (code: string) { diff --git a/test/exports.test.ts b/test/exports.test.ts index 94dbd09..7ec6819 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -1,4 +1,3 @@ -import { join } from 'path' import { describe, it, expect } from 'vitest' import { ESMExport, findExports, findExportNames, resolveModuleExportNames } from '../src' @@ -132,13 +131,15 @@ describe('findExports', () => { }) // https://github.com/nuxt/framework/issues/7658 - it('works the same with or without comment', () => { + it.only('works the same with or without comment', () => { const code1 = ` - export default function useMain() {}` - const matches1 = findExports(code1) +export default function useMain() {} +` const code2 = ` export default function useMain() {} -// export default function useMain() {}` +// export default function useMain() {} +` + const matches1 = findExports(code1) const matches2 = findExports(code2) expect(matches1).toHaveLength(1) expect(matches1[0].name).toEqual('default') From f00194f9d874686dab5f91d1fb4971804160deb9 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 20 Sep 2022 10:37:39 +0800 Subject: [PATCH 3/4] chore: update --- test/exports.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/exports.test.ts b/test/exports.test.ts index 7ec6819..8ff73f6 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -131,7 +131,7 @@ describe('findExports', () => { }) // https://github.com/nuxt/framework/issues/7658 - it.only('works the same with or without comment', () => { + it('works the same with or without comment', () => { const code1 = ` export default function useMain() {} ` From fbf5c419df1d938286934b745d4be14104d3514d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 20 Sep 2022 09:56:22 +0200 Subject: [PATCH 4/4] add comments and use inside checker --- src/analyze.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index e42ad6b..954bf3a 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -178,10 +178,13 @@ interface TokenLocation { } function _isExportStatement (exportsLocation: TokenLocation[], exp: ESMExport) { - return exportsLocation.some(location => - (exp.start <= location.start && location.start <= exp.end) || - (exp.start <= location.end && location.end <= exp.end) - ) + return exportsLocation.some((location) => { + // AST token inside the regex match + return exp.start <= location.start && exp.end >= location.end + // AST Token start or end is within the regex match + // return (exp.start <= location.start && location.start <= exp.end) || + // (exp.start <= location.end && location.end <= exp.end) + }) } function _tryGetExportLocations (code: string) {