babel-codemod
Advanced tools
Comparing version 1.3.0 to 1.3.1
{ | ||
"name": "babel-codemod", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "babel-codemod rewrites JavaScript using babel plugins.", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"prepublish": "yarn test", | ||
"prepublishOnly": "yarn test", | ||
"lint": "tslint --project .", | ||
@@ -13,4 +13,11 @@ "lint-fix": "tslint --project . --fix", | ||
"semantic-release": "semantic-release", | ||
"commitmsg": "commitlint -e $GIT_PARAMS" | ||
"commitmsg": "commitlint -e $GIT_PARAMS", | ||
"precommit": "lint-staged" | ||
}, | ||
"lint-staged": { | ||
"*.ts": [ | ||
"prettier --parser typescript --write", | ||
"git add" | ||
] | ||
}, | ||
"bin": { | ||
@@ -28,3 +35,3 @@ "codemod": "./bin/codemod" | ||
"devDependencies": { | ||
"@commitlint/cli": "^5.2.8", | ||
"@commitlint/cli": "^6.0.1", | ||
"@commitlint/config-conventional": "^5.2.3", | ||
@@ -37,7 +44,11 @@ "@types/babel-core": "^6.7.14", | ||
"@types/mz": "0.0.32", | ||
"@types/node": "^8.0.0", | ||
"@types/node": "^9.3.0", | ||
"@types/rimraf": "2.0.2", | ||
"commitlint": "^5.2.8", | ||
"commitlint": "^6.0.1", | ||
"globby": "^7.1.1", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^6.0.0", | ||
"mocha": "^4.1.0", | ||
"prettier": "1.10.2", | ||
"prettier-check": "^2.0.0", | ||
"rimraf": "2.6.2", | ||
@@ -44,0 +55,0 @@ "semantic-release": "^11.0.2", |
@@ -80,39 +80,31 @@ "use strict"; | ||
let dryRun = options.dry; | ||
let sourcesIterator; | ||
if (options.stdio) { | ||
let stdinData = yield readStream(stdin); | ||
let stdinSource = new TransformRunner_1.Source('<stdin>', stdinData); | ||
runner = new TransformRunner_1.default([stdinSource][Symbol.iterator](), plugins, { | ||
transformSourceEnd(runner, transformed) { | ||
if (transformed.output) { | ||
stdout.write(`${transformed.output}\n`); | ||
} | ||
else if (transformed.error) { | ||
stderr.write(`${transformed.error.stack}\n`); | ||
} | ||
} | ||
}); | ||
sourcesIterator = [new TransformRunner_1.Source('<stdin>', yield readStream(stdin))][Symbol.iterator](); | ||
} | ||
else { | ||
let sourcesIterator = iterateSources_1.default(options.sourcePaths, options.extensions, options.ignore, fs.statSync, fs.readdirSync, fs.readFileSync); | ||
runner = new TransformRunner_1.default(sourcesIterator, plugins, { | ||
transformSourceEnd(runner, transformed) { | ||
if (transformed.output) { | ||
if (transformed.output !== transformed.source.content) { | ||
stats.modified++; | ||
stdout.write(`${transformed.source.path}\n`); | ||
if (!dryRun) { | ||
fs.writeFileSync(transformed.source.path, transformed.output); | ||
} | ||
sourcesIterator = iterateSources_1.default(options.sourcePaths, options.extensions, options.ignore, fs.statSync, fs.readdirSync, fs.readFileSync); | ||
} | ||
runner = new TransformRunner_1.default(sourcesIterator, plugins); | ||
for (let result of runner.run()) { | ||
if (result.output) { | ||
if (options.stdio) { | ||
stdout.write(`${result.output}\n`); | ||
} | ||
else { | ||
if (result.output !== result.source.content) { | ||
stats.modified++; | ||
stdout.write(`${result.source.path}\n`); | ||
if (!dryRun) { | ||
fs.writeFileSync(result.source.path, result.output); | ||
} | ||
} | ||
else if (transformed.error) { | ||
stderr.write(`Encountered an error while processing ${transformed.source.path}:\n`); | ||
stderr.write(`${transformed.error.stack}\n`); | ||
} | ||
} | ||
}); | ||
} | ||
for (let result of runner.run()) { | ||
if (result.error !== null) { | ||
} | ||
else if (result.error) { | ||
stats.errors++; | ||
if (!options.stdio) { | ||
stderr.write(`Encountered an error while processing ${result.source.path}:\n`); | ||
} | ||
stderr.write(`${result.error.stack}\n`); | ||
} | ||
@@ -119,0 +111,0 @@ stats.total++; |
@@ -74,8 +74,10 @@ "use strict"; | ||
loadBabelTranspile() { | ||
let pluginOptions; | ||
if (!this.findBabelConfig) { | ||
pluginOptions = require('babel-preset-env').default(); | ||
pluginOptions.babelrc = false; // ignore babelrc file if present | ||
if (this.transpilePlugins) { | ||
let pluginOptions; | ||
if (!this.findBabelConfig) { | ||
pluginOptions = require('babel-preset-env').default(); | ||
pluginOptions.babelrc = false; // ignore babelrc file if present | ||
} | ||
require('babel-register')(pluginOptions); | ||
} | ||
require('babel-register')(pluginOptions); | ||
} | ||
@@ -93,4 +95,3 @@ getPlugin(name) { | ||
for (let plugin of this.getPlugins()) { | ||
let options = plugin.declaredName && | ||
this.pluginOptions.get(plugin.declaredName) || | ||
let options = (plugin.declaredName && this.pluginOptions.get(plugin.declaredName)) || | ||
this.pluginOptions.get(plugin.inferredName); | ||
@@ -147,3 +148,5 @@ if (options) { | ||
if (optionsRaw && optionsRaw[0] === '@') { | ||
optionsRaw = fs_1.readFileSync(optionsRaw.slice(1), { encoding: 'utf8' }); | ||
optionsRaw = fs_1.readFileSync(optionsRaw.slice(1), { | ||
encoding: 'utf8' | ||
}); | ||
pluginOptions.set(name, JSON.parse(optionsRaw)); | ||
@@ -173,5 +176,3 @@ } | ||
i++; | ||
extensions = new Set(args[i] | ||
.split(',') | ||
.map(ext => ext[0] === '.' ? ext : `.${ext}`)); | ||
extensions = new Set(args[i].split(',').map(ext => (ext[0] === '.' ? ext : `.${ext}`))); | ||
break; | ||
@@ -178,0 +179,0 @@ case '-s': |
@@ -20,15 +20,8 @@ import * as Babel from 'babel-core'; | ||
export declare type BabelPlugin = RawBabelPlugin | RawBabelPluginWithOptions; | ||
export declare type TransformRunnerDelegate = { | ||
transformStart?: (runner: TransformRunner) => void; | ||
transformEnd?: (runner: TransformRunner) => void; | ||
transformSourceStart?: (runner: TransformRunner, source: Source) => void; | ||
transformSourceEnd?: (runner: TransformRunner, transformed: SourceTransformResult) => void; | ||
}; | ||
export default class TransformRunner { | ||
readonly sources: IterableIterator<Source> | Array<Source>; | ||
readonly plugins: Array<BabelPlugin>; | ||
private readonly delegate; | ||
constructor(sources: IterableIterator<Source> | Array<Source>, plugins: Array<BabelPlugin>, delegate?: TransformRunnerDelegate); | ||
constructor(sources: IterableIterator<Source> | Array<Source>, plugins: Array<BabelPlugin>); | ||
run(): IterableIterator<SourceTransformResult>; | ||
private transformSource(source); | ||
} |
@@ -22,15 +22,8 @@ "use strict"; | ||
class TransformRunner { | ||
constructor(sources, plugins, delegate = {}) { | ||
constructor(sources, plugins) { | ||
this.sources = sources; | ||
this.plugins = plugins; | ||
this.delegate = delegate; | ||
} | ||
*run() { | ||
if (this.delegate.transformStart) { | ||
this.delegate.transformStart(this); | ||
} | ||
for (let source of this.sources) { | ||
if (this.delegate.transformSourceStart) { | ||
this.delegate.transformSourceStart(this, source); | ||
} | ||
let transformed; | ||
@@ -45,9 +38,3 @@ try { | ||
yield transformed; | ||
if (this.delegate.transformSourceEnd) { | ||
this.delegate.transformSourceEnd(this, transformed); | ||
} | ||
} | ||
if (this.delegate && this.delegate.transformEnd) { | ||
this.delegate.transformEnd(this); | ||
} | ||
} | ||
@@ -69,13 +56,13 @@ transformSource(source) { | ||
plugins: [ | ||
"flow", | ||
"jsx", | ||
"asyncGenerators", | ||
"classProperties", | ||
"doExpressions", | ||
"exportExtensions", | ||
"functionBind", | ||
"functionSent", | ||
"objectRestSpread", | ||
"dynamicImport", | ||
"decorators" | ||
'flow', | ||
'jsx', | ||
'asyncGenerators', | ||
'classProperties', | ||
'doExpressions', | ||
'exportExtensions', | ||
'functionBind', | ||
'functionSent', | ||
'objectRestSpread', | ||
'dynamicImport', | ||
'decorators' | ||
] | ||
@@ -82,0 +69,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52806
21
551