From ed50105a17b6c608d8d64478b575e0733d8c57ab Mon Sep 17 00:00:00 2001 From: "Eduardo L. Buratti" <elburatti@inf.ufpr.br> Date: Thu, 18 Aug 2016 15:05:59 -0300 Subject: [PATCH] Add tslint --- package.json | 7 +++-- src/api/controllers/collect.ts | 6 ++-- src/api/controllers/data.ts | 8 ++--- src/api/router-v1.ts | 10 +++---- src/boot.ts | 20 ++++++------- src/core/aggregate.spec.ts | 24 +++++++-------- src/core/aggregate.ts | 10 +++---- src/core/server.ts | 26 +++++++--------- src/core/source.ts | 10 +++---- src/core/transformer.ts | 14 ++++----- src/util/hash.spec.ts | 54 +++++++++++++++++----------------- src/util/hash.ts | 16 +++++----- test/global.ts | 2 +- tslint.json | 9 ++++++ 14 files changed, 109 insertions(+), 107 deletions(-) create mode 100644 tslint.json diff --git a/package.json b/package.json index 7fd92ed7..9a3f2986 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "prestart": "tsc", "start": "node build/src/boot", "pretest": "tsc", - "test": "mocha" + "test": "mocha", + "lint": "tslint -s node_modules/tslint-stylish -t stylish src/**/*.ts test/**/*.ts" }, "repository": { "type": "git", @@ -40,6 +41,8 @@ }, "devDependencies": { "chai": "^3.5.0", - "mocha": "^3.0.2" + "mocha": "^3.0.2", + "tslint": "^3.14.0", + "tslint-stylish": "^2.1.0-beta" } } diff --git a/src/api/controllers/collect.ts b/src/api/controllers/collect.ts index 76b3e3bd..beedfd67 100644 --- a/src/api/controllers/collect.ts +++ b/src/api/controllers/collect.ts @@ -22,13 +22,13 @@ import * as express from "express"; export class CollectCtrl { static write(req: express.Request, res: express.Response, next: express.NextFunction) { - if ('_id' in req.body) { + if ("_id" in req.body) { res.status(400) - .json({ message: 'Property named \'_id\' is protected.' }); + .json({ message: "Property named \"_id\" is protected." }); return; } res.status(500) - .json({ message: 'Error while writing to the database.' }); + .json({ message: "Error while writing to the database." }); } } diff --git a/src/api/controllers/data.ts b/src/api/controllers/data.ts index 3497c5b5..098110d4 100644 --- a/src/api/controllers/data.ts +++ b/src/api/controllers/data.ts @@ -22,10 +22,10 @@ import * as express from "express"; export class DataCtrl { static read(req: express.Request, res: express.Response, next: express.NextFunction) { - let metrics = req.query.metrics.split(','); - let dimensions = req.query.dimensions.split(','); + let metrics = req.query.metrics.split(","); + let dimensions = req.query.dimensions.split(","); - res.status(500).json({ message: 'Query execution failed ' + - 'because of an unknown error.' }); + res.status(500).json({ message: "Query execution failed " + + "because of an unknown error." }); } } diff --git a/src/api/router-v1.ts b/src/api/router-v1.ts index d8c4211c..1ac9ba57 100644 --- a/src/api/router-v1.ts +++ b/src/api/router-v1.ts @@ -18,13 +18,13 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -const osprey = require('osprey'); +const osprey = require("osprey"); // import controllers -import { DataCtrl } from './controllers/data'; -import { CollectCtrl } from './controllers/collect'; +import { DataCtrl } from "./controllers/data"; +import { CollectCtrl } from "./controllers/collect"; export const router = osprey.Router(); -router.get('/data', DataCtrl.read); -router.post('/collect/{class}', CollectCtrl.write); +router.get("/data", DataCtrl.read); +router.post("/collect/{class}", CollectCtrl.write); diff --git a/src/boot.ts b/src/boot.ts index f5c103de..cf7ae691 100755 --- a/src/boot.ts +++ b/src/boot.ts @@ -20,21 +20,21 @@ */ // external libraries -import express = require('express'); -import path = require('path'); -const osprey = require('osprey'); -const ramlParser = require('raml-parser'); +import express = require("express"); +import path = require("path"); +const osprey = require("osprey"); +const ramlParser = require("raml-parser"); // load router -import { router } from './api/router-v1'; +import { router } from "./api/router-v1"; // create a new express app const app = module.exports = express(); // parse the RAML spec and load osprey middleware -ramlParser.loadFile('specs/blendb-api-v1.raml') +ramlParser.loadFile("specs/blendb-api-v1.raml") .then((raml: any) => { - app.use('/v1', + app.use("/v1", osprey.security(raml), osprey.server(raml), router); @@ -43,8 +43,8 @@ ramlParser.loadFile('specs/blendb-api-v1.raml') let port = process.env.PORT || 3000; app.listen(port); - if (app.get('env') === 'development') { - console.log('Server listening on port ' + port + '.'); + if (app.get("env") === "development") { + console.log("Server listening on port " + port + "."); } } else { @@ -53,6 +53,6 @@ ramlParser.loadFile('specs/blendb-api-v1.raml') } }, (err: any) => { - console.error('RAML Parsing Error: ' + err.message); + console.error("RAML Parsing Error: " + err.message); process.exit(1); }); diff --git a/src/core/aggregate.spec.ts b/src/core/aggregate.spec.ts index 5db3d9a2..bcc3a3f6 100644 --- a/src/core/aggregate.spec.ts +++ b/src/core/aggregate.spec.ts @@ -18,23 +18,23 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -import { expect } from 'chai'; +import { expect } from "chai"; -import { Aggregate } from './aggregate'; +import { Aggregate } from "./aggregate"; -describe('aggregate class', () => { - it('should be instantiated with an array metrics and one of dimensions', () => { - let aggr = new Aggregate(['met:one'], ['dim:one', 'dim:two']); - expect(aggr).to.be.an('object'); +describe("aggregate class", () => { + it("should be instantiated with an array metrics and one of dimensions", () => { + let aggr = new Aggregate(["met:one"], ["dim:one", "dim:two"]); + expect(aggr).to.be.an("object"); }); - it('should not be instantiated with an empty array of metrics', () => { - let aggr = new Aggregate([], ['dim:one', 'dim:two']); - expect(aggr).to.be.an('object'); + it("should not be instantiated with an empty array of metrics", () => { + let aggr = new Aggregate([], ["dim:one", "dim:two"]); + expect(aggr).to.be.an("object"); }); - it('should not be instantiated with an empty array of dimensions', () => { - let aggr = new Aggregate(['met:one'], []); - expect(aggr).to.be.an('object'); + it("should not be instantiated with an empty array of dimensions", () => { + let aggr = new Aggregate(["met:one"], []); + expect(aggr).to.be.an("object"); }); }); diff --git a/src/core/aggregate.ts b/src/core/aggregate.ts index aa46fd8e..489b82b6 100644 --- a/src/core/aggregate.ts +++ b/src/core/aggregate.ts @@ -19,9 +19,9 @@ */ export class Aggregate { - metrics: string[]; - dimensions: string[]; - data: any[]; + public metrics: string[]; + public dimensions: string[]; + private data: any[]; constructor(metrics: string[], dimensions: string[], options?: any) { this.metrics = metrics; @@ -30,11 +30,11 @@ export class Aggregate { this.data = []; } - push(data: any) { + public push(data: any) { this.data.push(data); } - truncate() { + public truncate() { this.data = []; } } diff --git a/src/core/server.ts b/src/core/server.ts index 097d1000..8445d178 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -18,18 +18,16 @@ * along with blend. If not, see <http://www.gnu.org/licenses/>. */ -'use strict'; +import { Hash } from "../util/hash"; -import { Hash } from '../util/hash'; - -import { Source } from './source'; -import { Transformer } from './transformer'; -import { Aggregate } from './aggregate'; +import { Source } from "./source"; +import { Transformer } from "./transformer"; +import { Aggregate } from "./aggregate"; export class Server { - sources: Map<string,Source>; - transformers: Map<string,Transformer>; - aggregates: Map<string,Aggregate>; + private sources: Map<string, Source>; + private transformers: Map<string, Transformer>; + private aggregates: Map<string, Aggregate>; constructor() { this.sources = new Map(); @@ -37,7 +35,7 @@ export class Server { this.aggregates = new Map(); } - source(name: string, options?: any) { + public source(name: string, options?: any) { if (this.sources.has(name)) { return this.sources.get(name); } @@ -48,7 +46,7 @@ export class Server { } } - transformer(name: string, options?: any) { + public transformer(name: string, options?: any) { if (this.transformers.has(name)) { return this.transformers.get(name); } @@ -59,7 +57,7 @@ export class Server { } } - aggregate(metrics: string[], dimensions: string[], options?: any) { + public aggregate(metrics: string[], dimensions: string[], options?: any) { const id = Hash.sha1(metrics.sort(), dimensions.sort()); if (this.aggregates.has(id)) { @@ -72,7 +70,7 @@ export class Server { } } - process() { + public process() { this.transformers.forEach((transformer: Transformer) => { const source = this.source(transformer.source); const aggr = this.aggregate(transformer.metrics, @@ -90,7 +88,5 @@ export class Server { // .pipe(transformer.stream()); // .pipe(aggregate.stream()); }); - - console.log(this.aggregates); } } diff --git a/src/core/source.ts b/src/core/source.ts index 38c8833d..f2784fad 100644 --- a/src/core/source.ts +++ b/src/core/source.ts @@ -18,11 +18,9 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -'use strict'; - export class Source { - name: string; - data: any[]; + public name: string; + private data: any[]; constructor(name: string, options: any) { this.name = name; @@ -30,11 +28,11 @@ export class Source { this.data = []; } - push(doc: any) { + public push(doc: any) { this.data.push(doc); } - forEach(callback: Function) { + public forEach(callback: Function) { this.data.forEach((value: any, index: number, array: any[]) => { callback(value); }); diff --git a/src/core/transformer.ts b/src/core/transformer.ts index fab5549d..31471ffd 100644 --- a/src/core/transformer.ts +++ b/src/core/transformer.ts @@ -18,13 +18,11 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -'use strict'; - export class Transformer { - source: string; - metrics: string[]; - dimensions: string[]; - extractors: any; + public source: string; + public metrics: string[]; + public dimensions: string[]; + private extractors: any; constructor(name: string, options: any) { this.source = options.source || null; @@ -36,11 +34,11 @@ export class Transformer { }; } - extractMetrics(doc: any) { + public extractMetrics(doc: any) { return this.extractors.metrics(doc); } - extractDimensions(doc: any) { + public extractDimensions(doc: any) { return this.extractors.dimensions(doc); } } diff --git a/src/util/hash.spec.ts b/src/util/hash.spec.ts index 0aa91073..9ce39c2f 100644 --- a/src/util/hash.spec.ts +++ b/src/util/hash.spec.ts @@ -18,52 +18,52 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -import { expect } from 'chai'; +import { expect } from "chai"; -import { Hash } from './hash'; +import { Hash } from "./hash"; -describe('hash utility library', () => { - it('should generate a sha1 hash for a collection of objects', () => { - let h = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); +describe("hash utility library", () => { + it("should generate a sha1 hash for a collection of objects", () => { + let h = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]); - expect(h).to.be.a('string'); + expect(h).to.be.a("string"); expect(h).to.not.be.empty; }); - it('should generate the same hash for the same input', () => { - let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); - let h2 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); + it("should generate the same hash for the same input", () => { + let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]); + let h2 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]); - expect(h1).to.be.a('string'); - expect(h2).to.be.a('string'); + expect(h1).to.be.a("string"); + expect(h2).to.be.a("string"); expect(h1).to.be.equal(h2); }); - it('should generate the same hash for different order of input', () => { - let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); - let h2 = Hash.sha1('test', ['list', 'of', 'things'], { obj: 'test' }); + it("should generate the same hash for different order of input", () => { + let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]); + let h2 = Hash.sha1("test", ["list", "of", "things"], { obj: "test" }); - expect(h1).to.be.a('string'); - expect(h2).to.be.a('string'); + expect(h1).to.be.a("string"); + expect(h2).to.be.a("string"); expect(h1).to.be.equal(h2); }); - it('should not generate the same hash for distinct input', () => { - let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); - let h2 = Hash.sha1('test', { obj: 'test', x: true }, - ['list', 'of', 'things']); + it("should not generate the same hash for distinct input", () => { + let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]); + let h2 = Hash.sha1("test", { obj: "test", x: true }, + ["list", "of", "things"]); - expect(h1).to.be.a('string'); - expect(h2).to.be.a('string'); + expect(h1).to.be.a("string"); + expect(h2).to.be.a("string"); expect(h1).to.not.be.equal(h2); }); - it('should not generate the same hash for different order in deep lists', () => { - let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']); - let h2 = Hash.sha1('test', { obj: 'test' }, ['of', 'list', 'things']); + it("should not generate the same hash for different order in deep lists", () => { + let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]); + let h2 = Hash.sha1("test", { obj: "test" }, ["of", "list", "things"]); - expect(h1).to.be.a('string'); - expect(h2).to.be.a('string'); + expect(h1).to.be.a("string"); + expect(h2).to.be.a("string"); expect(h1).to.not.be.equal(h2); }); }); diff --git a/src/util/hash.ts b/src/util/hash.ts index f89379f0..39a33d9b 100644 --- a/src/util/hash.ts +++ b/src/util/hash.ts @@ -18,24 +18,22 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -'use strict'; - -import crypto = require('crypto'); +import crypto = require("crypto"); export class Hash { - static sha1(...objects: any[]): string { - let hash = crypto.createHash('sha1'); + public static sha1(...objects: any[]): string { + let hash = crypto.createHash("sha1"); objects .map((obj) => { switch (typeof obj) { - case 'string': + case "string": return obj; - case 'object': + case "object": return JSON.stringify(obj); default: throw new TypeError(typeof obj + - ' cannot be hashed'); + " cannot be hashed"); } }) .sort() @@ -43,6 +41,6 @@ export class Hash { hash.update(objStr); }); - return hash.digest('hex'); + return hash.digest("hex"); } } diff --git a/test/global.ts b/test/global.ts index 557810b6..0f37353c 100644 --- a/test/global.ts +++ b/test/global.ts @@ -18,4 +18,4 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -process.env.NODE_ENV = 'test'; +process.env.NODE_ENV = "test"; diff --git a/tslint.json b/tslint.json new file mode 100644 index 00000000..0309ae07 --- /dev/null +++ b/tslint.json @@ -0,0 +1,9 @@ +{ + "extends": "tslint:recommended", + "rules": { + "no-var-requires": false, + "object-literal-sort-keys": false, + "one-line": false, + "trailing-comma": false + } +} -- GitLab