Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Add Prettier with tslint plugin and precommit hook #94

Merged
merged 6 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,119 changes: 995 additions & 124 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
},
"scripts": {
"prepublish": "grunt peerDepInstall",
"precommit": "lint-staged",
"prettier": "prettier --write 'src/**/*.ts' 'tests/**/*.ts'",
"test": "grunt test"
},
"peerDependencies": {
Expand All @@ -46,8 +48,28 @@
"@types/sinon": "^1.16.31",
"grunt": "~1.0.1",
"grunt-dojo2": "latest",
"grunt-tslint": "5.0.1",
"husky": "0.14.3",
"intern": "~4.1.0",
"lint-staged": "6.0.0",
"prettier": "1.9.2",
"sinon": "^2.2.0",
"tslint": "5.8.0",
"tslint-plugin-prettier": "1.3.0",
"typescript": "~2.6.1"
},
"lint-staged": {
"*.{ts,tsx}": [
"prettier --write",
"git add"
]
},
"prettier": {
"singleQuote": true,
"tabWidth": 4,
"useTabs": true,
"parser": "typescript",
"printWidth": 120,
"arrowParens": "always"
}
}
45 changes: 22 additions & 23 deletions src/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ export function findDNodeByKey(target: RenderResults, key: string | object): HNo
if (findDNodeByKey(node, key)) {
console.warn(`Duplicate key of "${key}" found.`);
}
}
else {
} else {
found = findDNodeByKey(node, key);
}
});
return found;
}
else {
} else {
if (target && typeof target === 'object') {
if (target.properties && target.properties.key === key) {
return target;
Expand Down Expand Up @@ -88,9 +86,10 @@ function decorateTarget(target: DNode): void {
const { widgetConstructor, properties } = dNode;
dNode.widgetConstructor = StubWidget;
(properties as StubWidgetProperties)._stubTag = WIDGET_STUB_CUSTOM_ELEMENT;
(properties as StubWidgetProperties)._widgetName = typeof widgetConstructor === 'string'
? widgetConstructor
: (widgetConstructor as any).name || '<Anonymous>';
(properties as StubWidgetProperties)._widgetName =
typeof widgetConstructor === 'string'
? widgetConstructor
: (widgetConstructor as any).name || '<Anonymous>';
},
isWNode
);
Expand Down Expand Up @@ -123,8 +122,10 @@ interface SpyTarget {
* @param base The base class to add the render spy to
* @param target An object with a property named `lastRender` which will be set to the result of the `render()` method
*/
function SpyWidgetMixin<T extends Constructor<WidgetBase<WidgetProperties>>>(base: T, target: SpyTarget): T & Constructor<SpyWidgetMixin> {

function SpyWidgetMixin<T extends Constructor<WidgetBase<WidgetProperties>>>(
base: T,
target: SpyTarget
): T & Constructor<SpyWidgetMixin> {
class SpyRender extends base {
@afterRender()
spyRender(result: RenderResults): RenderResults {
Expand All @@ -150,7 +151,7 @@ interface MetaData {
* the harnessed widget.
*/
class WidgetHarness<W extends WidgetBase> extends WidgetBase {
private _id = ROOT_CUSTOM_ELEMENT_NAME + '-' + (++harnessId);
private _id = ROOT_CUSTOM_ELEMENT_NAME + '-' + ++harnessId;
private _metaData: WeakMap<Constructor<WidgetMetaBase>, MetaData>;
private _widgetConstructor: Constructor<W>;

Expand Down Expand Up @@ -213,11 +214,7 @@ class WidgetHarness<W extends WidgetBase> extends WidgetBase {
*/
public render(): RenderResults {
const { _id: id, _widgetConstructor, children, properties } = this;
return v(
ROOT_CUSTOM_ELEMENT_NAME,
{ id },
[ w(_widgetConstructor, properties, children) ]
);
return v(ROOT_CUSTOM_ELEMENT_NAME, { id }, [w(_widgetConstructor, properties, children)]);
}
}

Expand Down Expand Up @@ -248,7 +245,8 @@ export type MetaMockContext<T extends WidgetMetaBase = WidgetMetaBase> = T & {
invalidate(): void;
};

type ProjectorWidgetHarness<W extends WidgetBase<WidgetProperties>> = ProjectorMixin<W['properties']> & WidgetHarness<W>;
type ProjectorWidgetHarness<W extends WidgetBase<WidgetProperties>> = ProjectorMixin<W['properties']> &
WidgetHarness<W>;

const ProjectorWidgetHarness = ProjectorMixin(WidgetHarness);

Expand Down Expand Up @@ -283,7 +281,7 @@ export class Harness<W extends WidgetBase<WidgetProperties>> extends Evented {
constructor(widgetConstructor: Constructor<W>, root?: HTMLElement) {
super();

const widgetHarness = this._widgetHarness = new ProjectorWidgetHarness(widgetConstructor, this._metaMap);
const widgetHarness = (this._widgetHarness = new ProjectorWidgetHarness(widgetConstructor, this._metaMap));
// we want to control when the render gets scheduled, so we will hijack the projects one
this._scheduleRender = widgetHarness.scheduleRender.bind(widgetHarness);
widgetHarness.scheduleRender = () => {};
Expand Down Expand Up @@ -333,7 +331,7 @@ export class Harness<W extends WidgetBase<WidgetProperties>> extends Evented {
if (!this._projectorHandle) {
this._invalidate();
}
if (!(this._widgetHarness.lastRender) || !(this._widgetHarness.lastRender as any).domNode) {
if (!this._widgetHarness.lastRender || !(this._widgetHarness.lastRender as any).domNode) {
throw new Error('No root node has been rendered');
}
return (this._widgetHarness.lastRender as any).domNode as HTMLElement;
Expand All @@ -354,8 +352,7 @@ export class Harness<W extends WidgetBase<WidgetProperties>> extends Evented {
// TODO: no need to coerce in 2.5.2
mocks: mocks as any
});
}
else {
} else {
// TODO: no need to coerce in 2.5.2
_metaMap.get(provider)!.mocks = mocks as any;
}
Expand Down Expand Up @@ -386,8 +383,7 @@ export class Harness<W extends WidgetBase<WidgetProperties>> extends Evented {
const dnode = findDNodeByKey(this._widgetHarness.lastRender, key);
if (isHNode(dnode)) {
target = (dnode as any).domNode as Element;
}
else {
} else {
throw new Error(`Could not find key of "${key}" to sendEvent`);
}
}
Expand Down Expand Up @@ -419,6 +415,9 @@ export class Harness<W extends WidgetBase<WidgetProperties>> extends Evented {
* @param widgetConstructor The constructor function/class of widget that should be harnessed.
* @param root The root where the harness should append itself to the DOM. Defaults to `document.body`
*/
export default function harness<W extends WidgetBase<WidgetProperties>>(widgetConstructor: Constructor<W>, root?: HTMLElement): Harness<W> {
export default function harness<W extends WidgetBase<WidgetProperties>>(
widgetConstructor: Constructor<W>,
root?: HTMLElement
): Harness<W> {
return new Harness(widgetConstructor, root);
}
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import harness from './harness';
import assertRender from './support/assertRender';
import callListener from './support/callListener';
import sendEvent from './support/sendEvent';
import { assignChildProperties, assignProperties, findIndex, findKey, replaceChild, replaceChildProperties, replaceProperties } from './support/d';
import {
assignChildProperties,
assignProperties,
findIndex,
findKey,
replaceChild,
replaceChildProperties,
replaceProperties
} from './support/d';

export {
assertRender,
Expand Down
3 changes: 1 addition & 2 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export function createResolvers() {
rAFStub = stub(global, 'requestAnimationFrame').returns(1);
if (global.requestIdleCallback) {
rICStub = stub(global, 'requestIdleCallback').returns(1);
}
else {
} else {
rICStub = stub(global, 'setTimeout').returns(1);
}
},
Expand Down
15 changes: 6 additions & 9 deletions src/support/AssertionError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
* @param excluds excluded properties
*/
function exclude(...excludes: string[]) {

function excludeProps(res: any, obj: any) {
Object.keys(obj).forEach(function (key) {
Object.keys(obj).forEach(function(key) {
if (!~excludes.indexOf(key)) {
res[key] = obj[key];
}
Expand All @@ -53,7 +52,7 @@ function exclude(...excludes: string[]) {

return res;
};
};
}

export interface AssertionError extends Error {
[prop: string]: any;
Expand Down Expand Up @@ -91,12 +90,10 @@ function AssertionError(this: AssertionError, message?: string, _props?: any, ss
// capture stack trace
if (ssf && Error.captureStackTrace) {
Error.captureStackTrace(this, ssf);
}
else {
} else {
try {
throw new Error();
}
catch (e) {
} catch (e) {
this.stack = e.stack;
}
}
Expand Down Expand Up @@ -125,7 +122,7 @@ AssertionError.prototype.constructor = AssertionError;
*
* @param stack include stack (default: `true`)
*/
AssertionError.prototype.toJSON = function (this: AssertionError, stack?: boolean) {
AssertionError.prototype.toJSON = function(this: AssertionError, stack?: boolean) {
const extend = exclude('constructor', 'toJSON', 'stack');
const props: { [prop: string]: any } = extend({ name: this.name }, this);

Expand All @@ -138,6 +135,6 @@ AssertionError.prototype.toJSON = function (this: AssertionError, stack?: boolea
};

/* tslint:disable:variable-name */
const AssertionErrorConstructor: AssertionErrorConstructor = <any> AssertionError;
const AssertionErrorConstructor: AssertionErrorConstructor = <any>AssertionError;

export default AssertionErrorConstructor;
Loading