From 3e6b4e1f311b43aa1dc77d78150a601d9fe4b280 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 7 May 2026 19:36:11 +0200 Subject: [PATCH] fix: error unexpected token in fetch JS compatibility issue with Webpack 4 (#10864) * chore: added npm run lint step between install and build in build-and-run-vitest * chore: acorn ^8.16.0 added as a devDependency * test: added test to parse all lib files via acorn * feat: added globalThis: "readonly" * feat: ?? replaced with the verbose ternary --- .github/workflows/run-ci.yml | 2 ++ eslint.config.js | 13 ++++++++++--- lib/adapters/fetch.js | 5 ++++- package-lock.json | 1 + package.json | 1 + tests/unit/syntaxCompat.test.js | 30 ++++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/unit/syntaxCompat.test.js diff --git a/.github/workflows/run-ci.yml b/.github/workflows/run-ci.yml index bc12947a..e514f192 100644 --- a/.github/workflows/run-ci.yml +++ b/.github/workflows/run-ci.yml @@ -27,6 +27,8 @@ jobs: cache: npm - name: Install dependencies run: npm ci --ignore-scripts + - name: Lint + run: npm run lint - name: Build project run: npm run build - name: Install Playwright with deps diff --git a/eslint.config.js b/eslint.config.js index 411e17c4..ce2b5e0e 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -33,20 +33,27 @@ export default [ languageOptions: { globals: { ...globals.browser, - ...globals.node + ...globals.node, + globalThis: 'readonly' } } }, { files: ['lib/adapters/http.js', 'lib/platform/node/**/*.js'], languageOptions: { - globals: globals.node + globals: { + ...globals.node, + globalThis: 'readonly' + } } }, { files: ['lib/adapters/xhr.js', 'lib/platform/browser/**/*.js'], languageOptions: { - globals: globals.browser + globals: { + ...globals.browser, + globalThis: 'readonly' + } } } ]; diff --git a/lib/adapters/fetch.js b/lib/adapters/fetch.js index e0b2600e..b9015a9b 100644 --- a/lib/adapters/fetch.js +++ b/lib/adapters/fetch.js @@ -28,7 +28,10 @@ const test = (fn, ...args) => { }; const factory = (env) => { - const globalObject = utils.global ?? globalThis; + const globalObject = + utils.global !== undefined && utils.global !== null + ? utils.global + : globalThis; const { ReadableStream, TextEncoder } = globalObject; env = utils.merge.call( diff --git a/package-lock.json b/package-lock.json index bc85e2a4..fa34acba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "@vitest/browser": "^4.1.5", "@vitest/browser-playwright": "^4.1.5", "abortcontroller-polyfill": "^1.7.8", + "acorn": "^8.16.0", "body-parser": "^2.2.2", "chalk": "^5.6.2", "cross-env": "^10.1.0", diff --git a/package.json b/package.json index 7a7a8c56..4b2204f2 100644 --- a/package.json +++ b/package.json @@ -141,6 +141,7 @@ "@vitest/browser": "^4.1.5", "@vitest/browser-playwright": "^4.1.5", "abortcontroller-polyfill": "^1.7.8", + "acorn": "^8.16.0", "body-parser": "^2.2.2", "chalk": "^5.6.2", "cross-env": "^10.1.0", diff --git a/tests/unit/syntaxCompat.test.js b/tests/unit/syntaxCompat.test.js new file mode 100644 index 00000000..2b8e1766 --- /dev/null +++ b/tests/unit/syntaxCompat.test.js @@ -0,0 +1,30 @@ +import { describe, it, expect } from 'vitest'; +import { readFileSync, readdirSync, statSync } from 'node:fs'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { Parser } from 'acorn'; + +const LIB_DIR = fileURLToPath(new URL('../../lib/', import.meta.url)); +const ECMA_VERSION = 2018; + +function walk(dir) { + const out = []; + for (const name of readdirSync(dir)) { + const full = join(dir, name); + if (statSync(full).isDirectory()) out.push(...walk(full)); + else if (full.endsWith('.js')) out.push(full); + } + return out; +} + +describe('lib/ source files parse as ES2018', () => { + for (const file of walk(LIB_DIR)) { + const rel = file.slice(LIB_DIR.length); + it(rel, () => { + const src = readFileSync(file, 'utf8'); + expect(() => + Parser.parse(src, { ecmaVersion: ECMA_VERSION, sourceType: 'module' }) + ).not.toThrow(); + }); + } +});