Skip to content

Commit e1c59ba

Browse files
authored
Add prettier for better linting (#28)
* Add prettier for better linting * Review feedback
1 parent 4becf17 commit e1c59ba

File tree

7 files changed

+274
-124
lines changed

7 files changed

+274
-124
lines changed

.eslintrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"parser": "@typescript-eslint/parser",
33
"plugins": ["@typescript-eslint"],
44
"extends": [
5-
"plugin:@typescript-eslint/recommended"
5+
"plugin:@typescript-eslint/recommended",
6+
"prettier/@typescript-eslint",
7+
"plugin:prettier/recommended"
68
],
79
"rules": {
810
"@typescript-eslint/member-ordering": 2,

.prettierrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"printWidth": 100
5+
}

package.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"browser": "target/web/index.js",
77
"types": "target/index.d.ts",
88
"scripts": {
9-
"lint": "tsc --noEmit && yarn lint-es",
10-
"lint-es": "eslint --ext .ts,.tsx",
11-
"test": "yarn lint && jest",
9+
"prelint": "tsc --noEmit",
10+
"lint": "eslint . --ext .ts,.tsx",
11+
"pretest": "yarn lint",
12+
"test": "jest",
1213
"build": "tsc --emitDeclarationOnly && yarn build-web && yarn build-node",
1314
"build-web": "BABEL_ENV=web babel src --extensions \".ts,.tsx\" --config-file=./babel.config.js --out-dir=target/web --delete-dir-on-start",
1415
"build-node": "BABEL_ENV=node babel src --extensions \".ts,.tsx\" --config-file=./babel.config.js --out-dir=target/node --delete-dir-on-start"
@@ -28,6 +29,11 @@
2829
"url": "https://github.com/elastic/ems-client/issues"
2930
},
3031
"homepage": "https://github.com/elastic/ems-client#readme",
32+
"husky": {
33+
"hooks": {
34+
"pre-commit": "yarn test"
35+
}
36+
},
3137
"dependencies": {
3238
"lodash": "^4.17.15",
3339
"semver": "^6.3.0"
@@ -48,8 +54,12 @@
4854
"@typescript-eslint/parser": "^2.33.0",
4955
"babel-jest": "^24.9.0",
5056
"eslint": "^7.0.0",
57+
"eslint-config-prettier": "^6.11.0",
58+
"eslint-plugin-prettier": "^3.1.3",
59+
"husky": "^4.2.5",
5160
"jest": "^25.5.4",
5261
"node-fetch": "^1.7.3",
62+
"prettier": "^2.0.5",
5363
"ts-jest": "^25.5.1",
5464
"typescript": "^3.9.2"
5565
}

src/ems_client.ts

+91-91
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function modifyUrlLocal(
8484
}
8585

8686
const extendUrl = (url: string, props: URLMeaningfulParts) =>
87-
modifyUrlLocal(url, parsed => _.merge(parsed, props));
87+
modifyUrlLocal(url, (parsed) => _.merge(parsed, props));
8888

8989
/**
9090
* Unescape a url template that was escaped by encodeURI() so leaflet
@@ -272,16 +272,6 @@ export class EMSClient {
272272
return i18nObject[this._language] ? i18nObject[this._language] : i18nObject[DEFAULT_LANGUAGE];
273273
}
274274

275-
private _getEmsVersion(version: string | undefined): string {
276-
const userVersion = semver.valid(semver.coerce(version));
277-
const semverVersion = userVersion ? userVersion : semver.coerce(DEFAULT_EMS_VERSION);
278-
if (semverVersion) {
279-
return `v${semver.major(semverVersion)}.${semver.minor(semverVersion)}`;
280-
} else {
281-
throw new Error(`Invalid version: ${version}`);
282-
}
283-
}
284-
285275
/**
286276
* this internal method is overridden by the tests to simulate custom manifest.
287277
*/
@@ -301,6 +291,96 @@ export class EMSClient {
301291
}
302292
}
303293

294+
/**
295+
* Add optional query-parameters to all requests
296+
*
297+
* @param additionalQueryParams
298+
*/
299+
addQueryParams(additionalQueryParams: { [key: string]: string }): void {
300+
for (const key in additionalQueryParams) {
301+
if (additionalQueryParams.hasOwnProperty(key)) {
302+
if (additionalQueryParams[key] !== this._queryParams[key]) {
303+
//changes detected.
304+
this._queryParams = _.assign({}, this._queryParams, additionalQueryParams);
305+
this._invalidateSettings();
306+
break;
307+
}
308+
}
309+
}
310+
}
311+
312+
async getMainManifest(): Promise<EmsCatalogManifest> {
313+
return await this._getMainCatalog();
314+
}
315+
316+
async getDefaultFileManifest(): Promise<EmsFileCatalog> {
317+
return await this._getDefaultFileCatalog();
318+
}
319+
320+
async getDefaultTMSManifest(): Promise<EmsTmsCatalog> {
321+
return await this._getDefaultTMSCatalog();
322+
}
323+
324+
async getFileLayers(): Promise<FileLayer[]> {
325+
return await this._loadFileLayers();
326+
}
327+
328+
async getTMSServices(): Promise<TMSService[]> {
329+
return await this._loadTMSServices();
330+
}
331+
332+
getTileApiUrl(): string {
333+
return this._tileApiUrl;
334+
}
335+
336+
getFileApiUrl(): string {
337+
return this._fileApiUrl;
338+
}
339+
340+
getLandingPageUrl(): string {
341+
return this._emsLandingPageUrl;
342+
}
343+
344+
sanitizeHtml(html: string): string {
345+
return this._sanitizer(html);
346+
}
347+
348+
extendUrlWithParams(url: string): string {
349+
return unescapeTemplateVars(
350+
extendUrl(url, {
351+
query: this._queryParams,
352+
})
353+
);
354+
}
355+
356+
async findFileLayerById(id: string): Promise<FileLayer | undefined> {
357+
const fileLayers = await this.getFileLayers();
358+
for (let i = 0; i < fileLayers.length; i++) {
359+
if (fileLayers[i].hasId(id)) {
360+
return fileLayers[i];
361+
}
362+
}
363+
}
364+
365+
async findTMSServiceById(id: string): Promise<TMSService | undefined> {
366+
const tmsServices = await this.getTMSServices();
367+
for (let i = 0; i < tmsServices.length; i++) {
368+
if (tmsServices[i].hasId(id)) {
369+
return tmsServices[i];
370+
}
371+
}
372+
}
373+
374+
private _getEmsVersion(version: string | undefined): string {
375+
const userVersion = semver.valid(semver.coerce(version));
376+
const semverVersion = userVersion ? userVersion : semver.coerce(DEFAULT_EMS_VERSION);
377+
if (semverVersion) {
378+
return `v${semver.major(semverVersion)}.${semver.minor(semverVersion)}`;
379+
} else {
380+
throw new Error(`Invalid version: ${version}`);
381+
}
382+
}
383+
304384
private _fetchWithTimeout(url: string): Promise<Body> {
305385
return new Promise<Body>((resolve, reject) => {
306386
const timer = setTimeout(
@@ -320,24 +400,6 @@ export class EMSClient {
320400
});
321401
}
322402

323-
/**
324-
* Add optional query-parameters to all requests
325-
*
326-
* @param additionalQueryParams
327-
*/
328-
addQueryParams(additionalQueryParams: { [key: string]: string }): void {
329-
for (const key in additionalQueryParams) {
330-
if (additionalQueryParams.hasOwnProperty(key)) {
331-
if (additionalQueryParams[key] !== this._queryParams[key]) {
332-
//changes detected.
333-
this._queryParams = _.assign({}, this._queryParams, additionalQueryParams);
334-
this._invalidateSettings();
335-
break;
336-
}
337-
}
338-
}
339-
}
340-
341403
private async _getManifestWithParams<T>(url: string): Promise<T> {
342404
const extendedUrl = this.extendUrlWithParams(url);
343405
return await this.getManifest(extendedUrl);
@@ -417,66 +479,4 @@ export class EMSClient {
417479
}
418480
);
419481
}
420-
421-
async getMainManifest(): Promise<EmsCatalogManifest> {
422-
return await this._getMainCatalog();
423-
}
424-
425-
async getDefaultFileManifest(): Promise<EmsFileCatalog> {
426-
return await this._getDefaultFileCatalog();
427-
}
428-
429-
async getDefaultTMSManifest(): Promise<EmsTmsCatalog> {
430-
return await this._getDefaultTMSCatalog();
431-
}
432-
433-
async getFileLayers(): Promise<FileLayer[]> {
434-
return await this._loadFileLayers();
435-
}
436-
437-
async getTMSServices(): Promise<TMSService[]> {
438-
return await this._loadTMSServices();
439-
}
440-
441-
getTileApiUrl(): string {
442-
return this._tileApiUrl;
443-
}
444-
445-
getFileApiUrl(): string {
446-
return this._fileApiUrl;
447-
}
448-
449-
getLandingPageUrl(): string {
450-
return this._emsLandingPageUrl;
451-
}
452-
453-
sanitizeHtml(html: string): string {
454-
return this._sanitizer(html);
455-
}
456-
457-
extendUrlWithParams(url: string): string {
458-
return unescapeTemplateVars(
459-
extendUrl(url, {
460-
query: this._queryParams,
461-
})
462-
);
463-
}
464-
465-
async findFileLayerById(id: string): Promise<FileLayer | undefined> {
466-
const fileLayers = await this.getFileLayers();
467-
for (let i = 0; i < fileLayers.length; i++) {
468-
if (fileLayers[i].hasId(id)) {
469-
return fileLayers[i];
470-
}
471-
}
472-
}
473-
474-
async findTMSServiceById(id: string): Promise<TMSService | undefined> {
475-
const tmsServices = await this.getTMSServices();
476-
for (let i = 0; i < tmsServices.length; i++) {
477-
if (tmsServices[i].hasId(id)) {
478-
return tmsServices[i];
479-
}
480-
}
481-
}
482482
}

src/ems_service.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export abstract class AbstractEmsService implements IEmsService {
4444
}
4545

4646
getAttributions(): { url: string; label: string }[] {
47-
return this._config.attribution.map(attribution => {
47+
return this._config.attribution.map((attribution) => {
4848
const url = this._emsClient.getValueInLanguage(attribution.url);
4949
const label = this._emsClient.getValueInLanguage(attribution.label);
5050
return {
@@ -55,7 +55,7 @@ export abstract class AbstractEmsService implements IEmsService {
5555
}
5656

5757
getHTMLAttribution(): string {
58-
const attributions = this._config.attribution.map(attribution => {
58+
const attributions = this._config.attribution.map((attribution) => {
5959
const url = this._emsClient.getValueInLanguage(attribution.url);
6060
const label = this._emsClient.getValueInLanguage(attribution.label);
6161
const html = url ? `<a rel="noreferrer noopener" href="${url}">${label}</a>` : label;
@@ -65,7 +65,7 @@ export abstract class AbstractEmsService implements IEmsService {
6565
}
6666

6767
getMarkdownAttribution(): string {
68-
const attributions = this._config.attribution.map(attribution => {
68+
const attributions = this._config.attribution.map((attribution) => {
6969
const url = this._emsClient.getValueInLanguage(attribution.url);
7070
const label = this._emsClient.getValueInLanguage(attribution.label);
7171
return `[${label}](${url})`;
@@ -79,7 +79,7 @@ export abstract class AbstractEmsService implements IEmsService {
7979

8080
/**
8181
* Checks if url is absolute. If not, prepend the basePath.
82-
*/
82+
*/
8383
protected _getAbsoluteUrl = (url: string) => {
8484
if (/^https?:\/\//.test(url)) {
8585
return url;
@@ -95,6 +95,4 @@ export abstract class AbstractEmsService implements IEmsService {
9595
abstract hasId(id: string): boolean;
9696

9797
abstract getApiUrl(): string;
98-
99-
10098
}

src/file_layer.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class FileLayer extends AbstractEmsService {
3535
}
3636

3737
getFieldsInLanguage(): { type: string; name: string; description: string }[] {
38-
return this._config.fields.map(field => {
38+
return this._config.fields.map((field) => {
3939
return {
4040
type: field.type,
4141
name: field.id,
@@ -58,16 +58,6 @@ export class FileLayer extends AbstractEmsService {
5858
return this._config.layer_id === id || matchesLegacyId;
5959
}
6060

61-
private _getDefaultFormat(): EmsFileLayerFormatGeoJson | EmsFileLayerFormatTopoJson {
62-
const defaultFormat = this._config.formats.find(format => {
63-
return format.legacy_default;
64-
});
65-
if (defaultFormat) {
66-
return defaultFormat;
67-
}
68-
return this._config.formats[0];
69-
}
70-
7161
getEMSHotLink(): string {
7262
const landingPageString = this._emsClient.getLandingPageUrl();
7363
const urlObject = url.parse(landingPageString, true);
@@ -106,4 +96,14 @@ export class FileLayer extends AbstractEmsService {
10696
getApiUrl(): string {
10797
return this._emsClient.getFileApiUrl();
10898
}
99+
100+
private _getDefaultFormat(): EmsFileLayerFormatGeoJson | EmsFileLayerFormatTopoJson {
101+
const defaultFormat = this._config.formats.find((format) => {
102+
return format.legacy_default;
103+
});
104+
if (defaultFormat) {
105+
return defaultFormat;
106+
}
107+
return this._config.formats[0];
108+
}
109109
}

0 commit comments

Comments
 (0)