diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000000000..fdc1df00a7e26 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +print-width: 120 +trailing-comma: all +tab-width: 4 +# Thanks prettier-miscellaneous. +# This doesn't seem to be documented though. +# https://github.com/arijs/prettier-miscellaneous/pull/21 +break-before-else: true diff --git a/Gulpfile.ts b/Gulpfile.ts index 4ca099b56e407..588e0d932416d 100644 --- a/Gulpfile.ts +++ b/Gulpfile.ts @@ -1045,16 +1045,23 @@ function spawnLintWorker(files: {path: string}[], callback: (failures: number) = sendNextFile(files, child, callback, failures); } -gulp.task("lint", "Runs tslint on the compiler sources. Optional arguments are: --f[iles]=regex", ["build-rules"], () => { - if (fold.isTravis()) console.log(fold.start("lint")); - const fileMatcher = cmdLineOptions["files"]; - const files = fileMatcher - ? `src/**/${fileMatcher}` - : "Gulpfile.ts 'scripts/tslint/**/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'"; - const cmd = `node node_modules/tslint/bin/tslint ${files} --formatters-dir ./built/local/tslint/formatters --format autolinkableStylish`; - console.log("Linting: " + cmd); - child_process.execSync(cmd, { stdio: [0, 1, 2] }); - if (fold.isTravis()) console.log(fold.end("lint")); +gulp.task("prettier", "Runs prettier.", [], () => { + withFold("prettier", () => { + const cmd = "node node_modules/prettier-miscellaneous/bin/prettier --write src/**/*.ts"; + child_process.execSync(cmd, { stdio: [0, 1, 2] }); + }); +}); + +gulp.task("lint", "Runs tslint on the compiler sources. Optional arguments are: --f[iles]=regex", ["build-rules", "pretty"], () => { + withFold("lint", () => { + const fileMatcher = cmdLineOptions["files"]; + const files = fileMatcher + ? `src/**/${fileMatcher}` + : "Gulpfile.ts 'scripts/tslint/**/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'"; + const cmd = `node node_modules/tslint/bin/tslint ${files} --formatters-dir ./built/local/tslint/formatters --format autolinkableStylish`; + console.log("Linting: " + cmd); + child_process.execSync(cmd, { stdio: [0, 1, 2] }); + }); }); gulp.task("default", "Runs 'local'", ["local"]); @@ -1062,3 +1069,13 @@ gulp.task("default", "Runs 'local'", ["local"]); gulp.task("watch", "Watches the src/ directory for changes and executes runtests-parallel.", [], () => { gulp.watch("src/**/*.*", ["runtests-parallel"]); }); + +function withFold(name: string, action: () => void): void { + if (fold.isTravis()) { + console.log(fold.start(name)); + } + action(); + if (fold.isTravis()) { + console.log(fold.end(name)); + } +} diff --git a/Jakefile.js b/Jakefile.js index 7de0635542b27..302a357e4bf21 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -1219,17 +1219,43 @@ function spawnLintWorker(files, callback) { sendNextFile(files, child, callback, failures); } +task("prettier", [], () => { + withFold("prettier", endFold => { + const cmd = "node node_modules/prettier-miscellaneous/bin/prettier --write src/**/*.ts"; + jake.exec([cmd], { interactive: true }, () => { + endFold(); + complete(); + }); + }); +}); + desc("Runs tslint on the compiler sources. Optional arguments are: f[iles]=regex"); -task("lint", ["build-rules"], () => { - if (fold.isTravis()) console.log(fold.start("lint")); - const fileMatcher = process.env.f || process.env.file || process.env.files; - const files = fileMatcher - ? `src/**/${fileMatcher}` - : "Gulpfile.ts 'scripts/tslint/**/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'"; - const cmd = `node node_modules/tslint/bin/tslint ${files} --formatters-dir ./built/local/tslint/formatters --format autolinkableStylish`; - console.log("Linting: " + cmd); - jake.exec([cmd], { interactive: true }, () => { - if (fold.isTravis()) console.log(fold.end("lint")); - complete(); +task("lint", ["build-rules", "prettier"], () => { + withFold("lint", endFold => { + const fileMatcher = process.env.f || process.env.file || process.env.files; + const files = fileMatcher + ? `src/**/${fileMatcher}` + : "Gulpfile.ts 'scripts/tslint/**/*.ts' 'src/**/*.ts' --exclude src/lib/es5.d.ts --exclude 'src/lib/*.generated.d.ts'"; + const cmd = `node node_modules/tslint/bin/tslint ${files} --formatters-dir ./built/local/tslint/formatters --format autolinkableStylish`; + console.log("Linting: " + cmd); + jake.exec([cmd], { interactive: true }, () => { + endFold(); + complete(); + }); }); }); + +/** + * @param {string} name + * @param {(endFold: () => void) => void} action + */ +function withFold(name, action) { + if (fold.isTravis()) { + console.log(fold.start(name)); + } + action(() => { + if (fold.isTravis()) { + console.log(fold.end(name)); + } + }); +} diff --git a/package.json b/package.json index ca7d48c77746e..9d860dedb5e53 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "browser-resolve": "^1.11.2", "browserify": "latest", "chai": "latest", + "colors": "latest", "convert-source-map": "latest", "del": "latest", "gulp": "3.X", @@ -62,13 +63,16 @@ "gulp-newer": "latest", "gulp-sourcemaps": "latest", "gulp-typescript": "latest", + "husky": "latest", "istanbul": "latest", "jake": "latest", + "lint-staged": "latest", "merge2": "latest", "minimist": "latest", "mkdirp": "latest", "mocha": "latest", "mocha-fivemat-progress-reporter": "latest", + "prettier-miscellaneous": "latest", "q": "latest", "run-sequence": "latest", "sorcery": "latest", @@ -76,10 +80,10 @@ "travis-fold": "latest", "ts-node": "latest", "tslint": "latest", - "colors": "latest", "typescript": "next" }, "scripts": { + "precommit": "lint-staged", "pretest": "jake tests", "test": "jake runtests-parallel", "build": "npm run build:compiler && npm run build:tests", @@ -92,6 +96,12 @@ "lint": "jake lint", "setup-hooks": "node scripts/link-hooks.js" }, + "lint-staged": { + "src/**/*.ts": [ + "prettier --write", + "git add" + ] + }, "browser": { "fs": false, "os": false,