Skip to content

Commit 680d184

Browse files
committed
Add --ignore-prettier-errors option
1 parent d82070d commit 680d184

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as path from 'path';
77
import * as prettier from 'prettier';
88

99
import { run } from '.';
10+
import { CompilationOptions } from './compiler';
1011

1112
program
1213
.version('1.0.0')
@@ -20,6 +21,7 @@ program
2021
.option('--tab-width <int>', 'Number of spaces per indentation level.', 2)
2122
.option('--trailing-comma <none|es5|all>', 'Print trailing commas wherever possible when multi-line.', 'none')
2223
.option('--use-tabs', 'Indent with tabs instead of spaces.', false)
24+
.option('--ignore-prettier-errors', 'Ignore (but warn about) errors in Prettier', false)
2325
.usage('[options] <filename or glob>')
2426
.command('* <glob>')
2527
.action(globPattern => {
@@ -38,14 +40,17 @@ program
3840
trailingComma: program.trailingComma,
3941
useTabs: !!program.useTabs,
4042
};
43+
const compilationOptions: CompilationOptions = {
44+
ignorePrettierErrors: !!program.ignorePrettierErrors,
45+
};
4146
const files = glob.sync(globPattern, {});
4247
for (const file of files) {
4348
const filePath = path.resolve(file);
4449
const newPath = filePath.replace(/\.jsx?$/, '.tsx');
4550
const temporaryPath = filePath.replace(/\.jsx?$/, `_js2ts_${+new Date()}.tsx`);
4651
try {
4752
fs.copyFileSync(filePath, temporaryPath);
48-
const result = run(temporaryPath, prettierOptions);
53+
const result = run(temporaryPath, prettierOptions, compilationOptions);
4954
fs.writeFileSync(newPath, result);
5055
fs.unlinkSync(filePath);
5156
fs.unlinkSync(temporaryPath);

src/compiler.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@ import * as detectIndent from 'detect-indent';
88

99
import { TransformFactoryFactory } from '.';
1010

11+
export interface CompilationOptions {
12+
ignorePrettierErrors: boolean;
13+
}
14+
15+
const DEFAULT_COMPILATION_OPTIONS: CompilationOptions = {
16+
ignorePrettierErrors: false,
17+
};
18+
19+
export { DEFAULT_COMPILATION_OPTIONS };
20+
1121
/**
1222
* Compile and return result TypeScript
1323
* @param filePath Path to file to compile
1424
*/
1525
export function compile(
1626
filePath: string,
1727
factoryFactories: TransformFactoryFactory[],
18-
incomingPrettierOptions: prettier.Options = {}
28+
incomingPrettierOptions: prettier.Options = {},
29+
compilationOptions: CompilationOptions = DEFAULT_COMPILATION_OPTIONS,
1930
) {
2031
const compilerOptions: ts.CompilerOptions = {
2132
target: ts.ScriptTarget.ES2017,
@@ -56,7 +67,16 @@ export function compile(
5667
const inputSource = fs.readFileSync(filePath, 'utf-8');
5768
const prettierOptions = getPrettierOptions(filePath, inputSource, incomingPrettierOptions);
5869

59-
return prettier.format(printed, prettierOptions);
70+
try {
71+
return prettier.format(printed, prettierOptions);
72+
} catch (prettierError) {
73+
if (compilationOptions.ignorePrettierErrors) {
74+
console.warn(`Prettier failed for ${filePath} (ignorePrettierErrors is on):`);
75+
console.warn(prettierError);
76+
return printed;
77+
}
78+
throw prettierError;
79+
}
6080
}
6181

6282
/**

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from 'typescript';
22
import * as prettier from 'prettier';
33

4-
import { compile } from './compiler';
4+
import { compile, CompilationOptions, DEFAULT_COMPILATION_OPTIONS } from './compiler';
55
import { reactJSMakePropsAndStateInterfaceTransformFactoryFactory } from './transforms/react-js-make-props-and-state-transform';
66
import { reactRemovePropTypesAssignmentTransformFactoryFactory } from './transforms/react-remove-prop-types-assignment-transform';
77
import { reactMovePropTypesToClassTransformFactoryFactory } from './transforms/react-move-prop-types-to-class-transform';
@@ -37,6 +37,10 @@ export type TransformFactoryFactory = (typeChecker: ts.TypeChecker) => ts.Transf
3737
* Run React JavaScript to TypeScript transform for file at `filePath`
3838
* @param filePath
3939
*/
40-
export function run(filePath: string, prettierOptions: prettier.Options = {}): string {
41-
return compile(filePath, allTransforms, prettierOptions);
40+
export function run(
41+
filePath: string,
42+
prettierOptions: prettier.Options = {},
43+
compilationOptions: CompilationOptions = DEFAULT_COMPILATION_OPTIONS,
44+
): string {
45+
return compile(filePath, allTransforms, prettierOptions, compilationOptions);
4246
}

0 commit comments

Comments
 (0)