Skip to content

Commit 6ebd642

Browse files
committed
chore(repeat-matcher): linting & comments
1 parent 60e06e8 commit 6ebd642

File tree

7 files changed

+75
-62
lines changed

7 files changed

+75
-62
lines changed

src/array-repeat-strategy.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {createFullOverrideContext, updateOverrideContexts, updateOverrideContext, indexOf} from './repeat-utilities';
22
import {mergeSplice} from 'aurelia-binding';
3+
import { Repeat } from './repeat';
34

45
/**
56
* A strategy for repeating a template over an array.
@@ -20,29 +21,30 @@ export class ArrayRepeatStrategy {
2021
* @param items The new array instance.
2122
*/
2223
instanceChanged(repeat, items) {
24+
const $repeat = repeat as Repeat;
2325
const itemsLength = items.length;
2426

2527
// if the new instance does not contain any items,
2628
// just remove all views and don't do any further processing
2729
if (!items || itemsLength === 0) {
28-
repeat.removeAllViews(true, !repeat.viewsRequireLifecycle);
30+
$repeat.removeAllViews(true, !$repeat.viewsRequireLifecycle);
2931
return;
3032
}
3133

32-
const children = repeat.views();
34+
const children = $repeat.views();
3335
const viewsLength = children.length;
3436

3537
// likewise, if we previously didn't have any views,
3638
// simply make them and return
3739
if (viewsLength === 0) {
38-
this._standardProcessInstanceChanged(repeat, items);
40+
this._standardProcessInstanceChanged($repeat, items);
3941
return;
4042
}
4143

42-
if (repeat.viewsRequireLifecycle) {
44+
if ($repeat.viewsRequireLifecycle) {
4345
const childrenSnapshot = children.slice(0);
44-
const itemNameInBindingContext = repeat.local;
45-
const matcher = repeat.matcher();
46+
const itemNameInBindingContext = $repeat.local;
47+
const matcher = $repeat.matcher();
4648

4749
// the cache of the current state (it will be transformed along with the views to keep track of indicies)
4850
let itemsPreviouslyInViews = [];
@@ -65,7 +67,7 @@ export class ArrayRepeatStrategy {
6567
let removePromise;
6668

6769
if (itemsPreviouslyInViews.length > 0) {
68-
removePromise = repeat.removeViews(viewsToRemove, true, !repeat.viewsRequireLifecycle);
70+
removePromise = $repeat.removeViews(viewsToRemove, true, !$repeat.viewsRequireLifecycle);
6971
updateViews = () => {
7072
// update views (create new and move existing)
7173
for (let index = 0; index < itemsLength; index++) {
@@ -74,16 +76,16 @@ export class ArrayRepeatStrategy {
7476
let view;
7577

7678
if (indexOfView === -1) { // create views for new items
77-
const overrideContext = createFullOverrideContext(repeat, items[index], index, itemsLength);
78-
repeat.insertView(index, overrideContext.bindingContext, overrideContext);
79+
const overrideContext = createFullOverrideContext($repeat, items[index], index, itemsLength);
80+
$repeat.insertView(index, overrideContext.bindingContext, overrideContext);
7981
// reflect the change in our cache list so indicies are valid
8082
itemsPreviouslyInViews.splice(index, 0, undefined);
8183
} else if (indexOfView === index) { // leave unchanged items
8284
view = children[indexOfView];
8385
itemsPreviouslyInViews[indexOfView] = undefined;
8486
} else { // move the element to the right place
8587
view = children[indexOfView];
86-
repeat.moveView(indexOfView, index);
88+
$repeat.moveView(indexOfView, index);
8789
itemsPreviouslyInViews.splice(indexOfView, 1);
8890
itemsPreviouslyInViews.splice(index, 0, undefined);
8991
}
@@ -95,12 +97,12 @@ export class ArrayRepeatStrategy {
9597

9698
// remove extraneous elements in case of duplicates,
9799
// also update binding contexts if objects changed using the matcher function
98-
this._inPlaceProcessItems(repeat, items);
100+
this._inPlaceProcessItems($repeat, items);
99101
};
100102
} else {
101103
// if all of the items are different, remove all and add all from scratch
102-
removePromise = repeat.removeAllViews(true, !repeat.viewsRequireLifecycle);
103-
updateViews = () => this._standardProcessInstanceChanged(repeat, items);
104+
removePromise = $repeat.removeAllViews(true, !$repeat.viewsRequireLifecycle);
105+
updateViews = () => this._standardProcessInstanceChanged($repeat, items);
104106
}
105107

106108
if (removePromise instanceof Promise) {
@@ -110,7 +112,7 @@ export class ArrayRepeatStrategy {
110112
}
111113
} else {
112114
// no lifecycle needed, use the fast in-place processing
113-
this._inPlaceProcessItems(repeat, items);
115+
this._inPlaceProcessItems($repeat, items);
114116
}
115117
}
116118

test/map-repeat-strategy.spec.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,57 +45,64 @@ describe('MapRepeatStrategy', () => {
4545
view3.bindingContext = { item: ['john', 'doe'] };
4646
view3.overrideContext = {};
4747
viewSlot.children = [view1, view2, view3];
48-
viewFactorySpy = spyOn(viewFactory, 'create').and.callFake(() => {});
48+
viewFactorySpy = spyOn(viewFactory, 'create').and.callFake(() => {/**/});
4949
});
50-
50+
5151
it('should correctly handle adding item (i.e Map.prototype.set())', () => {
52-
repeat = new Repeat(new ViewFactoryMock(), instructionMock, viewSlot, viewResourcesMock, new ObserverLocator());
52+
repeat = new Repeat(
53+
new ViewFactoryMock(),
54+
instructionMock,
55+
viewSlot,
56+
viewResourcesMock,
57+
new ObserverLocator(),
58+
null
59+
);
5360
let bindingContext = {};
5461
repeat.scope = { bindingContext, overrideContext: createOverrideContext(bindingContext) };
5562
records = [
56-
{"type": "add", "object": {}, "key": 'norf'}
57-
]
63+
{'type': 'add', 'object': {}, 'key': 'norf'}
64+
];
5865
items = new Map([['foo', 'bar'], ['qux', 'qax'], ['john', 'doe'], ['norf', 'narf']]);
59-
spyOn(viewSlot, 'removeAt').and.callFake(() => { return new ViewMock();});
66+
spyOn(viewSlot, 'removeAt').and.callFake(() => { return new ViewMock(); });
6067
strategy.instanceMutated(repeat, items, records);
61-
68+
6269
expect(viewSlot.children.length).toBe(4);
6370
expect(viewSlot.children[3].bindingContext.key).toBe('norf');
6471
expect(viewSlot.children[3].overrideContext.$index).toBe(3);
6572
expect(viewSlot.children[3].overrideContext.$first).toBe(false);
6673
expect(viewSlot.children[3].overrideContext.$last).toBe(true);
6774
});
68-
75+
6976
it('should correctly handle clear items (i.e Map.prototype.clear())', () => {
7077
let view4 = new ViewMock();
7178
view4.bindingContext = { item: ['norf', 'narf'] };
7279
view4.overrideContext = {};
7380
let viewSlotMock = new ViewSlotMock();
7481
viewSlotMock.children = [view1, view2, view3, view4];
75-
repeat = new Repeat(new ViewFactoryMock(), instructionMock, viewSlotMock, viewResourcesMock, new ObserverLocator());
82+
repeat = new Repeat(new ViewFactoryMock(), instructionMock, viewSlotMock, viewResourcesMock, new ObserverLocator(), null);
7683
let bindingContext = {};
7784
repeat.scope = { bindingContext, overrideContext: createOverrideContext(bindingContext) };
7885
records = [
79-
{"type": "clear", "object": {}}
80-
]
86+
{'type': 'clear', 'object': {}}
87+
];
8188
items = new Map();
8289
strategy.instanceMutated(repeat, items, records);
83-
90+
8491
expect(viewSlotMock.children.length).toBe(0);
8592
});
86-
93+
8794
it('should correctly handle adding items after clear (issue 287)', () => {
8895
viewSlot.children = [view1, view2, view3];
89-
repeat = new Repeat(new ViewFactoryMock(), instructionMock, viewSlot, viewResourcesMock, new ObserverLocator());
96+
repeat = new Repeat(new ViewFactoryMock(), instructionMock, viewSlot, viewResourcesMock, new ObserverLocator(), null);
9097
let bindingContext = {};
9198
repeat.scope = { bindingContext, overrideContext: createOverrideContext(bindingContext) };
9299
records = [
93-
{"type": "clear", "object": {}},
94-
{"type": "add", "object": {}, "key": 'foo'},
95-
{"type": "add", "object": {}, "key": 'qux'},
96-
{"type": "add", "object": {}, "key": 'john'},
97-
{"type": "add", "object": {}, "key": 'norf'}
98-
]
100+
{'type': 'clear', 'object': {}},
101+
{'type': 'add', 'object': {}, 'key': 'foo'},
102+
{'type': 'add', 'object': {}, 'key': 'qux'},
103+
{'type': 'add', 'object': {}, 'key': 'john'},
104+
{'type': 'add', 'object': {}, 'key': 'norf'}
105+
];
99106
items = new Map([['foo', 'bar'], ['qux', 'qax'], ['john', 'doe'], ['norf', 'narf']]);
100107
strategy.instanceMutated(repeat, items, records);
101108

test/mocks.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
// tslint:disable:no-empty
12
import { Repeat } from '../src/repeat';
23

34
export class ViewSlotMock {
45
children: any[];
56
constructor() {
67
this.children = [];
78
}
8-
removeAll(){
9+
removeAll() {
910
this.children.splice(0, this.children.length);
1011
}
11-
add(view){
12+
add(view) {
1213
this.children.push(view);
1314
}
14-
insert(index, view){
15-
if(index < 0) {
16-
throw "negative index";
15+
insert(index, view) {
16+
if (index < 0) {
17+
throw 'negative index';
1718
}
1819
this.children.splice(index, 0, view);
1920
}
20-
removeAt(index){
21-
if(index < 0) {
22-
throw "negative index";
21+
removeAt(index) {
22+
if (index < 0) {
23+
throw 'negative index';
2324
}
2425
this.children.splice(index, 1);
2526
}
@@ -32,30 +33,30 @@ export class ViewMock {
3233
this.bindingContext = bindingContext;
3334
this.overrideContext = overrideContext;
3435
}
35-
attached(){}
36-
detached(){}
37-
unbind(){}
38-
returnToCache(){}
36+
attached() {}
37+
detached() {}
38+
unbind() {}
39+
returnToCache() {}
3940
}
4041

4142
export class BoundViewFactoryMock {
4243
_viewsRequireLifecycle = true;
43-
create(){
44-
return { bind(){} };
45-
};
46-
removeAll(){};
44+
create() {
45+
return { bind() {} };
46+
}
47+
removeAll() {}
4748
}
4849

4950
export class ViewFactoryMock {
5051
_viewsRequireLifecycle = true;
51-
create(){
52+
create() {
5253
return new ViewMock();
5354
}
5455
}
5556

5657
export class ArrayObserverMock {
57-
subscribe(){};
58-
unsubscribe(){};
58+
subscribe() {}
59+
unsubscribe() {}
5960
}
6061

6162
export class RepeatStrategyMock {

test/number-repeat-strategy.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {Container} from 'aurelia-dependency-injection';
66
import {Repeat} from '../src/repeat';
77
import {RepeatStrategyLocator} from '../src/repeat-strategy-locator';
88
import {NumberRepeatStrategy} from '../src/number-repeat-strategy';
9-
import {ViewSlotMock, BoundViewFactoryMock, RepeatStrategyMock, ViewMock, ArrayObserverMock, ViewFactoryMock, instructionMock, viewResourcesMock} from './mocks';
9+
import {ViewSlotMock, BoundViewFactoryMock, RepeatStrategyMock, ViewFactoryMock, instructionMock, viewResourcesMock} from './mocks';
1010
import {bootstrap} from 'aurelia-bootstrapper';
1111

1212
describe('NumberRepeatStrategy', () => {
@@ -27,7 +27,7 @@ describe('NumberRepeatStrategy', () => {
2727
container.registerInstance(ObserverLocator, observerLocator);
2828
container.registerInstance(RepeatStrategyLocator, repeatStrategyLocator);
2929

30-
component = StageComponent.withResources().inView('<div repeat.for="item of items"></div>').boundTo({ items: [] })
30+
component = StageComponent.withResources().inView('<div repeat.for="item of items"></div>').boundTo({ items: [] });
3131

3232
component.create(bootstrap).then(() => {
3333
repeat = component.viewModel;
@@ -41,7 +41,7 @@ describe('NumberRepeatStrategy', () => {
4141

4242
describe('instanceChanged', () => {
4343
beforeEach(() => {
44-
repeat = new Repeat(new ViewFactoryMock(), instructionMock, viewSlot, viewResourcesMock, new ObserverLocator());
44+
repeat = new Repeat(new ViewFactoryMock(), instructionMock, viewSlot, viewResourcesMock, new ObserverLocator(), null);
4545
let bindingContext = {};
4646
repeat.scope = { bindingContext, overrideContext: createOverrideContext(bindingContext) };
4747
viewSlot.children = [];

test/repeat.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('repeat', () => {
4343
view1 = new ViewMock();
4444
view2 = new ViewMock();
4545
viewSlot.children = [view1, view2];
46-
spyOn(repeatStrategyLocator, 'getStrategy').and.callFake(() => { return repeatStrategyMock});
46+
spyOn(repeatStrategyLocator, 'getStrategy').and.callFake(() => { return repeatStrategyMock; });
4747
});
4848

4949
it('should do nothing if no items provided', () => {
@@ -69,7 +69,7 @@ describe('repeat', () => {
6969
spyOn(viewSlot, 'removeAll');
7070
repeat.unbind();
7171

72-
expect(viewSlot.removeAll).toHaveBeenCalled(); //With(true, true);
72+
expect(viewSlot.removeAll).toHaveBeenCalled(); // With(true, true);
7373
});
7474

7575
it('should unsubscribe collection', () => {
@@ -101,7 +101,7 @@ describe('repeat', () => {
101101

102102
describe('itemsChanged', () => {
103103
beforeEach(() => {
104-
spyOn(repeatStrategyLocator, 'getStrategy').and.callFake(() => { return repeatStrategyMock});
104+
spyOn(repeatStrategyLocator, 'getStrategy').and.callFake(() => { return repeatStrategyMock; });
105105
});
106106

107107
it('should unsubscribe collection', () => {
@@ -124,7 +124,7 @@ describe('repeat', () => {
124124
it('should subscribe to changes', () => {
125125
repeat.items = ['foo', 'bar'];
126126
let collectionObserver = new ArrayObserverMock();
127-
spyOn(repeatStrategyMock, 'getCollectionObserver').and.callFake(() => { return collectionObserver });
127+
spyOn(repeatStrategyMock, 'getCollectionObserver').and.callFake(() => { return collectionObserver; });
128128
spyOn(collectionObserver, 'subscribe');
129129
repeat.itemsChanged();
130130

test/sanitize-html.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {SanitizeHTMLValueConverter} from '../src/sanitize-html';
33
import {HTMLSanitizer} from '../src/html-sanitizer';
44

55
describe('SanitizeHtmlValueConverter', () => {
6-
var converter;
6+
let converter;
77

88
beforeEach(() => {
99
converter = new SanitizeHTMLValueConverter(new HTMLSanitizer());
1010
});
1111

1212
it('defaultSanitizer should remove script tags', () => {
13-
var a = '<script src="http://www.evil.org"></script>',
13+
let a = '<script src="http://www.evil.org"></script>',
1414
b = '<div><script src="http://www.evil.org"></script></div>',
1515
c = 'foo <script src="http://www.evil.org"></script> bar',
1616
d = '<div></div>',

test/test-interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import 'aurelia-templating';
22

3+
/**
4+
* @internal
5+
*/
36
declare module 'aurelia-templating' {
47
interface HtmlBehaviorResource {
58
elementName: string | null;

0 commit comments

Comments
 (0)