Module: TypeScript::Merge
- Extended by:
- Merge
- Included in:
- Merge
- Defined in:
- lib/typescript/merge.rb,
lib/typescript/merge/version.rb
Defined Under Namespace
Modules: Version
Constant Summary collapse
- PACKAGE_NAME =
"typescript-merge"- TREE_SITTER_BACKEND =
TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND
- DESTINATION_WINS_ARRAY_POLICY =
{ surface: "array", name: "destination_wins_array" }.freeze
- VERSION =
Version::VERSION
Instance Method Summary collapse
- #available_type_script_backends ⇒ Object
- #match_type_script_owners(template, destination) ⇒ Object
- #merge_type_script(template_source, destination_source, dialect) ⇒ Object
- #parse_type_script(source, dialect) ⇒ Object
- #type_script_backend_feature_profile(backend: nil) ⇒ Object
- #type_script_feature_profile ⇒ Object
- #type_script_plan_context(backend: nil) ⇒ Object
Instance Method Details
#available_type_script_backends ⇒ Object
18 19 20 |
# File 'lib/typescript/merge.rb', line 18 def available_type_script_backends [TREE_SITTER_BACKEND] end |
#match_type_script_owners(template, destination) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/typescript/merge.rb', line 55 def match_type_script_owners(template, destination) destination_paths = destination[:owners].to_h { |owner| [owner[:path], true] } template_paths = template[:owners].to_h { |owner| [owner[:path], true] } { matched: template[:owners].filter { |owner| destination_paths[owner[:path]] }.map { |owner| { template_path: owner[:path], destination_path: owner[:path] } }, unmatched_template: template[:owners].map { |owner| owner[:path] }.reject { |path| destination_paths[path] }, unmatched_destination: destination[:owners].map { |owner| owner[:path] }.reject { |path| template_paths[path] } } end |
#merge_type_script(template_source, destination_source, dialect) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/typescript/merge.rb', line 65 def merge_type_script(template_source, destination_source, dialect) template = parse_type_script(template_source, dialect) return { ok: false, diagnostics: template[:diagnostics], policies: [] } unless template[:ok] destination = parse_type_script(destination_source, dialect) unless destination[:ok] return { ok: false, diagnostics: destination[:diagnostics].map { |diagnostic| diagnostic[:category] == "parse_error" ? diagnostic.merge(category: "destination_parse_error") : diagnostic }, policies: [] } end destination_declarations = destination.dig(:analysis, :declarations).to_h { |item| [item[:path], item] } merged_declaration_texts = destination.dig(:analysis, :declarations).map { |item| item[:text] } + template.dig(:analysis, :declarations).reject { |item| destination_declarations[item[:path]] }.map { |item| item[:text] } import_block = destination.dig(:analysis, :imports).map { |item| item[:text] }.join declaration_block = merged_declaration_texts.join("\n").rstrip sections = [import_block.rstrip, declaration_block].reject(&:empty?) { ok: true, diagnostics: [], output: "#{sections.join("\n\n").rstrip}\n", policies: [DESTINATION_WINS_ARRAY_POLICY] } end |
#parse_type_script(source, dialect) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/typescript/merge.rb', line 47 def parse_type_script(source, dialect) requested = TREE_SITTER_BACKEND.id return unsupported_feature_result("Unsupported TypeScript backend #{requested}.") unless requested == TREE_SITTER_BACKEND.id return analyze_type_script_module(source) if dialect == "typescript" { ok: false, diagnostics: [{ severity: "error", category: "unsupported_feature", message: "Unsupported TypeScript dialect #{dialect}." }], policies: [] } end |
#type_script_backend_feature_profile(backend: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/typescript/merge.rb', line 22 def type_script_backend_feature_profile(backend: nil) requested = backend.to_s.empty? ? TREE_SITTER_BACKEND.id : backend.to_s return unsupported_feature_result("Unsupported TypeScript backend #{requested}.") unless requested == TREE_SITTER_BACKEND.id type_script_feature_profile.merge( backend: requested, backend_ref: TREE_SITTER_BACKEND.to_h, supports_dialects: true ) end |
#type_script_feature_profile ⇒ Object
14 15 16 |
# File 'lib/typescript/merge.rb', line 14 def type_script_feature_profile { family: "typescript", supported_dialects: ["typescript"], supported_policies: [DESTINATION_WINS_ARRAY_POLICY] } end |
#type_script_plan_context(backend: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/typescript/merge.rb', line 33 def type_script_plan_context(backend: nil) profile = type_script_backend_feature_profile(backend: backend) return profile if profile[:ok] == false { family_profile: type_script_feature_profile, feature_profile: { backend: profile[:backend], supports_dialects: true, supported_policies: profile[:supported_policies] } } end |