-
-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: use "prettier-tslint" and "prettier-eslint" when they exist #54
Changes from all commits
3ae5e08
9ec3d60
ab98aeb
299ddd2
f1d88ae
829524a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,49 @@ | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { resolveConfig, format } from 'prettier'; | ||
import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
import { resolveConfig, getFileInfo, format as prettierFormat } from 'prettier'; | ||
import { join } from 'path'; | ||
|
||
export default ( | ||
directory, | ||
rootDirectory, | ||
files, | ||
{ config, onWriteFile, onExamineFile } = {} | ||
) => { | ||
// Use local "prettier-tslint" if possible. | ||
const tslintFormat = loadFormat(rootDirectory, 'prettier-tslint', 'format'); | ||
// Use local "prettier-eslint" if possible. | ||
const eslintFormat = loadFormat(rootDirectory, 'prettier-eslint'); | ||
|
||
for (const relative of files) { | ||
onExamineFile && onExamineFile(relative); | ||
const file = join(directory, relative); | ||
const options = resolveConfig.sync(file, { config, editorconfig: true }); | ||
const file = join(rootDirectory, relative); | ||
const input = readFileSync(file, 'utf8'); | ||
const output = format( | ||
input, | ||
Object.assign({}, options, { | ||
filepath: file, | ||
}) | ||
); | ||
|
||
const parser = getFileInfo.sync(file).inferredParser; | ||
const format = | ||
(parser === 'babylon' && (eslintFormat || tslintFormat)) || | ||
(parser === 'typescript' && (tslintFormat || eslintFormat)) || | ||
prettierFormat; | ||
|
||
const options = resolveConfig.sync(file, { config, editorconfig: true }); | ||
const output = | ||
format === prettierFormat | ||
? format(input, ((options.filepath = file), options)) | ||
: format({ | ||
text: input, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for this code path? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tested locally (and you can too) with: git clone -b feat1-test https://github.com/aleclarson/pretty-quick
cd pretty-quick
cat README.md I'll try to get around to writing a test when I can. |
||
filePath: file, | ||
prettierOptions: options, | ||
}); | ||
|
||
if (output !== input) { | ||
writeFileSync(file, output); | ||
onWriteFile && onWriteFile(relative); | ||
} | ||
} | ||
}; | ||
|
||
function loadFormat(rootDirectory, formatName, exportKey) { | ||
const formatPath = join(rootDirectory, 'node_modules', formatName); | ||
if (existsSync(formatPath)) { | ||
const exports = require(formatPath); | ||
return exportKey ? exports[exportKey] : exports; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { extname } from 'path'; | ||
import { getFileInfo, getSupportInfo } from 'prettier'; | ||
|
||
const extensions = getSupportInfo().languages.reduce( | ||
(prev, language) => prev.concat(language.extensions || []), | ||
[] | ||
); | ||
|
||
export default file => | ||
extensions.includes(extname(file)) || | ||
getFileInfo.sync(file).inferredParser !== null; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or
flow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does eslint support flow? edit: Nope!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
babylon
supports flow language too. You can write JavaScript and still use theflow
parser.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, but passing Flow-typed JavaScript into
eslint
does not work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you saying there's a chance that
file
will contain Flow-typed JavaScript whenparser === 'babylon'
?