UNPKG

ts-transformer-inline-file

Version:
171 lines 6.83 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const ts = __importStar(require("typescript")); const stubModuleSource = fs.readFileSync(path.join(__dirname, 'index.d.ts'), 'utf8'); function transformer(program) { return (context) => (source) => visitNodeAndChildren(source, program, context); } exports.default = transformer; function visitNodeAndChildren(node, program, context) { const newNode = visitNode(node, program); return newNode ? ts.visitEachChild(newNode, (child) => visitNodeAndChildren(child, program, context), context) : undefined; } function visitNode(node, program) { try { if (ts.isCallExpression(node)) { return visitCallExpression(node, program); } if (ts.isImportDeclaration(node)) { return visitImportClause(node, program); } return node; } catch (err) { if (err instanceof Error) { enhanceErrorStack(err, node); } throw err; } } function visitCallExpression(node, program) { var _a; const typeChecker = program.getTypeChecker(); const signature = typeChecker.getResolvedSignature(node); if (!signature) { return node; } const { declaration } = signature; if (!declaration || ts.isJSDocSignature(declaration) || !isOurStubModule(declaration.getSourceFile())) { return node; } const funcName = (_a = declaration.name) === null || _a === void 0 ? void 0 : _a.getText(); if (!funcName) { return node; } return handleInlineCallExpression(node, funcName); } function visitImportClause(node, program) { if (!node.importClause) { return node; } const namedBindings = node.importClause.namedBindings; if (!node.importClause.name && !namedBindings) { return node; } const importSymbol = program.getTypeChecker().getSymbolAtLocation(node.moduleSpecifier); if (!(importSymbol === null || importSymbol === void 0 ? void 0 : importSymbol.valueDeclaration) || !isOurStubModule(importSymbol.valueDeclaration.getSourceFile())) { return node; } return undefined; // drop the import } function handleInlineCallExpression(node, funcName) { const arg0 = node.arguments[0]; if (!arg0 || !ts.isStringLiteral(arg0)) { throw TypeError(`The first argument of ${funcName} function must be a string literal`); } const filename = arg0.text; const baseDir = path.dirname(node.getSourceFile().fileName); const content = fs.readFileSync(path.resolve(baseDir, filename), 'utf-8'); switch (funcName) { case '$INLINE_FILE': { return ts.factory.createStringLiteral(content); } case '$INLINE_JSON': { const parent = node.parent; let obj = JSON.parse(content); if (ts.isVariableDeclaration(parent) && ts.isObjectBindingPattern(parent.name)) { if (typeof obj !== 'object') { throw TypeError(`${filename} does not contain an object as expected`); } if (obj !== null) { obj = filterObjectByBindingPattern(obj, parent.name); } } return jsonToAST(obj); } default: { throw RangeError(`Unknown function: ${funcName}`); } } } function isOurStubModule(sourceFile) { // Comparing of the file names may not be reliable, it doesn't work e.g. with // yarn's "link" resolution used in this module's end-to-end tests. return sourceFile.text === stubModuleSource; } function filterObjectByBindingPattern(obj, binding) { return binding.elements.reduce((acc, { propertyName, name }) => { const propName = propertyName && ts.isIdentifier(propertyName) ? propertyName.text : ts.isIdentifier(name) ? name.text : undefined; if (propName) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access acc[propName] = obj[propName]; } return acc; }, {}); } function jsonToAST(obj) { switch (typeof obj) { case 'object': { if (obj === null) { return ts.factory.createNull(); } if (Array.isArray(obj)) { return ts.factory.createArrayLiteralExpression(obj.map(jsonToAST)); } return ts.factory.createObjectLiteralExpression(Object.keys(obj).map(key => { const propName = ts.factory.createStringLiteral(key); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return ts.factory.createPropertyAssignment(propName, jsonToAST(obj[key])); })); } case 'number': return ts.factory.createNumericLiteral(obj); case 'boolean': return obj ? ts.factory.createTrue() : ts.factory.createFalse(); case 'string': return ts.factory.createStringLiteral(obj, /* isSingleQuote */ undefined); default: throw TypeError(`Unexpected type in JSON object: "${String(obj)}" (${typeof obj})`); } } function enhanceErrorStack(err, node) { if (err.stack == null) { return; } const lines = err.stack.split('\n'); const line1 = lines[1] || ''; const indent = ' '.repeat(line1.length - line1.trimStart().length); const source = node.getSourceFile(); const loc = ts.getLineAndCharacterOfPosition(source, node.pos); const newLine = `${indent}at ${source.fileName}:${loc.line + 1}:${loc.character + 1}`; lines.splice(1, 0, newLine); err.stack = lines.join('\n'); } //# sourceMappingURL=transformer.js.map