Skip to content

Commit

Permalink
Fix range of export declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 25, 2022
1 parent b688e1b commit 592e9d0
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 162 deletions.
@@ -1,10 +1,8 @@
/* 3 */
/* 1 */@dec1
/* 2 */@dec2
export /* 4 */class /* 5 */C /* 6 */ {/* 7 */} /* 8 */
/* 3 */export /* 4 */class /* 5 */C /* 6 */ {/* 7 */} /* 8 */

/* A */
/* C */
@dec1
/* B */@dec2
export default /* E */class /* F */{/* G */} /* H */
/* C */export default /* E */class /* F */{/* G */} /* H */
@@ -1,10 +1,8 @@
/* 3 */
/* 1 */@dec1
/* 2 */@dec2
export /* 4 */class /* 5 */C /* 6 */ {/* 7 */} /* 8 */
/* 3 */export /* 4 */class /* 5 */C /* 6 */ {/* 7 */} /* 8 */

/* A */
/* C */
@dec1
/* B */@dec2
export default /* E */class /* F */{/* G */} /* H */
/* C */export default /* E */class /* F */{/* G */} /* H */
14 changes: 8 additions & 6 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -549,13 +549,15 @@ export default abstract class StatementParser extends ExpressionParser {

maybeTakeDecorators<T extends N.Class>(
maybeDecorators: N.Decorator[] | null,
node: T,
classNode: T,
exportNode?: Undone<N.ExportDefaultDeclaration | N.ExportNamedDeclaration>,
): T {
if (maybeDecorators) {
node.decorators = maybeDecorators;
this.resetStartLocationFromNode(node, maybeDecorators[0]);
classNode.decorators = maybeDecorators;
this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
}
return node;
return classNode;
}

canHaveLeadingDecorator(): boolean {
Expand Down Expand Up @@ -2184,7 +2186,7 @@ export default abstract class StatementParser extends ExpressionParser {
const node2 = node as Undone<N.ExportNamedDeclaration>;
this.checkExport(node2, true, false, !!node2.source);
if (node2.declaration?.type === "ClassDeclaration") {
this.maybeTakeDecorators(decorators, node2.declaration);
this.maybeTakeDecorators(decorators, node2.declaration, node2);
} else if (decorators) {
throw this.raise(Errors.UnsupportedDecoratorExport, { at: node });
}
Expand All @@ -2198,7 +2200,7 @@ export default abstract class StatementParser extends ExpressionParser {
node2.declaration = decl;

if (decl.type === "ClassDeclaration") {
this.maybeTakeDecorators(decorators, decl as N.ClassDeclaration);
this.maybeTakeDecorators(decorators, decl as N.ClassDeclaration, node2);
} else if (decorators) {
throw this.raise(Errors.UnsupportedDecoratorExport, { at: node });
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
"body": [
{
"type": "ExportNamedDeclaration",
"start":36,"end":86,"loc":{"start":{"line":2,"column":0,"index":36},"end":{"line":2,"column":50,"index":86}},
"start":8,"end":86,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":2,"column":50,"index":86}},
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -54,13 +54,6 @@
}
]
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " 4 ",
"start":43,"end":50,"loc":{"start":{"line":2,"column":7,"index":43},"end":{"line":2,"column":14,"index":50}}
}
],
"decorators": [
{
"type": "Decorator",
Expand All @@ -76,13 +69,6 @@
"value": " 2 ",
"start":14,"end":21,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":21,"index":21}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 1 ",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}}
}
]
},
{
Expand All @@ -108,6 +94,13 @@
}
]
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 4 ",
"start":43,"end":50,"loc":{"start":{"line":2,"column":7,"index":43},"end":{"line":2,"column":14,"index":50}}
}
]
},
"trailingComments": [
Expand All @@ -125,14 +118,14 @@
"leadingComments": [
{
"type": "CommentBlock",
"value": " 3 ",
"start":28,"end":35,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":35,"index":35}}
"value": " 1 ",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}}
}
]
},
{
"type": "ExportDefaultDeclaration",
"start":132,"end":188,"loc":{"start":{"line":5,"column":0,"index":132},"end":{"line":5,"column":56,"index":188}},
"start":104,"end":188,"loc":{"start":{"line":4,"column":8,"index":104},"end":{"line":5,"column":56,"index":188}},
"declaration": {
"type": "ClassDeclaration",
"start":104,"end":188,"loc":{"start":{"line":4,"column":8,"index":104},"end":{"line":5,"column":56,"index":188}},
Expand All @@ -157,13 +150,6 @@
}
]
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " E ",
"start":155,"end":162,"loc":{"start":{"line":5,"column":23,"index":155},"end":{"line":5,"column":30,"index":162}}
}
],
"decorators": [
{
"type": "Decorator",
Expand All @@ -179,18 +165,6 @@
"value": " B ",
"start":110,"end":117,"loc":{"start":{"line":4,"column":14,"index":110},"end":{"line":4,"column":21,"index":117}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 8 ",
"start":87,"end":94,"loc":{"start":{"line":2,"column":51,"index":87},"end":{"line":2,"column":58,"index":94}}
},
{
"type": "CommentBlock",
"value": " A ",
"start":96,"end":103,"loc":{"start":{"line":4,"column":0,"index":96},"end":{"line":4,"column":7,"index":103}}
}
]
},
{
Expand All @@ -216,6 +190,13 @@
}
]
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " E ",
"start":155,"end":162,"loc":{"start":{"line":5,"column":23,"index":155},"end":{"line":5,"column":30,"index":162}}
}
]
},
"innerComments": [
Expand All @@ -235,8 +216,13 @@
"leadingComments": [
{
"type": "CommentBlock",
"value": " C ",
"start":124,"end":131,"loc":{"start":{"line":4,"column":28,"index":124},"end":{"line":4,"column":35,"index":131}}
"value": " 8 ",
"start":87,"end":94,"loc":{"start":{"line":2,"column":51,"index":87},"end":{"line":2,"column":58,"index":94}}
},
{
"type": "CommentBlock",
"value": " A ",
"start":96,"end":103,"loc":{"start":{"line":4,"column":0,"index":96},"end":{"line":4,"column":7,"index":103}}
}
]
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
"body": [
{
"type": "ExportNamedDeclaration",
"start":36,"end":86,"loc":{"start":{"line":2,"column":0,"index":36},"end":{"line":2,"column":50,"index":86}},
"start":8,"end":86,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":2,"column":50,"index":86}},
"specifiers": [],
"source": null,
"declaration": {
Expand Down Expand Up @@ -54,13 +54,6 @@
}
]
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " 4 ",
"start":43,"end":50,"loc":{"start":{"line":2,"column":7,"index":43},"end":{"line":2,"column":14,"index":50}}
}
],
"decorators": [
{
"type": "Decorator",
Expand All @@ -76,13 +69,6 @@
"value": " 2 ",
"start":14,"end":21,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":21,"index":21}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 1 ",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}}
}
]
},
{
Expand All @@ -108,6 +94,13 @@
}
]
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 4 ",
"start":43,"end":50,"loc":{"start":{"line":2,"column":7,"index":43},"end":{"line":2,"column":14,"index":50}}
}
]
},
"trailingComments": [
Expand All @@ -125,14 +118,14 @@
"leadingComments": [
{
"type": "CommentBlock",
"value": " 3 ",
"start":28,"end":35,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":35,"index":35}}
"value": " 1 ",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}}
}
]
},
{
"type": "ExportDefaultDeclaration",
"start":132,"end":188,"loc":{"start":{"line":5,"column":0,"index":132},"end":{"line":5,"column":56,"index":188}},
"start":104,"end":188,"loc":{"start":{"line":4,"column":8,"index":104},"end":{"line":5,"column":56,"index":188}},
"declaration": {
"type": "ClassDeclaration",
"start":104,"end":188,"loc":{"start":{"line":4,"column":8,"index":104},"end":{"line":5,"column":56,"index":188}},
Expand All @@ -157,13 +150,6 @@
}
]
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " E ",
"start":155,"end":162,"loc":{"start":{"line":5,"column":23,"index":155},"end":{"line":5,"column":30,"index":162}}
}
],
"decorators": [
{
"type": "Decorator",
Expand All @@ -179,18 +165,6 @@
"value": " B ",
"start":110,"end":117,"loc":{"start":{"line":4,"column":14,"index":110},"end":{"line":4,"column":21,"index":117}}
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " 8 ",
"start":87,"end":94,"loc":{"start":{"line":2,"column":51,"index":87},"end":{"line":2,"column":58,"index":94}}
},
{
"type": "CommentBlock",
"value": " A ",
"start":96,"end":103,"loc":{"start":{"line":4,"column":0,"index":96},"end":{"line":4,"column":7,"index":103}}
}
]
},
{
Expand All @@ -216,6 +190,13 @@
}
]
}
],
"leadingComments": [
{
"type": "CommentBlock",
"value": " E ",
"start":155,"end":162,"loc":{"start":{"line":5,"column":23,"index":155},"end":{"line":5,"column":30,"index":162}}
}
]
},
"innerComments": [
Expand All @@ -235,8 +216,13 @@
"leadingComments": [
{
"type": "CommentBlock",
"value": " C ",
"start":124,"end":131,"loc":{"start":{"line":4,"column":28,"index":124},"end":{"line":4,"column":35,"index":131}}
"value": " 8 ",
"start":87,"end":94,"loc":{"start":{"line":2,"column":51,"index":87},"end":{"line":2,"column":58,"index":94}}
},
{
"type": "CommentBlock",
"value": " A ",
"start":96,"end":103,"loc":{"start":{"line":4,"column":0,"index":96},"end":{"line":4,"column":7,"index":103}}
}
]
}
Expand Down
Expand Up @@ -9,23 +9,12 @@
"body": [
{
"type": "ExportNamedDeclaration",
"start":5,"end":22,"loc":{"start":{"line":2,"column":0,"index":5},"end":{"line":2,"column":17,"index":22}},
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":17,"index":22}},
"specifiers": [],
"source": null,
"declaration": {
"type": "ClassDeclaration",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":17,"index":22}},
"decorators": [
{
"type": "Decorator",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":4,"index":4}},
"expression": {
"type": "Identifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":4,"index":4},"identifierName":"foo"},
"name": "foo"
}
}
],
"id": {
"type": "Identifier",
"start":18,"end":19,"loc":{"start":{"line":2,"column":13,"index":18},"end":{"line":2,"column":14,"index":19},"identifierName":"A"},
Expand All @@ -36,7 +25,18 @@
"type": "ClassBody",
"start":20,"end":22,"loc":{"start":{"line":2,"column":15,"index":20},"end":{"line":2,"column":17,"index":22}},
"body": []
}
},
"decorators": [
{
"type": "Decorator",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":4,"index":4}},
"expression": {
"type": "Identifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":4,"index":4},"identifierName":"foo"},
"name": "foo"
}
}
]
}
}
],
Expand Down

0 comments on commit 592e9d0

Please sign in to comment.