Skip to content

Commit

Permalink
fix: avoid conflicts between prettier and tslint (#348)
Browse files Browse the repository at this point in the history
Use tslint-config-prettier to avoid prettier and tslint stepping on
each other's toes.

Fixes: #347
Fixes: #326
  • Loading branch information
ofrobots committed May 17, 2019
1 parent 4a3c511 commit 83cb107
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 78 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"prettier.config.js",
"tsconfig-google.json",
"tsconfig.json",
"tslint-rules.json",
"tslint.json"
],
"scripts": {
Expand Down Expand Up @@ -52,6 +53,7 @@
"prettier": "^1.15.3",
"rimraf": "^2.6.2",
"tslint": "^5.12.0",
"tslint-config-prettier": "^1.18.0",
"update-notifier": "^3.0.0",
"write-file-atomic": "^2.3.0"
},
Expand Down
45 changes: 44 additions & 1 deletion test/test-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as path from 'path';
import { Options } from '../src/cli';
import { TSLINT_CONFIG } from '../src/init';
import * as lint from '../src/lint';
import * as format from '../src/format';
import { nop } from '../src/util';

import { withFixtures } from 'inline-fixtures';
Expand All @@ -39,7 +40,7 @@ describe('lint', () => {
const GOOD_CODE = `throw new Error('hello world');`;

// missing semicolon, array-type simple.
const FIXABLE_CODE = 'const x : Array<string> = []';
const FIXABLE_CODE = 'const x : Array<string> = [];';
const FIXABLE_CODE_FIXED = 'const x : string[] = [];';

it('createProgram should return an object', () => {
Expand Down Expand Up @@ -275,6 +276,47 @@ describe('lint', () => {
);
});

it('should not conflict with format', async () => {
const FIXTURE = {
'tsconfig.json': JSON.stringify({ files: ['far.ts'] }),
'far.ts': `export function far(
ceiling: string,
vines: string,
sailed: number,
ocean: number,
tumbled: string,
): string {
return 'where the wild things are';
}
`,
};

// tslint should not complain about the trailing comma in functions,
// and let prettier complain.
await withFixtures(FIXTURE, async () => {
const lintOkay = await lint.lint(OPTIONS, [], false);
assert.strictEqual(lintOkay, true);
const formatOkay = await format.format(OPTIONS, [], false);
assert.strictEqual(formatOkay, false);
});

const fixtureWithPrettierConfig = {
...FIXTURE,
'prettier.config.js': `module.exports = {
singleQuote: true,
trailingComma: 'all',
};`,
};

// Both the linter and the formatter should be okay with this.
await withFixtures(fixtureWithPrettierConfig, async () => {
const lintOkay = await lint.lint(OPTIONS, [], false);
assert.strictEqual(lintOkay, true);
const formatOkay = await format.format(OPTIONS, [], false);
assert.strictEqual(formatOkay, true);
});
});

it('should handle json files correctly resolveJsonModule', () => {
return withFixtures(
{
Expand All @@ -289,6 +331,7 @@ describe('lint', () => {
'tslint.json': JSON.stringify(TSLINT_CONFIG),
node_modules: {
gts: {
'tslint-rules.json': fs.readFileSync('tslint-rules.json', 'utf8'),
'tslint.json': fs.readFileSync('tslint.json', 'utf8'),
},
},
Expand Down
78 changes: 78 additions & 0 deletions tslint-rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"rules": {
"array-type": [true, "array-simple"],
"arrow-return-shorthand": true,
"ban": [true,
{"name": ["it", "skip"]},
{"name": ["it", "only"]},
{"name": ["it", "async", "skip"]},
{"name": ["it", "async", "only"]},
{"name": ["describe", "skip"]},
{"name": ["describe", "only"]},
{"name": "parseInt", "message": "tsstyle#type-coercion"},
{"name": "parseFloat", "message": "tsstyle#type-coercion"},
{"name": "Array", "message": "tsstyle#array-constructor"},
{"name": ["*", "innerText"], "message": "Use .textContent instead. tsstyle#browser-oddities"}
],
"ban-ts-ignore": true,
"ban-types": [true,
["Object", "Use {} instead."],
["String", "Use 'string' instead."],
["Number", "Use 'number' instead."],
["Boolean", "Use 'boolean' instead."]
],
"class-name": true,
"curly": [true, "ignore-same-line"],
"deprecation": true,
"forin": true,
"interface-name": [true, "never-prefix"],
"interface-over-type-literal": true,
"jsdoc-format": true,
"label-position": true,
"member-access": [true, "no-public"],
"new-parens": true,
"no-angle-bracket-type-assertion": true,
"no-any": true,
"no-arg": true,
"no-conditional-assignment": true,
"no-construct": true,
"no-debugger": true,
"no-default-export": true,
"no-duplicate-variable": true,
"no-inferrable-types": true,
"no-namespace": [true, "allow-declarations"],
"no-reference": true,
"no-string-throw": true,
"no-return-await": true,
"no-unsafe-finally": true,
"no-unused-expression": true,
"no-var-keyword": true,
"object-literal-shorthand": true,
"only-arrow-functions": [true, "allow-declarations", "allow-named-functions"],
"prefer-const": true,
"radix": true,
"semicolon": [true, "always", "ignore-bound-class-methods"],
"switch-default": true,
"trailing-comma": [
true,
{
"multiline": {
"objects": "always",
"arrays": "always",
"functions": "never",
"typeLiterals": "ignore"
},
"esSpecCompliant": true
}
],
"triple-equals": [true, "allow-null-check"],
"use-isnan": true,
"variable-name": [
true,
"check-format",
"ban-keywords",
"allow-leading-underscore",
"allow-trailing-underscore"
]
}
}
82 changes: 5 additions & 77 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,78 +1,6 @@
{
"rules": {
"array-type": [true, "array-simple"],
"arrow-return-shorthand": true,
"ban": [true,
{"name": ["it", "skip"]},
{"name": ["it", "only"]},
{"name": ["it", "async", "skip"]},
{"name": ["it", "async", "only"]},
{"name": ["describe", "skip"]},
{"name": ["describe", "only"]},
{"name": "parseInt", "message": "tsstyle#type-coercion"},
{"name": "parseFloat", "message": "tsstyle#type-coercion"},
{"name": "Array", "message": "tsstyle#array-constructor"},
{"name": ["*", "innerText"], "message": "Use .textContent instead. tsstyle#browser-oddities"}
],
"ban-ts-ignore": true,
"ban-types": [true,
["Object", "Use {} instead."],
["String", "Use 'string' instead."],
["Number", "Use 'number' instead."],
["Boolean", "Use 'boolean' instead."]
],
"class-name": true,
"curly": [true, "ignore-same-line"],
"deprecation": true,
"forin": true,
"interface-name": [true, "never-prefix"],
"interface-over-type-literal": true,
"jsdoc-format": true,
"label-position": true,
"member-access": [true, "no-public"],
"new-parens": true,
"no-angle-bracket-type-assertion": true,
"no-any": true,
"no-arg": true,
"no-conditional-assignment": true,
"no-construct": true,
"no-debugger": true,
"no-default-export": true,
"no-duplicate-variable": true,
"no-inferrable-types": true,
"no-namespace": [true, "allow-declarations"],
"no-reference": true,
"no-string-throw": true,
"no-return-await": true,
"no-unsafe-finally": true,
"no-unused-expression": true,
"no-var-keyword": true,
"object-literal-shorthand": true,
"only-arrow-functions": [true, "allow-declarations", "allow-named-functions"],
"prefer-const": true,
"radix": true,
"semicolon": [true, "always", "ignore-bound-class-methods"],
"switch-default": true,
"trailing-comma": [
true,
{
"multiline": {
"objects": "always",
"arrays": "always",
"functions": "never",
"typeLiterals": "ignore"
},
"esSpecCompliant": true
}
],
"triple-equals": [true, "allow-null-check"],
"use-isnan": true,
"variable-name": [
true,
"check-format",
"ban-keywords",
"allow-leading-underscore",
"allow-trailing-underscore"
]
}
}
"extends": [
"./tslint-rules.json",
"tslint-config-prettier"
]
}

0 comments on commit 83cb107

Please sign in to comment.