Merge pull request #93 from screepers/feature/prettier-rfc

Prettier formatting
This commit is contained in:
Resi Respati
2018-08-01 12:20:24 +07:00
committed by GitHub
8 changed files with 88 additions and 10 deletions

7
.prettierrc Normal file
View File

@@ -0,0 +1,7 @@
{
"semi": true,
"tabWidth": 2,
"printWidth": 120,
"singleQuote": false,
"trailingComma": "none"
}

11
.vscode/settings.json vendored
View File

@@ -10,5 +10,14 @@
"node_modules/**": true, "node_modules/**": true,
"typings/**": 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
}
} }

View File

@@ -15,6 +15,7 @@
* [Module bundling](./in-depth/module-bundling.md) * [Module bundling](./in-depth/module-bundling.md)
* [Deploy destinations](./in-depth/deploy-destinations.md) * [Deploy destinations](./in-depth/deploy-destinations.md)
* [TypeScript](./in-depth/typescript.md) * [TypeScript](./in-depth/typescript.md)
* [Prettier](./in-depth/prettier.md)
* [Cookbook](./in-depth/cookbook.md) * [Cookbook](./in-depth/cookbook.md)
* [Environment variables](./in-depth/cookbook/environment-variables.md) * [Environment variables](./in-depth/cookbook/environment-variables.md)
* [One-line PowerShell setup](./in-depth/cookbook/one-line-powershell.md) * [One-line PowerShell setup](./in-depth/cookbook/one-line-powershell.md)

58
docs/in-depth/prettier.md Normal file
View File

@@ -0,0 +1,58 @@
# 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.
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
}
}
```
## 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
}
}

View File

@@ -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: 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 [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 [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`. * 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`. * in the [variable-name](http://palantir.github.io/tslint/rules/variable-name/) rule, added `allow-leading-underscore`.
### Customising TSLint ### Customising TSLint

View File

@@ -28,6 +28,7 @@
"devDependencies": { "devDependencies": {
"@types/lodash": "^3.10.1", "@types/lodash": "^3.10.1",
"@types/screeps": "^2.4.0", "@types/screeps": "^2.4.0",
"prettier": "^1.14.0",
"rollup": "^0.57.1", "rollup": "^0.57.1",
"rollup-plugin-clear": "^2.0.7", "rollup-plugin-clear": "^2.0.7",
"rollup-plugin-commonjs": "^9.1.4", "rollup-plugin-commonjs": "^9.1.4",
@@ -35,6 +36,7 @@
"rollup-plugin-screeps": "^0.1.2", "rollup-plugin-screeps": "^0.1.2",
"rollup-plugin-typescript2": "^0.16.1", "rollup-plugin-typescript2": "^0.16.1",
"tslint": "^5.9.1", "tslint": "^5.9.1",
"tslint-config-prettier": "^1.14.0",
"typescript": "^2.9.2" "typescript": "^2.9.2"
}, },
"dependencies": { "dependencies": {

View File

@@ -1,5 +1,5 @@
// tslint:disable:no-conditional-assignment // tslint:disable:no-conditional-assignment
import {SourceMapConsumer} from "source-map"; import { SourceMapConsumer } from "source-map";
export class ErrorMapper { export class ErrorMapper {
// Cache consumer // Cache consumer
@@ -26,7 +26,7 @@ export class ErrorMapper {
* @returns {string} The source-mapped stack trace * @returns {string} The source-mapped stack trace
*/ */
public static sourceMappedStackTrace(error: Error | string): string { 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)) { if (this.cache.hasOwnProperty(stack)) {
return this.cache[stack]; return this.cache[stack];
} }
@@ -35,7 +35,7 @@ export class ErrorMapper {
let match: RegExpExecArray | null; let match: RegExpExecArray | null;
let outStack = error.toString(); let outStack = error.toString();
while (match = re.exec(stack)) { while ((match = re.exec(stack))) {
if (match[2] === "main") { if (match[2] === "main") {
const pos = this.consumer.originalPositionFor({ const pos = this.consumer.originalPositionFor({
column: parseInt(match[4], 10), column: parseInt(match[4], 10),

View File

@@ -1,6 +1,8 @@
{ {
"rulesDirectory": "tslint-plugin-prettier",
"extends" : [ "extends" : [
"tslint:recommended" "tslint:recommended",
"tslint-config-prettier"
], ],
"rules": { "rules": {
"forin": false, "forin": false,
@@ -8,7 +10,6 @@
"member-ordering": [false], "member-ordering": [false],
"no-console": [false], "no-console": [false],
"no-namespace": [true, "allow-declarations"], "no-namespace": [true, "allow-declarations"],
"trailing-comma": [true, {"multiline": "never", "singleline": "never"}],
"variable-name": [ "variable-name": [
true, true,
"ban-keywords", "ban-keywords",