remove unused file and add source links

This commit is contained in:
Pavel Glac 2024-10-21 14:27:58 +02:00
Родитель f4b3fef871
Коммит 1fa0463f7c
5 изменённых файлов: 16 добавлений и 278 удалений

Просмотреть файл

@ -1,3 +1,8 @@
/**
* Taken from https://github.com/relay-tools/relay-compiler-language-typescript/blob/3231ea09205cc341be704c00e78db0d8ff78b34a/src/TypeScriptGenerator.ts
* Copyright 2018 Kaare Hoff Skovgaard kaare@kaareskovgaard.net, Eloy Durán eloy.de.enige@gmail.com
*/
import {
Condition,
Fragment,

Просмотреть файл

@ -1,3 +1,9 @@
/**
* Taken from https://github.com/relay-tools/relay-compiler-language-typescript/blob/3231ea09205cc341be704c00e78db0d8ff78b34a/src/TypeScriptTypeTransformers.ts
* License: MIT
* Copyright 2018 Kaare Hoff Skovgaard kaare@kaareskovgaard.net, Eloy Durán eloy.de.enige@gmail.com
*/
import { EnumTypeID, FieldID, Schema, TypeID } from "relay-compiler";
import { TypeGeneratorOptions } from "relay-compiler/lib/language/RelayLanguagePluginInterface";
import * as ts from "typescript";

Просмотреть файл

@ -1,174 +0,0 @@
import * as ts from "typescript";
import { formatterFactory } from "../formatGeneratedModule";
describe("formatGeneratedModule", () => {
it("works", () => {
const formatGeneratedModule = formatterFactory({ noImplicitAny: true });
expect(
formatGeneratedModule({
moduleName: "complete-example",
// @ts-ignore
documentType: "ConcreteFragment",
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } } }),
typeText: "export type CompleteExample = { readonly id: string }",
hash: "@relayHash abcde",
sourceHash: "edcba",
}),
).toMatchInlineSnapshot(`
"/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
/* @relayHash abcde */
import { ConcreteFragment } from \\"relay-runtime\\";
export type CompleteExample = { readonly id: string }
const node: ConcreteFragment = { \\"the\\": { \\"fragment\\": { \\"data\\": 42 } } } as any;
(node as any).hash = 'edcba';
export default node;
"
`);
});
it("works without passing relay runtime module explicitly", () => {
const formatGeneratedModule = formatterFactory({ noImplicitAny: true });
expect(
formatGeneratedModule({
moduleName: "complete-example",
// @ts-ignore
documentType: "ConcreteFragment",
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } } }),
typeText: "export type CompleteExample = { readonly id: string }",
hash: "@relayHash abcde",
sourceHash: "edcba",
}),
).toMatchInlineSnapshot(`
"/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
/* @relayHash abcde */
import { ConcreteFragment } from \\"relay-runtime\\";
export type CompleteExample = { readonly id: string }
const node: ConcreteFragment = { \\"the\\": { \\"fragment\\": { \\"data\\": 42 } } } as any;
(node as any).hash = 'edcba';
export default node;
"
`);
});
it("doesn't add a typecast if noImplicitAny is not set", () => {
const formatGeneratedModule = formatterFactory();
expect(
formatGeneratedModule({
moduleName: "complete-example",
// @ts-ignore
documentType: "ConcreteFragment",
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } } }),
typeText: "export type CompleteExample = { readonly id: string }",
hash: null,
sourceHash: "edcba",
}),
).toMatchInlineSnapshot(`
"/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
import { ConcreteFragment } from \\"relay-runtime\\";
export type CompleteExample = { readonly id: string }
const node: ConcreteFragment = {\\"the\\":{\\"fragment\\":{\\"data\\":42}}};
(node as any).hash = 'edcba';
export default node;
"
`);
});
it("doesn't replace cjs require calls if module system is not esm", () => {
const formatGeneratedModule = formatterFactory();
expect(
formatGeneratedModule({
moduleName: "complete-example",
// @ts-ignore
documentType: "ReaderFragment",
docText: null,
concreteText: JSON.stringify({
the: {
fragment: {
metadata: {
refetch: {
operation: "require('./RefetchableFragmentQuery.graphql.ts')",
},
},
},
},
}),
typeText: "export type CompleteExample = { readonly id: string }",
hash: null,
sourceHash: "edcba",
}),
).toMatchInlineSnapshot(`
"/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
import { ReaderFragment } from \\"relay-runtime\\";
export type CompleteExample = { readonly id: string }
const node: ReaderFragment = {\\"the\\":{\\"fragment\\":{\\"metadata\\":{\\"refetch\\":{\\"operation\\":\\"require('./RefetchableFragmentQuery.graphql.ts')\\"}}}}};
(node as any).hash = 'edcba';
export default node;
"
`);
});
it("replaces cjs require calls by (top-level) esm imports if module system is esm", () => {
const formatGeneratedModule = formatterFactory({
module: ts.ModuleKind.ES2015,
});
expect(
formatGeneratedModule({
moduleName: "complete-example",
// @ts-ignore
documentType: "ReaderFragment",
docText: null,
concreteText: JSON.stringify({
the: {
fragment: {
metadata: {
refetch: {
operation: "require('./RefetchableFragmentQuery.graphql.ts')",
},
},
},
},
}),
typeText: "export type CompleteExample = { readonly id: string }",
hash: null,
sourceHash: "edcba",
}),
).toMatchInlineSnapshot(`
"/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
import { ReaderFragment } from \\"relay-runtime\\";
import RefetchableFragmentQuery from \\"./RefetchableFragmentQuery.graphql\\";
export type CompleteExample = { readonly id: string }
const node: ReaderFragment = {\\"the\\":{\\"fragment\\":{\\"metadata\\":{\\"refetch\\":{\\"operation\\":\\"RefetchableFragmentQuery\\"}}}}};
(node as any).hash = 'edcba';
export default node;
"
`);
});
});

Просмотреть файл

@ -1,3 +1,8 @@
/**
* Taken from https://github.com/relay-tools/relay-compiler-language-typescript/blob/3231ea09205cc341be704c00e78db0d8ff78b34a/src/addAnyTypeCast.ts
* Copyright 2018 Kaare Hoff Skovgaard kaare@kaareskovgaard.net, Eloy Durán eloy.de.enige@gmail.com
*/
import * as ts from "typescript";
export default function addAsAnyToObjectLiterals(oldSource: string): string {

Просмотреть файл

@ -1,104 +0,0 @@
import { FormatModule } from "relay-compiler";
import * as ts from "typescript";
import addAnyTypeCast from "./addAnyTypeCast";
const createRequireRegex = () => /require\('(.*)'\)/g;
function getModuleName(path: string) {
const [moduleName] = path.replace("./", "").split(".");
return moduleName;
}
// collects all require calls and converts them top-level imports
const requireToImport = (content: string): string => {
const requireRegex = createRequireRegex();
// collect all require paths (unique)
const requirePaths = new Set<string>();
// eslint-disable-next-line no-constant-condition
while (true) {
const res = requireRegex.exec(content);
if (res === null) {
break;
}
requirePaths.add(res[1]);
}
// replace all require paths
Array.from(requirePaths).forEach((requirePath) => {
content = content.replace(
`require('${requirePath}')`,
getModuleName(requirePath),
);
});
// create top-level imports
const topLevelImports = Array.from(requirePaths)
.sort()
.map(
(requirePath) =>
`import ${getModuleName(requirePath)} from "${requirePath.replace(
".ts",
"",
)}";`,
);
// add top-level imports
content = `${topLevelImports.join("\n")}
${content}`;
return content;
};
type FormatContentOptions = {
replaceRequire: boolean;
};
function formatContent(
rawContent: string,
options: FormatContentOptions,
): string {
if (!options.replaceRequire) {
return rawContent;
}
return requireToImport(rawContent);
}
export const formatterFactory =
(compilerOptions: ts.CompilerOptions = {}): FormatModule =>
({
moduleName: _moduleName,
documentType,
docText,
concreteText,
typeText,
hash,
sourceHash,
}) => {
const { noImplicitAny, module = -1 } = compilerOptions;
const documentTypeImport = documentType
? `import { ${documentType} } from "relay-runtime";`
: "";
const docTextComment = docText ? "\n/*\n" + docText.trim() + "\n*/\n" : "";
let nodeStatement = `const node: ${
documentType || "never"
} = ${concreteText};`;
if (noImplicitAny) {
nodeStatement = addAnyTypeCast(nodeStatement).trim();
}
const rawContent = `${typeText || ""}
${docTextComment}
${nodeStatement}
(node as any).hash = '${sourceHash}';
export default node;
`;
const banner = compilerOptions.banner ? compilerOptions.banner + "\n" : "";
const content = `${banner}/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
${hash ? `/* ${hash} */\n` : ""}
${documentTypeImport}
${formatContent(rawContent, {
replaceRequire: module >= ts.ModuleKind.ES2015,
})}`;
return content;
};