Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

fix: Support class features when transforming source code #201

Merged
merged 1 commit into from
Mar 3, 2023

Conversation

dividedmind
Copy link
Contributor

appmap-agent-js uses @babel/parser to parse the ECMAScript code for transformation; the output generated by that library is not 100% estree-compliant [1]. It has an estree plugin that reverts the deviations; however, by default not all of them are supported (apparently for @babel/parser's own backward compatibility reasons). Fortunately it has a config setting that enables them.

One of these differences is that the standard node type of PropertyDefinition is called ClassProperty instead, which made the transformer unable to correctly handle ES classes.

[1] https://babeljs.io/docs/babel-parser#output

Fixes #199

@lachrist
Copy link
Contributor

lachrist commented Mar 3, 2023

The official name is MethodDefinition and not PropertyDefinition. It would be good to verify in testing that astring is handling it.

> require("acorn").parse("class c { m () {} }").body[0].body.body[0].type
'MethodDefinition'

https://github.com/estree/estree/blob/master/es2015.md#methoddefinition

@lachrist
Copy link
Contributor

lachrist commented Mar 3, 2023

It will be probably worthwhile to move to a babel-only format so that we do not have to rely to the less used astring and use @babel/generator instead. I tried doing that but it requires some non-trivial changes on the instrumentation visitors.

Copy link
Contributor

@lachrist lachrist left a comment

Choose a reason for hiding this comment

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

Good work!
Let's check that astring is handling PropertyDefinition first before merging.

@dividedmind
Copy link
Contributor Author

The official name is MethodDefinition and not PropertyDefinition. It would be good to verify in testing that astring is handling it.

> require("acorn").parse("class c { m () {} }").body[0].body.body[0].type
'MethodDefinition'

https://github.com/estree/estree/blob/master/es2015.md#methoddefinition

MethodDefinition is a different thing. This is about PropertyDefinition: https://github.com/estree/estree/blob/master/es2022.md#propertydefinition

@dividedmind
Copy link
Contributor Author

Good work! Let's check that astring is handling PropertyDefinition first before merging.

I checked manually, it seems to work fine.

appmap-agent-js uses @babel/parser to parse the ECMAScript code for
transformation; the output generated by that library is not 100%
estree-compliant [1]. It has an `estree` plugin that reverts the
deviations; however, by default not all of them are supported
(apparently for @babel/parser's own backward compatibility reasons).
Fortunately it has a config setting that enables them.

One of these differences is that the standard node type of
PropertyDefinition is called ClassProperty instead, which made the
transformer unable to correctly handle ES classes.

[1] https://babeljs.io/docs/babel-parser#output

Fixes #199
@dividedmind
Copy link
Contributor Author

Good work! Let's check that astring is handling PropertyDefinition first before merging.

Added a test on the instrumentation side.

@dividedmind
Copy link
Contributor Author

It will be probably worthwhile to move to a babel-only format so that we do not have to rely to the less used astring and use @babel/generator instead. I tried doing that but it requires some non-trivial changes on the instrumentation visitors.

I don't think that's a good idea unless it's really necessary. It will make it more difficult if we want to switch the parser and/or generator in the future.

@lachrist
Copy link
Contributor

lachrist commented Mar 3, 2023

It will be probably worthwhile to move to a babel-only format so that we do not have to rely to the less used astring and use @babel/generator instead. I tried doing that but it requires some non-trivial changes on the instrumentation visitors.

I don't think that's a good idea unless it's really necessary. It will make it more difficult if we want to switch the parser and/or generator in the future.

Yeah it is really unfortunate that babel defined its own ast format. Let's keep it like this for the moment then.

Copy link
Contributor

@lachrist lachrist left a comment

Choose a reason for hiding this comment

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

Good work, thanks!

@@ -84,7 +84,7 @@ const parseBabelLog = (content, url) => {
return parseBabel(content, {
sourceType: "module",
sourceFilename: url,
plugins: ["estree"],
plugins: [["estree", { classFeatures: true }]],
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI This file is used to build the agent and it does not contain any PropertyDefinition. But it is fine to add it as well here.

Copy link
Contributor Author

@dividedmind dividedmind Mar 3, 2023

Choose a reason for hiding this comment

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

I figured, but it turned up in grep and I thought it won't hurt for completeness ;)

@lachrist
Copy link
Contributor

lachrist commented Mar 3, 2023

MethodDefinition is a different thing. This is about PropertyDefinition: https://github.com/estree/estree/blob/master/es2022.md#propertydefinition

Arf, I should keep up with the standard :s

@dividedmind dividedmind merged commit 46f03b0 into main Mar 3, 2023
@dividedmind dividedmind deleted the fix/estree-class-features branch March 3, 2023 21:51
@appland-release
Copy link

🎉 This PR is included in version 13.5.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: this[statement.type] is not a function
3 participants