Skip to content

Instantly share code, notes, and snippets.

@maxlath
Last active November 10, 2019 21:37
Show Gist options
  • Select an option

  • Save maxlath/c7c3a3809a0ac22c3ca315887570e20f to your computer and use it in GitHub Desktop.

Select an option

Save maxlath/c7c3a3809a0ac22c3ca315887570e20f to your computer and use it in GitHub Desktop.

Revisions

  1. maxlath revised this gist Nov 10, 2019. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions decaffeinate_cleanup.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,13 @@
    #!/usr/bin/env node

    // To be run after bulk-decaffeinate is done
    // bulk-decaffeinate convert --num-workers 8 --config ~/bulk-decaffeinate.config.js
    // cleanup(){
    // decaffeinate_cleanup.js "$@" && npm run lint-fix "$@"
    // }
    //
    // cleanup **/*.js

    const path = require('path')
    const fs = require('fs')

  2. maxlath created this gist Nov 10, 2019.
    31 changes: 31 additions & 0 deletions decaffeinate_cleanup.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/usr/bin/env node
    const path = require('path')
    const fs = require('fs')

    const cleanupLine = line => {
    return line
    .replace(/^(\s+(?!(if|for))\w+) (\(.*\)) \{/g, '$1: $3 => {')
    .replace(/= function (\(.*\)) {/g, '= $1 => {')
    .replace(/=> function (\(.*\)) {/g, '=> $1 => {')
    .replace(/(^\s+if) (\(.*\)) { return\s?(.*) }/g, '$1 $2 return $3')
    .replace(/(^\s+if) (\(.*\)) { throw\s?(.*) }/g, '$1 $2 throw $3')
    .replace(/(^\s+else) { return\s?(.*) }/g, '$1 return $2')
    .replace(/(^\s+else) { throw\s?(.*) }/g, '$1 throw $2')
    .replace(/return done\(\)/, 'done()')
    .replace(/return it\(/, 'it(')
    }

    const update = file => {
    const current = fs.readFileSync(file).toString()

    const updated = current
    .split('\n')
    .map(cleanupLine)
    .join('\n')

    if (current !== updated) fs.writeFileSync(file, updated)
    }

    process.argv.slice(2)
    .map(file => path.resolve(file))
    .forEach(update)