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

Support default exported classes that extend other classes #4950

Merged
merged 1 commit into from Apr 21, 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
37 changes: 12 additions & 25 deletions src/ast/nodes/ExportDefaultDeclaration.ts
Expand Up @@ -21,18 +21,10 @@ function getDeclarationStart(code: string, start: number): number {
return findNonWhiteSpace(code, findFirstOccurrenceOutsideComment(code, 'default', start) + 7);
}

function getIdInsertPosition(
code: string,
declarationKeyword: string,
endMarker: string,
start: number
): number {
function getFunctionIdInsertPosition(code: string, start: number): number {
const declarationEnd =
findFirstOccurrenceOutsideComment(code, declarationKeyword, start) + declarationKeyword.length;
code = code.slice(
declarationEnd,
findFirstOccurrenceOutsideComment(code, endMarker, declarationEnd)
);
findFirstOccurrenceOutsideComment(code, 'function', start) + 'function'.length;
code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, '(', declarationEnd));
const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
if (generatorStarPos === -1) {
return declarationEnd;
Expand Down Expand Up @@ -76,18 +68,18 @@ export default class ExportDefaultDeclaration extends NodeBase {
this.renderNamedDeclaration(
code,
declarationStart,
'function',
'(',
this.declaration.id === null,
this.declaration.id === null
? getFunctionIdInsertPosition(code.original, declarationStart)
: null,
options
);
} else if (this.declaration instanceof ClassDeclaration) {
this.renderNamedDeclaration(
code,
declarationStart,
'class',
'{',
this.declaration.id === null,
this.declaration.id === null
? findFirstOccurrenceOutsideComment(code.original, 'class', start) + 'class'.length
: null,
options
);
} else if (this.variable.getOriginalVariable() !== this.variable) {
Expand All @@ -114,9 +106,7 @@ export default class ExportDefaultDeclaration extends NodeBase {
private renderNamedDeclaration(
code: MagicString,
declarationStart: number,
declarationKeyword: string,
endMarker: string,
needsId: boolean,
idInsertPosition: number | null,
options: RenderOptions
): void {
const {
Expand All @@ -128,11 +118,8 @@ export default class ExportDefaultDeclaration extends NodeBase {
// Remove `export default`
code.remove(this.start, declarationStart);

if (needsId) {
code.appendLeft(
getIdInsertPosition(code.original, declarationKeyword, endMarker, declarationStart),
` ${name}`
);
if (idInsertPosition !== null) {
code.appendLeft(idInsertPosition, ` ${name}`);
}
if (
format === 'system' &&
Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles default exported classes extending a regular expression argument (#4783)'
};
@@ -0,0 +1,7 @@
function foo(val) {
return val;
}

class main extends foo(/1/) {}

export { main as default };
@@ -0,0 +1,5 @@
function foo(val) {
return val;
}

export default class extends foo(/1/) {}