diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69e651f2..4cb8d1aa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - - run: npm install + - run: yarn install - run: npm run lint - run: npm run test - run: npm run coverage diff --git a/package.json b/package.json index 4931a8d3..459d6bcd 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,9 @@ "ts-jest": "^24.0.2", "typescript": "^3.4.4" }, + "resolutions": { + "eslint-plugin-import": "2.20.2" + }, "jest": { "coverageDirectory": "./coverage/", "collectCoverage": true, diff --git a/packages/icestark-sandbox/package.json b/packages/icestark-sandbox/package.json index ea10d880..697a81a6 100644 --- a/packages/icestark-sandbox/package.json +++ b/packages/icestark-sandbox/package.json @@ -1,6 +1,6 @@ { "name": "@ice/sandbox", - "version": "1.0.4", + "version": "1.0.5", "description": "sandbox for execute scripts", "main": "lib/index.js", "scripts": { diff --git a/packages/icestark-sandbox/src/index.ts b/packages/icestark-sandbox/src/index.ts index 3a872159..74438eb3 100644 --- a/packages/icestark-sandbox/src/index.ts +++ b/packages/icestark-sandbox/src/index.ts @@ -7,12 +7,14 @@ export interface SandboxContructor { } // check window contructor function, like Object Array function isConstructor(fn) { - const functionStr = fn.toString(); + // generator function and has own prototype properties + const hasConstructor = fn.prototype && fn.prototype.constructor === fn && Object.getOwnPropertyNames(fn.prototype).length > 1; + // unnecessary to call toString if it has contructor function + const functionStr = !hasConstructor && fn.toString(); const upperCaseRegex = /^function\s+[A-Z]/; return ( - // generator function and has own prototype properties - (fn.prototype && fn.prototype.constructor === fn && Object.getOwnPropertyNames(fn.prototype).length > 1) || + hasConstructor || // upper case upperCaseRegex.test(functionStr) || // ES6 class, window function do not have this case diff --git a/packages/icestark-sandbox/tests/index.spec.ts b/packages/icestark-sandbox/tests/index.spec.ts index 2ea9ddd7..6ecbd7ef 100644 --- a/packages/icestark-sandbox/tests/index.spec.ts +++ b/packages/icestark-sandbox/tests/index.spec.ts @@ -33,4 +33,14 @@ describe('sandbox: default props', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any expect((window as any).a).toBe(undefined); }); +}); + +describe('sandbox: access contructor', () => { + const sandbox = new Sandbox(); + + test('execute global functions', () => { + sandbox.execScriptInSandbox('window.error = new Error("errmsg");Error.toString();'); + const globalWindow = sandbox.getSandbox(); + expect((globalWindow as any).error.toString()).toBe('Error: errmsg'); + }); }); \ No newline at end of file diff --git a/setupJest.ts b/setupJest.ts index 75b3ddfc..950d8f94 100644 --- a/setupJest.ts +++ b/setupJest.ts @@ -1,6 +1,6 @@ import { GlobalWithFetchMock } from 'jest-fetch-mock'; -const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock; +const customGlobal: GlobalWithFetchMock = global as any; customGlobal.fetch = require('jest-fetch-mock'); customGlobal.fetchMock = customGlobal.fetch;