From 3c9073a3f25bbe991721862c7b5867d5cdb3b895 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sat, 8 Jan 2022 09:47:27 +0300 Subject: [PATCH] fix: Reflect.get called on non-object closes #479 --- packages/vitest/src/node/execute.ts | 16 +++++++++------- test/cjs/package.json | 1 + test/cjs/test/interpret-default.test.tsx | 5 +++++ test/cjs/tsconfig.json | 5 +++++ 4 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 test/cjs/tsconfig.json diff --git a/packages/vitest/src/node/execute.ts b/packages/vitest/src/node/execute.ts index dd4265ca12c9..4447e7bafb47 100644 --- a/packages/vitest/src/node/execute.ts +++ b/packages/vitest/src/node/execute.ts @@ -33,10 +33,12 @@ function hasNestedDefault(target: any) { return '__esModule' in target && target.__esModule && 'default' in target.default } -function proxyMethod(name: 'get' | 'set' | 'has' | 'deleteProperty', isNested: boolean) { +function proxyMethod(name: 'get' | 'set' | 'has' | 'deleteProperty', tryDefault: boolean) { return function(target: any, key: string | symbol, ...args: [any?, any?]) { const result = Reflect[name](target, key, ...args) - if ((isNested && key === 'default') || !result) + if(typeof target.default !== 'object') + return result + if ((tryDefault && key === 'default') || typeof result === 'undefined') return Reflect[name](target.default, key, ...args) return result } @@ -46,12 +48,12 @@ export async function interpretedImport(path: string, interpretDefault: boolean) const mod = await import(path) if (interpretDefault && 'default' in mod) { - const isNested = hasNestedDefault(mod) + const tryDefault = hasNestedDefault(mod) return new Proxy(mod, { - get: proxyMethod('get', isNested), - set: proxyMethod('set', isNested), - has: proxyMethod('has', isNested), - deleteProperty: proxyMethod('deleteProperty', isNested), + get: proxyMethod('get', tryDefault), + set: proxyMethod('set', tryDefault), + has: proxyMethod('has', tryDefault), + deleteProperty: proxyMethod('deleteProperty', tryDefault), }) } diff --git a/test/cjs/package.json b/test/cjs/package.json index 5ae2ce801fe8..dea5bfe4de43 100644 --- a/test/cjs/package.json +++ b/test/cjs/package.json @@ -12,6 +12,7 @@ "givens": "1.3.9", "history": "^5.2.0", "prettier": "^2.5.1", + "temp-dir": "^2.0.0", "vitest": "workspace:*" } } \ No newline at end of file diff --git a/test/cjs/test/interpret-default.test.tsx b/test/cjs/test/interpret-default.test.tsx index bb2a9e13a481..8e6b36651758 100644 --- a/test/cjs/test/interpret-default.test.tsx +++ b/test/cjs/test/interpret-default.test.tsx @@ -1,6 +1,7 @@ import { expect, it } from 'vitest' import { format } from 'prettier' import givens from 'givens' +import tempDir from 'temp-dir' it('prettier', () => { expect(format('const a : A = \'t\'', { parser: 'typescript' }).trim()) @@ -11,3 +12,7 @@ it('has nested default', () => { expect(typeof givens).toBe('function') expect(givens.name).toBe('getGiven') }) + +it('nested default is not an object', () => { + expect(typeof tempDir).toBe('string') +}) diff --git a/test/cjs/tsconfig.json b/test/cjs/tsconfig.json new file mode 100644 index 000000000000..ffe4e79f7055 --- /dev/null +++ b/test/cjs/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "esModuleInterop": true + } +} \ No newline at end of file