Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"precommit": "./bin/pretty-quick.js --staged"
},
"peerDependencies": {
"prettier": ">=1.8.0"
"prettier": ">=1.15.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand All @@ -51,9 +51,9 @@
"eslint-plugin-jest": "^21.5.0",
"eslint-plugin-prettier": "^2.4.0",
"husky": "^0.14.3",
"jest": "^22.0.4",
"jest": "^23.6.0",
"mock-fs": "^4.4.2",
"prettier": "1.9.2",
"prettier": "1.15.0",
"semantic-release": "^11.0.2"
}
}
44 changes: 33 additions & 11 deletions src/formatFiles.js
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)) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or flow

Copy link
Author

@aleclarson aleclarson Dec 27, 2018

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!

Copy link
Member

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 the flow parser.

Copy link
Author

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.

Copy link
Author

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 when parser === 'babylon'?

(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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this code path?

Copy link
Author

@aleclarson aleclarson Dec 27, 2018

Choose a reason for hiding this comment

The 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;
}
}
23 changes: 12 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import scms from './scms';
import formatFiles from './formatFiles';
import createIgnorer from './createIgnorer';
import isSupportedExtension from './isSupportedExtension';
import isFileSupported from './isFileSupported';

export default (
currentDirectory,
Expand All @@ -23,35 +23,36 @@ export default (
if (!scm) {
throw new Error('Unable to detect a source control manager.');
}
const directory = scm.rootDirectory;
const { rootDirectory } = scm;

const revision = since || scm.getSinceRevision(directory, { staged, branch });
const revision =
since || scm.getSinceRevision(rootDirectory, { staged, branch });

onFoundSinceRevision && onFoundSinceRevision(scm.name, revision);

const changedFiles = scm
.getChangedFiles(directory, revision, staged)
.filter(isSupportedExtension)
.filter(createIgnorer(directory));
.getChangedFiles(rootDirectory, revision, staged)
.filter(isFileSupported)
.filter(createIgnorer(rootDirectory));

const unstagedFiles = staged
? scm
.getUnstagedChangedFiles(directory, revision)
.filter(isSupportedExtension)
.filter(createIgnorer(directory))
.getUnstagedChangedFiles(rootDirectory, revision)
.filter(isFileSupported)
.filter(createIgnorer(rootDirectory))
: [];

const wasFullyStaged = f => unstagedFiles.indexOf(f) < 0;

onFoundChangedFiles && onFoundChangedFiles(changedFiles);

formatFiles(directory, changedFiles, {
formatFiles(rootDirectory, changedFiles, {
config,
onWriteFile: file => {
onWriteFile && onWriteFile(file);
if (staged && restage) {
if (wasFullyStaged(file)) {
scm.stageFile(directory, file);
scm.stageFile(rootDirectory, file);
} else {
onPartiallyStagedFile && onPartiallyStagedFile(file);
}
Expand Down
11 changes: 11 additions & 0 deletions src/isFileSupported.js
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;
9 changes: 0 additions & 9 deletions src/isSupportedExtension.js

This file was deleted.

Loading