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

```

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

```
  • Loading branch information
iamdustan committed Dec 21, 2016
1 parent afd4a54 commit 2fbde60
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
@@ -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
@@ -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 2fbde60

Please sign in to comment.