UNPKG

@graphql-codegen/testing

Version:
192 lines (191 loc) • 8.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileTs = exports.validateTs = void 0; const path_1 = require("path"); const lz_string_1 = require("lz-string"); const typescript_1 = require("typescript"); function validateTs(pluginOutput, options = { noEmitOnError: true, noImplicitAny: true, moduleResolution: typescript_1.ModuleResolutionKind.NodeJs, experimentalDecorators: true, emitDecoratorMetadata: true, target: typescript_1.ScriptTarget.ES5, typeRoots: [(0, path_1.resolve)(require.resolve('typescript'), '../../../@types/')], jsx: typescript_1.JsxEmit.React, allowJs: true, skipLibCheck: true, lib: [ (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.es5.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.es6.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.dom.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.scripthost.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.es2015.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.esnext.d.ts'), ], module: typescript_1.ModuleKind.ESNext, }, isTsx = false, isStrict = false, suspenseErrors = [], compileProgram = false) { if (process.env.SKIP_VALIDATION) { return; } if (isStrict) { options.strictNullChecks = true; options.strict = true; options.strictBindCallApply = true; options.strictPropertyInitialization = true; options.alwaysStrict = true; options.strictFunctionTypes = true; } const contents = typeof pluginOutput === 'string' ? pluginOutput : [...(pluginOutput.prepend || []), pluginOutput.content, ...(pluginOutput.append || [])].join('\n'); const testFile = `test-file.${isTsx ? 'tsx' : 'ts'}`; const errors = []; if (compileProgram) { const host = (0, typescript_1.createCompilerHost)(options); const program = (0, typescript_1.createProgram)([testFile], options, { ...host, getSourceFile: (fileName, languageVersion, onError, shouldCreateNewSourceFile) => { if (fileName === testFile) { return (0, typescript_1.createSourceFile)(fileName, contents, options.target); } return host.getSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile); }, writeFile() { }, useCaseSensitiveFileNames() { return false; }, getCanonicalFileName(filename) { return filename; }, getCurrentDirectory() { return ''; }, getNewLine() { return '\n'; }, }); const emitResult = program.emit(); const allDiagnostics = emitResult.diagnostics; for (const diagnostic of allDiagnostics) { if (diagnostic.file) { const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); const message = (0, typescript_1.flattenDiagnosticMessageText)(diagnostic.messageText, '\n'); errors.push(`${line + 1},${character + 1}: ${message} -> ${contents.split('\n')[line]}`); } else { errors.push(String((0, typescript_1.flattenDiagnosticMessageText)(diagnostic.messageText, '\n'))); } } } else { const result = (0, typescript_1.createSourceFile)(testFile, contents, typescript_1.ScriptTarget.ES2016, false, isTsx ? typescript_1.ScriptKind.TSX : undefined); const allDiagnostics = result.parseDiagnostics; if (allDiagnostics && allDiagnostics.length > 0) { for (const diagnostic of allDiagnostics) { if (diagnostic.file) { const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); const message = (0, typescript_1.flattenDiagnosticMessageText)(diagnostic.messageText, '\n'); errors.push(`${line + 1},${character + 1}: ${message} -> ${contents.split('\n')[line]}`); } else { errors.push(String((0, typescript_1.flattenDiagnosticMessageText)(diagnostic.messageText, '\n'))); } } } } const relevantErrors = errors.filter(e => { if (e.includes('Cannot find module')) { return false; } for (const suspenseError of suspenseErrors) { if (e.includes(suspenseError)) { return false; } } return true; }); if (relevantErrors && relevantErrors.length > 0) { throw new Error(relevantErrors.join('\n')); } } exports.validateTs = validateTs; function compileTs(contents, options = { noEmitOnError: true, noImplicitAny: true, moduleResolution: typescript_1.ModuleResolutionKind.NodeJs, allowSyntheticDefaultImports: true, experimentalDecorators: true, emitDecoratorMetadata: true, target: typescript_1.ScriptTarget.ES5, typeRoots: [(0, path_1.resolve)(require.resolve('typescript'), '../../../@types/')], jsx: typescript_1.JsxEmit.Preserve, allowJs: true, lib: [ (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.es5.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.es6.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.dom.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.scripthost.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.es2015.d.ts'), (0, path_1.join)((0, path_1.dirname)(require.resolve('typescript')), 'lib.esnext.asynciterable.d.ts'), ], module: typescript_1.ModuleKind.ESNext, }, isTsx = false, openPlayground = false) { if (process.env.SKIP_VALIDATION) { return; } try { const testFile = `test-file.${isTsx ? 'tsx' : 'ts'}`; const host = (0, typescript_1.createCompilerHost)(options); const program = (0, typescript_1.createProgram)([testFile], options, { ...host, getSourceFile: (fileName, languageVersion, onError, shouldCreateNewSourceFile) => { if (fileName === testFile) { return (0, typescript_1.createSourceFile)(fileName, contents, options.target); } return host.getSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile); }, writeFile() { }, useCaseSensitiveFileNames() { return false; }, getCanonicalFileName(filename) { return filename; }, getCurrentDirectory() { return ''; }, getNewLine() { return '\n'; }, }); const emitResult = program.emit(); const allDiagnostics = emitResult.diagnostics; const errors = []; for (const diagnostic of allDiagnostics) { if (diagnostic.file) { const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); const message = (0, typescript_1.flattenDiagnosticMessageText)(diagnostic.messageText, '\n'); errors.push(`${line + 1},${character + 1}: ${message} -> ${contents.split('\n')[line]}`); } else { errors.push(String((0, typescript_1.flattenDiagnosticMessageText)(diagnostic.messageText, '\n'))); } } const relevantErrors = errors.filter(e => !e.includes('Cannot find module')); if (relevantErrors && relevantErrors.length > 0) { throw new Error(relevantErrors.join('\n')); } } catch (e) { if (openPlayground) { const compressedCode = (0, lz_string_1.compressToEncodedURIComponent)(contents); open('http://www.typescriptlang.org/play/#code/' + compressedCode); } throw e; } } exports.compileTs = compileTs;