From 4691cba1cb4b896a88f16e4ba7ef9a77dc45f645 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Tue, 18 Apr 2023 13:19:57 +0200 Subject: [PATCH] Revert #4939 for now --- src/finalisers/es.ts | 16 +++---- src/utils/generateCodeSnippets.ts | 7 +-- src/utils/isValidIdentifier.ts | 5 --- src/utils/safeName.ts | 6 +-- .../_config.js | 10 ----- .../_expected/amd.js | 41 ----------------- .../_expected/cjs.js | 41 ----------------- .../_expected/es.js | 10 ----- .../_expected/iife.js | 44 ------------------ .../_expected/system.js | 18 -------- .../_expected/umd.js | 45 ------------------- .../main.js | 11 ----- 12 files changed, 11 insertions(+), 243 deletions(-) delete mode 100644 src/utils/isValidIdentifier.ts delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/_config.js delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/_expected/amd.js delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/_expected/cjs.js delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/_expected/es.js delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/_expected/iife.js delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/_expected/system.js delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/_expected/umd.js delete mode 100644 test/form/samples/illegal-identifiers-in-imports-exports/main.js diff --git a/src/finalisers/es.ts b/src/finalisers/es.ts index bc83fcef919..ac49a9113c6 100644 --- a/src/finalisers/es.ts +++ b/src/finalisers/es.ts @@ -3,12 +3,8 @@ import type { ChunkDependency, ChunkExports, ImportSpecifier, ReexportSpecifier import type { NormalizedOutputOptions } from '../rollup/types'; import type { GenerateCodeSnippets } from '../utils/generateCodeSnippets'; import { getHelpersBlock } from '../utils/interopHelpers'; -import { isValidIdentifier } from '../utils/isValidIdentifier'; import type { FinaliserOptions } from './index'; -const safeExportName = (name: string): string => - isValidIdentifier(name) ? name : JSON.stringify(name); - export default function es( magicString: MagicStringBundle, { accessedGlobals, indent: t, intro, outro, dependencies, exports, snippets }: FinaliserOptions, @@ -72,7 +68,7 @@ function getImportBlock( .map(specifier => specifier.imported === specifier.local ? specifier.imported - : `${safeExportName(specifier.imported)} as ${specifier.local}` + : `${specifier.imported} as ${specifier.local}` ) .join(`,${_}`)}${_}}${_}from${_}${pathWithAssertion}` ); @@ -104,9 +100,7 @@ function getImportBlock( for (const specifier of namespaceReexports) { importBlock.push( `export${_}{${_}${ - name === specifier.reexported - ? name - : `${name} as ${safeExportName(specifier.reexported)}` + name === specifier.reexported ? name : `${name} as ${specifier.reexported}` } };` ); } @@ -116,8 +110,8 @@ function getImportBlock( `export${_}{${_}${namedReexports .map(specifier => specifier.imported === specifier.reexported - ? safeExportName(specifier.imported) - : `${safeExportName(specifier.imported)} as ${safeExportName(specifier.reexported)}` + ? specifier.imported + : `${specifier.imported} as ${specifier.reexported}` ) .join(`,${_}`)}${_}}${_}from${_}${pathWithAssertion}` ); @@ -137,7 +131,7 @@ function getExportBlock(exports: ChunkExports, { _, cnst }: GenerateCodeSnippets exportDeclaration.push( specifier.exported === specifier.local ? specifier.local - : `${specifier.local} as ${safeExportName(specifier.exported)}` + : `${specifier.local} as ${specifier.exported}` ); } if (exportDeclaration.length > 0) { diff --git a/src/utils/generateCodeSnippets.ts b/src/utils/generateCodeSnippets.ts index 3117859ec19..da7ee4fae48 100644 --- a/src/utils/generateCodeSnippets.ts +++ b/src/utils/generateCodeSnippets.ts @@ -1,6 +1,5 @@ import type { NormalizedOutputOptions } from '../rollup/types'; import RESERVED_NAMES from './RESERVED_NAMES'; -import { isValidIdentifier } from './isValidIdentifier'; export interface GenerateCodeSnippets { _: string; @@ -84,8 +83,8 @@ export function getGenerateCodeSnippets({ ]; const isValidPropertyName = reservedNamesAsProps - ? isValidIdentifier - : (name: string): boolean => !RESERVED_NAMES.has(name) && isValidIdentifier(name); + ? (name: string): boolean => validPropertyName.test(name) + : (name: string): boolean => !RESERVED_NAMES.has(name) && validPropertyName.test(name); return { _, @@ -131,3 +130,5 @@ export function getGenerateCodeSnippets({ const wrapIfNeeded = (code: string, needsParens: boolean | undefined): string => needsParens ? `(${code})` : code; + +const validPropertyName = /^(?!\d)[\w$]+$/; diff --git a/src/utils/isValidIdentifier.ts b/src/utils/isValidIdentifier.ts deleted file mode 100644 index 1ad7b168221..00000000000 --- a/src/utils/isValidIdentifier.ts +++ /dev/null @@ -1,5 +0,0 @@ -const validIdentifier = /^(?!\d)[\w$]+$/; - -export function isValidIdentifier(name: string): boolean { - return validIdentifier.test(name); -} diff --git a/src/utils/safeName.ts b/src/utils/safeName.ts index 20342acc669..560d5a10ca8 100644 --- a/src/utils/safeName.ts +++ b/src/utils/safeName.ts @@ -1,17 +1,15 @@ import RESERVED_NAMES from './RESERVED_NAMES'; import { toBase64 } from './base64'; -import { isValidIdentifier } from './isValidIdentifier'; export function getSafeName( baseName: string, usedNames: Set, forbiddenNames: Set | null ): string { - const safeBase = isValidIdentifier(baseName) ? baseName : '_safe'; - let safeName = safeBase; + let safeName = baseName; let count = 1; while (usedNames.has(safeName) || RESERVED_NAMES.has(safeName) || forbiddenNames?.has(safeName)) { - safeName = `${safeBase}$${toBase64(count++)}`; + safeName = `${baseName}$${toBase64(count++)}`; } usedNames.add(safeName); return safeName; diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/_config.js b/test/form/samples/illegal-identifiers-in-imports-exports/_config.js deleted file mode 100644 index ddda2482ff9..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/_config.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - description: 'correctly handles illegal identifiers in exports/imports', - options: { - input: ['main'], - output: { - name: 'illegalIdentifiers' - }, - external: ['external'] - } -}; diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/amd.js b/test/form/samples/illegal-identifiers-in-imports-exports/_expected/amd.js deleted file mode 100644 index 7714dd2f464..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/amd.js +++ /dev/null @@ -1,41 +0,0 @@ -define(['exports', 'external'], (function (exports, external) { 'use strict'; - - function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); - } - - var external__namespace = /*#__PURE__*/_interopNamespaceDefault(external); - - console.log(external[":"], external["๐Ÿคทโ€โ™‚๏ธ"]); // retain those local bindings - - const legal = 10; - - Object.defineProperty(exports, '-', { - enumerable: true, - get: function () { return external.bar; } - }); - Object.defineProperty(exports, '/', { - enumerable: true, - get: function () { return external["/"]; } - }); - exports["๐Ÿ…"] = external__namespace; - Object.defineProperty(exports, '๐Ÿ˜ญ', { - enumerable: true, - get: function () { return external["๐Ÿ˜‚"]; } - }); - exports["๐Ÿ”ฅillegal"] = legal; - -})); diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/cjs.js b/test/form/samples/illegal-identifiers-in-imports-exports/_expected/cjs.js deleted file mode 100644 index 9680e49ac63..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/cjs.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - -var external = require('external'); - -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - -var external__namespace = /*#__PURE__*/_interopNamespaceDefault(external); - -console.log(external[":"], external["๐Ÿคทโ€โ™‚๏ธ"]); // retain those local bindings - -const legal = 10; - -Object.defineProperty(exports, '-', { - enumerable: true, - get: function () { return external.bar; } -}); -Object.defineProperty(exports, '/', { - enumerable: true, - get: function () { return external["/"]; } -}); -exports["๐Ÿ…"] = external__namespace; -Object.defineProperty(exports, '๐Ÿ˜ญ', { - enumerable: true, - get: function () { return external["๐Ÿ˜‚"]; } -}); -exports["๐Ÿ”ฅillegal"] = legal; diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/es.js b/test/form/samples/illegal-identifiers-in-imports-exports/_expected/es.js deleted file mode 100644 index 7fdcae43e5f..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/es.js +++ /dev/null @@ -1,10 +0,0 @@ -import { ":" as _safe, "๐Ÿคทโ€โ™‚๏ธ" as _safe$1 } from 'external'; -import * as external from 'external'; -export { external as "๐Ÿ…" }; -export { bar as "-", "/", "๐Ÿ˜‚" as "๐Ÿ˜ญ" } from 'external'; - -console.log(_safe, _safe$1); // retain those local bindings - -const legal = 10; - -export { legal as "๐Ÿ”ฅillegal" }; diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/iife.js b/test/form/samples/illegal-identifiers-in-imports-exports/_expected/iife.js deleted file mode 100644 index 8858b6fed35..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/iife.js +++ /dev/null @@ -1,44 +0,0 @@ -var illegalIdentifiers = (function (exports, external) { - 'use strict'; - - function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); - } - - var external__namespace = /*#__PURE__*/_interopNamespaceDefault(external); - - console.log(external[":"], external["๐Ÿคทโ€โ™‚๏ธ"]); // retain those local bindings - - const legal = 10; - - Object.defineProperty(exports, '-', { - enumerable: true, - get: function () { return external.bar; } - }); - Object.defineProperty(exports, '/', { - enumerable: true, - get: function () { return external["/"]; } - }); - exports["๐Ÿ…"] = external__namespace; - Object.defineProperty(exports, '๐Ÿ˜ญ', { - enumerable: true, - get: function () { return external["๐Ÿ˜‚"]; } - }); - exports["๐Ÿ”ฅillegal"] = legal; - - return exports; - -})({}, external); diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/system.js b/test/form/samples/illegal-identifiers-in-imports-exports/_expected/system.js deleted file mode 100644 index 08516b3b24d..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/system.js +++ /dev/null @@ -1,18 +0,0 @@ -System.register('illegalIdentifiers', ['external'], (function (exports) { - 'use strict'; - var _safe, _safe$1; - return { - setters: [function (module) { - _safe = module[":"]; - _safe$1 = module["๐Ÿคทโ€โ™‚๏ธ"]; - exports({ '-': module.bar, '/': module["/"], '๐Ÿ…': module, '๐Ÿ˜ญ': module["๐Ÿ˜‚"] }); - }], - execute: (function () { - - console.log(_safe, _safe$1); // retain those local bindings - - const legal = exports('๐Ÿ”ฅillegal', 10); - - }) - }; -})); diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/umd.js b/test/form/samples/illegal-identifiers-in-imports-exports/_expected/umd.js deleted file mode 100644 index b2e85246135..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/_expected/umd.js +++ /dev/null @@ -1,45 +0,0 @@ -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) : - typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.illegalIdentifiers = {}, global.external)); -})(this, (function (exports, external) { 'use strict'; - - function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); - } - - var external__namespace = /*#__PURE__*/_interopNamespaceDefault(external); - - console.log(external[":"], external["๐Ÿคทโ€โ™‚๏ธ"]); // retain those local bindings - - const legal = 10; - - Object.defineProperty(exports, '-', { - enumerable: true, - get: function () { return external.bar; } - }); - Object.defineProperty(exports, '/', { - enumerable: true, - get: function () { return external["/"]; } - }); - exports["๐Ÿ…"] = external__namespace; - Object.defineProperty(exports, '๐Ÿ˜ญ', { - enumerable: true, - get: function () { return external["๐Ÿ˜‚"]; } - }); - exports["๐Ÿ”ฅillegal"] = legal; - -})); diff --git a/test/form/samples/illegal-identifiers-in-imports-exports/main.js b/test/form/samples/illegal-identifiers-in-imports-exports/main.js deleted file mode 100644 index fca9262d46c..00000000000 --- a/test/form/samples/illegal-identifiers-in-imports-exports/main.js +++ /dev/null @@ -1,11 +0,0 @@ -import { ':' as baz, '๐Ÿคทโ€โ™‚๏ธ' as bazinga } from 'external'; -console.log(baz, bazinga); // retain those local bindings - -const legal = 10; - -export { legal as '๐Ÿ”ฅillegal' }; - -export { bar as '-', '/', '๐Ÿ˜‚' as '๐Ÿ˜ญ' } from 'external'; - -import * as lib from 'external'; -export { lib as '๐Ÿ…' }