Skip to content

Commit

Permalink
verification functions for casting configs
Browse files Browse the repository at this point in the history
  • Loading branch information
p0nch000 committed Dec 17, 2024
1 parent f8e15f1 commit 2875f7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 63 deletions.
63 changes: 9 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions packages/babel-plugin/src/utils/state-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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]) => [
Expand Down Expand Up @@ -888,15 +894,15 @@ const getProgramStatement = (path: NodePath<>): NodePath<> => {
return programPath;
};

function isPackageJSON(obj: mixed): boolean %checks {
function isPackageJSON(obj: mixed): boolean {
return (
obj != null &&
typeof obj === 'object' &&
(!('imports' in obj) || typeof obj.imports === 'object')
);
}

function isTSConfig(obj: mixed): boolean %checks {
function isTSConfig(obj: mixed): boolean {
return (
obj != null &&
typeof obj === 'object' &&
Expand All @@ -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' &&
Expand Down

0 comments on commit 2875f7e

Please sign in to comment.