Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 8202cf0

Browse files
Enable all tsconfig.json options (#293)
PR-URL: #293
1 parent 499112c commit 8202cf0

17 files changed

+369
-197
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ npm-debug.log
55
.DS_Store
66
.vscode
77
src
8+
definitions

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
},
2323
"devDependencies": {
2424
"@types/acorn": "^4.0.2",
25+
"@types/async": "^2.0.40",
2526
"@types/estree": "0.0.35",
27+
"@types/extend": "^2.0.30",
28+
"@types/lodash": "^4.14.66",
2629
"@types/node": "^7.0.18",
30+
"@types/semver": "^5.3.31",
2731
"@types/source-map": "^0.5.0",
2832
"changelog-maker": "^2.2.2",
2933
"clang-format": "^1.0.53",

src.ts/agent/debug-assert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import * as realAssert from 'assert';
1818

19-
const nop = _ => _;
19+
const nop = (_: any) => _;
2020
const fakeAssert: any = nop;
2121
fakeAssert.deepEqual = fakeAssert.deepStrictEqual = fakeAssert.doesNotThrow =
2222
fakeAssert.equal = fakeAssert.fail = fakeAssert.ifError =

src.ts/agent/debuglet.ts

Lines changed: 84 additions & 45 deletions
Large diffs are not rendered by default.

src.ts/agent/scanner.ts

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,34 @@
1515
*/
1616

1717
import * as crypto from 'crypto';
18-
import * as findit from 'findit2';
18+
19+
import * as events from 'events';
20+
// TODO: Make this more precise.
21+
const findit: (dir: string) => events.EventEmitter = require('findit2');
22+
1923
import * as fs from 'fs';
2024
import * as _ from 'lodash';
2125
import * as path from 'path';
22-
import * as split from 'split';
26+
27+
// TODO: Make this more precise.
28+
const split: () => any = require('split');
2329

2430
export interface FileStats {
25-
hash: string;
31+
// TODO: Verify that this member should actually be optional.
32+
hash?: string;
2633
lines: number;
2734
}
2835

29-
export interface ScanStats { [filename: string]: FileStats; }
36+
// TODO: Update the code so that `undefined is not a possible property value
37+
export interface ScanStats { [filename: string]: FileStats|undefined; }
38+
39+
export interface ScanResults {
40+
all(): ScanStats;
41+
selectStats(regex: RegExp): ScanStats;
42+
selectFiles(regex: RegExp, baseDir: string): string[];
43+
}
3044

31-
class ScanResults {
45+
class ScanResultsImpl implements ScanResults {
3246
private stats_: ScanStats;
3347

3448
/**
@@ -62,8 +76,7 @@ class ScanResults {
6276
* to determine if the scan results for that filename
6377
* should be included in the returned results.
6478
*/
65-
selectStats(regex: RegExp): FileStats[] {
66-
// TODO: Typescript: Determine why {} is needed here
79+
selectStats(regex: RegExp): ScanStats | {} {
6780
return _.pickBy(this.stats_, function(_, key) {
6881
return regex.test(key);
6982
});
@@ -97,14 +110,15 @@ class ScanResults {
97110

98111
export function scan(
99112
shouldHash: boolean, baseDir: string, regex: RegExp,
100-
callback: (err?: Error, results?: ScanResults, hash?: string) =>
113+
callback: (err: Error|null, results?: ScanResults, hash?: string) =>
101114
void): void {
102-
findFiles(baseDir, regex, function(err, fileList) {
115+
findFiles(baseDir, regex, function(err: Error|null, fileList?: string[]) {
103116
if (err) {
104117
callback(err);
105118
return;
106119
}
107-
computeStats(fileList, shouldHash, callback);
120+
// TODO: Handle the case where `fileList` is undefined.
121+
computeStats(fileList as string[], shouldHash, callback);
108122
});
109123
}
110124

@@ -121,27 +135,29 @@ export function scan(
121135
// call signature
122136
function computeStats(
123137
fileList: string[], shouldHash: boolean,
124-
callback: (err?: Error, results?: ScanResults, hash?: string) =>
138+
callback: (err: Error|null, results?: ScanResults, hash?: string) =>
125139
void): void {
126140
let pending = fileList.length;
127141
// return a valid, if fake, result when there are no js files to hash.
128142
if (pending === 0) {
129-
callback(null, new ScanResults({}), 'EMPTY-no-js-files');
143+
callback(null, new ScanResultsImpl({}), 'EMPTY-no-js-files');
130144
return;
131145
}
132146

133-
const hashes: string[] = [];
134-
const statistics = {};
147+
// TODO: Address the case where the array contains `undefined`.
148+
const hashes: Array<string|undefined> = [];
149+
const statistics: ScanStats = {};
135150
fileList.forEach(function(filename) {
136-
stats(filename, shouldHash, function(err, fileStats) {
151+
stats(filename, shouldHash, function(err: Error, fileStats: FileStats|undefined) {
137152
if (err) {
138153
callback(err);
139154
return;
140155
}
141156

142157
pending--;
143158
if (shouldHash) {
144-
hashes.push(fileStats.hash);
159+
// TODO: Address the case when `fileStats` is `undefined`
160+
hashes.push((fileStats as FileStats).hash);
145161
}
146162
statistics[filename] = fileStats;
147163

@@ -154,7 +170,7 @@ function computeStats(
154170
const sha1 = crypto.createHash('sha1').update(buffer).digest('hex');
155171
hash = 'SHA1-' + sha1;
156172
}
157-
callback(null, new ScanResults(statistics), hash);
173+
callback(null, new ScanResultsImpl(statistics), hash);
158174
}
159175
});
160176
});
@@ -171,7 +187,7 @@ function computeStats(
171187
*/
172188
function findFiles(
173189
baseDir: string, regex: RegExp,
174-
callback: (err?: Error, fileList?: string[]) => void): void {
190+
callback: (err: Error|null, fileList?: string[]) => void): void {
175191
let errored = false;
176192

177193
if (!baseDir) {
@@ -182,20 +198,20 @@ function findFiles(
182198
const find = findit(baseDir);
183199
const fileList: string[] = [];
184200

185-
find.on('error', function(err) {
201+
find.on('error', function(err: Error) {
186202
errored = true;
187203
callback(err);
188204
return;
189205
});
190206

191-
find.on('directory', function(dir, _, stop) {
207+
find.on('directory', function(dir: string, _: fs.Stats, stop: () => void) {
192208
const base = path.basename(dir);
193209
if (base === '.git' || base === 'node_modules') {
194210
stop(); // do not descend
195211
}
196212
});
197213

198-
find.on('file', function(file) {
214+
find.on('file', function(file: string) {
199215
if (regex.test(file)) {
200216
fileList.push(file);
201217
}
@@ -220,26 +236,27 @@ function findFiles(
220236
*/
221237
function stats(
222238
filename: string, shouldHash: boolean,
223-
cb: (err, stats?: FileStats) => void): void {
224-
let shasum;
239+
cb: (err: Error|null, stats?: FileStats) => void): void {
240+
let shasum: crypto.Hash;
225241
if (shouldHash) {
226242
shasum = crypto.createHash('sha1');
227243
}
228244
// TODO: Determine why property 'ReadStream' does not exist on type 'fs'
229245
const s = (fs as any).ReadStream(filename);
230246
let lines = 0;
231247
const byLine = s.pipe(split());
232-
byLine.on('error', function(e) {
248+
byLine.on('error', function(e: Error) {
233249
cb(e);
234250
});
235-
byLine.on('data', function(d) {
251+
byLine.on('data', function(d: string) {
236252
if (shouldHash) {
237253
shasum.update(d);
238254
}
239255
lines++;
240256
});
241257
byLine.on('end', function() {
242-
let d;
258+
// TODO: Address the case where `d` is `undefined`.
259+
let d: string | undefined;
243260
if (shouldHash) {
244261
d = shasum.digest('hex');
245262
}

src.ts/agent/sourcemapper.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import * as sourceMap from 'source-map';
2222

2323
/** @define {string} */ const MAP_EXT = '.map';
2424

25-
interface MapInfoInput {
25+
export interface MapInfoInput {
2626
outputFile: string;
2727
mapFile: string;
28-
mapConsumer: sourceMap.SourceMapConsumer;
28+
mapConsumer: sourceMap.RawSourceMap;
2929
}
3030

3131
export interface MapInfoOutput {
@@ -36,11 +36,11 @@ export interface MapInfoOutput {
3636

3737
export function create(
3838
sourcemapPaths: string[],
39-
callback: (err?: Error, mapper?: SourceMapper) => void): void {
39+
callback: (err: Error|null, mapper?: SourceMapper) => void): void {
4040
const mapper = new SourceMapper();
4141
const callList =
42-
Array.prototype.slice.call(sourcemapPaths).map(function(path) {
43-
return function(cb) {
42+
Array.prototype.slice.call(sourcemapPaths).map(function(path: string) {
43+
return function(cb: (err: Error|null) => void) {
4444
processSourcemap(mapper.infoMap_, path, cb);
4545
};
4646
});
@@ -64,7 +64,7 @@ export function create(
6464
*/
6565
function processSourcemap(
6666
infoMap: Map<string, MapInfoInput>, mapPath: string,
67-
callback: (err?: Error) => void): void {
67+
callback: (err: Error|null) => void): void {
6868
// this handles the case when the path is undefined, null, or
6969
// the empty string
7070
if (!mapPath || !_.endsWith(mapPath, MAP_EXT)) {
@@ -75,15 +75,18 @@ function processSourcemap(
7575
}
7676
mapPath = path.normalize(mapPath);
7777

78-
fs.readFile(mapPath, 'utf8', function(err, data) {
78+
fs.readFile(mapPath, 'utf8', function(err: Error, data: string) {
7979
if (err) {
8080
return callback(
8181
new Error('Could not read sourcemap file ' + mapPath + ': ' + err));
8282
}
8383

84-
let consumer;
84+
let consumer: sourceMap.RawSourceMap;
8585
try {
86-
consumer = new sourceMap.SourceMapConsumer(data);
86+
// TODO: Determine how to reconsile the type conflict where `consumer`
87+
// is constructed as a SourceMapConsumer but is used as a
88+
// RawSourceMap.
89+
consumer = new sourceMap.SourceMapConsumer(data) as any as sourceMap.RawSourceMap;
8790
} catch (e) {
8891
return callback(new Error(
8992
'An error occurred while reading the ' +
@@ -102,12 +105,12 @@ function processSourcemap(
102105
const outputPath = path.normalize(path.join(parentDir, outputBase));
103106

104107
const sources = Array.prototype.slice.call(consumer.sources)
105-
.filter(function(value) {
108+
.filter(function(value: string) {
106109
// filter out any empty string, null, or undefined
107110
// sources
108111
return !!value;
109112
})
110-
.map(function(relPath) {
113+
.map(function(relPath: string) {
111114
// resolve the paths relative to the map file so that
112115
// they are relative to the process's current working
113116
// directory
@@ -119,7 +122,7 @@ function processSourcemap(
119122
new Error('No sources listed in the sourcemap file ' + mapPath));
120123
}
121124

122-
sources.forEach(function(src) {
125+
sources.forEach(function(src: string) {
123126
infoMap.set(
124127
path.normalize(src),
125128
{outputFile: outputPath, mapFile: mapPath, mapConsumer: consumer});
@@ -187,15 +190,17 @@ export class SourceMapper {
187190
return null;
188191
}
189192

190-
const entry = this.infoMap_.get(inputPath);
193+
// TODO: `entry` could be `undefined` here. Address this case.
194+
const entry = this.infoMap_.get(inputPath) as any as MapInfoInput;
191195
const sourcePos = {
192196
source: path.relative(path.dirname(entry.mapFile), inputPath),
193197
line: lineNumber + 1, // the SourceMapConsumer expects the line number
194198
// to be one-based but expects the column number
195199
column: colNumber // to be zero-based
196200
};
197201

198-
const consumer = entry.mapConsumer;
202+
// TODO: Determine how to remove the explicit cast here.
203+
const consumer: sourceMap.SourceMapConsumer = entry.mapConsumer as any as sourceMap.SourceMapConsumer;
199204
const allPos = consumer.allGeneratedPositionsFor(sourcePos);
200205
/*
201206
* Based on testing, it appears that the following code is needed to
@@ -204,10 +209,10 @@ export class SourceMapper {
204209
* In particular, the generatedPositionFor() alone doesn't appear to
205210
* give the correct mapping information.
206211
*/
207-
const mappedPos = allPos && allPos.length > 0 ?
212+
const mappedPos: sourceMap.Position = allPos && allPos.length > 0 ?
208213
Array.prototype.reduce.call(
209214
allPos,
210-
function(accumulator, value /*, index, arr*/) {
215+
function(accumulator: sourceMap.Position, value: sourceMap.Position /*, index, arr*/) {
211216
return value.line < accumulator.line ? value : accumulator;
212217
}) :
213218
consumer.generatedPositionFor(sourcePos);
@@ -217,9 +222,12 @@ export class SourceMapper {
217222
line: mappedPos.line - 1, // convert the one-based line numbers returned
218223
// by the SourceMapConsumer to the expected
219224
// zero-based output.
220-
column: mappedPos.col // SourceMapConsumer uses zero-based column
221-
// numbers which is the same as the expected
222-
// output
225+
// TODO: The `sourceMap.Position` type definition has a `column`
226+
// attribute and not a `col` attribute. Determine if the type
227+
// definition or this code is correct.
228+
column: (mappedPos as any).col // SourceMapConsumer uses zero-based column
229+
// numbers which is the same as the
230+
// expected output
223231
};
224232
}
225233
}

0 commit comments

Comments
 (0)