From 2875f7e46d53a4dddc9692032cfe3c203e4df901 Mon Sep 17 00:00:00 2001 From: p0nch000 Date: Tue, 17 Dec 2024 20:49:05 +0100 Subject: [PATCH] verification functions for casting configs --- package-lock.json | 63 +++---------------- .../babel-plugin/src/utils/state-manager.js | 24 ++++--- 2 files changed, 24 insertions(+), 63 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3c80b98a..c5a91145 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27010,6 +27010,7 @@ "cpu": [ "arm64" ], + "dev": true, "optional": true, "os": [ "darwin" @@ -27025,6 +27026,7 @@ "cpu": [ "x64" ], + "dev": true, "optional": true, "os": [ "darwin" @@ -27040,6 +27042,7 @@ "cpu": [ "arm64" ], + "dev": true, "optional": true, "os": [ "linux" @@ -27055,6 +27058,7 @@ "cpu": [ "arm64" ], + "dev": true, "optional": true, "os": [ "linux" @@ -27070,6 +27074,7 @@ "cpu": [ "x64" ], + "dev": true, "optional": true, "os": [ "linux" @@ -27085,6 +27090,7 @@ "cpu": [ "x64" ], + "dev": true, "optional": true, "os": [ "linux" @@ -27100,6 +27106,7 @@ "cpu": [ "arm64" ], + "dev": true, "optional": true, "os": [ "win32" @@ -27115,6 +27122,7 @@ "cpu": [ "ia32" ], + "dev": true, "optional": true, "os": [ "win32" @@ -27130,6 +27138,7 @@ "cpu": [ "x64" ], + "dev": true, "optional": true, "os": [ "win32" @@ -45923,60 +45932,6 @@ "version": "1.0.5", "resolved": "https://registry.npmjs.org/zwitch/-/zwitch-1.0.5.tgz", "integrity": "sha512-V50KMwwzqJV0NpZIZFwfOD5/lyny3WlSzRiXgA0G7VUnRlqttta1L6UQIHzd6EuBY/cHGfwTIck7w1yH6Q5zUw==" - }, - "@next/swc-darwin-arm64": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.0.1.tgz", - "integrity": "sha512-JyxnGCS4qT67hdOKQ0CkgFTp+PXub5W1wsGvIq98TNbF3YEIN7iDekYhYsZzc8Ov0pWEsghQt+tANdidITCLaw==", - "optional": true - }, - "@next/swc-darwin-x64": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-14.0.1.tgz", - "integrity": "sha512-625Z7bb5AyIzswF9hvfZWa+HTwFZw+Jn3lOBNZB87lUS0iuCYDHqk3ujuHCkiyPtSC0xFBtYDLcrZ11mF/ap3w==", - "optional": true - }, - "@next/swc-linux-arm64-gnu": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.0.1.tgz", - "integrity": "sha512-iVpn3KG3DprFXzVHM09kvb//4CNNXBQ9NB/pTm8LO+vnnnaObnzFdS5KM+w1okwa32xH0g8EvZIhoB3fI3mS1g==", - "optional": true - }, - "@next/swc-linux-arm64-musl": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.0.1.tgz", - "integrity": "sha512-mVsGyMxTLWZXyD5sen6kGOTYVOO67lZjLApIj/JsTEEohDDt1im2nkspzfV5MvhfS7diDw6Rp/xvAQaWZTv1Ww==", - "optional": true - }, - "@next/swc-linux-x64-gnu": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.0.1.tgz", - "integrity": "sha512-wMqf90uDWN001NqCM/auRl3+qVVeKfjJdT9XW+RMIOf+rhUzadmYJu++tp2y+hUbb6GTRhT+VjQzcgg/QTD9NQ==", - "optional": true - }, - "@next/swc-linux-x64-musl": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.0.1.tgz", - "integrity": "sha512-ol1X1e24w4j4QwdeNjfX0f+Nza25n+ymY0T2frTyalVczUmzkVD7QGgPTZMHfR1aLrO69hBs0G3QBYaj22J5GQ==", - "optional": true - }, - "@next/swc-win32-arm64-msvc": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.0.1.tgz", - "integrity": "sha512-WEmTEeWs6yRUEnUlahTgvZteh5RJc4sEjCQIodJlZZ5/VJwVP8p2L7l6VhzQhT4h7KvLx/Ed4UViBdne6zpIsw==", - "optional": true - }, - "@next/swc-win32-ia32-msvc": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.0.1.tgz", - "integrity": "sha512-oFpHphN4ygAgZUKjzga7SoH2VGbEJXZa/KL8bHCAwCjDWle6R1SpiGOdUdA8EJ9YsG1TYWpzY6FTbUA+iAJeww==", - "optional": true - }, - "@next/swc-win32-x64-msvc": { - "version": "14.0.1", - "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.0.1.tgz", - "integrity": "sha512-FFp3nOJ/5qSpeWT0BZQ+YE1pSMk4IMpkME/1DwKBwhg4mJLB9L+6EXuJi4JEwaJdl5iN+UUlmUD3IsR1kx5fAg==", - "optional": true } } } diff --git a/packages/babel-plugin/src/utils/state-manager.js b/packages/babel-plugin/src/utils/state-manager.js index 156aead5..5db952d9 100644 --- a/packages/babel-plugin/src/utils/state-manager.js +++ b/packages/babel-plugin/src/utils/state-manager.js @@ -637,11 +637,13 @@ export default class StateManager { try { const packageJsonPath = path.join(projectDir, 'package.json'); if (fs.existsSync(packageJsonPath)) { - const rawConfig = JSON5.parse(fs.readFileSync(packageJsonPath, 'utf8')); + const rawConfig: mixed = JSON5.parse( + fs.readFileSync(packageJsonPath, 'utf8'), + ); if (!isPackageJSON(rawConfig)) { throw new Error('Invalid package.json format'); } - const packageJson = rawConfig; + const packageJson: PackageJSON = rawConfig as $FlowFixMe; // Handle Node.js native imports const imports = packageJson.imports; @@ -664,11 +666,13 @@ export default class StateManager { try { const tsconfigPath = path.join(projectDir, 'tsconfig.json'); if (fs.existsSync(tsconfigPath)) { - const rawConfig = JSON5.parse(fs.readFileSync(tsconfigPath, 'utf8')); + const rawConfig: mixed = JSON5.parse( + fs.readFileSync(tsconfigPath, 'utf8'), + ); if (!isTSConfig(rawConfig)) { throw new Error('Invalid tsconfig.json format'); } - const tsconfig = rawConfig; + const tsconfig: TSConfig = rawConfig as $FlowFixMe; const baseUrl = tsconfig.compilerOptions?.baseUrl || '.'; if (tsconfig.compilerOptions?.paths) { tsconfigAliases = Object.fromEntries( @@ -699,11 +703,13 @@ export default class StateManager { try { const denoConfigPath = path.join(projectDir, 'deno.json'); if (fs.existsSync(denoConfigPath)) { - const rawConfig = JSON5.parse(fs.readFileSync(denoConfigPath, 'utf8')); + const rawConfig: mixed = JSON5.parse( + fs.readFileSync(denoConfigPath, 'utf8'), + ); if (!isDenoConfig(rawConfig)) { throw new Error('Invalid deno.json format'); } - const denoConfig = rawConfig; + const denoConfig: DenoConfig = rawConfig as $FlowFixMe; if (denoConfig.imports) { denoAliases = Object.fromEntries( Object.entries(denoConfig.imports).map(([key, value]) => [ @@ -888,7 +894,7 @@ const getProgramStatement = (path: NodePath<>): NodePath<> => { return programPath; }; -function isPackageJSON(obj: mixed): boolean %checks { +function isPackageJSON(obj: mixed): boolean { return ( obj != null && typeof obj === 'object' && @@ -896,7 +902,7 @@ function isPackageJSON(obj: mixed): boolean %checks { ); } -function isTSConfig(obj: mixed): boolean %checks { +function isTSConfig(obj: mixed): boolean { return ( obj != null && typeof obj === 'object' && @@ -906,7 +912,7 @@ function isTSConfig(obj: mixed): boolean %checks { ); } -function isDenoConfig(obj: mixed): boolean %checks { +function isDenoConfig(obj: mixed): boolean { return ( obj != null && typeof obj === 'object' &&