Skip to content

Commit dae4e41

Browse files
authored
BREAKING CHANGE: chore: sync with internal lint (#75)
This synchronizes the lint rules to be closer to the lint rules used internally at Google. Not all rules are available externally, so this is not *exactly* the same. Also updated the TypeScript version to 2.6.1. A fair number of new lint rules are being applied now, which means that the code will be alot more consistent with other Google code, but it also means that users of gts may need to modify the source to pick up this release. For most of the new lint rules, a fixer is also available which should help with migration. TypeScript 2.6.x enables contravariant types for function parameters, which may also require some changes. See [1] for more info. [1] https://blogs.msdn.microsoft.com/typescript/2017/10/31/announcing-typescript-2-6/
1 parent 7b3db0f commit dae4e41

File tree

11 files changed

+100
-54
lines changed

11 files changed

+100
-54
lines changed

package-lock.json

Lines changed: 22 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"compile": "tsc -p .",
2626
"format-check": "./bin/format-check.sh",
2727
"format": "clang-format -i -style='{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}' src/*.ts test/*.ts",
28-
"lint": "tslint -c tslint.json --project . --type-check -t codeFrame",
29-
"lint-fix": "tslint -c tslint.json --project . --type-check -t codeFrame --fix",
28+
"lint": "tslint -c tslint.json --project . -t codeFrame",
29+
"lint-fix": "tslint -c tslint.json --project . -t codeFrame --fix",
3030
"prepare": "npm run compile",
3131
"test": "npm run compile && nyc ava build/test/test*.js",
3232
"posttest": "npm run lint && npm run format-check"
@@ -44,7 +44,7 @@
4444
"meow": "^3.7.0",
4545
"pify": "^3.0.0",
4646
"rimraf": "^2.6.1",
47-
"tslint": "^5.5.0",
47+
"tslint": "^5.8.0",
4848
"update-notifier": "^2.2.0",
4949
"write-file-atomic": "^2.1.0"
5050
},
@@ -69,7 +69,7 @@
6969
"typescript": "~2.6.1"
7070
},
7171
"peerDependencies": {
72-
"typescript": "^2.4.1"
72+
"typescript": "^2.6.1"
7373
},
7474
"ava": {
7575
"require": [

src/clean.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,20 @@
1414
* limitations under the License.
1515
*/
1616
import chalk from 'chalk';
17+
import * as ts from 'typescript';
1718

1819
import {Options} from './cli';
1920
import {getTSConfig, rimrafp} from './util';
2021

22+
interface TSConfig {
23+
compilerOptions: ts.CompilerOptions;
24+
}
25+
2126
/**
2227
* Remove files generated by the build.
2328
*/
2429
export async function clean(options: Options): Promise<boolean> {
25-
const tsconfig = (await getTSConfig(options.targetRootDir));
30+
const tsconfig = (await getTSConfig(options.targetRootDir)) as TSConfig;
2631
if (tsconfig.compilerOptions && tsconfig.compilerOptions.outDir) {
2732
const outDir = tsconfig.compilerOptions.outDir;
2833
if (outDir === '.') {

src/cli.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import {init} from './init';
2020
import {clean} from './clean';
2121

2222
export interface Logger {
23-
log: (...args: any[]) => void;
24-
error: (...args: any[]) => void;
25-
dir: (obj: any, options?: any) => void;
23+
log: (...args: Array<{}>) => void;
24+
error: (...args: Array<{}>) => void;
25+
dir: (obj: {}, options?: {}) => void;
2626
}
2727

2828
export interface Options {
@@ -74,14 +74,15 @@ async function run(verb: string, files: string[]): Promise<boolean> {
7474
gtsRootDir: `${process.cwd()}/node_modules/gts`,
7575
targetRootDir: process.cwd(),
7676
yes: cli.flags.yes || cli.flags.y || false,
77-
logger: logger
77+
logger
7878
};
7979
// Linting/formatting depend on typescript. We don't want to load the
8080
// typescript module during init, since it might not exist.
8181
// See: https://github.com/google/ts-style/issues/48
8282
if (verb === 'init') {
8383
return await init(options);
8484
}
85+
8586
const lint: VerbFilesFunction = require('./lint').lint;
8687
const format: VerbFilesFunction = require('./format').format;
8788
switch (verb) {

src/init.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ import * as path from 'path';
2121
import {Options} from './cli';
2222
import {readFilep as read, readJsonp as readJson, writeFileAtomicp as write} from './util';
2323

24-
interface Bag<T> {
25-
[script: string]: T;
24+
const pkg = require('../../package.json') as PackageJson;
25+
26+
export interface Bag<T> { [script: string]: T; }
27+
28+
// TODO: is this type available from definitelytyped.org? Find it, and drop the
29+
// local definition.
30+
export interface PackageJson {
31+
version?: string;
32+
devDependencies?: Bag<string>;
33+
scripts?: Bag<string>;
2634
}
2735

2836
async function query(
@@ -42,7 +50,7 @@ async function query(
4250
}
4351

4452
async function addScripts(
45-
packageJson: any, options: Options): Promise<boolean> {
53+
packageJson: PackageJson, options: Options): Promise<boolean> {
4654
let edits = false;
4755
const scripts: Bag<string> = {
4856
check: `gts check`,
@@ -81,9 +89,9 @@ async function addScripts(
8189
}
8290

8391
async function addDependencies(
84-
packageJson: any, options: Options): Promise<boolean> {
92+
packageJson: PackageJson, options: Options): Promise<boolean> {
8593
let edits = false;
86-
const deps: Bag<string> = {'gts': 'latest', 'typescript': '^2.4.1'};
94+
const deps: Bag<string> = {'gts': `^${pkg.version}`, 'typescript': '^2.6.1'};
8795

8896
if (!packageJson.devDependencies) {
8997
packageJson.devDependencies = {};
@@ -111,14 +119,14 @@ async function addDependencies(
111119
return edits;
112120
}
113121

114-
function formatJson(object: any) {
122+
function formatJson(object: {}) {
115123
// TODO: preserve the indent from the input file.
116124
const json = JSON.stringify(object, null, ' ');
117125
return `${json}\n`;
118126
}
119127

120128
async function writePackageJson(
121-
packageJson: any, options: Options): Promise<void> {
129+
packageJson: PackageJson, options: Options): Promise<void> {
122130
options.logger.log('Writing package.json...');
123131
if (!options.dryRun) {
124132
await write('./package.json', formatJson(packageJson));

src/lint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function lint(
3232
const program = createProgram(options);
3333
const configuration =
3434
Configuration.findConfiguration(tslintConfigPath, '').results;
35-
const linter = new Linter({fix: fix, formatter: 'codeFrame'}, program);
35+
const linter = new Linter({fix, formatter: 'codeFrame'}, program);
3636
const srcFiles = files.length > 0 ? files : Linter.getFileNames(program);
3737
srcFiles.forEach(file => {
3838
const fileContents = program.getSourceFile(file).getFullText();

src/util.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export async function readJsonp(jsonPath: string) {
2727
return JSON.parse(await readFilep(jsonPath));
2828
}
2929

30-
export interface ReadFileP { (path: string, encoding: string): Promise<any>; }
30+
export interface ReadFileP {
31+
(path: string, encoding: string): Promise<string>;
32+
}
3133

3234
export function nop() {
3335
/* empty */
@@ -38,7 +40,7 @@ export function nop() {
3840
* @param rootDir Directory where the tsconfig.json should be found.
3941
*/
4042
export async function getTSConfig(
41-
rootDir: string, customReadFilep?: ReadFileP): Promise<any> {
43+
rootDir: string, customReadFilep?: ReadFileP): Promise<{}> {
4244
const tsconfigPath = path.join(rootDir, 'tsconfig.json');
4345
customReadFilep = customReadFilep || readFilep;
4446
const json = await customReadFilep(tsconfigPath, 'utf8');

test/fixtures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface Fixtures {
3333
async function setupFixtures(dir: string, fixtures: Fixtures) {
3434
await makeDir(dir);
3535
const keys = Object.keys(fixtures);
36-
for (let key of keys) {
36+
for (const key of keys) {
3737
const filePath = path.join(dir, key);
3838
if (typeof fixtures[key] === 'string') {
3939
const contents = fixtures[key] as string;
@@ -46,9 +46,9 @@ async function setupFixtures(dir: string, fixtures: Fixtures) {
4646
}
4747

4848
export async function withFixtures(
49-
fixtures: Fixtures, fn: (fixturesDir: string) => Promise<any>) {
49+
fixtures: Fixtures, fn: (fixturesDir: string) => Promise<{}|void>) {
5050
const keep = !!process.env.GTS_KEEP_TEMPDIRS;
51-
const dir = tmp.dirSync({keep: keep, unsafeCleanup: true});
51+
const dir = tmp.dirSync({keep, unsafeCleanup: true});
5252

5353
await setupFixtures(dir.name, fixtures);
5454

test/test-kitchen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface ExecError extends Error {
2929
}
3030

3131
function isExecError(err: Error|ExecError): err is ExecError {
32-
return (<ExecError>err).code !== undefined;
32+
return (err as ExecError).code !== undefined;
3333
}
3434

3535
// cp.exec doesn't fit the (err ^ result) pattern because a process can write
@@ -54,7 +54,7 @@ const execp =
5454
};
5555

5656
const keep = !!process.env.GTS_KEEP_TEMPDIRS;
57-
const stagingDir = tmp.dirSync({keep: keep, unsafeCleanup: true});
57+
const stagingDir = tmp.dirSync({keep, unsafeCleanup: true});
5858
const stagingPath = stagingDir.name;
5959
const execOpts = {
6060
cwd: `${stagingPath}/kitchen`

test/test-util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {getTSConfig} from '../src/util';
2121
test('get should parse the correct tsconfig file', async t => {
2222
const FAKE_DIRECTORY = '/some/fake/directory';
2323
const FAKE_CONFIG = {a: 'b'};
24-
function fakeReadFilep(configPath: string, encoding: string): Promise<any> {
24+
function fakeReadFilep(
25+
configPath: string, encoding: string): Promise<string> {
2526
t.is(configPath, path.join(FAKE_DIRECTORY, 'tsconfig.json'));
2627
t.is(encoding, 'utf8');
2728
return Promise.resolve(JSON.stringify(FAKE_CONFIG));

0 commit comments

Comments
 (0)