Skip to content

Commit

Permalink
refactor: flow -> ts (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <[email protected]>
  • Loading branch information
agilgur5 and nicolo-ribaudo authored Apr 7, 2022
1 parent 3d5e9d3 commit 4f49c9c
Show file tree
Hide file tree
Showing 39 changed files with 902 additions and 549 deletions.
11 changes: 10 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
"env": { "node": true },
"overrides": [
{
"files": ["packages/*/src/**/*.js"],
"files": ["**/*.ts"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error"
}
},
{
"files": ["packages/*/src/**/*.{js,ts}"],
"rules": {
"@babel/development/no-undefined-identifier": "error",
"@babel/development/no-deprecated-clone": "error",
Expand Down
13 changes: 0 additions & 13 deletions .flowconfig

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ jobs:
run: yarn install
- name: Lint
run: yarn lint
- name: Flow
run: yarn flow
- name: TSCheck
run: yarn tscheck
- name: Check compat-data
run: yarn build-es-shims-data
env:
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"printWidth": 80,
"overrides": [
{
"files": ["**/packages/*/src/**/*.js", "**/packages/*/test/**/*.js"],
"files": ["**/packages/*/src/**/*.{js,ts}", "**/packages/*/test/**/*.{js,ts}"],
"parser": "babylon",
"options": {
"trailingComma": "all"
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $ yarn lint
#### Type-check

```sh
$ yarn flow
$ yarn tscheck
```

#### Test
Expand Down
6 changes: 3 additions & 3 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function build() {
const base = path.join(__dirname, "packages");

return gulp
.src("./packages/*/src/**/*.js", { base: base })
.src("./packages/*/src/**/*.{js,ts}", { base: base })
.pipe(errorsLogger())
.pipe(newer({ dest: base, map: swapSrcWithLib }))
.pipe(compilationLogger())
Expand Down Expand Up @@ -85,7 +85,7 @@ async function buildRollup() {
};

const bundle = await rollup({
input: `${dir}/src/index.js`,
input: `${dir}/src/index.ts`,
external,
plugins: [
rollupJson(),
Expand Down Expand Up @@ -127,7 +127,7 @@ gulp.task(
"watch",
gulp.series("build", function watch() {
gulpWatch(
"./packages/*/src/**/*.js",
"./packages/*/src/**/*.{js,ts}",
{ debounceDelay: 200 },
gulp.task("build")
);
Expand Down
7 changes: 5 additions & 2 deletions babel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"sourceType": "unambiguous",
"presets": [
["@babel/preset-env", { "loose": true }],
"@babel/preset-flow"
"@babel/preset-typescript"
],
"plugins": [
"./scripts/babel-plugin-remove-create-require",
"./scripts/babel-plugin-compat-core-import"
],
"plugins": ["./scripts/babel-plugin-remove-create-require"],
"targets": { "node": "current" },
"env": {
"production": {
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"build-es-shims-data": "./scripts/download-compat-table.sh && node ./scripts/build-es-shims-data",
"clean": "rimraf packages/*/lib packages/*/esm",
"clean-all": "yarn clean && rimraf packages/*/node_modules node_modules",
"flow": "flow check --strip-root",
"lint": "eslint packages '*.js' --format=codeframe",
"lint": "eslint packages '*.{js,ts}' --ext .js,.ts --format=codeframe",
"publish": "yarn clean && NODE_ENV=production yarn build && yarn validate && lerna publish from-git",
"test": "jest && yarn test:esm",
"test:esm": "node test/esm/index.mjs",
"validate": "yarn lint && yarn flow && yarn test",
"tscheck": "yarn tsc",
"validate": "yarn lint && yarn tscheck && yarn test",
"watch": "gulp watch"
},
"devDependencies": {
Expand All @@ -23,11 +23,14 @@
"@babel/eslint-plugin-development": "^7.17.7",
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/preset-env": "^7.16.11",
"@babel/preset-flow": "^7.16.7",
"@babel/preset-typescript": "^7.16.7",
"@babel/runtime": "^7.17.8",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.2.0",
"@types/babel__traverse": "^7.14.2",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"babel-jest": "^24.8.0",
"chalk": "^3.0.0",
"electron-to-chromium": "^1.3.683",
Expand All @@ -37,7 +40,6 @@
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-prettier": "^3.1.0",
"fancy-log": "^1.3.3",
"flow-bin": "^0.107.0",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-newer": "^1.0.0",
Expand All @@ -51,7 +53,8 @@
"prettier": "^2.6.1",
"rimraf": "^2.6.3",
"rollup": "^2.42.3",
"through2": "^2.0.0"
"through2": "^2.0.0",
"typescript": "^4.6.3"
},
"resolutions": {
"@lerna/npm-publish": "patch:@lerna/npm-publish@npm:3.18.5#.yarn-patches/@lerna/npm-publish.patch",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

export function resolve(
dirname: string,
moduleName: string,
Expand All @@ -12,13 +10,13 @@ export function resolve(
);
}

// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function has(basedir: string, name: string) {
return true;
}

// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function logMissing(missingDeps: Set<string>) {}

// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function laterLogMissing(missingDeps: Set<string>) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import { prettifyTargets } from "@babel/helper-compilation-targets";

import type { Targets } from "./types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// @flow

import type { PolyfillProviderInternal, PolyfillProvider } from "./types";

export function defineProvider<Options>(
factory: PolyfillProvider<Options>,
): PolyfillProviderInternal<Options> {
// This will allow us to do some things

return (factory: any);
return factory as any;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// @flow

import type { NodePath } from "@babel/traverse";
import * as babel from "@babel/core";
const { types: t } = babel.default || babel;
import { types as t } from "@babel/core";

type StrMap<K> = Map<string, K>;

export default class ImportsCache {
_imports: WeakMap<NodePath, StrMap<string>>;
_anonymousImports: WeakMap<NodePath, Set<string>>;
_lastImports: WeakMap<NodePath, NodePath>;
_imports: WeakMap<NodePath<t.Program>, StrMap<string>>;
_anonymousImports: WeakMap<NodePath<t.Program>, Set<string>>;
_lastImports: WeakMap<NodePath<t.Program>, NodePath<t.Node>>;
_resolver: (url: string) => string;

constructor(resolver: (url: string) => string) {
Expand All @@ -20,13 +17,17 @@ export default class ImportsCache {
}

storeAnonymous(
programPath: NodePath,
programPath: NodePath<t.Program>,
url: string,
// eslint-disable-next-line no-undef
getVal: (isScript: boolean, source: t.StringLiteral) => t.Node,
) {
const key = this._normalizeKey(programPath, url);
const imports = this._ensure(this._anonymousImports, programPath, Set);
const imports = this._ensure<Set<string>>(
this._anonymousImports,
programPath,
Set,
);

if (imports.has(key)) return;

Expand All @@ -39,7 +40,7 @@ export default class ImportsCache {
}

storeNamed(
programPath: NodePath,
programPath: NodePath<t.Program>,
url: string,
name: string,
getVal: (
Expand All @@ -48,10 +49,14 @@ export default class ImportsCache {
source: t.StringLiteral,
// eslint-disable-next-line no-undef
name: t.Identifier,
) => { node: t.Node, name: string },
) => { node: t.Node; name: string },
) {
const key = this._normalizeKey(programPath, url, name);
const imports = this._ensure(this._imports, programPath, Map);
const imports = this._ensure<Map<string, any>>(
this._imports,
programPath,
Map,
);

if (!imports.has(key)) {
const { node, name: id } = getVal(
Expand All @@ -66,8 +71,9 @@ export default class ImportsCache {
return t.identifier(imports.get(key));
}

_injectImport(programPath: NodePath, node: t.Node) {
let lastImport = this._lastImports.get(programPath);
_injectImport(programPath: NodePath<t.Program>, node: t.Node) {
const lastImport = this._lastImports.get(programPath);
let newNodes: [NodePath];
if (
lastImport &&
lastImport.node &&
Expand All @@ -76,12 +82,12 @@ export default class ImportsCache {
lastImport.parent === programPath.node &&
lastImport.container === programPath.node.body
) {
lastImport = lastImport.insertAfter(node);
newNodes = lastImport.insertAfter(node);
} else {
lastImport = programPath.unshiftContainer("body", node);
newNodes = programPath.unshiftContainer("body", node);
}
lastImport = lastImport[lastImport.length - 1];
this._lastImports.set(programPath, lastImport);
const newNode = newNodes[newNodes.length - 1];
this._lastImports.set(programPath, newNode);

/*
let lastImport;
Expand All @@ -106,10 +112,10 @@ export default class ImportsCache {
});*/
}

_ensure<C: Map<*, *> | Set<*>>(
map: WeakMap<NodePath, C>,
programPath: NodePath,
Collection: Class<C>,
_ensure<C extends Map<string, any> | Set<string>>(
map: WeakMap<NodePath<t.Program>, C>,
programPath: NodePath<t.Program>,
Collection: { new (...args: any): C },
): C {
let collection = map.get(programPath);
if (!collection) {
Expand All @@ -119,7 +125,11 @@ export default class ImportsCache {
return collection;
}

_normalizeKey(programPath: NodePath, url: string, name: string = ""): string {
_normalizeKey(
programPath: NodePath<t.Program>,
url: string,
name: string = "",
): string {
const { sourceType } = programPath.node;

// If we rely on the imported binding (the "name" parameter), we also need to cache
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import { declare } from "@babel/helper-plugin-utils";
import type { NodePath } from "@babel/traverse";

Expand Down Expand Up @@ -41,13 +39,15 @@ function resolveOptions<Options>(
options: PluginOptions,
babelApi,
): {
method: MethodString,
methodName: "usageGlobal" | "entryGlobal" | "usagePure",
targets: Targets,
debug: boolean,
shouldInjectPolyfill: ?(name: string, shouldInject: boolean) => boolean,
providerOptions: ProviderOptions<Options>,
absoluteImports: string | boolean,
method: MethodString;
methodName: "usageGlobal" | "entryGlobal" | "usagePure";
targets: Targets;
debug: boolean | typeof presetEnvSilentDebugHeader;
shouldInjectPolyfill:
| ((name: string, shouldInject: boolean) => boolean)
| undefined;
providerOptions: ProviderOptions<Options>;
absoluteImports: string | boolean;
} {
const {
method,
Expand Down Expand Up @@ -141,7 +141,7 @@ See more options at https://github.com/babel/babel-polyfills/blob/main/docs/usag
absoluteImports: absoluteImports ?? false,
shouldInjectPolyfill,
debug: !!debug,
providerOptions: ((providerOptions: Object): ProviderOptions<Options>),
providerOptions: providerOptions as any as ProviderOptions<Options>,
};
}

Expand Down Expand Up @@ -282,7 +282,6 @@ function instantiateProvider<Options>(
provider,
callProvider(payload: MetaDescriptor, path: NodePath) {
const utils = getUtils(path);
// $FlowIgnore
provider[methodName](payload, utils, path);
},
};
Expand Down Expand Up @@ -336,11 +335,9 @@ export default function definePolyfillProvider<Options>(
missingDeps: new Set(),
};

// $FlowIgnore - Flow doesn't support optional calls
provider.pre?.apply(this, arguments);
},
post() {
// $FlowIgnore - Flow doesn't support optional calls
provider.post?.apply(this, arguments);

if (missingDependencies !== false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @flow

import type {
MetaDescriptor,
ResolverPolyfills,
Expand Down
Loading

0 comments on commit 4f49c9c

Please sign in to comment.