Skip to content

Commit

Permalink
feat(lint): Optionally use prettier with --prettier flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-kvale-sap committed May 30, 2019
1 parent 42fb986 commit 4332881
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
7 changes: 0 additions & 7 deletions .eslintrc.js

This file was deleted.

8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,15 @@ Options
--fix Fixes fixable errors and warnings
--ignore-pattern Ignore a pattern
--write-file Write the config file locally
--prettier Use Prettier to lint
-h, --help Displays this message

Examples
$ tsdx lint src test
$ tsdx lint src test --fix
$ tsdx lint src
$ tsdx lint src --fix
$ tsdx lint src test --ignore-pattern test/foobar.ts
$ tsdx lint src test --write-file
$ tsdx lint src --write-file
$ tsdx lint src test --prettier
```

## Author
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"prepare": "tsc -p tsconfig.json",
"build": "tsc -p tsconfig.json",
"lint": "yarn build && yarn lint:post-build",
"lint:post-build": "node dist/index.js lint src test --ignore-pattern 'test/tests/lint'",
"lint:post-build": "node dist/index.js lint src test --ignore-pattern 'test/tests/lint' --prettier",
"test": "jest --config ./test/jest.config.json"
},
"files": [
Expand All @@ -50,6 +50,8 @@
"cross-env": "5.2.0",
"enquirer": "^2.3.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.3.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-config-react-app": "^4.0.1",
"eslint-plugin-flowtype": "^2.50.3",
"eslint-plugin-import": "^2.17.3",
Expand Down Expand Up @@ -93,8 +95,6 @@
"@types/react": "^16.8.17",
"@types/rollup-plugin-json": "^3.0.2",
"@types/rollup-plugin-sourcemaps": "^0.4.2",
"eslint-config-prettier": "^4.3.0",
"eslint-plugin-prettier": "^3.1.0",
"husky": "^2.3.0",
"prettier": "^1.17.1",
"pretty-quick": "^1.10.0",
Expand Down
10 changes: 9 additions & 1 deletion src/createEslintConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import path from 'path';
import { CLIEngine } from 'eslint';

interface CreateEslintConfigArgs {
prettier: boolean;
rootDir: string;
writeFile: boolean;
}
export function createEslintConfig({
prettier,
rootDir,
writeFile,
}: CreateEslintConfigArgs): CLIEngine.Options['baseConfig'] {
const baseExtends = ['react-app'];
const config = {
extends: ['react-app'],
extends: prettier
? baseExtends.concat([
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
])
: baseExtends,
};

if (writeFile) {
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,12 @@ prog
.example('lint src test --ignore-pattern test/foobar.ts')
.option('--write-file', 'Write the config file locally')
.example('lint src test --write-file')
.option('--prettier', 'Use Prettier to lint')
.example('lint src test --prettier')
.action(
async (opts: {
fix: boolean;
prettier: boolean;
'ignore-pattern': string;
'write-file': boolean;
_: string[];
Expand All @@ -419,6 +422,7 @@ prog
ignorePattern: opts['ignore-pattern'],
baseConfig: {
...createEslintConfig({
prettier: opts.prettier,
rootDir: paths.appRoot,
writeFile: opts['write-file'],
}),
Expand Down
5 changes: 1 addition & 4 deletions test/tests/lint/file-with-lint-errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export const foo = ( ) =>
!! ('bar')
;

export const foo () => !!'bar';
5 changes: 5 additions & 0 deletions test/tests/lint/file-with-prettier-lint-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const foo = ( ) =>
!! ('bar')
;


2 changes: 1 addition & 1 deletion test/tests/lint/react-file-with-lint-errors.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

export const Foobar = (props: any ) => {
export const Foobar (props: any ) => {
return <div {...props
} >foobar</div>;
}
Expand Down
15 changes: 15 additions & 0 deletions test/tests/lint/tsdx-lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ describe('tsdx lint', () => {
expect(output.code).toBe(0);
});

describe('prettier', () => {
const flags = '--prettier'
it('should fail to lint a ts file with errors', () => {
const testFile = 'test/tests/lint/file-with-prettier-lint-errors.ts';
const output = shell.exec(`node dist/index.js lint ${testFile} ${flags}`);
expect(output.code).toBe(1);
});

it('should succeed linting a ts file without errors', () => {
const testFile = 'test/tests/lint/file-without-lint-error.ts';
const output = shell.exec(`node dist/index.js lint ${testFile} ${flags}`);
expect(output.code).toBe(0);
});
})

it('should fail to lint a tsx file with errors', () => {
const testFile = 'test/tests/lint/react-file-with-lint-errors.tsx';
const output = shell.exec(`node dist/index.js lint ${testFile}`);
Expand Down

0 comments on commit 4332881

Please sign in to comment.