From b599125669cc6f0413976ab9304a0b6907999a23 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 16 Nov 2016 08:47:54 -0800 Subject: [PATCH] Start using linter --- .gitignore | 1 + PULL_REQUEST_TEMPLATE.md | 5 +++-- README.md | 22 ++++++++++++++++++++++ base64-js/tslint.json | 1 + package.json | 1 + scripts/lint.js | 11 +++++++++++ scripts/new-package.js | 1 + scripts/new-package.ts | 2 ++ tslint.json | 1 + 9 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 base64-js/tslint.json create mode 100644 scripts/lint.js create mode 100644 tslint.json diff --git a/.gitignore b/.gitignore index a78522ea9b..858238c5bf 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ _infrastructure/tests/build *.js.map !*.js/ !scripts/new-package.js +!scripts/lint.js node_modules diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index 053b2c3eae..a864dbcb13 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -1,15 +1,16 @@ Please fill in this template. - [ ] Prefer to make your PR against the `types-2.0` branch. -- [ ] The package does not provide its own types, and you can not add them. - [ ] Test the change in your own code. - [ ] Follow the advice from the [readme](https://github.com/DefinitelyTyped/DefinitelyTyped#make-a-pull-request). - [ ] Avoid [common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped#common-mistakes). +- [ ] Run `npm run lint -- package-name` if a `tslint.json` is present. If adding a new definition: +- [ ] The package does not provide its own types, and you can not add them. - [ ] If this is for an NPM package, match the name. If not, do not conflict with the name of an NPM package. - [ ] Run `tsc` without errors. -- [ ] Include the required [files](https://github.com/DefinitelyTyped/DefinitelyTyped#create-a-new-package) and header. +- [ ] Include the required [files](https://github.com/DefinitelyTyped/DefinitelyTyped#create-a-new-package) and header. Base these on the README, *not* on an existing project. If changing an existing definition: - [ ] Provide a URL to documentation or source code which provides context for the suggested changes: <> diff --git a/README.md b/README.md index 0d719e7f24..d3e6c0e27c 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ DefinitelyTyped members routinely monitor for new PRs, though keep in mind that For a good example package, see [base64-js](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/types-2.0/base64-js). + #### Common mistakes * First, follow advice from the [handbook](http://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html). @@ -153,6 +154,27 @@ Make a PR doing the following: To do this, add a `package.json` with `"dependencies": { "foo": "x.y.z" }`. +#### Lint + +To lint a package, just add a `tslint.json` to that package containing `{ "extends": "../tslint.json" }`. All new packages must be linted. +If a `tslint.json` turns rules off, this is because that hasn't been fixed yet. For example: + +```json +{ + "extends": "../tslint.json", + "rules": { + // This package uses the Function type, and it will take effort to fix. + "forbidden-types": false + } +} +``` + +(To indicate that a lint rule truly does not apply, use `// tslint:disable:rule-name` or better, `//tslint:disable-next-line:rule-name`.) + +Only `.d.ts` files are linted. +Test the linter by running `npm run lint -- package-name`. Do not use a globally installed tslint. + + ## FAQ #### What exactly is the relationship between this repository and the `@types` packages on NPM? diff --git a/base64-js/tslint.json b/base64-js/tslint.json new file mode 100644 index 0000000000..2221e40e4a --- /dev/null +++ b/base64-js/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } \ No newline at end of file diff --git a/package.json b/package.json index 58b29c1590..f8d482c4b8 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "scripts": { "compile-scripts": "tsc -p scripts", "new-package": "node scripts/new-package.js", + "lint": "node scripts/lint.js", "test": "node node_modules/types-publisher/bin/tester/test.js --run-from-definitely-typed --nProcesses 1" }, "devDependencies": { diff --git a/scripts/lint.js b/scripts/lint.js new file mode 100644 index 0000000000..0a840720eb --- /dev/null +++ b/scripts/lint.js @@ -0,0 +1,11 @@ +// Usage: npm run lint -- my-package-name +const pkg = process.argv[2]; +const execSync = require("child_process").execSync; +const cmd = `node node_modules/tslint/lib/tslint-cli --format stylish ${pkg}/**/*.d.ts`; +console.log(cmd); +try { + // Child process writes directly to our own stdout + execSync(cmd, { stdio: "inherit" }); +} catch (_) { + // Process should have printed out error info +} diff --git a/scripts/new-package.js b/scripts/new-package.js index 5a9690b43c..5adcdae176 100644 --- a/scripts/new-package.js +++ b/scripts/new-package.js @@ -36,6 +36,7 @@ const tsconfig = { ] }; write("tsconfig.json", JSON.stringify(tsconfig, undefined, 4)); +write("tslint.json", '{ "extends": "../tslint.json" }'); function write(name, content) { fs_1.writeFileSync(path.join(newPackageName, name), content); } diff --git a/scripts/new-package.ts b/scripts/new-package.ts index 69e914ffea..1818111a74 100644 --- a/scripts/new-package.ts +++ b/scripts/new-package.ts @@ -40,6 +40,8 @@ const tsconfig = { }; write("tsconfig.json", JSON.stringify(tsconfig, undefined, 4)); +write("tslint.json", '{ "extends": "../tslint.json" }'); + function write(name: string, content: string) { writeFileSync(path.join(newPackageName, name), content); } diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000000..6b04ae7f36 --- /dev/null +++ b/tslint.json @@ -0,0 +1 @@ +{ "extends": "./node_modules/types-publisher/tslint-definitions.json" } \ No newline at end of file