Skip to content

Commit

Permalink
Merge pull request #19517 from wagenet/types
Browse files Browse the repository at this point in the history
Improve internal types
  • Loading branch information
wagenet authored May 24, 2021
2 parents d28a09c + f5f8213 commit 802476f
Show file tree
Hide file tree
Showing 27 changed files with 1,292 additions and 812 deletions.
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.1.0",
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 Record<string, unknown>,
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
152 changes: 89 additions & 63 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,95 @@ 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;
declare getURL: () => string;
declare setURL: (url: string) => void;
declare onUpdateURL: (callback: UpdateCallback) => void;
declare formatURL: (url: string) => string;

concreteImplementation?: EmberLocation;

implementation = 'auto';

// FIXME: This is never set
// See https://github.com/emberjs/ember.js/issues/19515
/** @internal */
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
declare 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
declare 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
*/
// Added in reopen to allow overriding via extend
declare 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
declare 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
declare userAgent: string;

/**
@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
declare cancelRouterSetup: boolean;

/**
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 +197,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 +222,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?: Location;
declare location: Location;

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 {
declare location: Location;
declare 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
23 changes: 14 additions & 9 deletions packages/@ember/-internals/routing/lib/location/none_location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,22 @@ import { EmberLocation, UpdateCallback } from './api';
@protected
*/
export default class NoneLocation extends EmberObject implements EmberLocation {
updateCallback!: UpdateCallback;
declare updateCallback: UpdateCallback;
implementation = 'none';

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

/**
Will be pre-pended to path.
@private
@property rootURL
@default '/'
*/
// Set in reopen so it can be overwritten with extend
declare 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 802476f

Please sign in to comment.