Skip to content

Commit

Permalink
feat: externalize vega-expression and vega-event-selector (#7718)
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Sep 22, 2021
1 parent 8324e58 commit 4a86427
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 513 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -135,7 +135,7 @@
"json-stringify-pretty-compact": "~3.0.0",
"tslib": "~2.3.1",
"vega-event-selector": "~3.0.0",
"vega-expression": "~4.0.1",
"vega-expression": "~5.0.0",
"vega-util": "~1.17.0",
"yargs": "~17.1.1"
},
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Expand Up @@ -31,6 +31,8 @@ export function debugImports() {
const extensions = ['.js', '.ts'];

const globals = {
'vega-event-selector': 'vega',
'vega-expression': 'vega',
'vega-util': 'vega',
vega: 'vega'
};
Expand Down
7 changes: 4 additions & 3 deletions src/compile/data/expressions.ts
@@ -1,4 +1,4 @@
import {parse} from 'vega-expression';
import {parseExpression} from 'vega-expression';

function getName(node: any) {
const name: string[] = [];
Expand Down Expand Up @@ -27,9 +27,10 @@ function startsWithDatum(node: any): boolean {
}

export function getDependentFields(expression: string) {
const ast = parse(expression);
const ast = parseExpression(expression);
const dependents = new Set<string>();
ast.visit((node: any) => {
// visit is missing in types https://github.com/vega/vega/issues/3298
(ast as any).visit((node: any) => {
if (node.type === 'MemberExpression' && startsWithDatum(node)) {
dependents.add(getName(node).slice(1).join('.'));
}
Expand Down
25 changes: 12 additions & 13 deletions test/compile/axis/properties.test.ts
@@ -1,6 +1,6 @@
import {range} from 'd3-array';
import {AxisOrient, Orient, SignalRef} from 'vega';
import {codegen, parse} from 'vega-expression';
import {codegenExpression, parseExpression} from 'vega-expression';
import {stringValue} from 'vega-util';
import {getAxisConfigs} from '../../../src/compile/axis/config';
import * as properties from '../../../src/compile/axis/properties';
Expand All @@ -12,14 +12,13 @@ import {isSignalRef} from '../../../src/vega.schema';
describe('compile/axis/properties', () => {
function evalValueOrSignal(valueOrSignalRef: string | SignalRef, o: Orient) {
if (isSignalRef(valueOrSignalRef)) {
const ast = parse(valueOrSignalRef.signal);
const {code} = codegen({
const ast = parseExpression(valueOrSignalRef.signal);
const {code} = codegenExpression({
globalvar: ((v: string) => (v === 'o' ? stringValue(o) : undefined)) as any
})(ast);
return eval(code);
} else {
return valueOrSignalRef;
}
return valueOrSignalRef;
}

describe('defaultGrid()', () => {
Expand Down Expand Up @@ -231,13 +230,13 @@ describe('compile/axis/properties', () => {
});

it('correctly align y-axis labels for labelAngle and orient signals', () => {
const ast = parse(defaultLabelAlign({signal: 'a'}, {signal: 'o'}, 'y')['signal']);
const ast = parseExpression(defaultLabelAlign({signal: 'a'}, {signal: 'o'}, 'y')['signal']);
let a: number;
let o: AxisOrient;
// test all angles
for (a of range(-360, 375, 15)) {
for (o of ['left', 'right'] as AxisOrient[]) {
const {code} = codegen({
const {code} = codegenExpression({
globalvar: ((v: string) => (v === 'a' ? a : v === 'o' ? stringValue(o) : undefined)) as any
})(ast);

Expand All @@ -250,13 +249,13 @@ describe('compile/axis/properties', () => {

it('correctly align x-axis labels for labelAngle and orient signals', () => {
return new Promise<void>(done => {
const ast = parse(defaultLabelAlign({signal: 'a'}, {signal: 'o'}, 'x')['signal']);
const ast = parseExpression(defaultLabelAlign({signal: 'a'}, {signal: 'o'}, 'x')['signal']);
let a: number;
let o: AxisOrient;
// test all angles
for (a of range(-360, 375, 15)) {
for (o of ['top', 'bottom'] as AxisOrient[]) {
const {code} = codegen({
const {code} = codegenExpression({
globalvar: ((v: string) => (v === 'a' ? a : v === 'o' ? stringValue(o) : undefined)) as any
})(ast);

Expand Down Expand Up @@ -343,13 +342,13 @@ describe('compile/axis/properties', () => {

it('correctly align y-axis labels for labelAngle and orient signals', () => {
return new Promise<void>(done => {
const ast = parse(defaultLabelBaseline({signal: 'a'}, {signal: 'o'}, 'y')['signal']);
const ast = parseExpression(defaultLabelBaseline({signal: 'a'}, {signal: 'o'}, 'y')['signal']);
let a: number;
let o: AxisOrient;
// test all angles
for (a of range(-360, 375, 15)) {
for (o of ['left', 'right'] as AxisOrient[]) {
const {code} = codegen({
const {code} = codegenExpression({
globalvar: ((v: string) => (v === 'a' ? a : v === 'o' ? stringValue(o) : undefined)) as any
})(ast);

Expand All @@ -364,13 +363,13 @@ describe('compile/axis/properties', () => {

it('correctly align x-axis labels for labelAngle and orient signals', () => {
return new Promise<void>(done => {
const ast = parse(defaultLabelBaseline({signal: 'a'}, {signal: 'o'}, 'x')['signal']);
const ast = parseExpression(defaultLabelBaseline({signal: 'a'}, {signal: 'o'}, 'x')['signal']);
let a: number;
let o: AxisOrient;
// test all angles
for (a of range(-360, 375, 15)) {
for (o of ['top', 'bottom'] as AxisOrient[]) {
const {code} = codegen({
const {code} = codegenExpression({
globalvar: ((v: string) => (v === 'a' ? a : v === 'o' ? stringValue(o) : undefined)) as any
})(ast);

Expand Down

0 comments on commit 4a86427

Please sign in to comment.