Skip to content

Commit

Permalink
Fixing flow type error in index-of and slice expression
Browse files Browse the repository at this point in the history
  • Loading branch information
lbutler committed Mar 23, 2020
1 parent 74a2b68 commit 7a2f901
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
29 changes: 16 additions & 13 deletions src/style-spec/expression/definitions/index_of.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class IndexOf implements Expression {
type: Type;
needle: Expression;
haystack: Expression;
fromIndex: Expression;
fromIndex: ?Expression;

constructor(needle: Expression, haystack: Expression, fromIndex: Expression) {
constructor(needle: Expression, haystack: Expression, fromIndex?: Expression) {
this.type = NumberType;
this.needle = needle;
this.haystack = haystack;
Expand All @@ -52,16 +52,18 @@ class IndexOf implements Expression {

const haystack = context.parse(args[2], 2, ValueType);

const fromIndex = args.length === 4 ? context.parse(args[3], 3, NumberType) : undefined;

if (args.length === 3 && (!needle || !haystack)) return null;
if (args.length === 4 && (!needle || !haystack || !fromIndex)) return null;

if (!needle || !haystack) return null;
if (!isComparableType(needle.type)) {
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString(needle.type)} instead`);
}

return new IndexOf(needle, haystack, fromIndex);
if (args.length === 4) {
const fromIndex = context.parse(args[3], 3, NumberType);
if (!fromIndex) return null;
return new IndexOf(needle, haystack, fromIndex);
} else {
return new IndexOf(needle, haystack);
}
}

evaluate(ctx: EvaluationContext) {
Expand All @@ -76,7 +78,7 @@ class IndexOf implements Expression {
throw new RuntimeError(`Expected second argument to be of type array or string, but found ${toString(typeOf(haystack))} instead.`);
}

if (this.fromIndex !== undefined) {
if (this.fromIndex) {
const fromIndex = (this.fromIndex.evaluate(ctx): number);
return haystack.indexOf(needle, fromIndex);
}
Expand All @@ -87,7 +89,7 @@ class IndexOf implements Expression {
eachChild(fn: (_: Expression) => void) {
fn(this.needle);
fn(this.haystack);
if (this.fromIndex !== undefined) {
if (this.fromIndex) {
fn(this.fromIndex);
}
}
Expand All @@ -97,10 +99,11 @@ class IndexOf implements Expression {
}

serialize() {
if (this.fromIndex === undefined) {
return ["index-of", this.needle.serialize(), this.haystack.serialize()];
if (this.fromIndex != null && this.fromIndex !== undefined) {
const fromIndex = this.fromIndex.serialize();
return ["index-of", this.needle.serialize(), this.haystack.serialize(), fromIndex];
}
return ["index-of", this.needle.serialize(), this.haystack.serialize(), this.fromIndex.serialize()];
return ["index-of", this.needle.serialize(), this.haystack.serialize()];
}
}

Expand Down
30 changes: 17 additions & 13 deletions src/style-spec/expression/definitions/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class Slice implements Expression {
type: Type;
input: Expression;
beginIndex: Expression;
endIndex: Expression;
endIndex: ?Expression;

constructor(type: Type, input: Expression, beginIndex: Expression, endIndex: Expression) {
constructor(type: Type, input: Expression, beginIndex: Expression, endIndex?: Expression) {
this.type = type;
this.input = input;
this.beginIndex = beginIndex;
Expand All @@ -41,27 +41,30 @@ class Slice implements Expression {

const input = context.parse(args[1], 1, ValueType);
const beginIndex = context.parse(args[2], 2, NumberType);
const endIndex = args.length === 4 ? context.parse(args[3], 3, NumberType) : undefined;

if (args.length === 3 && (!input || !beginIndex)) return null;
if (args.length === 4 && (!input || !beginIndex || !endIndex)) return null;

if (!input || !beginIndex) return null;
if (!isSliceableType(input.type)) {
return context.error(`Expected first argument to be of type array or string, but found ${toString(input.type)} instead`);
}

return new Slice(input.type, input, beginIndex, endIndex);
if (args.length === 4) {
const endIndex = context.parse(args[3], 3, NumberType);
if (!endIndex) return null;
return new Slice(input.type, input, beginIndex, endIndex);
} else {
return new Slice(input.type, input, beginIndex);
}
}

evaluate(ctx: EvaluationContext) {
const input = (this.input.evaluate(ctx): Value);
const input = (this.input.evaluate(ctx): any);
const beginIndex = (this.beginIndex.evaluate(ctx): number);

if (!isAcceptableInputRuntimeValue(input)) {
throw new RuntimeError(`Expected first argument to be of type array or string, but found ${toString(typeOf(input))} instead.`);
}

if (this.endIndex !== undefined) {
if (this.endIndex) {
const endIndex = (this.endIndex.evaluate(ctx): number);
return input.slice(beginIndex, endIndex);
}
Expand All @@ -72,7 +75,7 @@ class Slice implements Expression {
eachChild(fn: (_: Expression) => void) {
fn(this.input);
fn(this.beginIndex);
if (this.endIndex !== undefined) {
if (this.endIndex) {
fn(this.endIndex);
}
}
Expand All @@ -82,10 +85,11 @@ class Slice implements Expression {
}

serialize() {
if (this.endIndex === undefined) {
return ["slice", this.input.serialize(), this.beginIndex.serialize()];
if (this.endIndex != null && this.endIndex !== undefined) {
const endIndex = this.endIndex.serialize();
return ["slice", this.input.serialize(), this.beginIndex.serialize(), endIndex];
}
return ["slice", this.input.serialize(), this.beginIndex.serialize(), this.endIndex.serialize()];
return ["slice", this.input.serialize(), this.beginIndex.serialize()];
}
}

Expand Down

0 comments on commit 7a2f901

Please sign in to comment.