Skip to content

Commit

Permalink
Improve types for @ember/object
Browse files Browse the repository at this point in the history
Includes:
  - Object, { action } from '@ember/object'
  - CoreObject from '@ember/object/core'
  - Observable from '@ember/object/observable'

Note that types for `extend`, `reopen`, and `reopenClass` are not
exported. Also, `get` and `set` have naive types. All of these will
be deprecated in future releases.
  • Loading branch information
wagenet committed Apr 30, 2021
1 parent ba337bb commit 4e2d211
Show file tree
Hide file tree
Showing 27 changed files with 917 additions and 421 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {

overrides: [
{
files: ['*.ts'],
files: ['*.ts', '*.d.ts'],

extends: ['plugin:@typescript-eslint/recommended', 'prettier/@typescript-eslint'],

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-qunit": "^4.0.0",
"execa": "^2.0.4",
"expect-type": "^0.11.0",
"express": "^4.17.1",
"finalhandler": "^1.1.2",
"fs-extra": "^9.0.1",
Expand All @@ -147,7 +148,8 @@
"simple-dom": "^1.4.0",
"testem": "^3.1.0",
"testem-failure-only-reporter": "^0.0.1",
"tslint": "^5.20.1"
"tslint": "^5.20.1",
"typescript": "^4.2.4"
},
"engines": {
"node": "10.* || >= 12.*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,13 @@ moduleFor(
}

'@test positional parameters are not allowed'() {
let TestComponent = class extends Component {};
TestComponent.reopenClass({
positionalParams: ['first', 'second'],
});

this.registerComponent('sample-component', {
ComponentClass: Component.extend().reopenClass({
positionalParams: ['first', 'second'],
}),
ComponentClass: TestComponent,
template: '{{this.first}}{{this.second}}',
});

Expand Down
10 changes: 9 additions & 1 deletion packages/@ember/-internals/metal/lib/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,15 @@ function mergeMixins(
}
}
} else {
mergeProps(meta, currentMixin, descs, values, base, keys, keysWithSuper);
mergeProps(
meta,
currentMixin as { [key: string]: any },
descs,
values,
base,
keys,
keysWithSuper
);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/@ember/-internals/routing/lib/location/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface EmberLocation {
formatURL(url: string): string;
detect?(): void;
initState?(): void;
destroy(): void;
}

export type UpdateCallback = (url: string) => void;
Expand Down
143 changes: 84 additions & 59 deletions packages/@ember/-internals/routing/lib/location/auto_location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,94 @@ import {
@protected
*/
export default class AutoLocation extends EmberObject implements EmberLocation {
cancelRouterSetup?: boolean | undefined;
getURL!: () => string;
setURL!: (url: string) => void;
onUpdateURL!: (callback: UpdateCallback) => void;
formatURL!: (url: string) => string;

concreteImplementation?: EmberLocation;

implementation = 'auto';

// FIXME: This is never set
// See https://github.com/emberjs/ember.js/issues/19515
documentMode: number | undefined;

/**
@private
Will be pre-pended to path upon state change.
@since 1.5.1
@property rootURL
@default '/'
*/
// Added in reopen to allow overriding via extend
rootURL!: string;

/**
@private
The browser's `location` object. This is typically equivalent to
`window.location`, but may be overridden for testing.
@property location
@default environment.location
*/
// Added in reopen to allow overriding via extend
location!: any;

/**
@private
The browser's `history` object. This is typically equivalent to
`window.history`, but may be overridden for testing.
@since 1.5.1
@property history
@default environment.history
*/
// Added in reopen to allow overriding via extend
history!: any;

/**
@private
The user agent's global variable. In browsers, this will be `window`.
@since 1.11
@property global
@default window
*/
// Added in reopen to allow overriding via extend
global!: any;

/**
@private
The browser's `userAgent`. This is typically equivalent to
`navigator.userAgent`, but may be overridden for testing.
@since 1.5.1
@property userAgent
@default environment.history
*/
// Added in reopen to allow overriding via extend
userAgent!: any;

/**
@private
This property is used by the router to know whether to cancel the routing
setup process, which is needed while we redirect the browser.
@since 1.5.1
@property cancelRouterSetup
@default false
*/
// Added in reopen to allow overriding via extend
cancelRouterSetup!: boolean | undefined;

/**
Called by the router to instruct the location to do any feature detection
necessary. In the case of AutoLocation, we detect whether to use history
Expand Down Expand Up @@ -115,79 +196,23 @@ export default class AutoLocation extends EmberObject implements EmberLocation {
}

AutoLocation.reopen({
/**
@private
Will be pre-pended to path upon state change.
@since 1.5.1
@property rootURL
@default '/'
*/
rootURL: '/',

initState: delegateToConcreteImplementation('initState'),
getURL: delegateToConcreteImplementation('getURL'),
setURL: delegateToConcreteImplementation('setURL'),
replaceURL: delegateToConcreteImplementation('replaceURL'),
onUpdateURL: delegateToConcreteImplementation('onUpdateURL'),
formatURL: delegateToConcreteImplementation('formatURL'),

/**
@private
The browser's `location` object. This is typically equivalent to
`window.location`, but may be overridden for testing.
@property location
@default environment.location
*/
location: location,

/**
@private
The browser's `history` object. This is typically equivalent to
`window.history`, but may be overridden for testing.
@since 1.5.1
@property history
@default environment.history
*/
history: history,

/**
@private
The user agent's global variable. In browsers, this will be `window`.
@since 1.11
@property global
@default window
*/
global: window,

/**
@private
The browser's `userAgent`. This is typically equivalent to
`navigator.userAgent`, but may be overridden for testing.
@since 1.5.1
@property userAgent
@default environment.history
*/
userAgent: userAgent,

/**
@private
This property is used by the router to know whether to cancel the routing
setup process, which is needed while we redirect the browser.
@since 1.5.1
@property cancelRouterSetup
@default false
*/
cancelRouterSetup: false,
});

Expand All @@ -196,7 +221,7 @@ function delegateToConcreteImplementation(methodName: string) {
let { concreteImplementation } = this;
assert(
"AutoLocation's detect() method should be called before calling any other hooks.",
Boolean(concreteImplementation)
concreteImplementation
);
return concreteImplementation[methodName]?.(...args);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export default class HashLocation extends EmberObject implements EmberLocation {
implementation = 'hash';
_hashchangeHandler?: EventListener;

private _location?: any;
declare location: any;

init(): void {
set(this, 'location', this._location || window.location);
this._hashchangeHandler = undefined;
Expand Down Expand Up @@ -114,6 +117,8 @@ export default class HashLocation extends EmberObject implements EmberLocation {
set(this, 'lastSetURL', path);
}

lastSetURL: string | null = null;

/**
Register a callback to be invoked when the hash changes. These
callbacks will execute when the user presses the back or forward
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ function _uuid() {
@protected
*/
export default class HistoryLocation extends EmberObject implements EmberLocation {
location!: any;
baseURL!: string;

history?: any;

implementation = 'history';
_previousURL?: string;
_popstateHandler?: EventListener;
Expand Down Expand Up @@ -86,9 +91,9 @@ export default class HistoryLocation extends EmberObject implements EmberLocatio
this._super(...arguments);

let base = document.querySelector('base');
let baseURL: string | null = '';
let baseURL = '';
if (base !== null && base.hasAttribute('href')) {
baseURL = base.getAttribute('href');
baseURL = base.getAttribute('href') ?? '';
}

set(this, 'baseURL', baseURL);
Expand Down
21 changes: 13 additions & 8 deletions packages/@ember/-internals/routing/lib/location/none_location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ export default class NoneLocation extends EmberObject implements EmberLocation {
updateCallback!: UpdateCallback;
implementation = 'none';

// Set in reopen so it can be overwritten with extend
path!: string;

/**
Will be pre-pended to path.
@private
@property rootURL
@default '/'
*/
// Set in reopen so it can be overwritten with extend
rootURL!: string;

detect(): void {
let { rootURL } = this;

Expand Down Expand Up @@ -114,13 +127,5 @@ export default class NoneLocation extends EmberObject implements EmberLocation {

NoneLocation.reopen({
path: '',

/**
Will be pre-pended to path.
@private
@property rootURL
@default '/'
*/
rootURL: '/',
});
Loading

0 comments on commit 4e2d211

Please sign in to comment.