Skip to content

Commit d4a325d

Browse files
authored
[compiler] Add snap subcommand to minimize a test input (facebook#35663)
Snap now supports subcommands 'test' (default) and 'minimize`. The minimize subcommand attempts to minimize a single failing input fixture by incrementally simplifying the ast so long as the same error occurs. I spot-checked it and it seemed to work pretty well. This is intended for use in a new subagent designed for investigating bugs — fixture simplification is an important part of the process and we can automate this rather than light tokens on fire. Example Input: ```js function Component(props) { const x = []; let result; for (let i = 0; i < 10; i++) { if (cond) { try { result = {key: bar([props.cond && props.foo])}; } catch (e) { console.log(e); } } } x.push(result); return <Stringify x={x} />; } ``` Command output: ``` $ yarn snap minimize --path .../input.js Minimizing: .../input.js Minimizing................ --- Minimized Code --- function Component(props) { try { props && props; } catch (e) {} } Reduced from 16 lines to 5 lines ``` This demonstrates things like: * Removing one statement at at time * Replacing if/else with the test, consequent, or alternate. Similar for other control-flow statements including try/catch * Removing individual array/object expression properties * Replacing single-value array/object with the value * Replacing control-flow expression (logical, consequent) w the test or left/right values * Removing call arguments * Replacing calls with a single argument with the argument * Replacing calls with multiple arguments with an array of the arguments * Replacing optional member/call with non-optional versions * Replacing member expression with the object. If computed, also try replacing w the key * And a bunch more strategies, see the code
1 parent 7b023d7 commit d4a325d

File tree

4 files changed

+2189
-35
lines changed

4 files changed

+2189
-35
lines changed

compiler/packages/snap/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"dependencies": {
2424
"@babel/code-frame": "^7.22.5",
25+
"@babel/generator": "^7.19.1",
2526
"@babel/plugin-syntax-jsx": "^7.18.6",
2627
"@babel/preset-flow": "^7.7.4",
2728
"@babel/preset-typescript": "^7.26.0",
@@ -58,6 +59,7 @@
5859
"resolutions": {
5960
"./**/@babel/parser": "7.7.4",
6061
"./**/@babel/types": "7.7.4",
62+
"@babel/generator": "7.2.0",
6163
"@babel/preset-flow": "7.22.5"
6264
}
6365
}

0 commit comments

Comments
 (0)