Skip to content

⬅️ Back to Table of Contents

📄 parseConfig

📊 Analysis Summary

Metric Count
🔧 Functions 4
📦 Imports 6
📊 Variables & Constants 1

📚 Table of Contents

🛠️ File Location:

📂 packages/website/src/components/lib/parseConfig.ts

📦 Imports

Name Source
EslintRC ../types
TSConfig ../types
isRecord ../ast/utils
ensureObject ./json
parseJSONObject ./json
toJson ./json

Variables & Constants

Name Type Kind Value Exported
moduleRegexp RegExp const /(module\.exports\s*=)/g

Functions

parseESLintRC(code: string): EslintRC

Parse a .eslintrc string into an object. This function is pretty naive, as it only validates rules and extends.

TODO: the user should probably write flat configs, but that requires work on the visual editor.

Raw JSDoc
/**
 * Parse a .eslintrc string into an object.
 * This function is pretty naive, as it only validates rules and extends.
 *
 * TODO: the user should probably write flat configs, but that requires
 * work on the visual editor.
 */

Calls:

  • parseJSONObject (from ./json)
  • ensureObject (from ./json)
  • Array.isArray
  • console.error
Code
export function parseESLintRC(code?: string): EslintRC {
  if (code) {
    try {
      const parsed = parseJSONObject(code);
      parsed.rules = ensureObject(parsed.rules);
      if (
        !Array.isArray(parsed.extends) &&
        typeof parsed.extends !== 'string'
      ) {
        parsed.extends = [];
      } else if (typeof parsed.extends === 'string') {
        parsed.extends = [parsed.extends];
      }
      return parsed as unknown as EslintRC;
    } catch (e) {
      console.error(e);
    }
  }
  return { extends: [], rules: {} };
}

parseTSConfig(code: string): TSConfig

Parse a tsconfig.json string into an object. This is done by typescript compiler.

Raw JSDoc
/**
 * Parse a tsconfig.json string into an object.
 * This is done by typescript compiler.
 */

Calls:

  • window.ts.parseConfigFileTextToJson
  • console.error
  • isRecord (from ../ast/utils)
Code
export function parseTSConfig(code?: string): TSConfig {
  if (code) {
    try {
      const parsed = window.ts.parseConfigFileTextToJson(
        '/tsconfig.json',
        code,
      );
      if (parsed.error) {
        console.error(parsed.error);
      }
      if (isRecord(parsed.config)) {
        return parsed.config as TSConfig;
      }
    } catch (e) {
      console.error(e);
    }
  }
  return { compilerOptions: {} };
}

tryParseEslintModule(value: string): string

Evaluate a string that contains a module.exports assignment.

Raw JSDoc
/**
 * Evaluate a string that contains a module.exports assignment.
 */

Calls:

  • moduleRegexp.test
  • toJson (from ./json)
  • constrainedScopeEval
  • console.error
Code
export function tryParseEslintModule(value: string): string {
  try {
    if (moduleRegexp.test(value)) {
      const newValue = toJson(constrainedScopeEval(value));
      if (newValue !== value) {
        return newValue;
      }
    }
  } catch (e) {
    console.error(e);
  }
  return value;
}

constrainedScopeEval(obj: string): unknown

Parameters:

  • obj string

Returns: unknown

Calls:

  • complex_call_1683

Internal Comments:

// eslint-disable-next-line @typescript-eslint/no-implied-eval, @typescript-eslint/no-unsafe-call

Code
function constrainedScopeEval(obj: string): unknown {
  // eslint-disable-next-line @typescript-eslint/no-implied-eval, @typescript-eslint/no-unsafe-call
  return new Function(`
    "use strict";
    var module = { exports: {} };
    (${obj});
    return module.exports
  `)();
}

Generated by Syntax Scribe