-
Notifications
You must be signed in to change notification settings - Fork 7.6k
JSUtils modification to handle es6 constructs #13635
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functionality works great and I only have couple of nits.
src/language/JSUtils.js
Outdated
match; | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: whitespace
src/language/JSUtils.js
Outdated
|
||
PerfUtils.addMeasurement(PerfUtils.JSUTILS_REGEXP); | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: whitespace
src/language/JSUtils.js
Outdated
/** | ||
* Set of utilities for simple parsing of JS text. | ||
*/ | ||
define(function (require, exports, module) { | ||
"use strict"; | ||
|
||
var _ = require("thirdparty/lodash"); | ||
var Acorn_Loose = require("node_modules/acorn/dist/acorn_loose"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Should we be consistent and use AcornLoose
and ASTWalker
?
FWIW I think all of those three broken ones should be invalid |
functionList.push(new FileLocation(null, funcEntry.lineStart, chFrom, chTo, funcEntry.name)); | ||
var chFrom = funcEntry.columnStart; | ||
var chTo = funcEntry.columnEnd; | ||
functionList.push(new FileLocation(null, funcEntry.nameLineStart, chFrom, chTo, funcEntry.label || funcEntry.name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, you can write directly
functionList.push(new FileLocation(null, funcEntry.nameLineStart, funcEntry.columnStart, funcEntry.columnEnd, funcEntry.label || funcEntry.name));
@petetnt Addressed code review NITs . Added test cases for classes, inheritance, getter/setter and static methods. |
src/language/JSUtils.js
Outdated
var _ = require("thirdparty/lodash"); | ||
var _ = require("thirdparty/lodash"), | ||
AcornLoose = require("node_modules/acorn/dist/acorn_loose"), | ||
ASTWalker = require("node_modules/acorn/dist/walk"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm quite sure this will not work on dist.
You should add some logic here:
Line 143 in abdf55d
} |
Maybe going forward we should use more webpack, but in another PR.
src/language/JSUtils.js
Outdated
functionName = (match[2] || match[5]).trim(); | ||
|
||
|
||
var AST = AcornLoose.parse_dammit(text, {locations: true}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this work by checking indentation of the code.
So you need
- use always first Acorn.parse and if it fails use AcornLoose.parse_dammit
- add some tests of code not indented correctly (and / or without indentation at all)
src/language/JSUtils.js
Outdated
function <functionName> () {} | ||
*/ | ||
FunctionDeclaration: function (node) { | ||
if (node.id.name !== '✖') { // A hack to reject anonymous declarations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If Acorn use '✖' I don't think the commit is really good.
Maybe something saying that Acorn use '✖' to detect anonymous declarations and we want to skip them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider this snippet -
key: function () {
}
This is actually a broken syntax and acorn can't parse it, but acorn_loose does it. FYI the acorn_loose code which uses this as is -
LooseParser.prototype.dummyIdent = function dummyIdent () {
var dummy = this.dummyNode("Identifier");
dummy.name = "✖";
return dummy
};
The problem here is FunctionDeclaration
walk option catches this and if I don't have a check, ends up putting "✖" as a function name. To actually put key
as function name we have a LabeledStatement
walk option. For your reference this is how the node looks like -
This is not exactly a normal case but rather a broken syntax handler where this gets initialized with this name. I am not sure of any better way to handle this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @swmitra I meant comment not commit !
I simply want you to rephrase the commit to avoid the word hack, since is how acorn works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry @ficristo , misinterpreted the comment 😞 . I have changed the comment in the code now and also added the ArrowFunctionExpression construct along with addressing other code review comments and test cases.
Is JSUtils supposed to work on code not completed? If so you need to add some tests. |
Is it possible to detect arrow functions? i.e. |
Arrow functions (and functionProperties) worked when I tested, I assume that it works with async ones too |
Great! Make sure to add some test (if there aren't) |
This never got handled though - var foo = () => {} Adding a walk option for this now 😃 |
…ions with new test case
@ficristo Can you please have a look at the change list again? I have addressed all the comments . |
Are generator functions supposed to be handled? If so, please add a test. I just want to note I actually didn't test this PR, but the code seems good. |
* JSUtils modification to handle es6 constructs * Addressed review comments and added es6 test cases * Address Code review comments and add support for ArrowFunctionExpressions with new test case * Update comments * Refactor to remove extra variables
* JSUtils modification to handle es6 constructs * Addressed review comments and added es6 test cases * Address Code review comments and add support for ArrowFunctionExpressions with new test case * Update comments * Refactor to remove extra variables
This PR will replace the existing regex based function def extraction with Acorn and also adds the capability to identify classes and methods (es6). Our current implementations is having some limitations and issues. One issue which is noticed during my dev test is wrong location information for function definition. Consider the following snippet -
From the function/type list when we select
some
, instead of highlighting the member "some" , "some" fromHandsome
gets highlighted. With the new implementation, this issue is resolved as we get location information from the AST generated by Acorn.Couple of other observations which are actually open questions to all reviewers, there are some odd syntax which is considered valid in our current regex based implementation. Consider the following snippets -
It has been handled in the new implementation to let the existing tests pass, should we consider this to be valid?
Second observation is regarding a broken syntax for which we have a negative test case(this single test fails in the new implementation), but in the new implementation, Acorn parses it successfully. The snippet in consideration is -
How do we handle this case?
@zaggino @petetnt @ficristo @marcelgerber Can you please have a look at this PR?