Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #4939 for now #4946

Merged
merged 1 commit into from Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/finalisers/es.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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}`
);
Expand Down Expand Up @@ -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}`
} };`
);
}
Expand All @@ -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}`
);
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions 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;
Expand Down Expand Up @@ -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 {
_,
Expand Down Expand Up @@ -131,3 +130,5 @@ export function getGenerateCodeSnippets({

const wrapIfNeeded = (code: string, needsParens: boolean | undefined): string =>
needsParens ? `(${code})` : code;

const validPropertyName = /^(?!\d)[\w$]+$/;
5 changes: 0 additions & 5 deletions src/utils/isValidIdentifier.ts

This file was deleted.

6 changes: 2 additions & 4 deletions 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<string>,
forbiddenNames: Set<string> | 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;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions test/form/samples/illegal-identifiers-in-imports-exports/main.js

This file was deleted.