Skip to content

Conversation

@re-engineer
Copy link

Currently, the following code causes shift-refactor to crash (infinite recursion):

const {parseScript}=require("shift-parser");
const RefactorSession=require("shift-refactor");
const script=`
window['example string with spaces'];
`;
const session=new RefactorSession(parseScript(script));
session.convertComputedToStatic();

Example code here
It happens because RefactorSession.prototype.convertComputedToStatic uses RefactorSession.prototype.replaceRecursive, and its replacement program returns the original node in certain cases. When the expression.value of a ComputedMemberExpression or any of its variants contains a space, the function will return the original node (signifying that there should be no replacement). When RefactorSession.prototype.replace is run in RefactorSession.prototype.replaceRecursive, the nodes that were not replaced are still counted in the number of replacements. This means that even though there are no more nodes to replace, RefactorSession.prototype.replaceRecursive runs again. This PR solves the issue by changing RefactorSession.prototype.replace to count only changed nodes.

Currently, the following code causes `shift-refactor` to crash (infinite recursion):
[```js
const {parseScript}=require("shift-parser");
const RefactorSession=require("shift-refactor");
const script=`
window['example string with spaces'];
`;
const session=new RefactorSession(parseScript(script));
session.convertComputedToStatic();
```](https://runkit.com/embed/5t8xd3wbw1am)
It happens because `RefactorSession.prototype.convertComputedToStatic` uses `RefactorSession.prototype.replaceRecursive`, and its replacement program returns the original node in certain cases. When the `expression.value` of a `ComputedMemberExpression` or any of its variants contains a space, the function will return the original node (signifying that there should be no replacement). When `RefactorSession.prototype.replace` is run in `RefactorSession.prototype.replaceRecursive`, the nodes that were not replaced are still counted in the number of replacements. This means that even though there are no more nodes to replace, `RefactorSession.prototype.replaceRecursive` runs again. This PR solves the issue by changing `RefactorSession.prototype.replace` to count only changed nodes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant