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

Commit

Permalink
Enable all tsconfig.json options (#293)
Browse files Browse the repository at this point in the history
PR-URL: #293
  • Loading branch information
DominicKramer committed Jun 29, 2017
1 parent 499112c commit 8202cf0
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 197 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ npm-debug.log
.DS_Store
.vscode
src
definitions
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
},
"devDependencies": {
"@types/acorn": "^4.0.2",
"@types/async": "^2.0.40",
"@types/estree": "0.0.35",
"@types/extend": "^2.0.30",
"@types/lodash": "^4.14.66",
"@types/node": "^7.0.18",
"@types/semver": "^5.3.31",
"@types/source-map": "^0.5.0",
"changelog-maker": "^2.2.2",
"clang-format": "^1.0.53",
Expand Down
2 changes: 1 addition & 1 deletion src.ts/agent/debug-assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import * as realAssert from 'assert';

const nop = _ => _;
const nop = (_: any) => _;
const fakeAssert: any = nop;
fakeAssert.deepEqual = fakeAssert.deepStrictEqual = fakeAssert.doesNotThrow =
fakeAssert.equal = fakeAssert.fail = fakeAssert.ifError =
Expand Down
129 changes: 84 additions & 45 deletions src.ts/agent/debuglet.ts

Large diffs are not rendered by default.

69 changes: 43 additions & 26 deletions src.ts/agent/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@
*/

import * as crypto from 'crypto';
import * as findit from 'findit2';

import * as events from 'events';
// TODO: Make this more precise.
const findit: (dir: string) => events.EventEmitter = require('findit2');

import * as fs from 'fs';
import * as _ from 'lodash';
import * as path from 'path';
import * as split from 'split';

// TODO: Make this more precise.
const split: () => any = require('split');

export interface FileStats {
hash: string;
// TODO: Verify that this member should actually be optional.
hash?: string;
lines: number;
}

export interface ScanStats { [filename: string]: FileStats; }
// TODO: Update the code so that `undefined is not a possible property value
export interface ScanStats { [filename: string]: FileStats|undefined; }

export interface ScanResults {
all(): ScanStats;
selectStats(regex: RegExp): ScanStats;
selectFiles(regex: RegExp, baseDir: string): string[];
}

class ScanResults {
class ScanResultsImpl implements ScanResults {
private stats_: ScanStats;

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

export function scan(
shouldHash: boolean, baseDir: string, regex: RegExp,
callback: (err?: Error, results?: ScanResults, hash?: string) =>
callback: (err: Error|null, results?: ScanResults, hash?: string) =>
void): void {
findFiles(baseDir, regex, function(err, fileList) {
findFiles(baseDir, regex, function(err: Error|null, fileList?: string[]) {
if (err) {
callback(err);
return;
}
computeStats(fileList, shouldHash, callback);
// TODO: Handle the case where `fileList` is undefined.
computeStats(fileList as string[], shouldHash, callback);
});
}

Expand All @@ -121,27 +135,29 @@ export function scan(
// call signature
function computeStats(
fileList: string[], shouldHash: boolean,
callback: (err?: Error, results?: ScanResults, hash?: string) =>
callback: (err: Error|null, results?: ScanResults, hash?: string) =>
void): void {
let pending = fileList.length;
// return a valid, if fake, result when there are no js files to hash.
if (pending === 0) {
callback(null, new ScanResults({}), 'EMPTY-no-js-files');
callback(null, new ScanResultsImpl({}), 'EMPTY-no-js-files');
return;
}

const hashes: string[] = [];
const statistics = {};
// TODO: Address the case where the array contains `undefined`.
const hashes: Array<string|undefined> = [];
const statistics: ScanStats = {};
fileList.forEach(function(filename) {
stats(filename, shouldHash, function(err, fileStats) {
stats(filename, shouldHash, function(err: Error, fileStats: FileStats|undefined) {
if (err) {
callback(err);
return;
}

pending--;
if (shouldHash) {
hashes.push(fileStats.hash);
// TODO: Address the case when `fileStats` is `undefined`
hashes.push((fileStats as FileStats).hash);
}
statistics[filename] = fileStats;

Expand All @@ -154,7 +170,7 @@ function computeStats(
const sha1 = crypto.createHash('sha1').update(buffer).digest('hex');
hash = 'SHA1-' + sha1;
}
callback(null, new ScanResults(statistics), hash);
callback(null, new ScanResultsImpl(statistics), hash);
}
});
});
Expand All @@ -171,7 +187,7 @@ function computeStats(
*/
function findFiles(
baseDir: string, regex: RegExp,
callback: (err?: Error, fileList?: string[]) => void): void {
callback: (err: Error|null, fileList?: string[]) => void): void {
let errored = false;

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

find.on('error', function(err) {
find.on('error', function(err: Error) {
errored = true;
callback(err);
return;
});

find.on('directory', function(dir, _, stop) {
find.on('directory', function(dir: string, _: fs.Stats, stop: () => void) {
const base = path.basename(dir);
if (base === '.git' || base === 'node_modules') {
stop(); // do not descend
}
});

find.on('file', function(file) {
find.on('file', function(file: string) {
if (regex.test(file)) {
fileList.push(file);
}
Expand All @@ -220,26 +236,27 @@ function findFiles(
*/
function stats(
filename: string, shouldHash: boolean,
cb: (err, stats?: FileStats) => void): void {
let shasum;
cb: (err: Error|null, stats?: FileStats) => void): void {
let shasum: crypto.Hash;
if (shouldHash) {
shasum = crypto.createHash('sha1');
}
// TODO: Determine why property 'ReadStream' does not exist on type 'fs'
const s = (fs as any).ReadStream(filename);
let lines = 0;
const byLine = s.pipe(split());
byLine.on('error', function(e) {
byLine.on('error', function(e: Error) {
cb(e);
});
byLine.on('data', function(d) {
byLine.on('data', function(d: string) {
if (shouldHash) {
shasum.update(d);
}
lines++;
});
byLine.on('end', function() {
let d;
// TODO: Address the case where `d` is `undefined`.
let d: string | undefined;
if (shouldHash) {
d = shasum.digest('hex');
}
Expand Down
46 changes: 27 additions & 19 deletions src.ts/agent/sourcemapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import * as sourceMap from 'source-map';

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

interface MapInfoInput {
export interface MapInfoInput {
outputFile: string;
mapFile: string;
mapConsumer: sourceMap.SourceMapConsumer;
mapConsumer: sourceMap.RawSourceMap;
}

export interface MapInfoOutput {
Expand All @@ -36,11 +36,11 @@ export interface MapInfoOutput {

export function create(
sourcemapPaths: string[],
callback: (err?: Error, mapper?: SourceMapper) => void): void {
callback: (err: Error|null, mapper?: SourceMapper) => void): void {
const mapper = new SourceMapper();
const callList =
Array.prototype.slice.call(sourcemapPaths).map(function(path) {
return function(cb) {
Array.prototype.slice.call(sourcemapPaths).map(function(path: string) {
return function(cb: (err: Error|null) => void) {
processSourcemap(mapper.infoMap_, path, cb);
};
});
Expand All @@ -64,7 +64,7 @@ export function create(
*/
function processSourcemap(
infoMap: Map<string, MapInfoInput>, mapPath: string,
callback: (err?: Error) => void): void {
callback: (err: Error|null) => void): void {
// this handles the case when the path is undefined, null, or
// the empty string
if (!mapPath || !_.endsWith(mapPath, MAP_EXT)) {
Expand All @@ -75,15 +75,18 @@ function processSourcemap(
}
mapPath = path.normalize(mapPath);

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

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

const sources = Array.prototype.slice.call(consumer.sources)
.filter(function(value) {
.filter(function(value: string) {
// filter out any empty string, null, or undefined
// sources
return !!value;
})
.map(function(relPath) {
.map(function(relPath: string) {
// resolve the paths relative to the map file so that
// they are relative to the process's current working
// directory
Expand All @@ -119,7 +122,7 @@ function processSourcemap(
new Error('No sources listed in the sourcemap file ' + mapPath));
}

sources.forEach(function(src) {
sources.forEach(function(src: string) {
infoMap.set(
path.normalize(src),
{outputFile: outputPath, mapFile: mapPath, mapConsumer: consumer});
Expand Down Expand Up @@ -187,15 +190,17 @@ export class SourceMapper {
return null;
}

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

const consumer = entry.mapConsumer;
// TODO: Determine how to remove the explicit cast here.
const consumer: sourceMap.SourceMapConsumer = entry.mapConsumer as any as sourceMap.SourceMapConsumer;
const allPos = consumer.allGeneratedPositionsFor(sourcePos);
/*
* Based on testing, it appears that the following code is needed to
Expand All @@ -204,10 +209,10 @@ export class SourceMapper {
* In particular, the generatedPositionFor() alone doesn't appear to
* give the correct mapping information.
*/
const mappedPos = allPos && allPos.length > 0 ?
const mappedPos: sourceMap.Position = allPos && allPos.length > 0 ?
Array.prototype.reduce.call(
allPos,
function(accumulator, value /*, index, arr*/) {
function(accumulator: sourceMap.Position, value: sourceMap.Position /*, index, arr*/) {
return value.line < accumulator.line ? value : accumulator;
}) :
consumer.generatedPositionFor(sourcePos);
Expand All @@ -217,9 +222,12 @@ export class SourceMapper {
line: mappedPos.line - 1, // convert the one-based line numbers returned
// by the SourceMapConsumer to the expected
// zero-based output.
column: mappedPos.col // SourceMapConsumer uses zero-based column
// numbers which is the same as the expected
// output
// TODO: The `sourceMap.Position` type definition has a `column`
// attribute and not a `col` attribute. Determine if the type
// definition or this code is correct.
column: (mappedPos as any).col // SourceMapConsumer uses zero-based column
// numbers which is the same as the
// expected output
};
}
}
Loading

0 comments on commit 8202cf0

Please sign in to comment.