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

enable IDE auto-completion for the main classes #10618

Open
wants to merge 1 commit into
base: develop
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
4 changes: 2 additions & 2 deletions modules/core/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { utilKeybinding, utilRebind, utilStringQs, utilCleanOsmString } from '..

export function coreContext() {
const dispatch = d3_dispatch('enter', 'exit', 'change');
let context = utilRebind({}, dispatch, 'on');
const context = {};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This works because JS autocompletion is powered by TypeScript, which is smart enough to infer the type of context when defined as an empty object

proof here

utilRebind can safely be moved to the return statement because this class does not use context.on internally.

let _deferred = new Set();

context.version = packageJSON.version;
Expand Down Expand Up @@ -591,5 +591,5 @@ export function coreContext() {
}
};

return context;
return utilRebind(context, dispatch, 'on');
}
4 changes: 2 additions & 2 deletions modules/core/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function coreUploader(context) {
.then(function(d) { _discardTags = d; })
.catch(function() { /* ignore */ });

var uploader = utilRebind({}, dispatch, 'on');
const uploader = {};

uploader.isSaving = function() {
return _isSaving;
Expand Down Expand Up @@ -402,5 +402,5 @@ export function coreUploader(context) {
};


return uploader;
return utilRebind(uploader, dispatch, 'on');
}
4 changes: 2 additions & 2 deletions modules/core/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as Validations from '../validations/index';

export function coreValidator(context) {
let dispatch = d3_dispatch('validated', 'focusedIssue');
let validator = utilRebind({}, dispatch, 'on');
const validator = {};

let _rules = {};
let _disabledRules = {};
Expand Down Expand Up @@ -773,7 +773,7 @@ export function coreValidator(context) {
}


return validator;
return utilRebind(validator, dispatch, 'on');
}


Expand Down
24 changes: 24 additions & 0 deletions modules/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ declare global {
declare var before: typeof beforeEach;
declare var after: typeof afterEach;
declare var VITEST: true;

declare type Tags = { [key: string]: string };

declare namespace iD {
export type Context = ReturnType<typeof iD.coreContext>;

export type Graph = InstanceType<typeof iD.coreGraph>;

export type OsmNode = import('./osm/node').OsmNode;
export type OsmWay = import('./osm/way').OsmWay;
export type OsmRelation = import('./osm/relation').OsmRelation;

export type AbstractEntity = InstanceType<typeof iD.osmEntity>;
export type OsmEntity = OsmNode | OsmWay | OsmRelation;
}

declare namespace d3 {
export type Selection = import('d3').Selection<
unknown,
unknown,
unknown,
unknown
>;
}
}

export {};
1 change: 1 addition & 0 deletions modules/osm/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ osmEntity.deprecatedTagValuesByKey = function(dataDeprecated) {

osmEntity.prototype = {

/** @type {Tags} */
tags: {},


Expand Down
9 changes: 7 additions & 2 deletions modules/osm/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const cardinal = {
northnorthwest: 337, nnw: 337
};

/**
* @typedef {typeof prototype & iD.AbstractEntity} OsmNode
* @returns {OsmNode}
*/
export function osmNode() {
if (!(this instanceof osmNode)) {
return (new osmNode()).initialize(arguments);
Expand All @@ -33,7 +37,7 @@ osmEntity.node = osmNode;

osmNode.prototype = Object.create(osmEntity.prototype);

Object.assign(osmNode.prototype, {
const prototype = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is required so we can use typeof prototype in the JSDoc comment above. No runtime impact.

type: 'node',
loc: [9999, 9999],

Expand Down Expand Up @@ -243,4 +247,5 @@ Object.assign(osmNode.prototype, {
coordinates: this.loc
};
}
});
};
Object.assign(osmNode.prototype, prototype);
10 changes: 7 additions & 3 deletions modules/osm/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { osmEntity } from './entity';
import { osmJoinWays } from './multipolygon';
import { geoExtent, geoPolygonContainsPolygon, geoPolygonIntersectsPolygon } from '../geo';


/**
* @typedef {typeof prototype & iD.AbstractEntity} OsmRelation
* @returns {OsmRelation}
*/
export function osmRelation() {
if (!(this instanceof osmRelation)) {
return (new osmRelation()).initialize(arguments);
Expand All @@ -28,7 +31,7 @@ osmRelation.creationOrder = function(a, b) {
};


Object.assign(osmRelation.prototype, {
const prototype = {
type: 'relation',
members: [],

Expand Down Expand Up @@ -363,4 +366,5 @@ Object.assign(osmRelation.prototype, {

return result;
}
});
};
Object.assign(osmRelation.prototype, prototype);
10 changes: 7 additions & 3 deletions modules/osm/way.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { osmLanes } from './lanes';
import { osmTagSuggestingArea, osmOneWayTags, osmRightSideIsInsideTags, osmRemoveLifecyclePrefix } from './tags';
import { utilArrayUniq } from '../util';


/**
* @typedef {typeof prototype & iD.AbstractEntity} OsmWay
* @returns {OsmWay}
*/
export function osmWay() {
if (!(this instanceof osmWay)) {
return (new osmWay()).initialize(arguments);
Expand All @@ -21,7 +24,7 @@ osmEntity.way = osmWay;
osmWay.prototype = Object.create(osmEntity.prototype);


Object.assign(osmWay.prototype, {
const prototype = {
type: 'way',
nodes: [],

Expand Down Expand Up @@ -533,7 +536,8 @@ Object.assign(osmWay.prototype, {
return isNaN(area) ? 0 : area;
});
}
});
};
Object.assign(osmWay.prototype, prototype);


// Filter function to eliminate consecutive duplicates.
Expand Down
4 changes: 2 additions & 2 deletions modules/renderer/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { utilArrayGroupBy, utilArrayUnion, utilQsString, utilStringQs } from '..

export function rendererFeatures(context) {
var dispatch = d3_dispatch('change', 'redraw');
var features = utilRebind({}, dispatch, 'on');
const features = {};
var _deferred = new Set();

var traffic_roads = {
Expand Down Expand Up @@ -616,5 +616,5 @@ export function rendererFeatures(context) {
});


return features;
return utilRebind(features, dispatch, 'on');
}
16 changes: 12 additions & 4 deletions modules/util/rebind.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// Copies a variable number of methods from source to target.
export function utilRebind(target, source) {
var i = 1, n = arguments.length, method;
while (++i < n) {
target[method = arguments[i]] = d3_rebind(target, source, source[method]);
/**
* @template T
* @template S
* @template {keyof S} Args
* @param {T} target
* @param {S} source
* @param {...Args} args
* @returns {T & Pick<S, Args>}
*/
export function utilRebind(target, source, ...args) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this had to be refactored to use spread arguments instead of the arguments keyword, because JSDoc doesn't support the arguments keyword.

there's a new unit test to confirm that this is still correctly bound.

for (const method of args) {
target[method] = d3_rebind(target, source, source[method]);
}
return target;
}
Expand Down
19 changes: 19 additions & 0 deletions test/spec/util/rebind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
describe('utilRebind', () => {
it('copies methods from source to target', () => {
const target = { original: 123 };
const source = new class {
value = 456;
method() {
return this.value;
}
};

const copied = iD.utilRebind(target, source, 'method');

expect(copied).toStrictEqual({
original: 123,
method: expect.any(Function)
});
expect(copied.method()).toBe(456);
});
});
Loading