UNPKG

jsii

Version:

[![Join the chat at https://cdk.Dev](https://img.shields.io/static/v1?label=Slack&message=cdk.dev&color=brightgreen&logo=slack)](https://cdk.dev) [![Build Status](https://github.com/aws/jsii-compiler/workflows/build/badge.svg)](https://github.com/aws/jsii

167 lines • 7.05 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeScriptConfigValidator = void 0; exports.getCompilerOptionsRuleSet = getCompilerOptionsRuleSet; exports.describeRuleSet = describeRuleSet; exports.readTypeScriptConfig = readTypeScriptConfig; exports.validateTypeScriptConfigFile = validateTypeScriptConfigFile; const path = __importStar(require("node:path")); const ts = __importStar(require("typescript")); const compiler_options_1 = require("./compiler-options"); const utils_1 = require("../utils"); const generated_public_1 = __importDefault(require("./rulesets/generated.public")); const minimal_public_1 = __importDefault(require("./rulesets/minimal.public")); const strict_public_1 = __importDefault(require("./rulesets/strict.public")); const validator_1 = require("./validator"); const RuleSets = { generated: generated_public_1.default, strict: strict_public_1.default, minimal: minimal_public_1.default, off: new validator_1.RuleSet(), }; class TypeScriptConfigValidator { ruleSet; validator; compilerOptions; constructor(ruleSet) { this.ruleSet = ruleSet; const topLevelRules = new validator_1.RuleSet({ unexpectedFields: validator_1.RuleType.PASS, }); topLevelRules.shouldPass('files', validator_1.Match.ANY); topLevelRules.shouldPass('extends', validator_1.Match.ANY); topLevelRules.shouldPass('include', validator_1.Match.ANY); topLevelRules.shouldPass('exclude', validator_1.Match.ANY); topLevelRules.shouldPass('references', validator_1.Match.ANY); topLevelRules.shouldPass('watchOptions', validator_1.Match.ANY); topLevelRules.shouldPass('typeAcquisition', validator_1.Match.MISSING); this.compilerOptions = new validator_1.ObjectValidator(RuleSets[ruleSet], 'compilerOptions'); topLevelRules.shouldPass('compilerOptions', (compilerOptions) => { this.compilerOptions.validate(compilerOptions); return true; }); this.validator = new validator_1.ObjectValidator(topLevelRules, 'tsconfig'); } /** * Validated the provided config against the set of rules. * * @throws when the config is invalid * * @param tsconfig the tsconfig to be validated, this MUST be a tsconfig as a user would have written it in tsconfig. */ validate(tsconfig) { this.validator.validate(tsconfig); } } exports.TypeScriptConfigValidator = TypeScriptConfigValidator; /** * Returns the `compilerOptions` rule set that backs the given validation setting. * * These are the substantive, set-specific rules (the top-level rules such as * `files`/`include`/`exclude` are identical across all sets). The `off` rule set * is empty, as it disables validation. * * @param ruleSet the validation setting to look up * @returns the `RuleSet` applied to `compilerOptions` */ function getCompilerOptionsRuleSet(ruleSet) { return RuleSets[ruleSet]; } /** * Produces a human-readable description of every `compilerOptions` rule in a rule set. * * @param ruleSet the validation setting to describe * @returns a description for every rule in the set */ function describeRuleSet(ruleSet) { return getCompilerOptionsRuleSet(ruleSet).describe(); } /** * Reads and parses a `tsconfig.json` file from disk into the format expected by * the validator. This resolves `extends` and normalizes the options the same way * the compiler does, so validation behaves identically to a real compilation. * * @param configPath the path to the tsconfig file to read * @throws {@link JsiiError} if the file cannot be read or parsed * @returns the parsed TypeScript configuration */ function readTypeScriptConfig(configPath) { const absolutePath = path.resolve(configPath); const { config, error } = ts.readConfigFile(absolutePath, ts.sys.readFile); if (error) { throw new utils_1.JsiiError(`Failed to read tsconfig at "${configPath}": ${ts.flattenDiagnosticMessageText(error.messageText, '\n')}`); } const basePath = path.dirname(absolutePath); const extended = ts.parseJsonConfigFileContent(config, ts.sys, basePath); // the tsconfig parser adds this in, but it is not an expected compilerOption delete extended.options.configFilePath; return { compilerOptions: extended.options, watchOptions: extended.watchOptions, include: extended.fileNames, }; } /** * Reads a `tsconfig.json` file from disk and validates its `compilerOptions` * against the given rule set, without running a compilation. * * @param configPath the path to the tsconfig file to validate * @param ruleSet the rule set to validate against * @throws {@link JsiiError} if the file cannot be read or parsed * @returns the list of rule violations (empty if the config is valid) */ function validateTypeScriptConfigFile(configPath, ruleSet) { const config = readTypeScriptConfig(configPath); const validator = new TypeScriptConfigValidator(ruleSet); try { validator.validate({ ...config, // convert the internal format to the user format which is what the validator operates on compilerOptions: (0, compiler_options_1.convertForJson)(config.compilerOptions), }); return []; } catch (error) { if (error instanceof validator_1.ValidationError) { return error.violations; } throw error; } } //# sourceMappingURL=tsconfig-validator.js.map