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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed imports/exports that are illegal identifiers in the es output #4939

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/finalisers/es.ts
Expand Up @@ -3,6 +3,7 @@ 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';

export default function es(
Expand Down Expand Up @@ -131,7 +132,11 @@ function getExportBlock(exports: ChunkExports, { _, cnst }: GenerateCodeSnippets
exportDeclaration.push(
specifier.exported === specifier.local
? specifier.local
: `${specifier.local} as ${specifier.exported}`
: `${specifier.local} as ${
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is the only place that has to be fixed here as no other code refers to specifier.exported and local specifiers (and reexporting ones) have to be legal already. It would be worth rechecking if I'm right though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isValidIdentifier(specifier.exported)
? specifier.exported
: JSON.stringify(specifier.exported)
}`
);
}
if (exportDeclaration.length > 0) {
Expand Down
7 changes: 3 additions & 4 deletions src/utils/generateCodeSnippets.ts
@@ -1,5 +1,6 @@
import type { NormalizedOutputOptions } from '../rollup/types';
import RESERVED_NAMES from './RESERVED_NAMES';
import { isValidIdentifier } from './isValidIdentifier';

export interface GenerateCodeSnippets {
_: string;
Expand Down Expand Up @@ -83,8 +84,8 @@ export function getGenerateCodeSnippets({
];

const isValidPropertyName = reservedNamesAsProps
? (name: string): boolean => validPropertyName.test(name)
: (name: string): boolean => !RESERVED_NAMES.has(name) && validPropertyName.test(name);
? isValidIdentifier
: (name: string): boolean => !RESERVED_NAMES.has(name) && isValidIdentifier(name);

return {
_,
Expand Down Expand Up @@ -130,5 +131,3 @@ export function getGenerateCodeSnippets({

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

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

export function isValidIdentifier(name: string): boolean {
return validIdentifier.test(name);
}
@@ -0,0 +1,6 @@
module.exports = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While chunkingForm is the only format to support multi-file output verification, this is not needed here and I would recommend to move this to a "form" test so that it will also verify UMD and IIFE output formats.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this to the requested location

description: 'correctly handles illegal identifiers in exports',
options: {
input: ['main']
}
};
@@ -0,0 +1,8 @@
define(['exports'], (function (exports) { 'use strict';

const legal = 10;

exports.legal = legal;
exports["馃敟illegal"] = legal;

}));
@@ -0,0 +1,6 @@
'use strict';

const legal = 10;

exports.legal = legal;
exports["馃敟illegal"] = legal;
@@ -0,0 +1,3 @@
const legal = 10;

export { legal, legal as "馃敟illegal" };
@@ -0,0 +1,10 @@
System.register([], (function (exports) {
'use strict';
return {
execute: (function () {

const legal = 10; exports({ legal: legal, '馃敟illegal': legal });

})
};
}));
3 changes: 3 additions & 0 deletions test/chunking-form/samples/export-illegal-identifier/main.js
@@ -0,0 +1,3 @@
export const legal = 10;

export { legal as '馃敟illegal' };