Skip to content

[compiler] Add snap subcommand to minimize a test input#447

Closed
everettbu wants to merge 1 commit into
mainfrom
worktree-2026-01-30-14-40-20-error-binary-search2
Closed

[compiler] Add snap subcommand to minimize a test input#447
everettbu wants to merge 1 commit into
mainfrom
worktree-2026-01-30-14-40-20-error-binary-search2

Conversation

@everettbu

@everettbu everettbu commented Jan 31, 2026

Copy link
Copy Markdown

Mirror of facebook/react#35663
Original author: josephsavona


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:

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

@everettbu everettbu added CLA Signed React Core Team Opened by a member of the React Core Team labels Jan 31, 2026
@greptile-apps

greptile-apps Bot commented Jan 31, 2026

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

This PR adds a new minimize subcommand to the snap testing tool that automatically reduces failing test inputs while preserving compiler errors. The implementation uses systematic AST simplification strategies (removing statements, simplifying expressions, etc.) to iteratively minimize code.

Key changes:

  • Added comprehensive minimize.ts with 2070 lines implementing 30+ AST simplification strategies
  • Refactored runner.ts from direct CLI parsing to yargs subcommand architecture (test as default, minimize as new command)
  • Added @babel/generator dependency for code generation from AST

Issues found:

  • Dependency version conflict in package.json: requests @babel/generator@^7.19.1 but forces resolution to 7.2.0
  • Edge case bug in string parsing when input files lack newlines (two occurrences)

Confidence Score: 3/5

  • Safe to merge after fixing the dependency conflict and newline edge case bugs
  • The implementation is well-structured and comprehensive, but has two critical bugs that will cause runtime errors: the @babel/generator version conflict may cause unpredictable behavior, and the indexOf('\n') returning -1 will cause incorrect string slicing for single-line files
  • Pay close attention to compiler/packages/snap/package.json and compiler/packages/snap/src/minimize.ts lines 49 and 2039

Important Files Changed

Filename Overview
compiler/packages/snap/src/minimize.ts Added new minimization feature with comprehensive AST simplification strategies to reduce failing test cases while preserving compiler errors
compiler/packages/snap/src/runner.ts Refactored to support subcommands (test and minimize), moving from direct argument parsing to command-based structure
compiler/packages/snap/package.json Added @babel/generator dependency with conflicting version resolution that may cause issues

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@everettbu
everettbu force-pushed the worktree-2026-01-30-14-40-20-error-binary-search2 branch from 969b035 to 97d94d6 Compare February 2, 2026 18:24

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment on lines 24 to +25
"@babel/code-frame": "^7.22.5",
"@babel/generator": "^7.19.1",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conflicting @babel/generator versions - dependency requests ^7.19.1 but resolution forces 7.2.0

Suggested change
"@babel/code-frame": "^7.22.5",
"@babel/generator": "^7.19.1",
"@babel/code-frame": "^7.22.5",
"@babel/generator": "^7.2.0",
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/snap/package.json
Line: 24:25

Comment:
conflicting `@babel/generator` versions - dependency requests `^7.19.1` but resolution forces `7.2.0`

```suggestion
    "@babel/code-frame": "^7.22.5",
    "@babel/generator": "^7.2.0",
```

How can I resolve this? If you propose a fix, please make it concise.

// Read the input file
const input = fs.readFileSync(inputPath, 'utf-8');
const filename = path.basename(inputPath);
const firstLine = input.substring(0, input.indexOf('\n'));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential issue if file contains no newline - indexOf('\n') returns -1, causing substring(0, -1) to return empty string

Suggested change
const firstLine = input.substring(0, input.indexOf('\n'));
const firstLine = input.includes('\n') ? input.substring(0, input.indexOf('\n')) : input;
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/snap/src/minimize.ts
Line: 2039:2039

Comment:
potential issue if file contains no newline - `indexOf('\n')` returns -1, causing `substring(0, -1)` to return empty string

```suggestion
  const firstLine = input.includes('\n') ? input.substring(0, input.indexOf('\n')) : input;
```

How can I resolve this? If you propose a fix, please make it concise.

return {kind: 'parse_error', message: (e as Error).message};
}

const firstLine = code.substring(0, code.indexOf('\n'));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same issue - if code has no newline, indexOf('\n') returns -1

Suggested change
const firstLine = code.substring(0, code.indexOf('\n'));
const firstLine = code.includes('\n') ? code.substring(0, code.indexOf('\n')) : code;
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/snap/src/minimize.ts
Line: 49:49

Comment:
same issue - if `code` has no newline, `indexOf('\n')` returns -1

```suggestion
  const firstLine = code.includes('\n') ? code.substring(0, code.indexOf('\n')) : code;
```

How can I resolve this? If you propose a fix, please make it concise.

@everettbu

Copy link
Copy Markdown
Author

Upstream PR was closed or merged. Code is synced via branch mirror.

@everettbu everettbu closed this Feb 3, 2026
@everettbu
everettbu deleted the worktree-2026-01-30-14-40-20-error-binary-search2 branch February 3, 2026 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants