Skip to content
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

Convert @babel/template from Flow to TS #12317

Merged
merged 3 commits into from Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -9,6 +9,7 @@ packages/babel-runtime-corejs2
packages/babel-runtime-corejs3
packages/*/node_modules
packages/*/lib
packages/*/dts
packages/*/dist
packages/*/test/fixtures
packages/*/test/tmp
Expand Down
25 changes: 19 additions & 6 deletions .eslintrc.js
Expand Up @@ -19,11 +19,24 @@ module.exports = {
node: true,
},
overrides: [
{
files: ["**/*.ts"],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"no-dupe-class-members": "off",
"@typescript-eslint/no-dupe-class-members": "error",
"no-undef": "off",
"no-redeclare": "off",
},
},
{
files: [
"packages/*/src/**/*.js",
"codemods/*/src/**/*.js",
"eslint/*/src/**/*.js",
"packages/*/src/**/*.{js,ts}",
"codemods/*/src/**/*.{js,ts}",
"eslint/*/src/**/*.{js,ts}",
],
rules: {
"@babel/development/no-undefined-identifier": "error",
Expand All @@ -37,7 +50,7 @@ module.exports = {
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
"packages/babel-helper-transform-fixture-test-runner/src/helpers.js",
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
"test/**/*.js",
],
env: {
Expand All @@ -53,15 +66,15 @@ module.exports = {
},
},
{
files: ["packages/babel-plugin-*/src/index.js"],
files: ["packages/babel-plugin-*/src/index.{js,ts}"],
excludedFiles: ["packages/babel-plugin-transform-regenerator/**/*.js"],
rules: {
"@babel/development/plugin-name": "error",
eqeqeq: ["error", "always", { null: "ignore" }],
},
},
{
files: ["packages/babel-parser/src/**/*.js"],
files: ["packages/babel-parser/src/**/*.{js,ts}"],
rules: {
"@babel/development-internal/dry-error-messages": [
"error",
Expand Down
1 change: 1 addition & 0 deletions .flowconfig
Expand Up @@ -16,6 +16,7 @@ lib/parser.js
lib/third-party-libs.js.flow
lib/preset-modules.js.flow
packages/babel-types/lib/index.js.flow
lib/babel-packages.js.flow

[options]
include_warnings=true
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Expand Up @@ -70,3 +70,10 @@ packages/babel-standalone/babel.min.js
/eslint/*/LICENSE
!/packages/babel-eslint-plugin/LICENSE
/.vscode

/tsconfig.json
/packages/*/tsconfig.json
/packages/*/tsconfig.tsbuildinfo
/packages/*/dts
/codemods/*/dts
/eslint/*/dts
7 changes: 3 additions & 4 deletions .prettierrc
Expand Up @@ -10,15 +10,14 @@
"printWidth": 80,
"overrides": [{
"files": [
"**/codemods/*/src/**/*.js",
"**/codemods/*/src/**/*.{js,ts}",
"**/codemods/*/test/**/*.js",
"**/packages/*/src/**/*.js",
"**/packages/*/src/**/*.{js,ts}",
"**/packages/*/test/**/*.js",
"**/eslint/*/src/**/*.js",
"**/eslint/*/src/**/*.{js,ts}",
"**/eslint/*/test/**/*.js"
],
"options": {
"parser": "babel",
"trailingComma": "all"
}
}]
Expand Down
14 changes: 11 additions & 3 deletions Gulpfile.js
Expand Up @@ -9,6 +9,7 @@ const fancyLog = require("fancy-log");
const filter = require("gulp-filter");
const gulp = require("gulp");
const path = require("path");
const fs = require("fs");
const rollup = require("rollup");
const rollupBabel = require("@rollup/plugin-babel").default;
const rollupBabelSource = require("./scripts/rollup-plugin-babel-source");
Expand All @@ -19,7 +20,7 @@ const rollupNodeResolve = require("@rollup/plugin-node-resolve").default;
const rollupReplace = require("@rollup/plugin-replace");
const { terser: rollupTerser } = require("rollup-plugin-terser");

const defaultSourcesGlob = "./@(codemods|packages|eslint)/*/src/**/*.js";
const defaultSourcesGlob = "./@(codemods|packages|eslint)/*/src/**/*.{js,ts}";

function swapSrcWithLib(srcPath) {
const parts = srcPath.split(path.sep);
Expand All @@ -28,7 +29,12 @@ function swapSrcWithLib(srcPath) {
}

function getIndexFromPackage(name) {
return `${name}/src/index.js`;
try {
fs.statSync(`./${name}/src/index.ts`);
return `${name}/src/index.ts`;
} catch {
return `${name}/src/index.js`;
}
}

function compilationLogger() {
Expand Down Expand Up @@ -121,8 +127,10 @@ function buildRollup(packages) {
babelrc: false,
babelHelpers: "bundled",
extends: "./babel.config.js",
extensions: [".mjs", ".cjs", ".ts", ".js"],
}),
rollupNodeResolve({
extensions: [".mjs", ".cjs", ".ts", ".js", ".json"],
browser: nodeResolveBrowser,
preferBuiltins: true,
//todo: remove when semver and source-map are bumped to latest versions
Expand All @@ -142,7 +150,7 @@ function buildRollup(packages) {
rollupJson(),
rollupNodePolyfills({
sourceMap: sourcemap,
include: "**/*.js",
include: "**/*.{js,ts}",
}),
],
});
Expand Down
24 changes: 19 additions & 5 deletions Makefile
Expand Up @@ -36,6 +36,9 @@ build-bundle: clean clean-lib
build-bundle-ci: bootstrap-only
$(MAKE) build-bundle

generate-tsconfig:
$(NODE) scripts/generators/tsconfig.js

generate-standalone:
$(NODE) packages/babel-standalone/scripts/generate.js

Expand All @@ -49,6 +52,7 @@ build-flow-typings:

build-typescript-typings:
$(NODE) packages/babel-types/scripts/generators/typescript.js > packages/babel-types/lib/index.d.ts
$(NODE) packages/babel-types/scripts/generators/typescript.js --ts3.7 > packages/babel-types/lib/index-ts3.7.d.ts

build-standalone: build-babel-standalone

Expand Down Expand Up @@ -85,12 +89,16 @@ watch: build-no-bundle
BABEL_ENV=development $(YARN) gulp watch

code-quality-ci: build-no-bundle-ci
$(MAKE) flowcheck-ci lint-ci
$(MAKE) tscheck flowcheck-ci lint-ci

flowcheck-ci:
$(MAKE) flow

code-quality: flow lint
code-quality: tscheck flow lint

tscheck: generate-tsconfig
make build-typescript-typings
$(YARN) tsc -b .

flow:
$(YARN) flow check --strip-root
Expand All @@ -109,15 +117,15 @@ check-compat-data-ci:
lint: lint-js lint-ts

lint-js:
BABEL_ENV=test $(YARN) eslint scripts $(SOURCES) '*.js' --format=codeframe
BABEL_ENV=test $(YARN) eslint scripts $(SOURCES) '*.{js,ts}' --format=codeframe --ext .js,.cjs,.mjs,.ts

lint-ts:
scripts/lint-ts-typings.sh

fix: fix-json fix-js

fix-js:
$(YARN) eslint scripts $(SOURCES) '*.js' --format=codeframe --fix
$(YARN) eslint scripts $(SOURCES) '*.{js,ts}' --format=codeframe --ext .js,.cjs,.mjs,.ts --fix

fix-json:
$(YARN) prettier "{$(COMMA_SEPARATED_SOURCES)}/*/test/fixtures/**/options.json" --write --loglevel warn
Expand All @@ -136,6 +144,10 @@ clean: test-clean
rm -rf packages/*/npm-debug*
rm -rf node_modules/.cache

clean-tsconfig:
rm -f tsconfig.json
rm -f packages/*/tsconfig.json

test-clean:
$(foreach source, $(SOURCES), \
$(call clean-source-test, $(source)))
Expand Down Expand Up @@ -211,6 +223,8 @@ clone-license:
prepublish-build: clean-lib clean-runtime-helpers
NODE_ENV=production BABEL_ENV=production $(MAKE) build-bundle
$(MAKE) prepublish-build-standalone clone-license
# We don't want to publish .d.ts files yet
rm -rf packages/*/dts

prepublish:
$(MAKE) check-yarn-bug-1882
Expand Down Expand Up @@ -271,7 +285,7 @@ clean-runtime-helpers:
rm -f packages/babel-runtime-corejs3/helpers/**/*.js
rm -rf packages/babel-runtime-corejs2/core-js

clean-all:
clean-all: clean-tsconfig
rm -rf node_modules
rm -rf package-lock.json
rm -rf .changelog
Expand Down
4 changes: 4 additions & 0 deletions babel.config.js
Expand Up @@ -103,6 +103,10 @@ module.exports = function (api) {
.filter(Boolean)
.map(normalize),
presets: [
[
"@babel/preset-typescript",
{ onlyRemoveTypeImports: true, allowDeclareFields: true },
],
["@babel/env", envOpts],
["@babel/preset-flow", { allowDeclareFields: true }],
],
Expand Down
36 changes: 36 additions & 0 deletions lib/babel-packages.js.flow
@@ -0,0 +1,36 @@
declare module "@babel/template" {
declare type PublicOpts = {
placeholderWhitelist?: ?Set<string>,
placeholderPattern?: ?(RegExp | false),
preserveComments?: ?boolean,
syntacticPlaceholders?: ?boolean,
};

declare type PublicReplacements = { [string]: mixed } | Array<mixed>;

declare type TemplateBuilder<T> = {
ast: {
(tpl: string, opts: ?PublicOpts): T,
(tpl: Array<string>, ...args: Array<mixed>): T,
},
(opts: PublicOpts): TemplateBuilder<T>,
(tpl: string, opts: ?PublicOpts): (?PublicReplacements) => T,
(tpl: Array<string>, ...args: Array<mixed>): (?PublicReplacements) => T,
};

declare type Smart = TemplateBuilder<
Array<BabelNodeStatement> | BabelNodeStatement
>;
declare type Statement = TemplateBuilder<BabelNodeStatement>;
declare type Statements = TemplateBuilder<Array<BabelNodeStatement>>;
declare type Expression = TemplateBuilder<BabelNodeExpression>;
declare type Program = TemplateBuilder<BabelNodeProgram>;

declare export default Smart & {
smart: Smart,
statement: Statement,
statements: Statements,
expression: Expression,
program: Program,
};
}
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -27,13 +27,16 @@
"@babel/plugin-transform-runtime": "^7.12.0",
"@babel/preset-env": "^7.12.0",
"@babel/preset-flow": "^7.10.4",
"@babel/preset-typescript": "^7.12.1",
"@babel/register": "^7.12.0",
"@babel/runtime": "^7.12.0",
"@rollup/plugin-babel": "^5.2.0",
"@rollup/plugin-commonjs": "^13.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-replace": "^2.3.3",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"babel-plugin-transform-charcodes": "^0.2.0",
"chalk": "^2.4.2",
"charcodes": "^0.2.0",
Expand Down Expand Up @@ -61,7 +64,7 @@
"rollup-plugin-terser": "^7.0.0",
"test262-stream": "^1.3.0",
"through2": "^2.0.0",
"typescript": "^3.6.3"
"typescript": "^4.0.5"
},
"workspaces": [
"codemods/*",
Expand All @@ -78,7 +81,7 @@
"yarn": ">=1.4.0"
},
"lint-staged": {
"*.js": [
"*.{js,ts}": [
"eslint --format=codeframe"
]
},
Expand Down