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

Implement demo of using JSDoc to support type checking js #2502

Open
wants to merge 1 commit into
base: multiple_maps
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
}
],
"scripts": {
"test": "jest && npm run eslint",
"check-types": "tsc",
"test": "npm run check-types && npm run jest && npm run eslint",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add type checking as part of the test process.

"jest": "jest",
"test:watch": "jest --watch",
"eslint": "node node_modules/eslint/bin/eslint www src"
Expand All @@ -55,8 +56,10 @@
},
"homepage": "https://github.com/mapsplugin/cordova-plugin-googlemaps",
"devDependencies": {
"@types/googlemaps": "^3.30.16",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will make sure we are passing the appropriate params to google maps api.

"cordova-js": "^4.2.4",
"eslint": "^5.7.0",
"jest": "^23.6.0"
"jest": "^23.6.0",
"typescript": "^3.1.6"
}
}
8 changes: 8 additions & 0 deletions src/browser/PluginGeocoder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


// @ts-ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed because of cordova custom module resolution

var BaseArrayClass = require('cordova-plugin-googlemaps.BaseArrayClass');

var geocoder = null;
Expand Down Expand Up @@ -146,6 +147,12 @@ QUEUE.on('next', function() {
});

module.exports = {
/**
*
* @param {(result: GeocoderResult[]) => void} onSuccess
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This provides type checking by using JsDoc format.

* @param {(err: string) => void} onError
* @param {[GeocoderRequest]} args
*/
'geocode': function(onSuccess, onError, args) {
var request = args[0];
var geocoderRequest = {};
Expand Down Expand Up @@ -176,4 +183,5 @@ module.exports = {
};


// @ts-ignore
require('cordova/exec/proxy').add('PluginGeocoder', module.exports);
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"allowJs": true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

allowJs and checkJs are what enable type checking. noEmit is added because we don't actually want to compile the files.

"checkJs": true,
"noEmit": true,
"esModuleInterop": true
},
"include": [
"src/browser/PluginGeocoder.js",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add sources as we go to incrementally type the project over time.

"typings/**/*.d.ts"
],
}
69 changes: 69 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
interface ILatLng {
lat: number;
lng: number;
}

interface ILatLngBounds {
northeast: ILatLng;
southwest: ILatLng;
}

interface GeocoderRequest {
/**
* The address property or position property is required.
* You can not specify both property at the same time.
*
* [geocoding usage1]
* let request: GeocoderRequest = {
* address: "Los Angeles, California, USA"
* }
*
* [geocoding usage2]
* let request: GeocoderRequest = {
* address: [
* "Los Angeles, California, USA",
* "San Francisco, California, USA",
* ]
* }
*/
address?: string | string[];
/**
*
* [reverse-geocoding usage1]
* let request: GeocoderRequest = {
* position: {"lat": 37.421655, "lng": -122.085637}
* }
*
* [reverse-geocoding usage2]
* let request: GeocoderRequest = {
* position: [
* {"lat": 37.421655, "lng": -122.085637},
* {"lat": 37.332, "lng": -122.030781}
* ]
* }
*/
position?: ILatLng | ILatLng[];

bounds?: ILatLng | ILatLng[];
}

interface GeocoderResult {
adminArea?: string;
country?: string;
countryCode?: string;
extra?: {
featureName?: string;
lines?: string[];
permises?: string;
phone?: string;
url?: string;
};
locale?: string;
locality?: string;
position?: ILatLng;
postalCode?: string;
subAdminArea?: string;
subLocality?: string;
subThoroughfare?: string;
thoroughfare?: string;
}