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

Commit e1e4a71

Browse files
committed
prettier config
1 parent 5c15f79 commit e1e4a71

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
"author": "Mohsen Azimi <me@azimi.me>",
3333
"license": "Apache-2.0",
3434
"dependencies": {
35+
"@types/detect-indent": "^5.0.0",
3536
"chalk": "^1.1.3",
3637
"commander": "^2.10.0",
38+
"detect-indent": "^5.0.0",
3739
"glob": "^7.1.2",
3840
"lodash": "^4.17.4",
3941
"prettier": "^1.10.2",

src/compiler.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import * as os from 'os';
2+
import * as fs from 'fs';
13
import * as ts from 'typescript';
24
import * as chalk from 'chalk';
5+
import * as _ from 'lodash';
36
import * as prettier from 'prettier';
7+
import * as detectIndent from 'detect-indent';
48

59
import { TransformFactoryFactory } from '.';
610

@@ -48,5 +52,37 @@ export function compile(
4852

4953
// TODO: fix the index 0 access... What if program have multiple source files?
5054
const printed = printer.printNode(ts.EmitHint.SourceFile, result.transformed[0], sourceFiles[0]);
55+
56+
// Default to original source code style if prettier options are not set
57+
const inputSource = fs.readFileSync(filePath, 'utf-8');
58+
const { amount: indentAmount, type: indentType } = detectIndent(inputSource);
59+
const sourceWidth = getCodeWidth(inputSource, 80);
60+
const semi = getUseOfSemi(inputSource);
61+
62+
_.defaults(prettierOptions, {
63+
tabWidth: indentAmount,
64+
useTabs: indentType === 'tab',
65+
printWidth: sourceWidth,
66+
semi,
67+
});
68+
5169
return prettier.format(printed, prettierOptions);
5270
}
71+
72+
/**
73+
* Given body of a source file, return its code width
74+
* @param source
75+
*/
76+
function getCodeWidth(source: string, defaultWidth: number): number {
77+
return source.split(os.EOL).reduce((result, line) => Math.max(result, line.length), defaultWidth);
78+
}
79+
80+
/**
81+
* Detect if a source file is using semicolon
82+
* TODO: use an actual parser. This is not a proper method
83+
* @param source
84+
* @return true if code is using semicolons
85+
*/
86+
function getUseOfSemi(source: string): boolean {
87+
return source.indexOf(';') !== -1;
88+
}

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
dependencies:
1313
commander "*"
1414

15+
"@types/detect-indent@^5.0.0":
16+
version "5.0.0"
17+
resolved "https://registry.yarnpkg.com/@types/detect-indent/-/detect-indent-5.0.0.tgz#8b1bbd7891268d5ed20d23ecd23ae333d33934cd"
18+
1519
"@types/events@*":
1620
version "1.1.0"
1721
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.1.0.tgz#93b1be91f63c184450385272c47b6496fd028e02"
@@ -616,6 +620,10 @@ detect-indent@^4.0.0:
616620
dependencies:
617621
repeating "^2.0.0"
618622

623+
detect-indent@^5.0.0:
624+
version "5.0.0"
625+
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
626+
619627
diff@^3.1.0, diff@^3.2.0:
620628
version "3.4.0"
621629
resolved "https://registry.yarnpkg.com/diff/-/diff-3.4.0.tgz#b1d85507daf3964828de54b37d0d73ba67dda56c"

0 commit comments

Comments
 (0)