|
1 | | -import { SourceFile } from "coli"; |
| 1 | +import { |
| 2 | + SourceFile, |
| 3 | + BlockStatement, |
| 4 | + FunctionDeclaration, |
| 5 | + Import, |
| 6 | + ImportDeclaration, |
| 7 | + Return, |
| 8 | + Declaration, |
| 9 | +} from "coli"; |
| 10 | +import { SyntaxKind } from "@coli.codes/core-syntax-kind"; |
| 11 | +import { react as react_config } from "@designto/config"; |
| 12 | +import { |
| 13 | + add_export_keyword_modifier_to_declaration, |
| 14 | + wrap_with_export_assignment_react_component_identifier, |
| 15 | +} from "../react-component-exporting"; |
2 | 16 |
|
3 | 17 | export class ReactModuleFile extends SourceFile { |
4 | 18 | constructor({ name, path }: { name: string; path: string }) { |
5 | 19 | super({ name, path }); |
6 | 20 | } |
7 | 21 | } |
| 22 | + |
| 23 | +export function makeReactModuleFile({ |
| 24 | + name, |
| 25 | + path, |
| 26 | + imports, |
| 27 | + body, |
| 28 | + declarations, |
| 29 | + config, |
| 30 | +}: { |
| 31 | + name: string; |
| 32 | + path: string; |
| 33 | + imports: ImportDeclaration[]; |
| 34 | + body: BlockStatement; |
| 35 | + declarations: Declaration[]; |
| 36 | + config: { |
| 37 | + exporting: react_config.ReactComponentExportingCofnig; |
| 38 | + }; |
| 39 | +}) { |
| 40 | + const { exporting } = config; |
| 41 | + const file = new ReactModuleFile({ |
| 42 | + name: `${name}.tsx`, |
| 43 | + path: path, |
| 44 | + }); |
| 45 | + file.imports(...imports); |
| 46 | + |
| 47 | + // console.log("exporting", exporting); |
| 48 | + switch (exporting.type) { |
| 49 | + case "export-default-anonymous-functional-component": { |
| 50 | + // exporting.declaration_syntax_choice; |
| 51 | + // exporting.export_declaration_syntax_choice; |
| 52 | + // exporting.exporting_position; |
| 53 | + |
| 54 | + const export_default_anaonymous_functional_component = |
| 55 | + new FunctionDeclaration(undefined, { |
| 56 | + body: body, |
| 57 | + modifiers: { |
| 58 | + default: SyntaxKind.DefaultKeyword, |
| 59 | + export: SyntaxKind.ExportKeyword, |
| 60 | + }, |
| 61 | + }); |
| 62 | + file.declare(export_default_anaonymous_functional_component); |
| 63 | + file.declare(...declarations); |
| 64 | + break; |
| 65 | + } |
| 66 | + case "export-named-functional-component": { |
| 67 | + // exporting.declaration_syntax_choice; |
| 68 | + // exporting.export_declaration_syntax_choice; |
| 69 | + |
| 70 | + const named_function_declaration = new FunctionDeclaration(name, { |
| 71 | + body: body, |
| 72 | + }); |
| 73 | + |
| 74 | + switch (exporting.exporting_position) { |
| 75 | + case "after-declaration": |
| 76 | + file.declare(named_function_declaration); |
| 77 | + file.export( |
| 78 | + wrap_with_export_assignment_react_component_identifier( |
| 79 | + named_function_declaration.id |
| 80 | + ) |
| 81 | + ); |
| 82 | + file.declare(...declarations); |
| 83 | + break; |
| 84 | + case "end-of-file": |
| 85 | + file.declare(named_function_declaration); |
| 86 | + file.declare(...declarations); |
| 87 | + file.export( |
| 88 | + wrap_with_export_assignment_react_component_identifier( |
| 89 | + named_function_declaration.id |
| 90 | + ) |
| 91 | + ); |
| 92 | + break; |
| 93 | + case "with-declaration": |
| 94 | + const _exported_named_function_declaration = |
| 95 | + add_export_keyword_modifier_to_declaration<FunctionDeclaration>( |
| 96 | + named_function_declaration |
| 97 | + ); |
| 98 | + file.declare(_exported_named_function_declaration); |
| 99 | + file.declare(...declarations); |
| 100 | + break; |
| 101 | + } |
| 102 | + break; |
| 103 | + } |
| 104 | + case "export-named-class-component": |
| 105 | + break; |
| 106 | + case "export-anonymous-class-component": |
| 107 | + throw new Error("Class component not supported"); |
| 108 | + } |
| 109 | + |
| 110 | + return file; |
| 111 | +} |
0 commit comments