Skip to content

Commit

Permalink
Merge pull request #47 from babel/main
Browse files Browse the repository at this point in the history
[pull] main from babel:main
  • Loading branch information
devkadirselcuk committed Dec 9, 2021
2 parents 8c558fa + 3908049 commit 8a6a0b8
Show file tree
Hide file tree
Showing 615 changed files with 5,724 additions and 2,497 deletions.
48 changes: 28 additions & 20 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ const path = require("path");

const cjsGlobals = ["__dirname", "__filename", "require", "module", "exports"];

const testFiles = [
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
];
const sourceFiles = exts => [
`packages/*/src/**/*.{${exts}}`,
`codemods/*/src/**/*.{${exts}}`,
`eslint/*/src/**/*.{${exts}}`,
];

module.exports = {
root: true,
plugins: [
"import",
"node",
"jest",
"prettier",
"@babel/development",
Expand Down Expand Up @@ -36,11 +48,7 @@ module.exports = {
},
},
{
files: [
"packages/*/src/**/*.{js,ts,cjs}",
"codemods/*/src/**/*.{js,ts,cjs}",
"eslint/*/src/**/*.{js,ts,cjs}",
],
files: sourceFiles("js,ts,cjs,mjs"),
rules: {
"@babel/development/no-undefined-identifier": "error",
"@babel/development/no-deprecated-clone": "error",
Expand All @@ -51,9 +59,7 @@ module.exports = {
},
{
files: [
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
...testFiles,
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
"test/**/*.js",
],
Expand All @@ -67,23 +73,25 @@ module.exports = {
"jest/no-standalone-expect": "off",
"jest/no-test-callback": "off",
"jest/valid-describe": "off",
"import/extensions": ["error", { json: "always", cjs: "always" }],
"import/extensions": ["error", "always"],
"import/no-extraneous-dependencies": "off",
"no-restricted-imports": ["error", { patterns: ["**/src/**"] }],
},
},
{
files: [
"packages/*/src/**/*.{js,ts}",
"codemods/*/src/**/*.{js,ts}",
"eslint/*/src/**/*.{js,ts}",
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
"test/**/*.js",
],
files: testFiles,
rules: {
"node/no-unsupported-features": [
"error",
{ version: "12.17.0", ignores: ["modules"] },
],
},
},
{
files: [...sourceFiles("js,ts,mjs"), ...testFiles, "test/**/*.js"],
excludedFiles: [
// @babel/register is the require() hook, so it will always be CJS-based
"packages/babel-register/**/*.js",
"packages/babel-register/**/*.{js,ts}",
],
rules: {
"no-restricted-globals": ["error", ...cjsGlobals],
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ package-lock.json
/packages/babel-runtime-corejs3/core-js/**/*.js
/packages/babel-runtime-corejs3/core-js-stable/**/*.js

/packages/babel-register/test/.babel
/packages/babel-register/test/.cache.babel
/packages/babel-register/test/.index.babel
/packages/babel-cli/test/tmp
/packages/babel-node/test/tmp
/packages/*/lib
Expand Down
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"semi": true,
"singleQuote": false,
"bracketSpacing": true,
"jsxBracketSameLine": false,
"bracketSameLine": false,
"tabWidth": 2,
"printWidth": 80,
"overrides": [{
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Note that the code shown in Chrome DevTools is compiled code and therefore diffe
- After the ESTree PR is accepted, update [ast/spec.md](https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md). Note that there are differences between Babel AST and ESTree. In these cases, consistency with current Babel AST outweighs alignment to ESTree. Otherwise it should follow ESTree.

- [ ] Implement parser plugins based on the new AST. The parser plugin name should be the unprefixed slug of the TC39 proposal URL in _camelcase_, i.e. `exportDefaultFrom` from `https://github.com/tc39/proposal-export-default-from`.
- [ ] Use the `this.expectPlugin("newSyntax")` check within `@babel/parser` to ensure your new plugin code only runs when that flag is turned on (not default behavior), and a friendly error is thrown if users forget to enable a plugin.
- [ ] Use the `this.expectPlugin("pluginName")` check within `@babel/parser` to ensure your new plugin code only runs when that flag is turned on (not default behavior), and a friendly error is thrown if users forget to enable a plugin. You can also supply an array pair to require certain configuration options, e.g., `this.expectPlugin(["pluginName", { configOption: value }])`.
- [ ] Add failing/passing tests according to spec behavior
- [ ] Add `@babel/syntax-new-syntax` package. You can copy `packages/babel-plugin-syntax-decimal` and replace `decimal` to `new-syntax`.
- [ ] Add `@babel/syntax-new-syntax` to `@babel/standalone`.
Expand Down
3 changes: 2 additions & 1 deletion Gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function mapSrcToLib(srcPath) {
function mapToDts(packageName) {
return packageName.replace(
/(?<=\\|\/|^)(packages|eslint|codemods)(?=\\|\/)/,
"dts"
"dts/$1"
);
}

Expand Down Expand Up @@ -461,6 +461,7 @@ function copyDts(packages) {

const libBundles = [
"packages/babel-parser",
"packages/babel-plugin-proposal-object-rest-spread",
"packages/babel-plugin-proposal-optional-chaining",
"packages/babel-preset-react",
"packages/babel-preset-typescript",
Expand Down
23 changes: 12 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FLOW_COMMIT = 92bbb5e9dacb8185aa73ea343954d0434b42c40b
TEST262_COMMIT = 26f1f4567ee7e33163d961c867d689173cbb9065
TEST262_COMMIT = 46f165ae490de7f1343165ab8d228db81eaa02c5
TYPESCRIPT_COMMIT = bbd9ff51f5fa5208b223bbb75a94e5e8184e3ffd

# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967
Expand All @@ -18,7 +18,7 @@ NODE := $(YARN) node

.PHONY: build build-dist watch lint fix clean test-clean test-only test test-ci publish bootstrap

build: build-bundle
build: build-no-bundle
ifneq ("$(BABEL_COVERAGE)", "true")
$(MAKE) build-standalone
endif
Expand All @@ -28,8 +28,15 @@ build-bundle: clean clean-lib
$(MAKE) build-flow-typings
$(MAKE) build-dist

build-bundle-ci: bootstrap-only
$(MAKE) build-bundle
build-no-bundle-ci: bootstrap-only
$(YARN) gulp build-dev
$(MAKE) build-flow-typings
$(MAKE) build-dist

build-no-bundle: clean clean-lib
BABEL_ENV=development $(YARN) gulp build-dev
$(MAKE) build-flow-typings
$(MAKE) build-dist

generate-tsconfig:
$(NODE) scripts/generators/tsconfig.js
Expand All @@ -46,7 +53,7 @@ build-typescript-legacy-typings:

build-standalone: build-babel-standalone

build-standalone-ci: build-bundle-ci
build-standalone-ci: build-no-bundle-ci
$(MAKE) build-standalone

build-babel-standalone:
Expand All @@ -61,12 +68,6 @@ build-plugin-transform-runtime-dist:
cd packages/babel-plugin-transform-runtime; \
$(NODE) scripts/build-dist.js

build-no-bundle: clean clean-lib
BABEL_ENV=development $(YARN) gulp build-dev
# Ensure that build artifacts for types are created during local
# development too.
$(MAKE) build-flow-typings

watch: build-no-bundle
BABEL_ENV=development $(YARN) gulp watch

Expand Down
67 changes: 52 additions & 15 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module.exports = function (api) {
"eslint/*/test",
];

const lazyRequireSources = [
"./packages/babel-cli",
"./packages/babel-core",
"./packages/babel-preset-env/src/available-plugins.js",
];

switch (env) {
// Configs used during bundling builds.
case "standalone":
Expand Down Expand Up @@ -165,9 +171,6 @@ module.exports = function (api) {
["@babel/proposal-object-rest-spread", { useBuiltIns: true }],

convertESM ? "@babel/proposal-export-namespace-from" : null,
convertESM
? ["@babel/transform-modules-commonjs", { importInterop }]
: null,
convertESM ? pluginImportMetaUrl : null,

pluginPackageJsonMacro,
Expand All @@ -194,17 +197,6 @@ module.exports = function (api) {
test: ["packages/babel-generator"].map(normalize),
plugins: ["babel-plugin-transform-charcodes"],
},
convertESM && {
test: [
"./packages/babel-cli",
"./packages/babel-core",
"./packages/babel-preset-env/src/available-plugins.js",
].map(normalize),
plugins: [
// Explicitly use the lazy version of CommonJS modules.
["@babel/transform-modules-commonjs", { importInterop, lazy: true }],
],
},
convertESM && {
test: ["./packages/babel-node/src"].map(normalize),
// Used to conditionally import kexec
Expand All @@ -215,6 +207,35 @@ module.exports = function (api) {
assumptions: sourceAssumptions,
plugins: [transformNamedBabelTypesImportToDestructuring],
},
convertESM && {
test: lazyRequireSources.map(normalize),
plugins: [
// Explicitly use the lazy version of CommonJS modules.
[
"@babel/transform-modules-commonjs",
{ importInterop: importInteropSrc, lazy: true },
],
],
},
convertESM && {
test: sources.map(normalize),
exclude: lazyRequireSources.map(normalize),
plugins: [
[
"@babel/transform-modules-commonjs",
{ importInterop: importInteropSrc },
],
],
},
{
test: sources.map(source => normalize(source.replace("/src", "/test"))),
plugins: [
[
"@babel/transform-modules-commonjs",
{ importInterop: importInteropTest },
],
],
},
{
test: unambiguousSources.map(normalize),
sourceType: "unambiguous",
Expand Down Expand Up @@ -244,7 +265,7 @@ const monorepoPackages = ["codemods", "eslint", "packages"]
.reduce((a, b) => a.concat(b))
.map(name => name.replace(/^babel-/, "@babel/"));

function importInterop(source) {
function importInteropSrc(source) {
if (
// These internal files are "real CJS" (whose default export is
// on module.exports) and not compiled ESM.
Expand All @@ -266,6 +287,22 @@ function importInterop(source) {
return "node";
}

function importInteropTest(source) {
// This file will soon have an esm entrypoint
if (source === "@babel/helper-plugin-test-runner") {
return "none";
}
if (
// non-test files
!source.startsWith(".") ||
// lib files
/(?:\.\.\/)+(?:babel-[a-z-]+\/)?lib/.test(source)
) {
return "node";
}
return "babel";
}

// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
function bool(value) {
return value && value !== "false" && value !== "0";
Expand Down
22 changes: 22 additions & 0 deletions benchmark/babel-parser/many-nested-block-elements/bench.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Benchmark from "benchmark";
import baseline from "@babel-baseline/parser";
import current from "@babel/parser";
import { report } from "../../util.mjs";

const suite = new Benchmark.Suite();
function createInput(length) {
return "{".repeat(length) + "0" + "}".repeat(length);
}
function benchCases(name, implementation, options) {
for (const length of [128, 256, 512, 1024]) {
const input = createInput(length);
suite.add(`${name} ${length} nested template elements`, () => {
implementation.parse(input, options);
});
}
}

benchCases("baseline", baseline);
benchCases("current", current);

suite.on("cycle", report).run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Benchmark from "benchmark";
import baseline from "@babel-baseline/parser";
import current from "@babel/parser";
import { report } from "../../util.mjs";

const suite = new Benchmark.Suite();
function createInput(length) {
return "<t a={x}>{y}".repeat(length) + "</t>".repeat(length);
}
function benchCases(name, implementation, options) {
for (const length of [128, 256, 512, 1024]) {
const input = createInput(length);
suite.add(
`${name} ${length} nested jsx elements with one attribute and text`,
() => {
implementation.parse(input, options);
}
);
}
}

benchCases("baseline", baseline, { plugins: ["jsx"] });
benchCases("current", current, { plugins: ["jsx"] });

suite.on("cycle", report).run();
22 changes: 22 additions & 0 deletions benchmark/babel-parser/many-nested-template-elements/bench.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Benchmark from "benchmark";
import baseline from "@babel-baseline/parser";
import current from "@babel/parser";
import { report } from "../../util.mjs";

const suite = new Benchmark.Suite();
function createInput(length) {
return "` ${".repeat(length) + "0" + "}`".repeat(length);
}
function benchCases(name, implementation, options) {
for (const length of [128, 256, 512, 1024]) {
const input = createInput(length);
suite.add(`${name} ${length} nested template elements`, () => {
implementation.parse(input, options);
});
}
}

benchCases("baseline", baseline);
benchCases("current", current);

suite.on("cycle", report).run();
23 changes: 23 additions & 0 deletions benchmark/babel-parser/many-template-elements/bench.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Benchmark from "benchmark";
import baseline from "@babel-baseline/parser";
import current from "@babel/parser";
import { report } from "../../util.mjs";

const suite = new Benchmark.Suite();
function createInput(length) {
return "`" + " ${0}".repeat(length) + "`";
}
function benchCases(name, implementation, options) {
for (const length of [128, 256, 512, 1024]) {
const input = createInput(length);
suite.add(`${name} ${length} template elements`, () => {
implementation.parse(input, options);
});
}
}

current.parse(createInput(1));
benchCases("baseline", baseline);
benchCases("current", current);

suite.on("cycle", report).run();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Loading

0 comments on commit 8a6a0b8

Please sign in to comment.