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

Commit 2fae7fd

Browse files
committed
wip
1 parent c2f1e1c commit 2fae7fd

File tree

7 files changed

+2924
-13
lines changed

7 files changed

+2924
-13
lines changed

.vscode/launch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"env": {
88
"NODE_ENV": "test"
99
},
10-
"externalConsole": false,
10+
"console": "internalConsole",
1111
"name": "Run Tests",
12-
"outDir": "${workspaceRoot}/dist",
13-
"preLaunchTask": "compile",
12+
"outFiles": ["${workspaceRoot}/dist"],
13+
// "preLaunchTask": "compile",
1414
"program": "${workspaceRoot}/node_modules/.bin/jest",
1515
"request": "launch",
1616
"runtimeArgs": [],

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"transform": {
2323
".ts": "<rootDir>/node_modules/ts-jest/preprocessor.js"
2424
},
25-
"testRegex": "test/runner.ts",
25+
"testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
2626
"moduleFileExtensions": ["ts", "js"]
2727
},
2828
"lint-staged": {
@@ -50,6 +50,7 @@
5050
"@types/node": "^8.0.2",
5151
"@types/prettier": "^1.10.0",
5252
"@types/react": "^15.0.31",
53+
"dedent": "^0.7.0",
5354
"husky": "^0.14.3",
5455
"jest": "^20.0.4",
5556
"lint-staged": "^6.0.1",

src/compiler.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { TransformFactoryFactory } from '.';
1515
export function compile(
1616
filePath: string,
1717
factoryFactories: TransformFactoryFactory[],
18-
prettierOptions: prettier.Options = {},
18+
incomingPrettierOptions: prettier.Options = {},
1919
) {
2020
const compilerOptions: ts.CompilerOptions = {
2121
target: ts.ScriptTarget.ES2017,
@@ -53,21 +53,32 @@ export function compile(
5353
// TODO: fix the index 0 access... What if program have multiple source files?
5454
const printed = printer.printNode(ts.EmitHint.SourceFile, result.transformed[0], sourceFiles[0]);
5555

56-
// Default to original source code style if prettier options are not set
5756
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-
const quotations = getQuotation(inputSource);
62-
_.defaults(prettierOptions, {
57+
const prettierOptions = getPrettierOptions(inputSource, incomingPrettierOptions);
58+
59+
return prettier.format(printed, incomingPrettierOptions);
60+
}
61+
62+
/**
63+
* Get Prettier options based on style of a JavaScript
64+
* @param source Body of a JavaScript
65+
* @param options Existing prettier option
66+
*/
67+
export function getPrettierOptions(source: string, options: prettier.Options): prettier.Options {
68+
const { amount: indentAmount, type: indentType } = detectIndent(source);
69+
const sourceWidth = getCodeWidth(source, 80);
70+
const semi = getUseOfSemi(source);
71+
const quotations = getQuotation(source);
72+
73+
_.defaults(options, {
6374
tabWidth: indentAmount,
64-
useTabs: indentType === 'tab',
75+
useTabs: indentType && indentType === 'tab',
6576
printWidth: sourceWidth,
6677
semi,
6778
singleQuote: quotations === 'single',
6879
});
6980

70-
return prettier.format(printed, prettierOptions);
81+
return options;
7182
}
7283

7384
/**

src/untyped-modules.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'dedent';

test/compiler.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as prettier from 'prettier';
2+
import * as dedent from "dedent";
3+
import { getPrettierOptions } from "../src/compiler";
4+
5+
describe('compiler', () => {
6+
describe('getPrettierOptions', () => {
7+
it('defaults to incoming options', () => {
8+
const incomingOptions: prettier.Options = {
9+
semi: true,
10+
printWidth: 120,
11+
singleQuote: true,
12+
arrowParens: 'avoid',
13+
useTabs: false,
14+
tabWidth: 4,
15+
};
16+
const options = getPrettierOptions('', incomingOptions);
17+
expect(options).toEqual(incomingOptions);
18+
});
19+
20+
it('detecs the tab option', () => {
21+
const source = dedent`
22+
const foo = {
23+
\tone: 1,
24+
\ttwo: 2,
25+
}
26+
`;
27+
const options = getPrettierOptions(source, {});
28+
expect(options).toEqual({useTabs: true});
29+
});
30+
});
31+
});
File renamed without changes.

0 commit comments

Comments
 (0)