Skip to content

Commit

Permalink
typescript: Enable strict checking (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Jul 17, 2020
1 parent 073c877 commit 5df6102
Show file tree
Hide file tree
Showing 53 changed files with 193 additions and 145 deletions.
19 changes: 14 additions & 5 deletions modules/arrow/src/lib/parse-arrow-in-batches.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
// TODO - this import defeats the sophisticated typescript checking in ArrowJS
import {RecordBatchReader} from 'apache-arrow/Arrow.es5.min';
import {isIterable} from '@loaders.gl/core';
// import {isIterable} from '@loaders.gl/core';

/**
* @param {AsyncIterator<ArrayBuffer> | Iterator<ArrayBuffer>} asyncIterator
* @param {object} options
*/
export async function parseArrowInBatches(asyncIterator, options) {
// Creates the appropriate RecordBatchReader subclasses from the input
// This will also close the underlying source in case of early termination or errors
const readers = await RecordBatchReader.readAll(asyncIterator);

// As an optimization, return a non-async iterator
/*
if (isIterable(readers)) {
return (function* arrowIterator() {
function* makeArrowIterator() {
for (const reader of readers) {
for (const batch of reader) {
yield processBatch(batch, reader);
}
break; // only processing one stream of batches
}
})();
}
const arrowIterator = makeArrowIterator();
}
*/

return (async function* arrowAsyncIterator() {
async function* makeArrowAsyncIterator() {
for await (const reader of readers) {
for await (const batch of reader) {
yield processBatch(batch, reader);
}
break; // only processing one stream of batches
}
})();
}
return makeArrowAsyncIterator();
}

function processBatch(batch, on) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function* makeBlobIterator(file, options = {}) {
async function readFileSlice(slice) {
return await new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onloadend = event => resolve(event.target.result);
fileReader.onloadend = event => resolve(event.target && event.target.result);
fileReader.onerror = error => reject(error);
fileReader.readAsArrayBuffer(slice);
});
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/lib/filesystems/browser-filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function readFileSlice(file, start, end) {
const slice = file.slice(start, end);
return await new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onloadend = event => resolve(event.target.result);
fileReader.onloadend = event => resolve(event.target && event.target.result);
fileReader.onerror = error => reject(error);
fileReader.readAsArrayBuffer(slice);
});
Expand Down
1 change: 1 addition & 0 deletions modules/core/test/lib/api/load.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ test('load#Blob(text) - BROWSER ONLY', async t => {
test('load#stream', async t => {
const response = await fetchFile(JSON_URL);
const stream = response.body;
// @ts-ignore
const data = await load(stream, JSONLoader);
t.equals(typeof data, 'object', 'load(stream) returned data');
t.end();
Expand Down
2 changes: 2 additions & 0 deletions modules/core/test/lib/api/select-loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ const URL_WITH_QUERYSTRING =
const DRACO_URL_QUERYSTRING = '@loaders.gl/draco/test/data/bunny.drc?query.string';

test('selectLoader#urls', async t => {
// @ts-ignore
t.throws(() => selectLoader(null), 'selectedLoader throws if no loader found');

t.equal(
// @ts-ignore
selectLoader('.', null, {nothrow: true}),
null,
'selectedLoader({nothrow: true}) returns null instead of throwing'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const modules = [
];

test('registerLoaders#isLoaderObject', t => {
// @ts-ignore
t.notOk(isLoaderObject(null), 'null is not a loader');

for (const module of modules) {
Expand Down
1 change: 1 addition & 0 deletions modules/csv/src/lib/async-iterator-streamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Ideally this can be contributed back to papaparse
// Or papaparse can expose Streamer API so we can extend without forking.

// @ts-nocheck
/* eslint-disable no-invalid-this */
/* global TextDecoder */

Expand Down
1 change: 1 addition & 0 deletions modules/csv/src/papaparse/async-iterator-streamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Ideally this can be contributed back to papaparse
// Or papaparse can expose Streamer API so we can extend without forking.

// @ts-nocheck
/* eslint-disable no-invalid-this */
/* global TextDecoder */

Expand Down
2 changes: 1 addition & 1 deletion modules/csv/test/csv-loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('CSVLoader#loader conformance', t => {

test('CSVLoader#load(states.csv)', async t => {
const response = await fetchFile(CSV_STATES_URL);
const rows = await load(response.body, CSVLoader);
const rows = await load(response, CSVLoader);
t.equal(rows.length, 111);
t.end();
});
Expand Down
2 changes: 2 additions & 0 deletions modules/gis/src/lib/geojson-to-binary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Convert GeoJSON features to flat binary arrays
// @ts-nocheck

export function geojsonToBinary(features, options = {}) {
const firstPassData = firstPass(features);
return secondPass(features, firstPassData, {
Expand Down
26 changes: 14 additions & 12 deletions modules/gis/test/geojson-to-binary.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-statements */
// @ts-nocheck
import test from 'tape-promise/tape';
import {fetchFile} from '@loaders.gl/core';
import {geojsonToBinary} from '@loaders.gl/gis';
Expand Down Expand Up @@ -386,6 +387,7 @@ test('gis#geojson-to-binary 3D features', async t => {
t.end();
});

// eslint-disable-next-line complexity
test('gis#geojson-to-binary position, featureId data types', async t => {
const response = await fetchFile(FEATURES_2D);
let {features} = await response.json();
Expand All @@ -402,18 +404,18 @@ test('gis#geojson-to-binary position, featureId data types', async t => {
const options = {PositionDataType: Float64Array};
const {points, lines, polygons} = geojsonToBinary(features, options);

t.ok(points.positions.value instanceof Float64Array);
t.ok(points.globalFeatureIds.value instanceof Uint32Array);
t.ok(points.featureIds.value instanceof Uint16Array);
t.ok(lines.positions.value instanceof Float64Array);
t.ok(lines.globalFeatureIds.value instanceof Uint32Array);
t.ok(lines.featureIds.value instanceof Uint16Array);
t.ok(lines.pathIndices.value instanceof Uint32Array);
t.ok(polygons.positions.value instanceof Float64Array);
t.ok(polygons.globalFeatureIds.value instanceof Uint32Array);
t.ok(polygons.featureIds.value instanceof Uint16Array);
t.ok(polygons.polygonIndices.value instanceof Uint32Array);
t.ok(polygons.primitivePolygonIndices.value instanceof Uint32Array);
t.ok(points && points.positions.value instanceof Float64Array);
t.ok(points && points.globalFeatureIds.value instanceof Uint32Array);
t.ok(points && points.featureIds.value instanceof Uint16Array);
t.ok(lines && lines.positions.value instanceof Float64Array);
t.ok(lines && lines.globalFeatureIds.value instanceof Uint32Array);
t.ok(lines && lines.featureIds.value instanceof Uint16Array);
t.ok(lines && lines.pathIndices.value instanceof Uint32Array);
t.ok(polygons && polygons.positions.value instanceof Float64Array);
t.ok(polygons && polygons.globalFeatureIds.value instanceof Uint32Array);
t.ok(polygons && polygons.featureIds.value instanceof Uint16Array);
t.ok(polygons && polygons.polygonIndices.value instanceof Uint32Array);
t.ok(polygons && polygons.primitivePolygonIndices.value instanceof Uint32Array);
t.end();
});

Expand Down
11 changes: 7 additions & 4 deletions modules/images/src/lib/category-api/parsed-image-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ export function getImageData(image) {
const canvas = document.createElement('canvas');
// TODO - reuse the canvas?
const context = canvas.getContext('2d');
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0);
return context.getImageData(0, 0, image.width, image.height);
if (context) {
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0);
return context.getImageData(0, 0, image.width, image.height);
}
// eslint-disable no-fallthrough
default:
return assert(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {getBinaryImageMetadata} from '../category-api/binary-image-api';
export function isBinaryImage(arrayBuffer, mimeType) {
const metadata = getBinaryImageMetadata(arrayBuffer);
if (mimeType) {
return Boolean(metadata) && metadata.mimeType === mimeType;
return Boolean(metadata && metadata.mimeType === mimeType);
}
// return true if any known type
return Boolean(metadata);
Expand All @@ -26,6 +26,6 @@ export function getBinaryImageSize(arrayBuffer, mimeType = null) {
};
}

mimeType = mimeType || (metadata && metadata.mimeType) || 'unknown';
mimeType = mimeType || 'unknown';
throw new Error(`invalid image data for type: ${mimeType}`);
}
1 change: 0 additions & 1 deletion modules/images/src/lib/encoders/encode-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ async function encodeImageInBrowser(image, options) {
drawImageToCanvas(image, canvas);

// The actual encoding is done asynchronously with `canvas.toBlob()`
/** @type {Blob} */
const blob = await new Promise((resolve, reject) => {
// get it back as a Blob
if (jpegQuality && qualityParamSupported) {
Expand Down
18 changes: 12 additions & 6 deletions modules/images/test/image-writer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ test('ImageWriter#write-and-read-image', async t => {
image: {mimeType: 'image/jpeg', jpegQuality: 90}
});
let metadata = getBinaryImageMetadata(arrayBuffer);
t.equal(metadata.width, IMAGE.width, 'encoded image width is correct');
t.equal(metadata.height, IMAGE.height, 'encoded image height is correct');
t.equal(metadata.mimeType, 'image/jpeg', 'encoded image mimeType is correct');
t.ok(metadata);
if (metadata) {
t.equal(metadata.width, IMAGE.width, 'encoded image width is correct');
t.equal(metadata.height, IMAGE.height, 'encoded image height is correct');
t.equal(metadata.mimeType, 'image/jpeg', 'encoded image mimeType is correct');
}

let image = await parse(arrayBuffer, ImageLoader, {image: {type: 'data'}});
t.deepEqual(image.width, IMAGE.width, 'encoded and parsed image widths are same');
Expand All @@ -30,9 +33,12 @@ test('ImageWriter#write-and-read-image', async t => {

arrayBuffer = await encode(IMAGE, ImageWriter, {image: {mimeType: 'image/png'}});
metadata = getBinaryImageMetadata(arrayBuffer);
t.equal(metadata.width, IMAGE.width, 'encoded image width is correct');
t.equal(metadata.height, IMAGE.height, 'encoded image height is correct');
t.equal(metadata.mimeType, 'image/png', 'encoded image mimeType is correct');
t.ok(metadata);
if (metadata) {
t.equal(metadata.width, IMAGE.width, 'encoded image width is correct');
t.equal(metadata.height, IMAGE.height, 'encoded image height is correct');
t.equal(metadata.mimeType, 'image/png', 'encoded image mimeType is correct');
}

image = await parse(arrayBuffer, ImageLoader, {image: {type: 'data'}});
t.deepEqual(image.width, IMAGE.width, 'encoded and parsed image widths are same');
Expand Down
11 changes: 7 additions & 4 deletions modules/images/test/lib/category-api/binary-image-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ test('getBinaryImageMetadata#mimeType', async t => {
const images = await loadImages();

for (const mimeType in images) {
const {mimeType: detectedMimeType} = getBinaryImageMetadata(images[mimeType]);
t.equals(detectedMimeType, mimeType, `getBinaryImageMetadata(${mimeType})`);
const metadata = getBinaryImageMetadata(images[mimeType]);
t.equals(metadata && metadata.mimeType, mimeType, `getBinaryImageMetadata(${mimeType})`);
}
t.end();
});
Expand All @@ -48,8 +48,11 @@ test('getBinaryImageMetadata#size', async t => {
const images = await loadImages();
for (const imageType in images) {
const dimensions = getBinaryImageMetadata(images[imageType]);
t.equals(dimensions.width, 480, `width, should work with ${imageType.toUpperCase()} files`);
t.equals(dimensions.height, 320, `height, should work with ${imageType.toUpperCase()} files`);
t.ok(dimensions, `got image metadata for ${imageType.toUpperCase()}`);
if (dimensions) {
t.equals(dimensions.width, 480, `width, should work with ${imageType.toUpperCase()} files`);
t.equals(dimensions.height, 320, `height, should work with ${imageType.toUpperCase()} files`);
}
}
t.end();
});
Expand Down
18 changes: 0 additions & 18 deletions modules/images/test/lib/load-image.worker.js

This file was deleted.

1 change: 0 additions & 1 deletion modules/json/src/geojson-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const GeoJSONWorkerLoader = {
},
*/
category: 'geometry',
testText: null,
text: true,
options: GeoJSONLoaderOptions
};
Expand Down
1 change: 0 additions & 1 deletion modules/json/src/json-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const JSONLoader = {
},
*/
category: 'table',
testText: null,
text: true,
parse,
parseTextSync,
Expand Down
1 change: 1 addition & 0 deletions modules/json/src/lib/parse-json-in-batches.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default async function* parseJSONInBatches(asyncIterator, options) {

const parser = new StreamingJSONParser({jsonpaths});
tableBatchBuilder =
// @ts-ignore
tableBatchBuilder || new TableBatchBuilder(TableBatchType, schema, {batchSize});

for await (const chunk of asyncIterator) {
Expand Down
3 changes: 3 additions & 0 deletions modules/json/src/lib/parser/json-parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// @ts-nocheck

import ClarinetParser from '../clarinet/clarinet';
import JSONPath from '../jsonpath/jsonpath';

// JSONParser builds a JSON object using the events emitted by the Clarinet parser

export default class JSONParser {
constructor() {
this.reset();
Expand Down
1 change: 1 addition & 0 deletions modules/json/src/lib/parser/streaming-json-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default class StreamingJSONParser extends JSONParser {
this.parser.onopenarray = () => {
if (!this.streamingArray) {
if (this._matchJSONPath()) {
// @ts-ignore
this.streamingJsonPath = this.getJsonPath().clone();
this.streamingArray = [];
this._openArray(this.streamingArray);
Expand Down
3 changes: 1 addition & 2 deletions modules/json/test/lib/parser/streaming-json-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ test('StreamingJSONParser#geojson', async t => {
// Can return text stream by setting `{encoding: 'utf8'}`, but only works on Node
const response = await fetchFile(GEOJSON_PATH, {highWaterMark: 16384});
// TODO - https requests under Node return a Promise
const body = await response.body;
for await (const chunk of makeIterator(body)) {
for await (const chunk of makeIterator(response)) {
const string = new TextDecoder().decode(chunk);
parser.write(string);
}
Expand Down
3 changes: 3 additions & 0 deletions modules/kml/src/lib/kml-to-geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function KMLtoGeoJson(kml, options) {
};
delete feature.properties.type;
delete feature.properties.coordinates;
// @ts-ignore
geojson.features.push(feature);
}

Expand All @@ -29,6 +30,7 @@ export default function KMLtoGeoJson(kml, options) {
};
delete feature.properties.type;
delete feature.properties.coordinates;
// @ts-ignore
geojson.features.push(feature);
}

Expand All @@ -43,6 +45,7 @@ export default function KMLtoGeoJson(kml, options) {
};
delete feature.properties.type;
delete feature.properties.coordinates;
// @ts-ignore
geojson.features.push(feature);
}

Expand Down
4 changes: 2 additions & 2 deletions modules/las/src/lib/laslaz-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ export class LASFile {
constructor(arraybuffer) {
this.arraybuffer = arraybuffer;

this.determineVersion();
if (this.version > 13) {
if (this.determineVersion() > 13) {
throw new Error('Only file versions <= 1.3 are supported at this time');
}

Expand Down Expand Up @@ -319,6 +318,7 @@ export class LASFile {
const ver = new Int8Array(this.arraybuffer, 24, 2);
this.version = ver[0] * 10 + ver[1];
this.versionAsString = `${ver[0]}.${ver[1]}`;
return this.version;
}

open() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function requireFromFile(filename) {
export function requireFromString(code, filename = '', options = {}) {
if (typeof filename === 'object') {
options = filename;
filename = undefined;
filename = '';
}

options = {
Expand Down
Loading

0 comments on commit 5df6102

Please sign in to comment.