Skip to content

Commit c022c36

Browse files
huntiefacebook-github-bot
authored andcommitted
Add and configure TypeScript, migrate .d.ts files for metro-cache
Summary: Configures repository to support TypeScript definitions inside each package directory under `types/`. This is an interim structure which preserves the current types and paths from DefinitelyTyped. In future, we may shift to inline definitions, or (more likely) auto-translated types when the [flow-api-translator](https://www.npmjs.com/package/flow-api-translator) tool is more mature. - Add `[email protected]` and configure project file. - Add step to `scripts/build.js` to copy TypeScript definitions (`.d.ts`) across to distributed code. - Add `yarn typecheck-ts` script and add to CircleCI. - Add and configure `typescript-eslint` against `types/` directories. - Migrates TypeScript definitions for `metro-cache` (self-contained package types), from DefinitelyTyped, used to validate all changes. Changelog: **[Types]** Add bundled TypeScript definitions (partial) for metro-cache Reviewed By: motiz88 Differential Revision: D44133264 fbshipit-source-id: b9b56cd751670964b57400bd9c63155349f78e81
1 parent b752459 commit c022c36

File tree

16 files changed

+352
-4
lines changed

16 files changed

+352
-4
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757
- checkout
5858
- yarn_install
5959
- run: yarn typecheck
60+
- run: yarn typecheck-ts
6061
- run: yarn lint
6162
- run: yarn test-smoke
6263

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ docs/
77
examples/react-native
88
flow-typed/**
99
packages/*/build/**
10-
types/**
1110
website/
1211
**/third-party/**

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
'use strict';
1212

13-
/** @type {import('eslint').Linter.Config} */
1413
module.exports = {
1514
extends: './scripts/eslint/base',
1615
overrides: [
@@ -21,6 +20,10 @@ module.exports = {
2120
'lint/flow-function-shape': 'off',
2221
},
2322
},
23+
{
24+
files: ['packages/*/types/**/*.d.ts'],
25+
extends: './scripts/eslint/typescript',
26+
},
2427
{
2528
files: ['packages/metro-source-map/**/*.js'],
2629
rules: {

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"@babel/plugin-syntax-class-properties": "^7.0.0",
66
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
77
"@babel/plugin-transform-modules-commonjs": "^7.0.0",
8+
"@tsconfig/node16": "1.0.1",
9+
"@typescript-eslint/eslint-plugin": "^5.30.5",
10+
"@typescript-eslint/parser": "^5.30.5",
811
"acorn": "^8.7.1",
912
"babel-jest": "^29.2.1",
1013
"chalk": "^4.0.0",
@@ -34,7 +37,8 @@
3437
"micromatch": "^4.0.4",
3538
"prettier": "2.7.1",
3639
"progress": "^2.0.0",
37-
"promise": "^8.3.0"
40+
"promise": "^8.3.0",
41+
"typescript": "4.1.3"
3842
},
3943
"scripts": {
4044
"build-clean": "rm -rf ./packages/*/build",
@@ -46,10 +50,11 @@
4650
"postpublish": "yarn workspaces run cleanup-release",
4751
"publish": "yarn run build-clean && yarn run build && yarn workspaces run prepare-release && npm publish --workspaces",
4852
"start": "node packages/metro/src/cli",
53+
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest",
4954
"test-coverage": "yarn run build && yarn run jest --coverage -i && node scripts/mapCoverage.js",
5055
"test-smoke": "yarn start build --config packages/metro/src/integration_tests/metro.config.js TestBundle.js --out /tmp/TestBundle",
51-
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest",
5256
"typecheck": "flow check",
57+
"typecheck-ts": "tsc --project tsconfig.json",
5358
"update-version": "node ./scripts/updateVersion"
5459
},
5560
"workspaces": [

packages/metro-cache/types/Cache.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall react_native
9+
*/
10+
11+
import {CacheStore} from './types';
12+
13+
/**
14+
* Main cache class. Receives an array of cache instances, and sequentially
15+
* traverses them to return a previously stored value. It also ensures setting
16+
* the value in all instances.
17+
*
18+
* All get/set operations are logged via Metro's logger.
19+
*/
20+
export default class Cache<T> {
21+
constructor(stores: ReadonlyArray<CacheStore<T>>);
22+
get(key: Buffer): Promise<T | null>;
23+
set(key: Buffer, value: T): void;
24+
get isDisabled(): boolean;
25+
}

packages/metro-cache/types/index.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall react_native
9+
*/
10+
11+
// <reference types="node" />
12+
13+
import AutoCleanFileStore from './stores/AutoCleanFileStore';
14+
import FileStore from './stores/FileStore';
15+
import HttpGetStore from './stores/HttpGetStore';
16+
import HttpStore from './stores/HttpStore';
17+
import Cache from './Cache';
18+
import stableHash from './stableHash';
19+
20+
export type {Options as FileOptions} from './stores/FileStore';
21+
export type {Options as HttpOptions} from './stores/HttpStore';
22+
export type {CacheStore} from './types';
23+
24+
export interface MetroCache {
25+
AutoCleanFileStore: typeof AutoCleanFileStore;
26+
Cache: typeof Cache;
27+
FileStore: typeof FileStore;
28+
HttpGetStore: typeof HttpGetStore;
29+
HttpStore: typeof HttpStore;
30+
stableHash: typeof stableHash;
31+
}
32+
33+
export {
34+
AutoCleanFileStore,
35+
Cache,
36+
FileStore,
37+
HttpGetStore,
38+
HttpStore,
39+
stableHash,
40+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall react_native
9+
*/
10+
11+
export default function stableHash(value: unknown): Buffer;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall react_native
9+
*/
10+
11+
import type FileStore from './FileStore';
12+
13+
export default class AutoCleanFileStore<T> extends FileStore<T> {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall react_native
9+
*/
10+
11+
export interface Options {
12+
root: string;
13+
}
14+
15+
export default class FileStore<T> {
16+
constructor(options: Options);
17+
get(key: Buffer): Promise<T | null>;
18+
set(key: Buffer, value: T): Promise<void>;
19+
clear(): void;
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall react_native
9+
*/
10+
11+
import type {Options} from './HttpStore';
12+
13+
export default class HttpGetStore<T> {
14+
constructor(options: Options);
15+
get(key: Buffer): Promise<T | null>;
16+
set(key: Buffer, value: T): Promise<void>;
17+
clear(): void;
18+
}

0 commit comments

Comments
 (0)