Skip to content

Commit

Permalink
Alternate typing for expression registration
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jan 4, 2018
1 parent 3eef1fe commit e86682b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/style-spec/expression/compound_expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const ParsingContext = require('./parsing_context');
const EvaluationContext = require('./evaluation_context');
const assert = require('assert');

import type { Expression } from './expression';
import type { Expression, ExpressionRegistry } from './expression';
import type { Type } from './types';
import type { Value } from './values';

Expand Down Expand Up @@ -83,7 +83,7 @@ class CompoundExpression implements Expression {
for (const [params, evaluate] of overloads) {
// Use a fresh context for each attempted signature so that, if
// we eventually succeed, we haven't polluted `context.errors`.
signatureContext = new ParsingContext(context.definitions, context.path, null, context.scope);
signatureContext = new ParsingContext(context.registry, context.path, null, context.scope);

if (Array.isArray(params)) {
if (params.length !== parsedArgs.length) {
Expand Down Expand Up @@ -122,13 +122,13 @@ class CompoundExpression implements Expression {
}

static register(
expressions: { [string]: Class<Expression> },
registry: ExpressionRegistry,
definitions: { [string]: Definition }
) {
assert(!CompoundExpression.definitions);
CompoundExpression.definitions = definitions;
for (const name in definitions) {
expressions[name] = CompoundExpression;
registry[name] = CompoundExpression;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const Interpolate = require('./interpolate');
const Coalesce = require('./coalesce');
const Equals = require('./equals');

import type { Expression } from '../expression';
import type { ExpressionRegistry } from '../expression';

const expressions: { [string]: Class<Expression> } = {
const expressions: ExpressionRegistry = {
// special forms
'!=': Equals,
'==': Equals,
Expand Down
9 changes: 5 additions & 4 deletions src/style-spec/expression/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import type {Type} from './types';
import type {Value} from './values';
import type ParsingContext from './parsing_context';
import type EvaluationContext from './evaluation_context';

export interface Expression {
+type: Type;

// Static interface properties are not supported in flow as of 0.62.0.
// https://github.com/facebook/flow/issues/5590
// static parse(args: Array<mixed>, context: ParsingContext): ?Expression;

evaluate(ctx: EvaluationContext): any;

eachChild(fn: Expression => void): void;
Expand All @@ -22,3 +19,7 @@ export interface Expression {
*/
possibleOutputs(): Array<Value | void>;
}

export type ExpressionParser = (args: Array<mixed>, context: ParsingContext) => ?Expression;
export type ExpressionRegistration = Class<Expression> & { +parse: ExpressionParser };
export type ExpressionRegistry = {[string]: ExpressionRegistration};
15 changes: 7 additions & 8 deletions src/style-spec/expression/parsing_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const Assertion = require('./definitions/assertion');
const ArrayAssertion = require('./definitions/array');
const Coercion = require('./definitions/coercion');

import type {Expression} from './expression';
import type {Expression, ExpressionRegistry} from './expression';
import type {Type} from './types';

/**
* State associated parsing at a given point in an expression tree.
* @private
*/
class ParsingContext {
definitions: {[string]: Class<Expression>};
registry: ExpressionRegistry;
path: Array<number>;
key: string;
scope: Scope;
Expand All @@ -29,13 +29,13 @@ class ParsingContext {
expectedType: ?Type;

constructor(
definitions: *,
registry: ExpressionRegistry,
path: Array<number> = [],
expectedType: ?Type,
scope: Scope = new Scope(),
errors: Array<ParsingError> = []
) {
this.definitions = definitions;
this.registry = registry;
this.path = path;
this.key = path.map(part => `[${part}]`).join('');
this.scope = scope;
Expand Down Expand Up @@ -77,10 +77,9 @@ class ParsingContext {
return null;
}

const Expr = context.definitions[op];
const Expr = context.registry[op];
if (Expr) {
// https://github.com/facebook/flow/issues/5590
let parsed: ?Expression = (Expr: any).parse(expr, context);
let parsed = Expr.parse(expr, context);
if (!parsed) return null;

if (context.expectedType) {
Expand Down Expand Up @@ -147,7 +146,7 @@ class ParsingContext {
const path = typeof index === 'number' ? this.path.concat(index) : this.path;
const scope = bindings ? this.scope.concat(bindings) : this.scope;
return new ParsingContext(
this.definitions,
this.registry,
path,
expectedType || null,
scope,
Expand Down
2 changes: 1 addition & 1 deletion src/util/web_worker_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ register('ZoomDependentExpression', ZoomDependentExpression);
register('ZoomConstantExpression', ZoomConstantExpression);
register('CompoundExpression', CompoundExpression, {omit: ['_evaluate']});
for (const name in expressions) {
if (expressions[name]._classRegistryKey) continue;
if ((expressions[name]: any)._classRegistryKey) continue;
register(`Expression_${name}`, expressions[name]);
}

Expand Down

0 comments on commit e86682b

Please sign in to comment.