Skip to content

Commit

Permalink
Cast return type of context.error() from void to null
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Wipfli authored and wipfli committed Jul 18, 2021
1 parent 4e72829 commit 95781ad
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/style-spec/expression/definitions/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Assertion implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length < 2)
return context.error(`Expected at least one argument.`);
return context.error(`Expected at least one argument.`) as null;

let i = 1;
let type;
Expand All @@ -47,7 +47,7 @@ class Assertion implements Expression {
if (args.length > 2) {
const type = args[1];
if (typeof type !== 'string' || !(type in types) || type === 'object')
return context.error('The item type argument of "array" must be one of string, number, boolean', 1);
return context.error('The item type argument of "array" must be one of string, number, boolean', 1) as null;
itemType = types[type];
i++;
} else {
Expand All @@ -61,7 +61,7 @@ class Assertion implements Expression {
args[2] < 0 ||
args[2] !== Math.floor(args[2]))
) {
return context.error('The length argument to "array" must be a positive integer literal', 2);
return context.error('The length argument to "array" must be a positive integer literal', 2) as null;
}
N = args[2];
i++;
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/expression/definitions/at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class At implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length !== 3)
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`) as null;

const index = context.parse(args[1], 1, NumberType);
const input = context.parse(args[2], 2, array(context.expectedType || ValueType));
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Case implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length < 4)
return context.error(`Expected at least 3 arguments, but found only ${args.length - 1}.`);
return context.error(`Expected at least 3 arguments, but found only ${args.length - 1}.`) as null;
if (args.length % 2 !== 0)
return context.error(`Expected an odd number of arguments.`);
return context.error(`Expected an odd number of arguments.`) as null;

let outputType: Type | undefined | null;
if (context.expectedType && context.expectedType.kind !== 'value') {
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/expression/definitions/coalesce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Coalesce implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length < 2) {
return context.error("Expectected at least one argument.");
return context.error("Expectected at least one argument.") as null;
}
let outputType: Type = (null as any);
const expectedType = context.expectedType;
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class Coercion implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length < 2)
return context.error(`Expected at least one argument.`);
return context.error(`Expected at least one argument.`) as null;

const name: string = (args[0] as any);
assert(types[name], name);

if ((name === 'to-boolean' || name === 'to-string') && args.length !== 2)
return context.error(`Expected one argument.`);
return context.error(`Expected one argument.`) as null;

const type = types[name];

Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/collator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export default class CollatorExpression implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length !== 2)
return context.error(`Expected one argument.`);
return context.error(`Expected one argument.`) as null;

const options = (args[1] as any);
if (typeof options !== "object" || Array.isArray(options))
return context.error(`Collator options argument must be an object.`);
return context.error(`Collator options argument must be an object.`) as null;

const caseSensitive = context.parse(
options['case-sensitive'] === undefined ? false : options['case-sensitive'], 1, BooleanType);
Expand Down
10 changes: 5 additions & 5 deletions src/style-spec/expression/definitions/comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,27 @@ function makeComparison(op: ComparisonOperator, compareBasic, compareWithCollato

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length !== 3 && args.length !== 4)
return context.error(`Expected two or three arguments.`);
return context.error(`Expected two or three arguments.`) as null;

const op: ComparisonOperator = (args[0] as any);

let lhs = context.parse(args[1], 1, ValueType);
if (!lhs) return null;
if (!isComparableType(op, lhs.type)) {
return context.concat(1).error(`"${op}" comparisons are not supported for type '${toString(lhs.type)}'.`);
return context.concat(1).error(`"${op}" comparisons are not supported for type '${toString(lhs.type)}'.`) as null;
}
let rhs = context.parse(args[2], 2, ValueType);
if (!rhs) return null;
if (!isComparableType(op, rhs.type)) {
return context.concat(2).error(`"${op}" comparisons are not supported for type '${toString(rhs.type)}'.`);
return context.concat(2).error(`"${op}" comparisons are not supported for type '${toString(rhs.type)}'.`) as null;
}

if (
lhs.type.kind !== rhs.type.kind &&
lhs.type.kind !== 'value' &&
rhs.type.kind !== 'value'
) {
return context.error(`Cannot compare types '${toString(lhs.type)}' and '${toString(rhs.type)}'.`);
return context.error(`Cannot compare types '${toString(lhs.type)}' and '${toString(rhs.type)}'.`) as null;
}

if (isOrderComparison) {
Expand All @@ -119,7 +119,7 @@ function makeComparison(op: ComparisonOperator, compareBasic, compareWithCollato
lhs.type.kind !== 'value' &&
rhs.type.kind !== 'value'
) {
return context.error(`Cannot use collator to compare non-string types.`);
return context.error(`Cannot use collator to compare non-string types.`) as null;
}
collator = context.parse(args[3], 3, CollatorType);
if (!collator) return null;
Expand Down
6 changes: 3 additions & 3 deletions src/style-spec/expression/definitions/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export default class FormatExpression implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length < 2) {
return context.error(`Expected at least one argument.`);
return context.error(`Expected at least one argument.`) as null;
}

const firstArg = args[1];
if (!Array.isArray(firstArg) && typeof firstArg === 'object') {
return context.error(`First argument must be an image or text section.`);
return context.error(`First argument must be an image or text section.`) as null;
}

const sections: Array<FormattedSectionExpression> = [];
Expand Down Expand Up @@ -79,7 +79,7 @@ export default class FormatExpression implements Expression {

const kind = content.type.kind;
if (kind !== 'string' && kind !== 'value' && kind !== 'null' && kind !== 'resolvedImage')
return context.error(`Formatted text type must be 'string', 'value', 'image' or 'null'.`);
return context.error(`Formatted text type must be 'string', 'value', 'image' or 'null'.`) as null;

nextTokenMayBeObject = true;
sections.push({content, scale: null, font: null, textColor: null});
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export default class ImageExpression implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length !== 2) {
return context.error(`Expected two arguments.`);
return context.error(`Expected two arguments.`) as null;
}

const name = context.parse(args[1], 1, StringType);
if (!name) return context.error(`No image name provided.`);
if (!name) return context.error(`No image name provided.`) as null;

return new ImageExpression(name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class In implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length !== 3) {
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`) as null;
}

const needle = context.parse(args[1], 1, ValueType);
Expand All @@ -39,7 +39,7 @@ class In implements Expression {
if (!needle || !haystack) return null;

if (!isValidType(needle.type, [BooleanType, StringType, NumberType, NullType, ValueType])) {
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString(needle.type)} instead`);
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString(needle.type)} instead`) as null;
}

return new In(needle, haystack);
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/index_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class IndexOf implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length <= 2 || args.length >= 5) {
return context.error(`Expected 3 or 4 arguments, but found ${args.length - 1} instead.`);
return context.error(`Expected 3 or 4 arguments, but found ${args.length - 1} instead.`) as null;
}

const needle = context.parse(args[1], 1, ValueType);
Expand All @@ -40,7 +40,7 @@ class IndexOf implements Expression {

if (!needle || !haystack) return null;
if (!isValidType(needle.type, [BooleanType, StringType, NumberType, NullType, ValueType])) {
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString(needle.type)} instead`);
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString(needle.type)} instead`) as null;
}

if (args.length === 4) {
Expand Down
18 changes: 9 additions & 9 deletions src/style-spec/expression/definitions/interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ class Interpolate implements Expression {
let [operator, interpolation, input, ...rest] = args;

if (!Array.isArray(interpolation) || interpolation.length === 0) {
return context.error(`Expected an interpolation type expression.`, 1);
return context.error(`Expected an interpolation type expression.`, 1) as null;
}

if (interpolation[0] === 'linear') {
interpolation = {name: 'linear'};
} else if (interpolation[0] === 'exponential') {
const base = interpolation[1];
if (typeof base !== 'number')
return context.error(`Exponential interpolation requires a numeric base.`, 1, 1);
return context.error(`Exponential interpolation requires a numeric base.`, 1, 1) as null;
interpolation = {
name: 'exponential',
base
Expand All @@ -81,23 +81,23 @@ class Interpolate implements Expression {
controlPoints.length !== 4 ||
controlPoints.some(t => typeof t !== 'number' || t < 0 || t > 1)
) {
return context.error('Cubic bezier interpolation requires four numeric arguments with values between 0 and 1.', 1);
return context.error('Cubic bezier interpolation requires four numeric arguments with values between 0 and 1.', 1) as null;
}

interpolation = {
name: 'cubic-bezier',
controlPoints: (controlPoints as any)
};
} else {
return context.error(`Unknown interpolation type ${String(interpolation[0])}`, 1, 0);
return context.error(`Unknown interpolation type ${String(interpolation[0])}`, 1, 0) as null;
}

if (args.length - 1 < 4) {
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`) as null;
}

if ((args.length - 1) % 2 !== 0) {
return context.error(`Expected an even number of arguments.`);
return context.error(`Expected an even number of arguments.`) as null;
}

input = context.parse(input, 2, NumberType);
Expand All @@ -120,11 +120,11 @@ class Interpolate implements Expression {
const valueKey = i + 4;

if (typeof label !== 'number') {
return context.error('Input/output pairs for "interpolate" expressions must be defined using literal numeric values (not computed expressions) for the input values.', labelKey);
return context.error('Input/output pairs for "interpolate" expressions must be defined using literal numeric values (not computed expressions) for the input values.', labelKey) as null;
}

if (stops.length && stops[stops.length - 1][0] >= label) {
return context.error('Input/output pairs for "interpolate" expressions must be arranged with input values in strictly ascending order.', labelKey);
return context.error('Input/output pairs for "interpolate" expressions must be arranged with input values in strictly ascending order.', labelKey) as null;
}

const parsed = context.parse(value, valueKey, outputType);
Expand All @@ -141,7 +141,7 @@ class Interpolate implements Expression {
typeof outputType.N === 'number'
)
) {
return context.error(`Type ${toString(outputType)} is not interpolatable.`);
return context.error(`Type ${toString(outputType)} is not interpolatable.`) as null;
}

return new Interpolate(outputType, (operator as any), interpolation, input, stops);
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class Length implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length !== 2)
return context.error(`Expected 1 argument, but found ${args.length - 1} instead.`);
return context.error(`Expected 1 argument, but found ${args.length - 1} instead.`) as null;

const input = context.parse(args[1], 1);
if (!input) return null;

if (input.type.kind !== 'array' && input.type.kind !== 'string' && input.type.kind !== 'value')
return context.error(`Expected argument of type string or array, but found ${toString(input.type)} instead.`);
return context.error(`Expected argument of type string or array, but found ${toString(input.type)} instead.`) as null;

return new Length(input);
}
Expand Down
6 changes: 3 additions & 3 deletions src/style-spec/expression/definitions/let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ class Let implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length < 4)
return context.error(`Expected at least 3 arguments, but found ${args.length - 1} instead.`);
return context.error(`Expected at least 3 arguments, but found ${args.length - 1} instead.`) as null;

const bindings: Array<[string, Expression]> = [];
for (let i = 1; i < args.length - 1; i += 2) {
const name = args[i];

if (typeof name !== 'string') {
return context.error(`Expected string, but found ${typeof name} instead.`, i);
return context.error(`Expected string, but found ${typeof name} instead.`, i) as null;
}

if (/[^a-zA-Z0-9_]/.test(name)) {
return context.error(`Variable names must contain only alphanumeric characters or '_'.`, i);
return context.error(`Variable names must contain only alphanumeric characters or '_'.`, i) as null;
}

const value = context.parse(args[i + 1], i + 1);
Expand Down
4 changes: 2 additions & 2 deletions src/style-spec/expression/definitions/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class Literal implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length !== 2)
return context.error(`'literal' expression requires exactly one argument, but found ${args.length - 1} instead.`);
return context.error(`'literal' expression requires exactly one argument, but found ${args.length - 1} instead.`) as null;

if (!isValue(args[1]))
return context.error(`invalid value`);
return context.error(`invalid value`) as null;

const value = (args[1] as any);
let type = typeOf(value);
Expand Down
14 changes: 7 additions & 7 deletions src/style-spec/expression/definitions/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class Match implements Expression {

static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression | undefined | null {
if (args.length < 5)
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`) as null;
if (args.length % 2 !== 1)
return context.error(`Expected an even number of arguments.`);
return context.error(`Expected an even number of arguments.`) as null;

let inputType;
let outputType;
Expand All @@ -55,17 +55,17 @@ class Match implements Expression {

const labelContext = context.concat(i);
if (labels.length === 0) {
return labelContext.error('Expected at least one branch label.');
return labelContext.error('Expected at least one branch label.') as null;
}

for (const label of labels) {
if (typeof label !== 'number' && typeof label !== 'string') {
return labelContext.error(`Branch labels must be numbers or strings.`);
return labelContext.error(`Branch labels must be numbers or strings.`) as null;
} else if (typeof label === 'number' && Math.abs(label) > Number.MAX_SAFE_INTEGER) {
return labelContext.error(`Branch labels must be integers no larger than ${Number.MAX_SAFE_INTEGER}.`);
return labelContext.error(`Branch labels must be integers no larger than ${Number.MAX_SAFE_INTEGER}.`) as null;

} else if (typeof label === 'number' && Math.floor(label) !== label) {
return labelContext.error(`Numeric branch labels must be integer values.`);
return labelContext.error(`Numeric branch labels must be integer values.`) as null;

} else if (!inputType) {
inputType = typeOf(label);
Expand All @@ -74,7 +74,7 @@ class Match implements Expression {
}

if (typeof cases[String(label)] !== 'undefined') {
return labelContext.error('Branch labels must be unique.');
return labelContext.error('Branch labels must be unique.') as null;
}

cases[String(label)] = outputs.length;
Expand Down
Loading

0 comments on commit 95781ad

Please sign in to comment.