jsii
Version:
[](https://cdk.dev) [](https://github.com/aws/jsii
170 lines • 6.42 kB
JavaScript
;
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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BASE_COMPILER_OPTIONS = void 0;
exports.enumAsCamel = enumAsCamel;
exports.enumAsLower = enumAsLower;
exports.enumAsKebab = enumAsKebab;
exports.convertForJson = convertForJson;
exports.convertEnumToJson = convertEnumToJson;
exports.convertLibForJson = convertLibForJson;
exports.convertNewLineForJson = convertNewLineForJson;
const ts = __importStar(require("typescript"));
const Case = __importStar(require("../case"));
exports.BASE_COMPILER_OPTIONS = {
alwaysStrict: true,
declaration: true,
esModuleInterop: true,
incremental: true,
lib: ['lib.es2023.d.ts'],
module: ts.ModuleKind.Node20,
noEmitOnError: true,
noFallthroughCasesInSwitch: true,
noImplicitAny: true,
noImplicitReturns: true,
noImplicitThis: true,
noUncheckedSideEffectImports: true,
noUnusedLocals: true,
noUnusedParameters: true,
resolveJsonModule: true,
skipLibCheck: true,
strict: true,
strictNullChecks: true,
strictPropertyInitialization: true,
stripInternal: false,
target: ts.ScriptTarget.ES2023,
};
/**
* Helper function to convert a TS enum into a list of allowed values,
* converting everything to camel case.
* This is used for example for the watch options
*/
function enumAsCamel(enumData) {
return Object.keys(enumData)
.filter((v) => isNaN(Number(v)))
.map(Case.camel);
}
/**
* Helper function to convert a TS enum into a list of allowed values,
* converting everything to lower case.
* This is used for example for the "target" compiler option
*/
function enumAsLower(enumData) {
return Object.keys(enumData)
.filter((v) => isNaN(Number(v)) && v !== 'None')
.map((v) => v.toLowerCase());
}
/**
* Helper function to convert a TS enum into a list of allowed values,
* converting everything to kebab case.
* This is used for example for the "jsx" compiler option
*/
function enumAsKebab(enumData) {
return Object.keys(enumData)
.filter((v) => isNaN(Number(v)) && v !== 'None')
.map(Case.kebab);
}
/**
* The compilerOptions in the programmatic API are slightly differently than the format used in tsconfig.json
* This helper performs the necessary conversion from the programmatic API format the one used in tsconfig.json
*
* @param opt compilerOptions in programmatic API format
* @returns compilerOptions ready to be written on disk
*/
function convertForJson(opt) {
return {
...opt,
// Drop the "lib." prefix and ".d.ts" suffix before writing up the tsconfig.json file
...valueHelper('lib', opt.lib, convertLibForJson),
// Re-write the module, targets & jsx to be the JSON format instead of Programmatic API
...enumHelper('importsNotUsedAsValues', opt.importsNotUsedAsValues, ts.ImportsNotUsedAsValues),
...enumHelper('jsx', opt.jsx, ts.JsxEmit, Case.kebab),
...enumHelper('module', opt.module, ts.ModuleKind),
...enumHelper('moduleResolution', opt.moduleResolution, ts.ModuleResolutionKind),
...enumHelper('moduleDetection', opt.moduleDetection, ts.ModuleDetectionKind),
...enumHelper('target', opt.target, ts.ScriptTarget),
// rewrite newline to be the JSON format instead of Programmatic API
...valueHelper('newLine', opt.newLine, convertNewLineForJson),
};
}
function valueHelper(name, value, converter) {
if (!value) {
return {};
}
return { [name]: converter(value) };
}
function enumHelper(name, value, enumObj, converter) {
if (!value) {
return {};
}
return { [name]: convertEnumToJson(value, enumObj, converter) };
}
/**
* Convert an internal enum value to what a user would write in tsconfig.json
* Possibly using a converter function to adjust casing.
* @param value The internal enum value
* @param enumObj The enum object to convert from
* @param converter The converter function, defaults to lowercase
* @returns The humanized version of the enum value
*/
function convertEnumToJson(value, enumObj, converter = (v) => v.toLowerCase()) {
return converter(enumObj[value]);
}
/**
* Convert the internal lib strings to what a user would write in tsconfig.json
* @param input The input libs array
* @returns The humanized version lib array
*/
function convertLibForJson(input) {
return input.map((lib) => lib.slice(4, lib.length - 5));
}
/**
* This is annoying - the values expected in the tsconfig.json file are not
* the same as the enum constant names, or their values. So we need this
* function to map the "compiler API version" to the "tsconfig.json version"
*
* @param newLine the compiler form of the new line configuration
*
* @return the equivalent value to put in tsconfig.json
*/
function convertNewLineForJson(newLine) {
switch (newLine) {
case ts.NewLineKind.CarriageReturnLineFeed:
return 'crlf';
case ts.NewLineKind.LineFeed:
return 'lf';
}
}
//# sourceMappingURL=compiler-options.js.map