Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function transform(code: string, config?: any): string {
const result = codemodTransform(code, {
plugins: [...plugins.map(plugin => (config ? [plugin, config] : plugin))]
});
if (!result || !result.code) {
throw new Error(`transform returned no code`);
}
return result.code;
}
}
if (!useBabelConfig) {
options.configFile = useBabelConfig;
}
if (!useBabelConfig) {
if (TypeScriptExtensions.has(ext)) {
presets.push(require.resolve('@babel/preset-typescript'));
}
presets.push([
require.resolve('@babel/preset-env'),
{ useBuiltIns: 'entry', corejs: { version: 3, proposals: true } }
]);
}
const result = transform(code, options);
if (!result) {
throw new Error(`[${filename}] babel transform returned null`);
}
return result.code as string;
}
static run(content: string, options: Options): StageResult {
const log = logger(this.name);
log(content);
const code = transform(content, {
plugins: Array.from(this.getPluginsForOptions(options))
}).code as string;
return {
code,
suggestions: []
};
}
async transform(filepath: string, content: string): Promise {
const options: TransformOptions = {
filename: filepath,
babelrc: this.findBabelConfig,
plugins: this.plugins,
printer: this.printer
};
if (!this.findBabelConfig) {
options.configFile = this.findBabelConfig;
}
const result = transform(content, options);
if (!result) {
throw new Error(`[${filepath}] babel transform returned null`);
}
return result.code as string;
}
}
export default async function main(
args: ReadonlyArray,
stdin: typeof process.stdin,
stdout: typeof process.stdout
): Promise {
const input = await readStream(stdin);
stdout.write(transform(input, {
plugins: [
codemodDeclarationsBlockScope,
codemodFunctionsArrow,
codemodModulesCommonJS,
codemodObjectsConcise,
codemodObjectsDestructuring,
codemodObjectsShorthand,
codemodStringsTemplate
]
}).code as string);
return 0;
}