-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
parse-function: guide/plugin for es6 class constructors #135
Comments
Thank you for raising this issue! We will try and get back to you as soon as possible. |
ok so i was able to create a plugin that handles this just in case anyone needs this right away. app.use(app => (node, result) => {
if (node.type === "ClassExpression") {
const nodeConstructor = node.body.body.find(b => b.kind === "constructor");
if (nodeConstructor) {
result.constructorArgs = nodeConstructor.params.map(n => n.name);
result.constructorParams = nodeConstructor.params.map(n => n.name).join(', ');
}
}
return result;
}); and now my results look like this {
name: null,
body: '',
args: [],
params: '',
defaults: {},
value: 'class MyClass {\n' +
' constructor(param1, param2) {\n' +
' this.something = param1;\n' +
' this.another = param2;\n' +
' }\n' +
'\n' +
' doSomething() {\n' +
' console.log(this.something);\n' +
' }\n' +
'}',
isValid: true,
isArrow: false,
isAsync: false,
isNamed: false,
isAnonymous: false,
isGenerator: false,
isExpression: false,
constructorArgs: [ 'param1', 'param2' ],
constructorParams: 'param1, param2'
} edit: this is not complete, it will not handle parameters with defaults, but it's a start |
Great report and thanks for the plugin. That's the beauty of plugins - anyone can fix his problems or find a temporary solution to them, almost immediately if he want. I guess, it's almost expected that it doesn't return what you expect. We completely depend on parse(myClass.constructor)
parse(MyClass.prototype.constructor) Also I think that |
We can also import the rest of the plugins manually and pass |
I forgot to mention in my original post that I had also tried I really do appreciate the extensibility the plugin system adds so thank you for that! I've got it working for my needs right now. This is what I'm using. I've added handling for parameters that have default values and for my use case I've separate those parameters from the other ones. Might not be the best solution for everyone but works for me. function getDefaultParams(paramNodes = []) {
return paramNodes
.filter((n) => n.type === "AssignmentPattern")
.map((d) => {
return {
param: d.left.name,
value: d.right.value,
};
});
}
app.use(app => (node, result) => {
result.constructor = {
args: [],
argsWithDefaults: []
};
if (node.type === "ClassExpression") {
const nodeConstructor = node.body.body.find(n => n.kind === "constructor");
if (nodeConstructor) {
result.constructor.argsWithDefaults = getDefaultParams(
nodeConstructor.params
);
result.constructor.args = nodeConstructor.params
.filter(p => p.type === "Identifier")
.map(n => n.name);
}
}
return result;
}); my example constructor now looks like this: and this is the result the above plugin produces. I'm not touching any of the existing properties, I'm only adding my own new property {
// ...
constructor: {
args: [
"param1",
"param2"
],
argsWithDefaults: [
{
param: "param3",
value: "test"
}
]
}
} |
@FranciscoG awesome! 😻 |
Support plan
Enterprise): Community
Context
What are you trying to achieve or the steps to reproduce?
When running
app.parse
on an es6 Class declaration that uses aconstructor
function, the results return an emptyargs
array.What was the result you got?
What result did you expect?
I expected
args
to be['param1', 'param2']
The text was updated successfully, but these errors were encountered: