Merge pull request #62 from GoogleChromeLabs/linting

Switch to tslint and run it as commit hook
This commit is contained in:
Surma
2018-06-26 15:16:42 +01:00
committed by GitHub
9 changed files with 3366 additions and 5265 deletions

8508
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,28 +8,14 @@
"build:codecs": "npm run build:mozjpeg_enc", "build:codecs": "npm run build:mozjpeg_enc",
"start": "webpack serve --host 0.0.0.0 --hot", "start": "webpack serve --host 0.0.0.0 --hot",
"build": "webpack -p", "build": "webpack -p",
"lint": "eslint src" "lint": "tslint -c tslint.json -t verbose 'src/**/*.{ts,js}'",
"lintfix": "tslint -c tslint.json -t verbose --fix 'src/**/*.{ts,js}'"
}, },
"eslintConfig": { "husky": {
"extends": [ "hooks": {
"standard", "pre-commit": "npm run lint"
"standard-jsx"
],
"rules": {
"indent": [
2,
2
],
"semi": [
2,
"always"
],
"prefer-const": 1
} }
}, },
"eslintIgnore": [
"build/*"
],
"devDependencies": { "devDependencies": {
"@types/node": "^9.4.7", "@types/node": "^9.4.7",
"@types/webassembly-js-api": "0.0.1", "@types/webassembly-js-api": "0.0.1",
@@ -47,17 +33,10 @@
"clean-webpack-plugin": "^0.1.19", "clean-webpack-plugin": "^0.1.19",
"copy-webpack-plugin": "^4.5.1", "copy-webpack-plugin": "^4.5.1",
"css-loader": "^0.28.11", "css-loader": "^0.28.11",
"eslint": "^4.18.2",
"eslint-config-standard": "^11.0.0",
"eslint-config-standard-jsx": "^5.0.0",
"eslint-plugin-import": "^2.10.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-react": "^7.7.0",
"eslint-plugin-standard": "^3.0.1",
"exports-loader": "^0.7.0", "exports-loader": "^0.7.0",
"file-loader": "^1.1.11", "file-loader": "^1.1.11",
"html-webpack-plugin": "^3.0.6", "html-webpack-plugin": "^3.0.6",
"husky": "^1.0.0-rc.9",
"if-env": "^1.0.4", "if-env": "^1.0.4",
"loader-utils": "^1.1.0", "loader-utils": "^1.1.0",
"mini-css-extract-plugin": "^0.3.0", "mini-css-extract-plugin": "^0.3.0",
@@ -70,7 +49,8 @@
"source-map-loader": "^0.2.3", "source-map-loader": "^0.2.3",
"style-loader": "^0.20.3", "style-loader": "^0.20.3",
"ts-loader": "^4.0.1", "ts-loader": "^4.0.1",
"tslint": "^5.9.1", "tslint": "^5.10.0",
"tslint-config-airbnb": "^5.9.2",
"tslint-config-semistandard": "^7.0.0", "tslint-config-semistandard": "^7.0.0",
"tslint-react": "^3.5.1", "tslint-react": "^3.5.1",
"typescript": "^2.7.2", "typescript": "^2.7.2",

View File

@@ -24,17 +24,17 @@ interface SetTransformOpts {
allowChangeEvent?: boolean; allowChangeEvent?: boolean;
} }
function getDistance (a: Point, b?: Point): number { function getDistance(a: Point, b?: Point): number {
if (!b) return 0; if (!b) return 0;
return Math.sqrt((b.clientX - a.clientX) ** 2 + (b.clientY - a.clientY) ** 2); return Math.sqrt((b.clientX - a.clientX) ** 2 + (b.clientY - a.clientY) ** 2);
} }
function getMidpoint (a: Point, b?: Point): Point { function getMidpoint(a: Point, b?: Point): Point {
if (!b) return a; if (!b) return a;
return { return {
clientX: (a.clientX + b.clientX) / 2, clientX: (a.clientX + b.clientX) / 2,
clientY: (a.clientY + b.clientY) / 2 clientY: (a.clientY + b.clientY) / 2,
}; };
} }
@@ -42,15 +42,15 @@ function getMidpoint (a: Point, b?: Point): Point {
// Given that, better to use something everything supports. // Given that, better to use something everything supports.
let cachedSvg: SVGSVGElement; let cachedSvg: SVGSVGElement;
function getSVG (): SVGSVGElement { function getSVG(): SVGSVGElement {
return cachedSvg || (cachedSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')); return cachedSvg || (cachedSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'));
} }
function createMatrix (): SVGMatrix { function createMatrix(): SVGMatrix {
return getSVG().createSVGMatrix(); return getSVG().createSVGMatrix();
} }
function createPoint (): SVGPoint { function createPoint(): SVGPoint {
return getSVG().createSVGPoint(); return getSVG().createSVGPoint();
} }
@@ -79,9 +79,9 @@ export default class PinchZoom extends HTMLElement {
event.preventDefault(); event.preventDefault();
return true; return true;
}, },
move: previousPointers => { move: (previousPointers) => {
this._onPointerMove(previousPointers, pointerTracker.currentPointers); this._onPointerMove(previousPointers, pointerTracker.currentPointers);
} },
}); });
this.addEventListener('wheel', event => this._onWheel(event)); this.addEventListener('wheel', event => this._onWheel(event));
@@ -109,12 +109,12 @@ export default class PinchZoom extends HTMLElement {
setTransform (opts: SetTransformOpts = {}) { setTransform (opts: SetTransformOpts = {}) {
const { const {
scale = this.scale, scale = this.scale,
allowChangeEvent = false allowChangeEvent = false,
} = opts; } = opts;
let { let {
x = this.x, x = this.x,
y = this.y y = this.y,
} = opts; } = opts;
// If we don't have an element to position, just set the value as given. // If we don't have an element to position, just set the value as given.
@@ -144,7 +144,7 @@ export default class PinchZoom extends HTMLElement {
bottomRight.y = positioningElBounds.height + topLeft.y; bottomRight.y = positioningElBounds.height + topLeft.y;
// Calculate the intended position of _positioningEl. // Calculate the intended position of _positioningEl.
let matrix = createMatrix() const matrix = createMatrix()
.translate(x, y) .translate(x, y)
.scale(scale) .scale(scale)
// Undo current transform // Undo current transform
@@ -239,7 +239,7 @@ export default class PinchZoom extends HTMLElement {
this._applyChange({ this._applyChange({
scaleDiff, scaleDiff,
originX: event.clientX - thisRect.left, originX: event.clientX - thisRect.left,
originY: event.clientY - thisRect.top originY: event.clientY - thisRect.top,
}); });
} }
@@ -263,7 +263,7 @@ export default class PinchZoom extends HTMLElement {
this._applyChange({ this._applyChange({
originX, originY, scaleDiff, originX, originY, scaleDiff,
panX: newMidpoint.clientX - prevMidpoint.clientX, panX: newMidpoint.clientX - prevMidpoint.clientX,
panY: newMidpoint.clientY - prevMidpoint.clientY panY: newMidpoint.clientY - prevMidpoint.clientY,
}); });
} }
@@ -272,7 +272,7 @@ export default class PinchZoom extends HTMLElement {
const { const {
panX = 0, panY = 0, panX = 0, panY = 0,
originX = 0, originY = 0, originX = 0, originY = 0,
scaleDiff = 1 scaleDiff = 1,
} = opts; } = opts;
const matrix = createMatrix() const matrix = createMatrix()
@@ -290,7 +290,7 @@ export default class PinchZoom extends HTMLElement {
scale: matrix.a, scale: matrix.a,
x: matrix.e, x: matrix.e,
y: matrix.f, y: matrix.f,
allowChangeEvent: true allowChangeEvent: true,
}); });
} }
} }

View File

@@ -46,9 +46,9 @@ export default class TwoUp extends HTMLElement {
move: () => { move: () => {
this._pointerChange( this._pointerChange(
pointerTracker.startPointers[0], pointerTracker.startPointers[0],
pointerTracker.currentPointers[0] pointerTracker.currentPointers[0],
); );
} },
}); });
} }

View File

@@ -11,6 +11,6 @@ interface Window {
declare namespace JSX { declare namespace JSX {
interface IntrinsicElements { interface IntrinsicElements {
"two-up": HTMLAttributes 'two-up': HTMLAttributes;
} }
} }

View File

@@ -32,9 +32,10 @@ const isPointerEvent = (event: any): event is PointerEvent =>
const noop = () => {}; const noop = () => {};
type StartCallback = ((pointer: Pointer, event: TouchEvent | PointerEvent | MouseEvent) => boolean); export type InputEvent = TouchEvent | PointerEvent | MouseEvent;
type MoveCallback = ((previousPointers: Pointer[], event: TouchEvent | PointerEvent | MouseEvent) => void); type StartCallback = ((pointer: Pointer, event: InputEvent) => boolean);
type EndCallback = ((pointer: Pointer, event: TouchEvent | PointerEvent | MouseEvent) => void); type MoveCallback = ((previousPointers: Pointer[], event: InputEvent) => void);
type EndCallback = ((pointer: Pointer, event: InputEvent) => void);
interface PointerTrackerCallbacks { interface PointerTrackerCallbacks {
/** /**
@@ -95,7 +96,7 @@ export class PointerTracker {
const { const {
start = () => true, start = () => true,
move = noop, move = noop,
end = noop end = noop,
} = callbacks; } = callbacks;
this._startCallback = start; this._startCallback = start;
@@ -120,7 +121,7 @@ export class PointerTracker {
* @param event Related event * @param event Related event
* @returns Whether the pointer is being tracked. * @returns Whether the pointer is being tracked.
*/ */
private _triggerPointerStart (pointer: Pointer, event: PointerEvent | MouseEvent | TouchEvent): boolean { private _triggerPointerStart (pointer: Pointer, event: InputEvent): boolean {
if (!this._startCallback(pointer, event)) return false; if (!this._startCallback(pointer, event)) return false;
this.currentPointers.push(pointer); this.currentPointers.push(pointer);
this.startPointers.push(pointer); this.startPointers.push(pointer);
@@ -193,7 +194,7 @@ export class PointerTracker {
* @param event Related event * @param event Related event
*/ */
@bind @bind
private _triggerPointerEnd (pointer: Pointer, event: PointerEvent | MouseEvent | TouchEvent): boolean { private _triggerPointerEnd (pointer: Pointer, event: InputEvent): boolean {
const index = this.currentPointers.findIndex(p => p.id === pointer.id); const index = this.currentPointers.findIndex(p => p.id === pointer.id);
// Not a pointer we're interested in? // Not a pointer we're interested in?
if (index === -1) return false; if (index === -1) return false;

View File

@@ -1,4 +1,4 @@
import {Encoder} from './codec'; import { Encoder } from './codec';
import mozjpeg_enc from '../../../codecs/mozjpeg_enc/mozjpeg_enc'; import mozjpeg_enc from '../../../codecs/mozjpeg_enc/mozjpeg_enc';
// Using require() so TypeScript doesnt complain about this not being a module. // Using require() so TypeScript doesnt complain about this not being a module.
@@ -19,13 +19,13 @@ export class MozJpegEncoder implements Encoder {
private emscriptenModule: Promise<EmscriptenWasm.Module>; private emscriptenModule: Promise<EmscriptenWasm.Module>;
private api: Promise<ModuleAPI>; private api: Promise<ModuleAPI>;
constructor() { constructor() {
this.emscriptenModule = new Promise(resolve => { this.emscriptenModule = new Promise((resolve) => {
const m = mozjpeg_enc({ const m = mozjpeg_enc({
// Just to be safe, dont automatically invoke any wasm functions // Just to be safe, dont automatically invoke any wasm functions
noInitialRun: false, noInitialRun: false,
locateFile(url: string): string { locateFile(url: string): string {
// Redirect the request for the wasm binary to whatever webpack gave us. // Redirect the request for the wasm binary to whatever webpack gave us.
if(url.endsWith('.wasm')) { if (url.endsWith('.wasm')) {
return wasmBinaryUrl; return wasmBinaryUrl;
} }
return url; return url;
@@ -38,12 +38,14 @@ export class MozJpegEncoder implements Encoder {
// TODO(surma@): File a bug with Emscripten on this. // TODO(surma@): File a bug with Emscripten on this.
delete (m as any).then; delete (m as any).then;
resolve(m); resolve(m);
} },
}); });
}); });
this.api = (async () => { this.api = (async () => {
// Not sure why, but TypeScript complains that I am using `emscriptenModule` before its getting assigned, which is clearly not true :shrug: Using `any` // Not sure why, but TypeScript complains that I am using
// `emscriptenModule` before its getting assigned, which is clearly not
// true :shrug: Using `any`
const m = await (this as any).emscriptenModule; const m = await (this as any).emscriptenModule;
return { return {
version: m.cwrap('version', 'number', []), version: m.cwrap('version', 'number', []),

View File

@@ -16,12 +16,12 @@ export function bind(target: any, propertyKey: string, descriptor: PropertyDescr
// define an instance property pointing to the bound function. // define an instance property pointing to the bound function.
// This effectively "caches" the bound prototype method as an instance property. // This effectively "caches" the bound prototype method as an instance property.
get() { get() {
let bound = descriptor.value.bind(this); const bound = descriptor.value.bind(this);
Object.defineProperty(this, propertyKey, { Object.defineProperty(this, propertyKey, {
value: bound value: bound,
}); });
return bound; return bound;
} },
}; };
} }
@@ -37,9 +37,8 @@ export async function bitmapToImageData(bitmap: ImageBitmap): Promise<ImageData>
// Draw image onto canvas // Draw image onto canvas
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
if (!ctx) { if (!ctx) {
throw new Error("Could not create canvas context"); throw new Error('Could not create canvas context');
} }
ctx.drawImage(bitmap, 0, 0); ctx.drawImage(bitmap, 0, 0);
return ctx.getImageData(0, 0, bitmap.width, bitmap.height); return ctx.getImageData(0, 0, bitmap.width, bitmap.height);
} }

View File

@@ -1,16 +1,19 @@
{ {
"extends": [ "extends": [
"tslint-config-semistandard", "tslint-config-airbnb",
"tslint-react" "tslint-react"
], ],
"rules": { "rules": {
"quotemark": [true, "single", "jsx-double", "avoid-escape"],
"no-use-before-declare": false,
"no-floating-promises": false,
"space-before-function-paren": [true, false],
"jsx-boolean-value": [true, "never"], "jsx-boolean-value": [true, "never"],
"jsx-no-multiline-js": false, "jsx-no-multiline-js": false,
"jsx-no-bind": true, "jsx-no-bind": true,
"jsx-no-lambda": true "jsx-no-lambda": true,
"function-name": false,
"variable-name": [true, "check-format", "allow-leading-underscore"]
},
"linterOptions": {
"exclude": [
"build"
]
} }
} }