Skip to content

Commit 6412c34

Browse files
authored
Merge pull request #225 from Auge19/strictBindCallApply
TS) Enable "strictBindCallApply"
2 parents 4bbc6d3 + 674114e commit 6412c34

File tree

22 files changed

+143
-100
lines changed

22 files changed

+143
-100
lines changed

packages/bind/spec/nodePreprocessingBehaviors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ describe('Node preprocessing', function () {
3636
options.bindingProviderInstance.preprocessNode = function (node) {
3737
// Example: replace <mySpecialNode /> with <span data-bind='text: someValue'></span>
3838
// This technique could be the basis for implementing custom element types that render templates
39-
if (node.tagName && node.tagName.toLowerCase() === 'myspecialnode') {
39+
if (node instanceof Element && node.tagName && node.tagName.toLowerCase() === 'myspecialnode') {
4040
const newNode = document.createElement('span')
4141
newNode.setAttribute('data-bind', 'text: someValue')
4242
expect(node.parentNode).not.toBe(null)
4343
node.parentNode?.insertBefore(newNode, node)
4444
node.parentNode?.removeChild(node)
4545
return [newNode]
4646
}
47-
return undefined
47+
return null
4848
}
4949
testNode.innerHTML = '<span>a</span><mySpecialNode></mySpecialNode><span>b</span>'
5050
const someValue = observable('hello')
@@ -77,7 +77,7 @@ describe('Node preprocessing', function () {
7777
node.parentNode.removeChild(node)
7878
return newNodes
7979
}
80-
return undefined
80+
return null
8181
}
8282
}
8383
options.bindingProviderInstance = new TestProvider()
@@ -104,7 +104,7 @@ describe('Node preprocessing', function () {
104104
node.parentNode.removeChild(node)
105105
return newNodes
106106
}
107-
return undefined
107+
return null
108108
}
109109
}
110110
const testProvider = new TestProvider()

packages/bind/src/applyBindings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function applyBindingsToDescendantsInternal(
102102
return
103103
}
104104

105-
let currentChild: ChildNode | null
105+
let currentChild: Node | null
106106
const provider = getBindingProvider()
107107
const preprocessNode = provider.preprocessNode
108108

packages/bind/src/arrayToDomNodeChildren.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export function setDomNodeChildrenFromArrayMapping<T = any>(
205205
itemsForBeforeRemoveCallbacks.push(mapData!)
206206
}
207207
}
208-
if (mapData) {
208+
if (mapData?.mappedNodes) {
209209
nodesToDelete.push.apply(nodesToDelete, mapData.mappedNodes)
210210
}
211211
}

packages/binding.template/spec/foreachBehaviors.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ describe('Binding: Foreach', function () {
321321
node.parentNode?.removeChild(node)
322322
return newNodes
323323
}
324-
return undefined
324+
return null
325325
}
326326

327327
// Now perform a foreach binding, and see that afterRender gets the output from the preprocessor and bindings
@@ -944,6 +944,10 @@ describe('Binding: Foreach', function () {
944944
it('Can modify the set of top-level nodes in a foreach loop', function () {
945945
options.bindingProviderInstance.preprocessNode = function (node) {
946946
// Replace <data /> with <span data-bind="text: $data"></span>
947+
if (!(node instanceof Element)) {
948+
return null
949+
}
950+
947951
if (node.tagName && node.tagName.toLowerCase() === 'data') {
948952
const newNode = document.createElement('span')
949953
newNode.setAttribute('data-bind', 'text: $data')
@@ -957,7 +961,7 @@ describe('Binding: Foreach', function () {
957961
node.parentNode?.removeChild(node)
958962
return []
959963
}
960-
return undefined
964+
return null
961965
}
962966
testNode.innerHTML =
963967
"<div data-bind='foreach: items'>"

packages/binding.template/spec/nativeTemplateEngineBehaviors.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global testNode */
22
import { applyBindings } from '@tko/bind'
33

4-
import { observable, observableArray } from '@tko/observable'
4+
import { observable, ObservableArray, observableArray } from '@tko/observable'
55

66
import { DataBindProvider } from '@tko/provider.databind'
77

@@ -141,17 +141,25 @@ describe('Native template engine', function () {
141141
+ "(Val: <span data-bind='text: $data'></span>, Invocations: <span data-bind='text: $root.invocationCount()'></span>, Parents: <span data-bind='text: $parents.length'></span>)"
142142
+ '</div>'
143143
+ '</div>'
144-
const viewModel = {
145-
invocations: 0, // Verifying # invocations to be sure we're not rendering anything multiple times and discarding the results
146-
items: observableArray([
147-
{ children: observableArray(['A1', 'A2', 'A3']) },
148-
{ children: observableArray(['B1', 'B2']) }
149-
]),
150-
invocationCount: undefined
144+
145+
class myViewModel {
146+
invocations: number = 0 // Verifying # invocations to be sure we're not rendering anything multiple times and discarding the results
147+
items: ObservableArray<any>
148+
149+
constructor() {
150+
this.items = observableArray([
151+
{ children: observableArray(['A1', 'A2', 'A3']) },
152+
{ children: observableArray(['B1', 'B2']) }
153+
])
154+
}
155+
156+
invocationCount() {
157+
return ++this.invocations
158+
}
151159
}
152-
viewModel.invocationCount = function () {
153-
return ++this.invocations
154-
}.bind(viewModel)
160+
161+
const viewModel = new myViewModel()
162+
155163
applyBindings(viewModel, testNode)
156164

157165
expect(testNode.childNodes[0].childNodes[0]).toContainText(

packages/binding.template/spec/templatingBehaviors.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,15 @@ describe('Templating', function () {
539539
return [1, 3, 8]
540540
}
541541

542-
override nodeHasBindings(node: Element, bindingContext?: BindingContext) {
543-
return node.tagName == 'EM' || originalBindingProvider.nodeHasBindings(node, bindingContext)
542+
override nodeHasBindings(node: Node, bindingContext?: BindingContext) {
543+
if (node instanceof Element) {
544+
return node.tagName == 'EM' || originalBindingProvider.nodeHasBindings(node, bindingContext)
545+
}
546+
return false
544547
}
545-
override getBindingAccessors(node: Element, bindingContext?: BindingContext) {
548+
override getBindingAccessors(node: Node, bindingContext?: BindingContext) {
549+
if (!(node instanceof Element)) return {}
550+
546551
if (node.tagName == 'EM') {
547552
return { text: ++model.numExternalBindings }
548553
}

packages/computed/src/computed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export function computed(
141141
if (arguments.length > 0) {
142142
if (typeof writeFunction === 'function') {
143143
// Writing a value
144-
writeFunction.apply(state.evaluatorFunctionTarget, arguments)
144+
writeFunction.apply(state.evaluatorFunctionTarget, arguments as any)
145145
} else {
146146
throw new Error(
147147
"Cannot write a value to a computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters."

packages/observable/src/Subscription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class Subscription {
55
private _target: any
66
private _callback: any
77
private _isDisposed: boolean
8-
private _domNodeDisposalCallback: null
8+
private _domNodeDisposalCallback: ((node: Node) => void) | null
99
private _node: Node
1010

1111
constructor(target, observer, disposeCallback) {

packages/provider.attr/src/AttributeProvider.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,33 @@ export default class AttrProvider extends Provider {
1515
return 'ko-'
1616
}
1717

18-
getBindingAttributesList(node: Element) {
18+
getBindingAttributesList(node: Node) {
19+
if (!(node instanceof Element)) {
20+
return []
21+
}
22+
1923
if (!node.hasAttributes()) {
2024
return []
2125
}
2226
return Array.from(node.attributes).filter(attr => attr.name.startsWith(this.PREFIX))
2327
}
2428

25-
override nodeHasBindings(node: Element) {
29+
override nodeHasBindings(node: Node) {
2630
return this.getBindingAttributesList(node).length > 0
2731
}
2832

29-
override getBindingAccessors(node: Element, context) {
33+
override getBindingAccessors(node: Node, context) {
3034
return Object.assign({}, ...this.handlersFromAttributes(node, context))
3135
}
3236

33-
*handlersFromAttributes(node: Element, context) {
37+
*handlersFromAttributes(node: Node, context) {
3438
for (const attr of this.getBindingAttributesList(node)) {
35-
const name = attr.name.substr(this.PREFIX.length)
39+
const name = attr.name.substring(this.PREFIX.length)
3640
yield { [name]: () => this.getValue(attr.value, context, node) }
3741
}
3842
}
3943

40-
getValue(token, $context, node: Element) {
44+
getValue(token, $context, node: Node) {
4145
/* FIXME: This duplicates Identifier.prototype.lookup_value; it should
4246
be refactored into e.g. a BindingContext method */
4347
if (!token) {

packages/provider.bindingstring/src/BindingStringProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class BindingStringProvider extends Provider {
5353
return new Parser().parse(processed, context, this.globals, node)
5454
}
5555

56-
getBindingString(node: Node): string | null | undefined {
56+
getBindingString(node: Node): string | null {
5757
throw new Error('Overload getBindingString.')
5858
}
5959
}

0 commit comments

Comments
 (0)