Skip to content

Commit

Permalink
Type annotations (#9025)
Browse files Browse the repository at this point in the history
* Type annotations

* fix more errors
  • Loading branch information
kkaefer authored and mourner committed Nov 26, 2019
1 parent b9be06e commit 63e86f5
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 34 deletions.
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 @@
// @flow

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 extends Error {
error: Error;
line: number;

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

export default class ValidationError {
constructor(key, value, message, identifier) {
this.message = (key ? `${key}: ` : '') + message;
export default class ValidationError extends Error {
identifier: ?string;
line: ?number;

constructor(key: string | null, value?: any, message?: string, identifier?: string) {
super([key, message].filter(a => a).join(': '));
if (identifier) this.identifier = identifier;

if (value !== null && value !== undefined && value.__line__) {
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: mixed): string {
if (val instanceof Number) {
return 'number';
} else if (val instanceof String) {
Expand Down
15 changes: 6 additions & 9 deletions src/style-spec/util/unbundle_jsonlint.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
function isPrimitive(value) {
return value instanceof Number || value instanceof String || value instanceof Boolean;
}
// @flow

// Turn jsonlint-lines-primitives objects into primitive objects
export function unbundle(value) {
if (isPrimitive(value)) {
export function unbundle(value: mixed) {
if (value instanceof Number || value instanceof String || value instanceof Boolean) {
return value.valueOf();
} else {
return value;
}
}

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

return unbundle(value);
}

11 changes: 10 additions & 1 deletion test/unit/style-spec/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import validate from '../../../src/style-spec/validate_style';

const UPDATE = !!process.env.UPDATE;

function sanitizeError(error) {
const sanitized = {};
sanitized.message = error.message;
for (const key in error) {
sanitized[key] = error[key];
}
return sanitized;
}

glob.sync(`${__dirname}/fixture/*.input.json`).forEach((file) => {
test(path.basename(file), (t) => {
const outputfile = file.replace('.input', '.output');
const style = fs.readFileSync(file);
const result = validate(style);
if (UPDATE) fs.writeFileSync(outputfile, JSON.stringify(result, null, 2));
const expect = JSON.parse(fs.readFileSync(outputfile));
t.deepEqual(result, expect);
t.deepEqual(result.map(sanitizeError), expect);
t.end();
});
});
Expand Down
11 changes: 10 additions & 1 deletion test/unit/style-spec/validate_mapbox_api_supported.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import validateMapboxApiSupported from '../../../src/style-spec/validate_mapbox_

const UPDATE = !!process.env.UPDATE;

function sanitizeError(error) {
const sanitized = {};
sanitized.message = error.message;
for (const key in error) {
sanitized[key] = error[key];
}
return sanitized;
}

glob.sync(`${__dirname}/fixture/*.input.json`).forEach((file) => {
test(path.basename(file), (t) => {
const outputfile = file.replace('.input', '.output-api-supported');
const style = fs.readFileSync(file);
const result = validateMapboxApiSupported(style);
if (UPDATE) fs.writeFileSync(outputfile, JSON.stringify(result, null, 2));
const expect = JSON.parse(fs.readFileSync(outputfile));
t.deepEqual(result, expect);
t.deepEqual(result.map(sanitizeError), expect);
t.end();
});
});

0 comments on commit 63e86f5

Please sign in to comment.