Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Identifier.set_value work with inherited properties #157

Merged
merged 2 commits into from
Jan 9, 2022
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
4 changes: 2 additions & 2 deletions packages/bind/src/bindingContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend, options, domData } from '@tko/utils'
import { extend, options, domData, isObjectLike } from '@tko/utils'

import {
pureComputed
Expand Down Expand Up @@ -118,7 +118,7 @@ Object.assign(bindingContext.prototype, {
}
const $data = this.$data
// instanceof Object covers 1. {}, 2. [], 3. function() {}, 4. new *; it excludes undefined, null, primitives.
if ($data instanceof Object && token in $data) { return $data[token] }
if (isObjectLike($data) && token in $data) { return $data[token] }
if (token in this) { return this[token] }
if (token in globals) { return globals[token] }

Expand Down
34 changes: 33 additions & 1 deletion packages/utils.parser/spec/identifierBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ import {
describe('Identifier', function () {
function testLookup (identifier, $data) {
const ctx = new bindingContext($data)
return new Identifier(null, identifier).get_value(undefined, ctx)
return new Identifier(null, identifier).get_value(undefined, ctx, {})
}

function testWrite (identifier, $data, newValue) {
const ctx = new bindingContext($data)
return new Identifier(null, identifier).set_value(newValue, ctx, {})
}

var c = 'Z',
Expand All @@ -46,6 +51,12 @@ describe('Identifier', function () {
assert.equal(testLookup('f', context), f)
})

it('looks up values on no-prototype $data', function () {
const $data = Object.create(null)
$data.c = c
assert.equal(testLookup('c', $data), 'Z')
})

it('returns null as expected', function () {
assert.equal(testLookup('$data', null), null)
})
Expand All @@ -54,6 +65,27 @@ describe('Identifier', function () {
assert.equal(testLookup('$data', undefined), undefined)
})

it('sets plain values on $data', () => {
const $data = { c: c }
assert.equal($data.c, 'Z')
testWrite('c', $data, 'X')
assert.equal($data.c, 'X')
})

it('sets observable values on $data', () => {
const $data = { c: observable(c) }
assert.equal($data.c(), 'Z')
testWrite('c', $data, 'X')
assert.equal($data.c(), 'X')
})

it('sets plain values on no-prototype $data', () => {
const $data = Object.create(null)
$data.c = c
testWrite('c', $data, 'X')
assert.equal($data.c, 'X')
})

it('dereferences values on the parser', function () {
var context = new bindingContext({ f: f })
var fake_args = new Arguments(null, [])
Expand Down
8 changes: 4 additions & 4 deletions packages/utils.parser/src/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Node from './Node'
import Arguments from './Arguments'

import { hasOwnProperty } from '@tko/utils'
import { hasOwnProperty, isObjectLike } from '@tko/utils'

import {
isWriteableObservable, isObservable
Expand Down Expand Up @@ -106,11 +106,11 @@ export default class Identifier {
let leaf = this.token
let i, n, root

if (hasOwnProperty($data, leaf)) {
if (isObjectLike($data) && leaf in $data) {
root = $data
} else if (hasOwnProperty($context, leaf)) {
} else if (leaf in $context) {
root = $context
} else if (hasOwnProperty(globals, leaf)) {
} else if (leaf in globals) {
root = globals
} else {
throw new Error('Identifier::set_value -- ' +
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function hasOwnProperty(obj, propName) {
return Object.prototype.hasOwnProperty.call(obj, propName)
}

export function isObjectLike(obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function')
}

export function extend (target, source) {
if (source) {
for (var prop in source) {
Expand Down