Skip to content

Commit dbeda53

Browse files
zimmeKent C. Dodds
authored andcommitted
feat: Add support for typescript files (#121)
1 parent 09eb76f commit dbeda53

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
22
"name": "prettier-eslint",
33
"version": "0.0.0-development",
4-
"description":
5-
"Formats your JavaScript using prettier followed by eslint --fix",
4+
"description": "Formats your JavaScript using prettier followed by eslint --fix",
65
"main": "dist/index.js",
76
"scripts": {
87
"start": "nps",
98
"test": "nps test",
109
"precommit": "opt --in pre-commit --exec \"npm start validate\""
1110
},
12-
"files": ["dist"],
11+
"files": [
12+
"dist"
13+
],
1314
"keywords": [],
1415
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
1516
"license": "MIT",
@@ -22,7 +23,9 @@
2223
"loglevel-colored-level-prefix": "^1.0.0",
2324
"prettier": "^1.6.0",
2425
"pretty-format": "^20.0.3",
25-
"require-relative": "^0.8.7"
26+
"require-relative": "^0.8.7",
27+
"typescript": "^2.4.2",
28+
"typescript-eslint-parser": "^7.0.0"
2629
},
2730
"devDependencies": {
2831
"all-contributors-cli": "^4.4.0",

src/__tests__/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ const tests = [
170170
},
171171
output: '{ "foo": "bar" }',
172172
},
173+
{
174+
title: 'TypeScript example',
175+
input: {
176+
text: 'function Foo (this: void) { return this; }',
177+
filePath: path.resolve('./test.ts'),
178+
},
179+
output: 'function Foo(this: void) {\n return this;\n}',
180+
},
173181
]
174182

175183
beforeEach(() => {

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint no-console:0, global-require:0, import/no-dynamic-require:0 */
2-
/* eslint complexity: [1, 6] */
2+
/* eslint complexity: [1, 7] */
33
import fs from 'fs'
44
import path from 'path'
55
import requireRelative from 'require-relative'
@@ -87,12 +87,18 @@ async function format(options) {
8787

8888
const isCss = /\.(css|less|scss)$/.test(filePath)
8989
const isJson = /\.json$/.test(filePath)
90+
const isTypeScript = /\.tsx?$/.test(filePath)
9091

9192
if (isCss) {
9293
formattingOptions.prettier.parser = 'postcss'
9394
} else if (isJson) {
9495
formattingOptions.prettier.parser = 'json'
9596
formattingOptions.prettier.trailingComma = 'none'
97+
} else if (isTypeScript) {
98+
formattingOptions.prettier.parser = 'typescript'
99+
// XXX: It seems babylon is getting a TypeScript plugin.
100+
// Should that be used instead?
101+
formattingOptions.eslint.parser = 'typescript-eslint-parser'
96102
}
97103

98104
const prettify = createPrettify(formattingOptions.prettier, prettierPath)

0 commit comments

Comments
 (0)