@@ -54,17 +54,22 @@ export function compile(
54
54
const printed = printer . printNode ( ts . EmitHint . SourceFile , result . transformed [ 0 ] , sourceFiles [ 0 ] ) ;
55
55
56
56
const inputSource = fs . readFileSync ( filePath , 'utf-8' ) ;
57
- const prettierOptions = getPrettierOptions ( inputSource , incomingPrettierOptions ) ;
57
+ const prettierOptions = getPrettierOptions ( filePath , inputSource , incomingPrettierOptions ) ;
58
58
59
59
return prettier . format ( printed , incomingPrettierOptions ) ;
60
60
}
61
61
62
62
/**
63
63
* Get Prettier options based on style of a JavaScript
64
+ * @param filePath Path to source file
64
65
* @param source Body of a JavaScript
65
66
* @param options Existing prettier option
66
67
*/
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
+ }
68
73
const { amount : indentAmount , type : indentType } = detectIndent ( source ) ;
69
74
const sourceWidth = getCodeWidth ( source , 80 ) ;
70
75
const semi = getUseOfSemi ( source ) ;
@@ -91,7 +96,7 @@ function getCodeWidth(source: string, defaultWidth: number): number {
91
96
92
97
/**
93
98
* 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
95
100
* @param source
96
101
* @return true if code is using semicolons
97
102
*/
@@ -101,9 +106,14 @@ function getUseOfSemi(source: string): boolean {
101
106
102
107
/**
103
108
* 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
105
110
* @param source
106
111
*/
107
112
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' ;
109
119
}
0 commit comments