Skip to content
This repository was archived by the owner on Sep 21, 2019. It is now read-only.

Commit 0e52452

Browse files
committed
Use prettier.resolveConfig
1 parent 9bdead0 commit 0e52452

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/compiler.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,22 @@ export function compile(
5454
const printed = printer.printNode(ts.EmitHint.SourceFile, result.transformed[0], sourceFiles[0]);
5555

5656
const inputSource = fs.readFileSync(filePath, 'utf-8');
57-
const prettierOptions = getPrettierOptions(inputSource, incomingPrettierOptions);
57+
const prettierOptions = getPrettierOptions(filePath, inputSource, incomingPrettierOptions);
5858

5959
return prettier.format(printed, incomingPrettierOptions);
6060
}
6161

6262
/**
6363
* Get Prettier options based on style of a JavaScript
64+
* @param filePath Path to source file
6465
* @param source Body of a JavaScript
6566
* @param options Existing prettier option
6667
*/
67-
export function getPrettierOptions(source: string, options: prettier.Options): prettier.Options {
68+
export function getPrettierOptions(filePath: string, source: string, options: prettier.Options): prettier.Options {
69+
const resolvedOptions = prettier.resolveConfig.sync(filePath);
70+
if (resolvedOptions) {
71+
return resolvedOptions;
72+
}
6873
const { amount: indentAmount, type: indentType } = detectIndent(source);
6974
const sourceWidth = getCodeWidth(source, 80);
7075
const semi = getUseOfSemi(source);
@@ -91,7 +96,7 @@ function getCodeWidth(source: string, defaultWidth: number): number {
9196

9297
/**
9398
* Detect if a source file is using semicolon
94-
* @todo: use an actual parser. This is not a proper method
99+
* @todo: use an actual parser. This is not a proper implementation
95100
* @param source
96101
* @return true if code is using semicolons
97102
*/
@@ -101,9 +106,14 @@ function getUseOfSemi(source: string): boolean {
101106

102107
/**
103108
* Detect if a source file is using single quotes or double quotes
104-
* @todo
109+
* @todo use an actual parser. This is not a proper implementation
105110
* @param source
106111
*/
107112
function getQuotation(source: string): 'single' | 'double' {
108-
return 'single';
113+
const numberOfSingleQuotes = (source.match(/\'/g) || []).length;
114+
const numberOfDoubleQuotes = (source.match(/\"/g) || []).length;
115+
if (numberOfSingleQuotes > numberOfDoubleQuotes) {
116+
return 'single';
117+
}
118+
return 'double';
109119
}

0 commit comments

Comments
 (0)