-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
chore: migrate jest-haste-map to TypeScript #7854
Changes from 9 commits
ea9e546
3717a2e
e405c06
542f336
5e2774f
ef6dfdc
e7476ab
e4bb9fe
1d58364
8ee92dc
c0db70f
a191303
faeae4c
66eb178
05e4b8a
c19c4b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,53 +3,49 @@ | |
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {Path} from 'types/Config'; | ||
import type { | ||
import {Config} from '@jest/types'; | ||
import { | ||
DuplicatesSet, | ||
HTypeValue, | ||
ModuleMetaData, | ||
RawModuleMap, | ||
ModuleMapData, | ||
DuplicatesIndex, | ||
MockData, | ||
} from 'types/HasteMap'; | ||
} from './types'; | ||
|
||
import * as fastPath from './lib/fast_path'; | ||
import H from './constants'; | ||
|
||
const EMPTY_OBJ = {}; | ||
const EMPTY_OBJ = {} as {[key: string]: any}; | ||
const EMPTY_MAP = new Map(); | ||
|
||
export opaque type SerializableModuleMap = { | ||
// There is no easier way to extract the type of the entries of a Map | ||
duplicates: $Call< | ||
typeof Array.from, | ||
$Call<$PropertyType<DuplicatesIndex, 'entries'>>, | ||
>, | ||
map: $Call<typeof Array.from, $Call<$PropertyType<ModuleMapData, 'entries'>>>, | ||
mocks: $Call<typeof Array.from, $Call<$PropertyType<MockData, 'entries'>>>, | ||
rootDir: string, | ||
type ValueType<T> = T extends Map<string, infer V> ? V : never; | ||
|
||
export type SerializableModuleMap = { | ||
duplicates: ReadonlyArray<[string, ValueType<DuplicatesIndex>]>; | ||
map: ReadonlyArray<[string, ValueType<ModuleMapData>]>; | ||
mocks: ReadonlyArray<[string, ValueType<MockData>]>; | ||
rootDir: Config.Path; | ||
}; | ||
|
||
export default class ModuleMap { | ||
_raw: RawModuleMap; | ||
static DuplicateHasteCandidatesError: Class<DuplicateHasteCandidatesError>; | ||
private readonly _raw: RawModuleMap; | ||
static DuplicateHasteCandidatesError: typeof DuplicateHasteCandidatesError; | ||
|
||
constructor(raw: RawModuleMap) { | ||
this._raw = raw; | ||
} | ||
|
||
getModule( | ||
name: string, | ||
platform: ?string, | ||
supportsNativePlatform: ?boolean, | ||
type: ?HTypeValue, | ||
): ?Path { | ||
if (!type) { | ||
platform: string | null, | ||
supportsNativePlatform: boolean | null, | ||
type: HTypeValue | null, | ||
): Config.Path | null { | ||
if (type == null) { | ||
type = H.MODULE; | ||
} | ||
const module = this._getModuleMetadata( | ||
|
@@ -66,13 +62,13 @@ export default class ModuleMap { | |
|
||
getPackage( | ||
name: string, | ||
platform: ?string, | ||
supportsNativePlatform: ?boolean, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why we pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea, just wanted to comment about it. maybe we should start passing it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDK. @rubennorte? |
||
): ?Path { | ||
platform: string | null, | ||
_supportsNativePlatform: boolean | null, | ||
): Config.Path | null { | ||
return this.getModule(name, platform, null, H.PACKAGE); | ||
} | ||
|
||
getMockModule(name: string): ?Path { | ||
getMockModule(name: string): Config.Path | undefined { | ||
const mockPath = | ||
this._raw.mocks.get(name) || this._raw.mocks.get(name + '/index'); | ||
return mockPath && fastPath.resolve(this._raw.rootDir, mockPath); | ||
|
@@ -113,11 +109,11 @@ export default class ModuleMap { | |
* extra sure. If metadata exists both in the `duplicates` object and the | ||
* `map`, this would be a bug. | ||
*/ | ||
_getModuleMetadata( | ||
private _getModuleMetadata( | ||
name: string, | ||
platform: ?string, | ||
platform: string | null, | ||
supportsNativePlatform: boolean, | ||
): ?ModuleMetaData { | ||
): ModuleMetaData | null { | ||
const map = this._raw.map.get(name) || EMPTY_OBJ; | ||
const dupMap = this._raw.duplicates.get(name) || EMPTY_MAP; | ||
if (platform != null) { | ||
|
@@ -154,11 +150,11 @@ export default class ModuleMap { | |
return null; | ||
} | ||
|
||
_assertNoDuplicates( | ||
private _assertNoDuplicates( | ||
name: string, | ||
platform: string, | ||
supportsNativePlatform: boolean, | ||
relativePathSet: ?DuplicatesSet, | ||
relativePathSet: DuplicatesSet | null, | ||
) { | ||
if (relativePathSet == null) { | ||
return; | ||
|
@@ -180,7 +176,7 @@ export default class ModuleMap { | |
); | ||
} | ||
|
||
static create(rootDir: Path) { | ||
static create(rootDir: string) { | ||
return new ModuleMap({ | ||
duplicates: new Map(), | ||
map: new Map(), | ||
|
@@ -192,7 +188,7 @@ export default class ModuleMap { | |
|
||
class DuplicateHasteCandidatesError extends Error { | ||
hasteName: string; | ||
platform: ?string; | ||
platform: string | null; | ||
supportsNativePlatform: boolean; | ||
duplicatesSet: DuplicatesSet; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ | |
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
/* eslint-disable sort-keys */ | ||
|
||
|
@@ -16,7 +14,10 @@ | |
* This constant key map allows to keep the map smaller without having to build | ||
* a custom serialization library. | ||
*/ | ||
export default { | ||
|
||
import {HType} from './types'; | ||
|
||
const constants: HType = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicitly typing this ensure the type remains narrow rather than expanded to |
||
/* file map attributes */ | ||
ID: 0, | ||
MTIME: 1, | ||
|
@@ -37,3 +38,5 @@ export default { | |
GENERIC_PLATFORM: 'g', | ||
NATIVE_PLATFORM: 'native', | ||
}; | ||
|
||
export default constants; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the types are right, then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only do
getModule
(outside of unit tests) right below:return this.getModule(name, platform, null, H.PACKAGE);
So I wonder if it's never
null
either? May be that FB uses it internally though, who knows