aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/linters/no_use_as_alias.rb
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-05-11 19:45:17 +0900
committernsfisis <nsfisis@gmail.com>2026-05-11 19:45:17 +0900
commit24cc697a9cd0dcac854359d65b8265f02f483b72 (patch)
treec9693dbf3136d840157609161a3a5695828e853b /scripts/linters/no_use_as_alias.rb
parent2aceeb116150b6d6e6d3f371c2af509902ceafea (diff)
downloadphp-mozart-24cc697a9cd0dcac854359d65b8265f02f483b72.tar.gz
php-mozart-24cc697a9cd0dcac854359d65b8265f02f483b72.tar.zst
php-mozart-24cc697a9cd0dcac854359d65b8265f02f483b72.zip
chore(lint): add Ruby linter scripts and apply rules
Adds scripts/lint with linters for mod.rs naming, contiguous use blocks, use-as aliasing, sorted Cargo dependencies, std::collections maps, and workspace dependency requirements. Renames mod.rs files, reorders use statements, drops unnecessary import aliases, and sorts Cargo.toml entries to satisfy the new rules.
Diffstat (limited to 'scripts/linters/no_use_as_alias.rb')
-rw-r--r--scripts/linters/no_use_as_alias.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/scripts/linters/no_use_as_alias.rb b/scripts/linters/no_use_as_alias.rb
new file mode 100644
index 0000000..adb2e67
--- /dev/null
+++ b/scripts/linters/no_use_as_alias.rb
@@ -0,0 +1,51 @@
+def no_use_as_alias(root_dir)
+ pattern = root_dir.join('crates', '**', '*.rs').to_s
+ errors = Dir.glob(pattern).sort.flat_map do |path|
+ relative = Pathname.new(path).relative_path_from(root_dir).to_s
+ find_use_aliases(path, relative)
+ end
+
+ return true if errors.empty?
+
+ puts 'Found `use ... as Name` aliases.'
+ puts 'Renaming imports is forbidden; only unnamed imports `as _` (e.g. `use std::io::Write as _;`) are allowed:'
+ errors.each do |err|
+ puts " #{err}"
+ end
+ false
+end
+
+USE_ALIAS_START_RE = /\A(?:pub(?:\([^)]*\))?\s+)?use\b/
+
+def find_use_aliases(path, relative)
+ errors = []
+ in_use = false
+ brace_depth = 0
+
+ File.readlines(path).each_with_index do |raw, idx|
+ code = raw.split('//', 2).first || raw
+ stripped = code.strip
+
+ unless in_use
+ next unless stripped =~ USE_ALIAS_START_RE
+
+ in_use = true
+ brace_depth = 0
+ end
+
+ code.scan(/\bas\s+([A-Za-z_][A-Za-z0-9_]*)/) do |m|
+ name = m[0]
+ next if name == '_'
+
+ errors << "#{relative}:#{idx + 1}: `as #{name}` aliasing in `use` statement"
+ end
+
+ brace_depth += code.count('{') - code.count('}')
+ if brace_depth <= 0 && code.rstrip.end_with?(';')
+ in_use = false
+ brace_depth = 0
+ end
+ end
+
+ errors
+end