1#![forbid(unsafe_code)]
2
3mod dependencies;
4mod entry_points;
5mod functions;
6mod go_mod;
7mod imports;
8mod lexer;
9mod resolution;
10mod rules;
11mod source_set;
12mod workspaces;
13
14use std::collections::BTreeSet;
15
16use codeint_model::language::{FileModel, LowerError, Token, TokenKind};
17use codeint_model::scan::CoreReferenceRanges;
18use codeint_model::token_text_hash;
19
20use crate::lexer::{GoKind, GoToken};
21
22pub use rules::flagged_line_rules;
23
24pub struct Go;
25
26fn lower_file(path: &str, text: &str) -> Result<FileModel, LowerError> {
27 if !path.ends_with(".go") {
28 return Err(LowerError::UnsupportedFile(format!(
29 "{path} is not a Go source file"
30 )));
31 }
32 let scanned = lexer::scan(text)?;
33 let source_lines = u32::try_from(scanned.code_lines.len()).map_err(|_| LowerError::TooLarge)?;
34 let scanned_imports = imports::scan(&scanned.tokens);
35 let extracted = functions::extract(&scanned.tokens, &scanned.code_lines);
36 let flagged_lines = rules::evaluate(&scanned.tokens, &extracted.spans, &scanned.code_lines);
37 let tokens = build_duplication_tokens(&scanned.tokens, &scanned_imports.marked_indices);
38 let source_set = source_set::classify(path, &scanned.header_line_comments);
39 Ok(FileModel {
40 runtime_imports: scanned_imports.specifiers,
41 flagged_lines,
42 tokens,
43 functions: extracted.functions,
44 source_lines,
45 program_start: scanned_imports.is_package_main,
46 ..codeint_model::empty_file_model(source_set)
47 })
48}
49
50fn build_duplication_tokens(raw: &[GoToken], import_indices: &BTreeSet<usize>) -> Vec<Token> {
51 raw.iter()
52 .enumerate()
53 .filter(|(_, tok)| tok.kind != GoKind::AutoSemi)
54 .map(|(idx, tok)| {
55 let kind = if import_indices.contains(&idx) {
56 TokenKind::ImportOrExport
57 } else if tok.kind == GoKind::Ident {
58 TokenKind::Identifier
59 } else if matches!(
60 tok.kind,
61 GoKind::StringLit | GoKind::RuneLit | GoKind::NumberLit
62 ) {
63 TokenKind::Literal
64 } else {
65 TokenKind::Other
66 };
67 Token {
68 kind,
69 text_hash: token_text_hash(&tok.text),
70 line: tok.line,
71 }
72 })
73 .collect()
74}
75
76fn lower_go(
77 _plugin: &Go,
78 path: &codeint_model::RepoPath,
79 text: &str,
80 _dialect: Option<&str>,
81) -> Result<FileModel, codeint_model::LowerError> {
82 lower_file(path.as_str(), text).map_err(codeint_model::LowerError::from)
83}
84
85const GO_RANGES: CoreReferenceRanges = CoreReferenceRanges::new(
86 (0.304, 0.746),
87 (0.306, 0.671),
88 (0.000, 0.004),
89 (0.034, 0.243),
90 Some((0.000, 0.000)),
91);
92
93codeint_model::impl_builtin_language_plugin!(
94 Go,
95 ["go"],
96 lower_go,
97 GO_RANGES.build("codeint stratified panel of 100 Go repositories, ADR 0035, codeint 0.8.0"),
98 flagged_lines(flagged_line_rules()),
99 imports(
100 resolution::find_go_mods,
101 workspaces::analyze_manifests,
102 |paths, manifests, requests, _| resolution::resolve_all(paths, manifests, requests, &[]),
103 entry_points::discover,
104 ),
105 frameworks(none),
106 hazards(none),
107);