Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.

Commit f8a5980

Browse files
committedApr 12, 2021
refactor: downgrade to with if deps not provided
1 parent 59fb3fb commit f8a5980

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed
 

‎src/component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* istanbul ignore next */
1+
/* istanbul ignore file */
2+
23
import { Directives, DirectiveProps, Watchers, State, ASTNode } from './models/structs';
34

45
import { directives } from './core/directive';

‎src/core/utils/__test__/computeExpression.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ describe('.compute', () => {
44
it('should use key-based retrieval during computation', () => {
55
const el = document.createElement('div');
66

7-
expect(computeExpression('count', el)({ count: 0 })).toEqual(0);
7+
expect(computeExpression('count', el, true, {}, ['count'])({ count: 0 })).toEqual(0);
88
});
99

1010
it('should compute correctly', () => {
1111
const el = document.createElement('div');
12-
expect(computeExpression('count + 1', el)({ count: 0 })).toEqual(1);
12+
expect(computeExpression('count + 1', el, true, {}, ['count'])({ count: 0 })).toEqual(1);
1313
});
1414

1515
it('should not return the value', () => {
1616
const el = document.createElement('div');
17-
expect(computeExpression('count + 1', el, false)({ count: 0 })).toEqual(undefined);
17+
expect(computeExpression('count + 1', el, false, {}, ['count'])({ count: 0 })).toEqual(undefined);
1818
});
1919

2020
it('should emit and access an event', () => {
2121
const el = document.createElement('div');
2222

2323
expect(
2424
// @ts-expect-error: 'foo' cannot be passed as an Event, but good enough for our use case
25-
computeExpression(`$emit('customEvent', $el); return $event`, el, false)({}, 'foo')
25+
computeExpression(`$emit('customEvent', $el); return $event`, el, false, {}, [])({}, 'foo')
2626
).toEqual('foo');
2727
});
2828
});

‎src/core/utils/computeExpression.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ export const computeExpression = (
77
el?: HTMLElement,
88
returnable = true,
99
refs: Refs = {},
10-
deps: string[] = []
10+
deps?: string[]
1111
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1212
): ((state: UnknownKV, event?: Event) => any) => {
1313
// This dynamically appends `$state.` to the front of standalone props, allowing the
1414
// user to write less and us to compile and run faster without with() {}
1515
let formattedExpression = `${returnable ? `return ${expression}` : expression}`;
16-
deps.forEach((dep) => {
17-
formattedExpression = formattedExpression.replace(expressionPropRE(dep), `$state.${dep}`);
18-
});
16+
if (deps) {
17+
deps.forEach((dep) => {
18+
formattedExpression = formattedExpression.replace(expressionPropRE(dep), `$state.${dep}`);
19+
});
20+
} else {
21+
formattedExpression = `with($state){${formattedExpression}}`;
22+
}
23+
1924
return (state: UnknownKV, event?: Event) => {
2025
try {
2126
const value = state[expression];

0 commit comments

Comments
 (0)
This repository has been archived.