Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type annotations #9025

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/style-spec/error/parsing_error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

function ParsingError(error) {
this.error = error;
this.message = error.message;
const match = error.message.match(/line (\d+)/);
this.line = match ? parseInt(match[1], 10) : 0;
}
export default class ParsingError {
error: Error;
message: string;
line: number;

export default ParsingError;
constructor(error: Error) {
this.error = error;
this.message = error.message;
const match = error.message.match(/line (\d+)/);
this.line = match ? parseInt(match[1], 10) : 0;
}
}
6 changes: 5 additions & 1 deletion src/style-spec/error/validation_error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

export default class ValidationError {
constructor(key, value, message, identifier) {
identifier?: string;
message: string;
line?: number;

constructor(key: string, value: any, message: string, identifier?: string) {
this.message = (key ? `${key}: ` : '') + message;
if (identifier) this.identifier = identifier;

Expand Down
6 changes: 3 additions & 3 deletions src/style-spec/expression/types/formatted.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ export default class Formatted {
}

serialize(): Array<mixed> {
const serialized = ["format"];
const serialized: Array<mixed> = ["format"];
for (const section of this.sections) {
if (section.image) {
serialized.push(["image", section.image.name]);
continue;
}
serialized.push(section.text);
const options = {};
const options: { [key: string]: mixed } = {};
if (section.fontStack) {
options["text-font"] = ["literal", section.fontStack.split(',')];
}
if (section.scale) {
options["font-scale"] = section.scale;
}
if (section.textColor) {
options["text-color"] = ["rgba"].concat(section.textColor.toArray());
options["text-color"] = (["rgba"]: Array<mixed>).concat(section.textColor.toArray());
}
serialized.push(options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/expression/types/resolved_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class ResolvedImage {
return new ResolvedImage({name, available: false});
}

serialize(): Array<mixed> {
serialize(): Array<string> {
return ["image", this.name];
}
}
4 changes: 2 additions & 2 deletions src/style-spec/expression/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {NullType, NumberType, StringType, BooleanType, ColorType, ObjectType, Va

import type {Type} from './types';

export function validateRGBA(r: mixed, g: mixed, b: mixed, a?: mixed): ?string {
export function validateRGBA(r: mixed, g: mixed, b: mixed, a?: mixed): string | null {
if (!(
typeof r === 'number' && r >= 0 && r <= 255 &&
typeof g === 'number' && g >= 0 && g <= 255 &&
Expand Down Expand Up @@ -86,7 +86,7 @@ export function typeOf(value: Value): Type {
return ResolvedImageType;
} else if (Array.isArray(value)) {
const length = value.length;
let itemType: ?Type;
let itemType: Type | typeof undefined;

for (const item of value) {
const t = typeOf(item);
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/util/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Color {
* Parses valid CSS color strings and returns a `Color` instance.
* @returns A `Color` instance, or `undefined` if the input is not a valid color string.
*/
static parse(input: ?string): Color | void {
static parse(input?: string | Color | null): Color | void {
if (!input) {
return undefined;
}
Expand Down
8 changes: 4 additions & 4 deletions src/style-spec/util/color_spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ const Xn = 0.950470, // D65 standard referent
rad2deg = 180 / Math.PI;

// Utilities
function xyz2lab(t) {
function xyz2lab(t: number) {
return t > t3 ? Math.pow(t, 1 / 3) : t / t2 + t0;
}

function lab2xyz(t) {
function lab2xyz(t: number) {
return t > t1 ? t * t * t : t2 * (t - t0);
}

function xyz2rgb(x) {
function xyz2rgb(x: number) {
return 255 * (x <= 0.0031308 ? 12.92 * x : 1.055 * Math.pow(x, 1 / 2.4) - 0.055);
}

function rgb2xyz(x) {
function rgb2xyz(x: number) {
x /= 255;
return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
}
Expand Down
3 changes: 2 additions & 1 deletion src/style-spec/util/extend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

export default function (output, ...inputs) {
export default function (output: any, ...inputs: Array<any>) {
for (const input of inputs) {
for (const k in input) {
output[k] = input[k];
Expand Down
3 changes: 2 additions & 1 deletion src/style-spec/util/get_type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

export default function getType(val) {
export default function getType(val: any): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering (didn't check), can you replace instances like this with mixed? Wherever the type either gets refined or doesn't matter for the resulting type.

if (val instanceof Number) {
return 'number';
} else if (val instanceof String) {
Expand Down
9 changes: 4 additions & 5 deletions src/style-spec/util/unbundle_jsonlint.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
function isPrimitive(value) {
function isPrimitive(value: any) {
return value instanceof Number || value instanceof String || value instanceof Boolean;
}

// Turn jsonlint-lines-primitives objects into primitive objects
export function unbundle(value) {
export function unbundle(value: any) {
if (isPrimitive(value)) {
return value.valueOf();
} else {
return value;
}
}

export function deepUnbundle(value) {
export function deepUnbundle(value: any): any {
if (Array.isArray(value)) {
return value.map(deepUnbundle);
} else if (value instanceof Object && !isPrimitive(value)) {
const unbundledValue = {};
const unbundledValue: { [key: string]: mixed } = {};
for (const key in value) {
unbundledValue[key] = deepUnbundle(value[key]);
}
Expand All @@ -24,4 +24,3 @@ export function deepUnbundle(value) {

return unbundle(value);
}