Fix: AST_Accessor missing start / end tokens#1493
Fix: AST_Accessor missing start / end tokens#1493OndrejSpanel wants to merge 7 commits intomishoo:masterfrom OpenGrabeso:prFixASTAccessorTokens
Conversation
|
Any test to show what issue it caused and whether this fix is working as intended? |
|
I will prepare the test. What location should I use for the test - a new file in |
|
|
test/mocha/accessorTokens-1492.js
Outdated
|
|
||
| // test there are no nodes without tokens | ||
| var checkWalker = new UglifyJS.TreeWalker(function(node, descend) { | ||
| assert(node.start !== undefined); |
There was a problem hiding this comment.
Hmm, can we be more precise about the value of start and end here?
Preferably with the source code under test spans multiple lines so we can reasonably sure the fields have been populated with sensible values?
lib/parse.js
Outdated
| var type = start.type; | ||
| var name = as_property_name(); | ||
| if (type == "name" && !is("punc", ":")) { | ||
| var createAccessor = function() { |
There was a problem hiding this comment.
This looks somewhat similar to embed_tokens() which is use extensively throughout this file. Would you mind considering whether we can reuse that in this case as well?
There was a problem hiding this comment.
I do not mind, however this is beyond my current JS abilities. What I have tried is:
a.push(new AST_ObjectGetter({
start : start,
key : as_atom_node(),
value : embed_tokens(function(){function_(AST_Accessor)}),
end : prev()
}));
This resulted in a parse error. If you can advise me how do to his properly, I will be glad to implement it.
There was a problem hiding this comment.
I'm typing from mobile, but this might work:
var createAccessor = embed_tokens(function() {
return function_(AST_Accessor);
});If not, I'll be back in a couple of hours and will take a look then.
There was a problem hiding this comment.
Tried that (and thought: "Silly me. off course I have to return in JS, this is not Scala "), but I still get the parsing error:
'SyntaxError: Unexpected token punc «(», expected punc «,»'
My code was:
var createAccessor = function() {embed_tokens(function() {
return function_(AST_Accessor);
})};
There was a problem hiding this comment.
I'm confused - did you try my suggestion or yours? Yours has an extra function() {...} wrapped around it.
There was a problem hiding this comment.
I tried both, first what you wrote, then this - I was afraid your solution executes outside of the get / set scopes, now I see it does not - it returns a function, and executes nothing on its own.
There was a problem hiding this comment.
Thanks for clarifying. I'll have a closer look when I get home.
There was a problem hiding this comment.
I have tried your solution once again and I see its error is different. It outputs no parsing error, only the test is not passed, with assertion:
AssertionError: 22 == 12
What is the correct result - In the test code var obj = { get latest() { return undefined; } }, where should the accessor node start? Pos. 22 is right behind latest, pos. 12 is the beginning of get.
There was a problem hiding this comment.
Checking http://lisperator.net/uglifyjs/ast, I think 22 makes more sense as a result, AST_Accessor is a child of AST_Lambda, and has no name. I am committing your implementation and adjusting the test,
There was a problem hiding this comment.
@OndrejSpanel thanks for the detailed investigation 👍
Adjusted test to match expected result.
| var checkWalker = new UglifyJS.TreeWalker(function(node, descend) { | ||
| if (node instanceof UglifyJS.AST_Accessor) { | ||
| assert.equal(node.start.pos, 22); | ||
| assert.equal(node.end.endpos, 46); |
There was a problem hiding this comment.
Doesn't the AST_Accessor start at 12 - the beginning of the get keyword?
get latest() { return undefined; }
There was a problem hiding this comment.
I think is is a matter of opinion. I will try to explain why I think with it should start at 22:
The AST hierarchy is:
AST_ObjectProperty contains
keyof typeAST_KeySymbolRefvalueof typeAST_Accessor
As key is what contains the property name, I think it makes a sense to let the AST_Accessor to start after it. Perhaps its name is a bit misleading and it should be called something like AST_AccessorBody, but I doubt you will want to break AST compatibility because of this.
There was a problem hiding this comment.
I see. For what it's worth, acorn reports the FunctionExpression start at 22 as well.
What is the start/end for the AST_ObjectProperty for get latest() { return undefined; }?
There was a problem hiding this comment.
The start/end for the AST_ObjectProperty is 12 / 46.
There was a problem hiding this comment.
Can you please check the AST_ObjectProperty start/end, as well as the AST_KeySymbolRef start/end in the test as well?
|
The build has failed because of timeout in completely unrelated code. I doubt it is related to my fix, but I do not know. |
|
@OndrejSpanel the build failed due to a spurious time-out, the fix of which is available but pending to be merged. You can restart the CI test by re-opening this PR. |
| assert.equal(node.start.pos, 12); | ||
| assert.equal(node.end.endpos, 46); | ||
|
|
||
| assert(node.key instanceof UglifyJS.AST_SymbolRef); |
There was a problem hiding this comment.
I could not use the same way as before, as Walker is not visiting the key node, therefore it needs to be accessed from its parent. I like it more this way anyway.
There was a problem hiding this comment.
Technically you don't need a tree walker at all. You could access each node from the toplevel ast node directly.
|
I think I have implemented all changes you required. Is there anything else? |
|
@OndrejSpanel IMHO two nits would be the naming ( Otherwise it looks functionally correct. If another reviewer approves, I'll lump this on top of #1485. Edit: also I think this project prefers the commits to be squashed into one. But I can do that if it proves to be too much hassle. |
|
@OndrejSpanel pushed to #1485 as 89e94b2 - thanks! |
Fix for #1492