Skip to content

Commit

Permalink
./scripts/fiber/find-errors to make future Umbrella task easier
Browse files Browse the repository at this point in the history
In facebook#7925 there is a task to:

> Ensure we replace errors with invariant calls and they have sensible
> messages

While it is likely a bit premature to begin that work, this script will
make it easier to find and replace the exact call sites to manage.

Usage:
``` sh
$ ./scripts/fiber/find-errors

...lots of output...

src/renderers/shared/fiber/ReactFiberScheduler.js#921:
  throw new Error('No error for given unit of work.');

src/renderers/shared/fiber/ReactFiberScheduler.js#940:
  throw new Error('Invalid type of work.');

src/renderers/shared/utils/ReactErrorUtils.js#55:
  throw error;

All done.
Results:
6 errors
0 unmodified
380 skipped
0 ok
Stats:
error: 42
Time elapsed: 4.031seconds
42 throw statements to to convert to `invariant`

```

``` sh
$ ./scripts/fiber/find-errors --quiet

...lots of output...

src/renderers/shared/fiber/ReactFiberScheduler.js#780:
src/renderers/shared/fiber/ReactFiberScheduler.js#785:
src/renderers/shared/fiber/ReactFiberScheduler.js#921:
src/renderers/shared/fiber/ReactFiberScheduler.js#940:
src/renderers/shared/utils/ReactErrorUtils.js#55:
All done.
Results:
6 errors
0 unmodified
380 skipped
0 ok
Stats:
error: 42
Time elapsed: 3.728seconds
42 throw statements to to convert to `invariant`
```
  • Loading branch information
iamdustan committed Dec 21, 2016
1 parent afd4a54 commit 6699170
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
28 changes: 28 additions & 0 deletions scripts/fiber/find-errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

'use strict';

const jscodeshift = require('jscodeshift');
const Runner = require('jscodeshift/dist/Runner');
const path = require('path');
console.log(jscodeshift.Runner);

const options = {
path: [path.join(process.cwd(), 'src')],
dry: true,
cpus: 1,
transform: path.join(__dirname, 'jscodeshift-find-error.js'),
};

process.env.JSCODESHIFT_PRINT_LEVEL = process.argv.some(arg => /quiet/.test(arg)) ? 'quiet' : '';
console.log('Detecting Errors to convert to invariant calls.');
Runner.run(
options.transform,
options.path,
options
).then((results) => {
console.log(
'%s throw statements to to convert to `invariant`',
results.stats.error
);
});
31 changes: 31 additions & 0 deletions scripts/fiber/jscodeshift-find-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
const printLevel = process.env.JSCODESHIFT_PRINT_LEVEL;

module.exports = (fileInfo, api) => {
const jscodeshift = api.jscodeshift;
if (
/__tests__|vendor/.test(fileInfo.path)
) {
return;
}
// slice off process.cwd() and leading /
const normalizedPath = fileInfo.path.replace(process.cwd(), '').slice(1);

jscodeshift(fileInfo.source)
.find(jscodeshift.ThrowStatement)
.forEach(path => {
api.stats('error');
console.log(
'%s#%s:',
normalizedPath,
path.value.loc.start.line
);
if (printLevel !== 'quiet') {
console.log(
jscodeshift(path).toSource().split('\n').map(line => ' ' + line).join('\n')
);
console.log();
}
});
};

0 comments on commit 6699170

Please sign in to comment.