From a0563bf15ed02b08ad70a6d50f84dc690f724fa9 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 1 Aug 2018 11:47:51 +0700 Subject: [PATCH 1/5] add prettier config + vscode formatter --- .prettierrc | 7 +++++++ .vscode/extensions.json | 3 +++ .vscode/settings.json | 1 + package.json | 3 +++ tslint.json | 4 +++- 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .prettierrc create mode 100644 .vscode/extensions.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..64bfb29 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "semi": true, + "tabWidth": 2, + "printWidth": 120, + "singleQuote": false, + "trailingComma": "es5" +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..b98e704 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["eg2.tslint", "EditorConfig.EditorConfig", "esbenp.prettier-vscode"] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 7ef1cfb..6d3582f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "editor.renderWhitespace": "boundary", + "editor.formatOnSave": true, "files.encoding": "utf8", "files.trimTrailingWhitespace": true, "files.insertFinalNewline": true, diff --git a/package.json b/package.json index b535b42..f8c535f 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "devDependencies": { "@types/lodash": "^3.10.1", "@types/screeps": "^2.4.0", + "prettier": "^1.14.0", "rollup": "^0.57.1", "rollup-plugin-clear": "^2.0.7", "rollup-plugin-commonjs": "^9.1.4", @@ -35,6 +36,8 @@ "rollup-plugin-screeps": "^0.1.2", "rollup-plugin-typescript2": "^0.16.1", "tslint": "^5.9.1", + "tslint-config-prettier": "^1.14.0", + "tslint-plugin-prettier": "^1.3.0", "typescript": "^2.9.2" }, "dependencies": { diff --git a/tslint.json b/tslint.json index 4f1b5bc..e92553c 100644 --- a/tslint.json +++ b/tslint.json @@ -1,6 +1,8 @@ { + "rulesDirectory": "tslint-plugin-prettier", "extends" : [ - "tslint:recommended" + "tslint:recommended", + "tslint-config-prettier" ], "rules": { "forin": false, From ebc46fb96e2eb82c9ed01ded8c0edfda31fe974a Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 1 Aug 2018 11:49:15 +0700 Subject: [PATCH 2/5] apply prettier formatting --- .prettierrc | 2 +- src/utils/ErrorMapper.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.prettierrc b/.prettierrc index 64bfb29..226b3a8 100644 --- a/.prettierrc +++ b/.prettierrc @@ -3,5 +3,5 @@ "tabWidth": 2, "printWidth": 120, "singleQuote": false, - "trailingComma": "es5" + "trailingComma": "none" } diff --git a/src/utils/ErrorMapper.ts b/src/utils/ErrorMapper.ts index 9d93c76..9702213 100644 --- a/src/utils/ErrorMapper.ts +++ b/src/utils/ErrorMapper.ts @@ -1,5 +1,5 @@ // tslint:disable:no-conditional-assignment -import {SourceMapConsumer} from "source-map"; +import { SourceMapConsumer } from "source-map"; export class ErrorMapper { // Cache consumer @@ -26,7 +26,7 @@ export class ErrorMapper { * @returns {string} The source-mapped stack trace */ public static sourceMappedStackTrace(error: Error | string): string { - const stack: string = error instanceof Error ? error.stack as string : error; + const stack: string = error instanceof Error ? (error.stack as string) : error; if (this.cache.hasOwnProperty(stack)) { return this.cache[stack]; } @@ -35,7 +35,7 @@ export class ErrorMapper { let match: RegExpExecArray | null; let outStack = error.toString(); - while (match = re.exec(stack)) { + while ((match = re.exec(stack))) { if (match[2] === "main") { const pos = this.consumer.originalPositionFor({ column: parseInt(match[4], 10), From 2a6b8c4a00703171931f0acdbae619ebc48fde52 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 1 Aug 2018 12:08:55 +0700 Subject: [PATCH 3/5] limit formatOnSave option to JS/TS files, add docs re: prettier --- .vscode/extensions.json | 3 --- .vscode/settings.json | 12 ++++++++++-- docs/in-depth/prettier.md | 19 +++++++++++++++++++ docs/in-depth/typescript.md | 8 ++++---- 4 files changed, 33 insertions(+), 9 deletions(-) delete mode 100644 .vscode/extensions.json create mode 100644 docs/in-depth/prettier.md diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index b98e704..0000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "recommendations": ["eg2.tslint", "EditorConfig.EditorConfig", "esbenp.prettier-vscode"] -} diff --git a/.vscode/settings.json b/.vscode/settings.json index 6d3582f..0b1a7d9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,5 @@ { "editor.renderWhitespace": "boundary", - "editor.formatOnSave": true, "files.encoding": "utf8", "files.trimTrailingWhitespace": true, "files.insertFinalNewline": true, @@ -11,5 +10,14 @@ "node_modules/**": true, "typings/**": true }, - "typescript.tsdk": "./node_modules/typescript/lib" + "typescript.tsdk": "./node_modules/typescript/lib", + "[json]": { + "editor.formatOnSave": true + }, + "[javascript]": { + "editor.formatOnSave": true + }, + "[typescript]": { + "editor.formatOnSave": true + } } diff --git a/docs/in-depth/prettier.md b/docs/in-depth/prettier.md new file mode 100644 index 0000000..b4ab6dc --- /dev/null +++ b/docs/in-depth/prettier.md @@ -0,0 +1,19 @@ +# Prettier + +[Prettier](https://prettier.io/) is an automatic code formatter which supports various languages, including TypeScript. It also has extensions for various text editors like [VSCode](https://github.com/prettier/prettier-vscode), [Atom](https://github.com/prettier/prettier-atom), and even [Vim](https://github.com/prettier/vim-prettier). If you have installed these extensions, it will use Prettier's service to automatically format your code after saving. + +If you would rather not use Prettier instead, you can easily disable it too. In VSCode, open `.vscode/settings.json`, then change the `"editor.formatOnSave"` option to `false`: + +```json +{ + "[json]": { + "editor.formatOnSave": false + }, + "[javascript]": { + "editor.formatOnSave": false + }, + "[typescript]": { + "editor.formatOnSave": false + } +} +``` diff --git a/docs/in-depth/typescript.md b/docs/in-depth/typescript.md index 3d7d984..eaf9fd1 100644 --- a/docs/in-depth/typescript.md +++ b/docs/in-depth/typescript.md @@ -33,10 +33,10 @@ This project provides TSLint rules through a `tslint.json` file, which extends t We've made some changes to these rules, which we considered necessary and/or relevant to a proper Screeps project: - - set the [forin](http://palantir.github.io/tslint/rules/forin/) rule to `false`, it was forcing `for ( ... in ...)` loops to check if object members were not coming from the class prototype. - - set the [interface-name](http://palantir.github.io/tslint/rules/interface-name/) rule to `false`, in order to allow interfaces that are not prefixed with `I`. - - set the [no-console](http://palantir.github.io/tslint/rules/no-console/) rule to `false`, in order to allow using `console`. - - in the [variable-name](http://palantir.github.io/tslint/rules/variable-name/) rule, added `allow-leading-underscore`. +* set the [forin](http://palantir.github.io/tslint/rules/forin/) rule to `false`, it was forcing `for ( ... in ...)` loops to check if object members were not coming from the class prototype. +* set the [interface-name](http://palantir.github.io/tslint/rules/interface-name/) rule to `false`, in order to allow interfaces that are not prefixed with `I`. +* set the [no-console](http://palantir.github.io/tslint/rules/no-console/) rule to `false`, in order to allow using `console`. +* in the [variable-name](http://palantir.github.io/tslint/rules/variable-name/) rule, added `allow-leading-underscore`. ### Customising TSLint From 26ca84da312e80c697d27078a9a4cf9a6dfcec7f Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 1 Aug 2018 12:18:09 +0700 Subject: [PATCH 4/5] add docs on configuring TSLint for Prettier --- docs/SUMMARY.md | 1 + docs/in-depth/prettier.md | 41 ++++++++++++++++++++++++++++++++++++++- tslint.json | 1 - 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 99f1b77..125c8f0 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -15,6 +15,7 @@ * [Module bundling](./in-depth/module-bundling.md) * [Deploy destinations](./in-depth/deploy-destinations.md) * [TypeScript](./in-depth/typescript.md) +* [Prettier](./in-depth/prettier.md) * [Cookbook](./in-depth/cookbook.md) * [Environment variables](./in-depth/cookbook/environment-variables.md) * [One-line PowerShell setup](./in-depth/cookbook/one-line-powershell.md) diff --git a/docs/in-depth/prettier.md b/docs/in-depth/prettier.md index b4ab6dc..9517e00 100644 --- a/docs/in-depth/prettier.md +++ b/docs/in-depth/prettier.md @@ -1,4 +1,4 @@ -# Prettier +# Code formatting with Prettier [Prettier](https://prettier.io/) is an automatic code formatter which supports various languages, including TypeScript. It also has extensions for various text editors like [VSCode](https://github.com/prettier/prettier-vscode), [Atom](https://github.com/prettier/prettier-atom), and even [Vim](https://github.com/prettier/vim-prettier). If you have installed these extensions, it will use Prettier's service to automatically format your code after saving. @@ -17,3 +17,42 @@ If you would rather not use Prettier instead, you can easily disable it too. In } } ``` + +## Configuring TSLint for Prettier + +The `.prettierrc` file configures how Prettier formats your code. By default we use the following options. + +```json +{ + "semi": true, + "tabWidth": 2, + "printWidth": 120, + "singleQuote": false, + "trailingComma": "none" +} +``` + +We can use `tslint-config-prettier` to override some TSLint rules with its Prettier counterparts. In your `tslint.json` file, extend `tslint-config-prettier`. + +```json +{ + "extends" : [ + "tslint:recommended", + "tslint-config-prettier" + ] +} +``` + +To make Prettier error out on formatting errors, we can also use `tslint-plugin-prettier` to add a custom rule for this. + +```bash +yarn add --dev tslint-plugin-prettier +``` + +```json +{ + "rulesDirectory": ["tslint-plugin-prettier"], + "rules": { + "prettier": true + } +} diff --git a/tslint.json b/tslint.json index e92553c..adbf116 100644 --- a/tslint.json +++ b/tslint.json @@ -10,7 +10,6 @@ "member-ordering": [false], "no-console": [false], "no-namespace": [true, "allow-declarations"], - "trailing-comma": [true, {"multiline": "never", "singleline": "never"}], "variable-name": [ true, "ban-keywords", From 48ebaa49af22748a6830fe74d9de6c1ec04323bc Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 1 Aug 2018 12:18:31 +0700 Subject: [PATCH 5/5] make tslint-plugin-prettier opt-in --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index f8c535f..5d162b1 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "rollup-plugin-typescript2": "^0.16.1", "tslint": "^5.9.1", "tslint-config-prettier": "^1.14.0", - "tslint-plugin-prettier": "^1.3.0", "typescript": "^2.9.2" }, "dependencies": {