UNPKG

auto-cr-cmd

Version:

Fast automated code review CLI powered by SWC-based static analysis

125 lines (124 loc) 4.44 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setTsConfigPath = setTsConfigPath; exports.loadParseOptions = loadParseOptions; var fs_1 = __importDefault(require("fs")); var path_1 = __importDefault(require("path")); var i18n_1 = require("./i18n"); var consola_1 = __importDefault(require("consola")); var cachedTsConfig; var tsConfigPathOverride = null; function setTsConfigPath(path) { tsConfigPathOverride = path ? path : null; cachedTsConfig = undefined; } function readTsConfig() { if (cachedTsConfig !== undefined) { return cachedTsConfig; } var tsConfigPath = tsConfigPathOverride !== null && tsConfigPathOverride !== void 0 ? tsConfigPathOverride : path_1.default.resolve(process.cwd(), 'tsconfig.json'); if (!fs_1.default.existsSync(tsConfigPath)) { cachedTsConfig = null; return cachedTsConfig; } try { var raw = fs_1.default.readFileSync(tsConfigPath, 'utf-8'); cachedTsConfig = JSON.parse(raw); } catch (error) { cachedTsConfig = null; var t = (0, i18n_1.getTranslator)(); consola_1.default.warn(t.tsconfigReadFailed(), error instanceof Error ? error.message : error); } return cachedTsConfig; } var TARGET_MAP = { es3: 'es3', es5: 'es5', es6: 'es2015', es2015: 'es2015', es2016: 'es2016', es2017: 'es2017', es2018: 'es2018', es2019: 'es2019', es2020: 'es2020', es2021: 'es2021', es2022: 'es2022', esnext: 'esnext', latest: 'esnext', }; function normalizeTarget(target) { if (!target) return undefined; var normalized = target.toLowerCase(); if (normalized in TARGET_MAP) { return TARGET_MAP[normalized]; } var match = normalized.match(/^es(\d{4})$/); if (match) { var year = "es".concat(match[1]); return TARGET_MAP[year]; } return undefined; } function isJsxEnabled(option) { if (!option) return false; var normalized = option.toLowerCase(); return normalized !== 'none'; } function createTsParserConfig(extension, options, enableDecorators) { var shouldEnableJsx = extension === '.tsx' || (extension === '.ts' && isJsxEnabled(options === null || options === void 0 ? void 0 : options.jsx)); return { syntax: 'typescript', tsx: shouldEnableJsx, decorators: enableDecorators, dynamicImport: true, }; } function createEsParserConfig(extension, options, enableDecorators) { var jsxEnabled = extension === '.jsx' || (extension === '.js' && isJsxEnabled(options === null || options === void 0 ? void 0 : options.jsx)); return { syntax: 'ecmascript', jsx: jsxEnabled, decorators: enableDecorators, importAttributes: true, }; } function loadParseOptions(filePath) { var extension = path_1.default.extname(filePath).toLowerCase(); var tsConfig = readTsConfig(); var compilerOptions = tsConfig === null || tsConfig === void 0 ? void 0 : tsConfig.compilerOptions; var enableDecorators = Boolean(compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.experimentalDecorators); var target = normalizeTarget(compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.target); var parserConfig; if (extension === '.ts' || extension === '.tsx') { parserConfig = createTsParserConfig(extension, compilerOptions, enableDecorators); } else if (extension === '.js' || extension === '.jsx') { parserConfig = createEsParserConfig(extension, compilerOptions, enableDecorators); } else { parserConfig = createTsParserConfig('.ts', compilerOptions, enableDecorators); } var options = __assign(__assign({}, parserConfig), { comments: true }); if (target) { options.target = target; } return options; }