Skip to content

Commit 73d1e5a

Browse files
committed
Optimizer: Add more comments
1 parent 9b2e5ed commit 73d1e5a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/optimizer/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ module.exports = {
4242
}
4343

4444
let result = new transform.TransformResult(ast);
45-
let prevResult;
45+
let prevResultString;
4646

4747
do {
48-
prevResult = result.toString();
48+
// Get a copy of the current state here so
49+
// we can compare it with the state at the
50+
// end of the loop.
51+
prevResultString = result.toString();
4952
ast = clone(result.getAST());
5053

5154
transformToApply.forEach(transformName => {
@@ -60,19 +63,24 @@ module.exports = {
6063

6164
const transformer = optimizationTransforms[transformName];
6265

66+
// Don't override result just yet since we
67+
// might want to rollback the transform
6368
let newResult = transform.transform(ast, transformer);
6469

6570
if (newResult.toString() !== result.toString()) {
6671
if (newResult.toString().length <= result.toString().length) {
6772
result = newResult;
6873
} else {
69-
// Result has changed but is not shorter: restore ast to its previous state.
74+
// Result has changed but is not shorter:
75+
// restore ast to its previous state.
7076
ast = clone(result.getAST());
7177
}
7278
}
7379
});
7480

75-
} while (result.toString() !== prevResult);
81+
// Keep running the optimizer until it stops
82+
// making any change to the regexp.
83+
} while (result.toString() !== prevResultString);
7684

7785
return result;
7886
},

0 commit comments

Comments
 (0)