Skip to content
Open

Babel 8 #1131

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/giant-worlds-swim.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

Drop support for Node.js 20, 21 and 23

Minimum Supported Node.js version is now 22.13.0
Minimum Supported Node.js version is now 22.18.0
25 changes: 24 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
tests:
strategy:
matrix:
node: ["22.13.0", "24", "25", "26"]
node: ["22.18.0", "24", "25", "26"]
os: ["ubuntu"]
include:
- os: macos
Expand Down Expand Up @@ -70,6 +70,29 @@ jobs:
with:
parallel-finished: true

babel7-tests:
name: Tests (Babel 7, Node.js v24, ubuntu)
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7

- name: Install pnpm
uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6

- name: Setup Node.js
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
with:
node-version: "24"
cache: "pnpm"

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Unit tests
run: pnpm run test:babel7

lint:
name: Lint
runs-on: ubuntu-latest
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"engines": {
"node": "^22.13.0 || >=24",
"node": "^22.18.0 || >=24",
"pnpm": ">=10.6.2"
},
"type": "module",
Expand All @@ -10,6 +10,7 @@
"lint": "eslint . --report-unused-disable-directives --max-warnings=0",
"fix": "eslint . --fix --report-unused-disable-directives --max-warnings=0",
"test": "nx run-many --target=test --exclude='@react-docgen-internal/*' --output-style=stream",
"test:babel7": "cross-env BABEL_VERSION=7 nx run-many --target=test --exclude='@react-docgen-internal/*' --output-style=stream",
"copy:changelog:react-docgen": "cp ./packages/react-docgen/CHANGELOG.md ./packages/website/src/app/docs/release-notes/react-docgen/page.mdx",
"copy:changelog:cli": "cp ./packages/react-docgen-cli/CHANGELOG.md ./packages/website/src/app/docs/release-notes/cli/page.mdx",
"changesets-release": "pnpm build && changeset publish",
Expand All @@ -23,6 +24,10 @@
"@eslint/js": "10.0.1",
"@types/node": "22.20.1",
"@vitest/coverage-v8": "4.1.10",
"babel7-core": "npm:@babel/core@^7.29.7",
"babel7-traverse": "npm:@babel/traverse@^7.29.8",
"babel7-types": "npm:@babel/types@^7.29.8",
"cross-env": "10.1.0",
"eslint": "10.8.1",
"eslint-config-next": "16.2.12",
"eslint-config-prettier": "10.1.8",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-docgen-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dist"
],
"engines": {
"node": "^22.13.0 || >=24"
"node": "^22.18.0 || >=24"
},
"scripts": {
"build": "rimraf dist/ && tsc",
Expand Down
14 changes: 14 additions & 0 deletions packages/react-docgen-cli/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { createRequire } from 'node:module';
import { defineConfig } from 'vitest/config';

const require = createRequire(import.meta.url);
const babel7Aliases =
process.env.BABEL_VERSION === '7'
? {
'@babel/core': require.resolve('babel7-core'),
'@babel/traverse': require.resolve('babel7-traverse'),
'@babel/types': require.resolve('babel7-types'),
}
: {};

export default defineConfig({
resolve: {
alias: babel7Aliases,
},
test: {
name: 'cli',
include: ['**/__tests__/**/*-test.ts', '**/tests/integration/**/*-test.ts'],
Expand Down
8 changes: 4 additions & 4 deletions packages/react-docgen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dist"
],
"engines": {
"node": "^22.13.0 || >=24"
"node": "^22.18.0 || >=24"
},
"main": "dist/main.js",
"typings": "dist/main.d.ts",
Expand All @@ -43,9 +43,9 @@
],
"license": "MIT",
"dependencies": {
"@babel/core": "^7.28.0",
"@babel/traverse": "^7.28.0",
"@babel/types": "^7.28.2",
"@babel/core": "^7.29.0 || ^8.0.0",
"@babel/traverse": "^7.29.0 || ^8.0.0",
"@babel/types": "^7.29.0 || ^8.0.0",
"@types/babel__core": "^7.20.5",
"@types/babel__traverse": "^7.20.7",
"@types/doctrine": "^0.0.9",
Expand Down
8 changes: 4 additions & 4 deletions packages/react-docgen/src/FileState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import babelTraverse, { NodePath } from '@babel/traverse';
import type { File, Node, Program } from '@babel/types';
import type { Importer, ImportPath } from './importer/index.js';
import babelParse from './babelParser.js';
import type { TransformOptions } from '@babel/core';
import type { InputOptions } from '@babel/core';

// Workaround while babel is not a proper ES module
const traverse = babelTraverse.default ?? (babelTraverse as never);
const traverse = babelTraverse;

export default class FileState {
opts: TransformOptions;
opts: InputOptions;
path: NodePath<Program>;
ast: File;
scope: Scope;
Expand Down Expand Up @@ -39,7 +39,7 @@ export default class FileState {
};

constructor(
options: TransformOptions,
options: InputOptions,
{ code, ast, importer }: { code: string; ast: File; importer: Importer },
) {
this.opts = options;
Expand Down
22 changes: 10 additions & 12 deletions packages/react-docgen/src/babelParser.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
import type { ParserOptions, TransformOptions } from '@babel/core';
import { loadPartialConfig, parseSync } from '@babel/core';
import type { InputOptions } from '@babel/core';
import { loadPartialConfigSync, parseSync } from '@babel/core';
import type { ParserOptions } from '@babel/parser';
import type { File } from '@babel/types';
import { extname } from 'path';

const TYPESCRIPT_EXTS = new Set(['.cts', '.mts', '.ts', '.tsx']);

function getDefaultPlugins(
options: TransformOptions,
options: InputOptions,
): NonNullable<ParserOptions['plugins']> {
return [
'jsx',
options.filename && TYPESCRIPT_EXTS.has(extname(options.filename))
? 'typescript'
: 'flow',
'asyncDoExpressions',
'decimal',
['decorators', { decoratorsBeforeExport: false }],
'decoratorAutoAccessors',
'destructuringPrivate',
'doExpressions',
'exportDefaultFrom',
'functionBind',
'importAssertions',
'moduleBlocks',
'partialApplication',
['pipelineOperator', { proposal: 'minimal' }],
['recordAndTuple', { syntaxType: 'bar' }],
['pipelineOperator', { proposal: 'fsharp' }],
'regexpUnicodeSets',
'throwExpressions',
];
}

function buildPluginList(
options: TransformOptions,
options: InputOptions,
): NonNullable<ParserOptions['plugins']> {
let plugins: NonNullable<ParserOptions['plugins']> = [];

Expand All @@ -42,7 +40,7 @@ function buildPluginList(

// Let's check if babel finds a config file for this source file
// If babel does find a config file we do not apply our defaults
const partialConfig = loadPartialConfig(options);
const partialConfig = loadPartialConfigSync(options);

if (
plugins.length === 0 &&
Expand All @@ -57,7 +55,7 @@ function buildPluginList(
return plugins.filter((plugin) => plugin !== 'estree');
}

function buildParserOptions(options: TransformOptions): ParserOptions {
function buildParserOptions(options: InputOptions): ParserOptions {
const plugins = buildPluginList(options);

return {
Expand All @@ -70,10 +68,10 @@ function buildParserOptions(options: TransformOptions): ParserOptions {

export default function babelParser(
src: string,
options: TransformOptions = {},
options: InputOptions = {},
): File {
const parserOpts = buildParserOptions(options);
const opts: TransformOptions = {
const opts: InputOptions = {
...options,
parserOpts,
};
Expand Down
4 changes: 2 additions & 2 deletions packages/react-docgen/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TransformOptions } from '@babel/core';
import type { InputOptions } from '@babel/core';
import type { Handler } from './handlers/index.js';
import {
childContextTypeHandler,
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface Config {
* to an relative path that is relative to the `babelOptions.cwd`.
*/
filename?: string;
babelOptions?: TransformOptions;
babelOptions?: InputOptions;
}
export type InternalConfig = Omit<Required<Config>, 'filename'>;

Expand Down
14 changes: 14 additions & 0 deletions packages/react-docgen/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { createRequire } from 'node:module';
import { defineConfig } from 'vitest/config';

const require = createRequire(import.meta.url);
const babel7Aliases =
process.env.BABEL_VERSION === '7'
? {
'@babel/core': require.resolve('babel7-core'),
'@babel/traverse': require.resolve('babel7-traverse'),
'@babel/types': require.resolve('babel7-types'),
}
: {};

export default defineConfig({
resolve: {
alias: babel7Aliases,
},
test: {
name: 'lib',
setupFiles: ['./tests/setupTestFramework.ts'],
Expand Down
Loading
Loading