-
Notifications
You must be signed in to change notification settings - Fork 108
feat: add CaseSensitivePlugin #1713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
The head ref may contain hidden characters: "case\u2014sensitive"
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| use std::collections::HashMap; | ||
| use std::fs; | ||
| use std::path::PathBuf; | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
| use anyhow::{anyhow, Result}; | ||
|
|
||
| use crate::ast::file::Content; | ||
| use crate::compiler::Context; | ||
| use crate::plugin::{Plugin, PluginLoadParam}; | ||
|
|
||
| pub struct CaseSensitivePlugin { | ||
| cache_map: Arc<Mutex<HashMap<String, Vec<String>>>>, | ||
| } | ||
|
|
||
| impl CaseSensitivePlugin { | ||
| pub fn new() -> Self { | ||
| CaseSensitivePlugin { | ||
| cache_map: Default::default(), | ||
| } | ||
| } | ||
|
|
||
| pub fn is_checkable(&self, _param: &PluginLoadParam, root: &String) -> bool { | ||
| let file_path = &_param.file.path.to_string_lossy().to_string(); | ||
| if !file_path.starts_with(root) { | ||
| return false; | ||
| } | ||
| if file_path.contains("/node_modules/") { | ||
| return false; | ||
| } | ||
| true | ||
| } | ||
|
|
||
| pub fn check_case_sensitive(&self, file: &PathBuf, root: &String) -> String { | ||
| // 可变变量,在循环内会被修改 | ||
| let mut file_path = file.clone(); | ||
| let mut case_name = String::new(); | ||
| // 缓存map,file path做为key存在对应路径下的文件名和文件夹名 | ||
| let mut cache_map = self.cache_map.lock().unwrap(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider handling the potential |
||
| while file_path.to_string_lossy().len() >= root.len() { | ||
| if let Some(current) = file_path.file_name() { | ||
| let current_str = current.to_string_lossy().to_string(); | ||
| file_path.pop(); // parent directory | ||
| let mut entries: Vec<String> = Vec::new(); | ||
| if let Some(dir) = file_path.to_str() { | ||
| if let Some(i) = cache_map.get(dir as &str) { | ||
| entries = i.to_vec(); | ||
| } else { | ||
| if let Ok(files) = fs::read_dir(file_path.clone()) { | ||
| for entry in files { | ||
| if let Ok(entry) = entry { | ||
| // Here, `entry` is a `DirEntry`. | ||
| entries.push(entry.file_name().to_string_lossy().to_string()); | ||
| } | ||
| } | ||
| } | ||
| cache_map.insert(dir.to_string(), entries.to_vec()); | ||
| } | ||
| } | ||
| if !entries.contains(¤t_str) { | ||
| if let Some(correct_name) = entries | ||
| .iter() | ||
| .find(|&x| x.to_lowercase() == current_str.to_lowercase()) | ||
| { | ||
| case_name = correct_name.clone(); | ||
| println!( | ||
| "File name is case-insensitive. Correct name is: {}", | ||
| correct_name | ||
| ); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| case_name | ||
| } | ||
| } | ||
|
|
||
| impl Plugin for CaseSensitivePlugin { | ||
| fn name(&self) -> &str { | ||
| "case_sensitive_plugin" | ||
| } | ||
|
|
||
| fn load(&self, _param: &PluginLoadParam, _context: &Arc<Context>) -> Result<Option<Content>> { | ||
| println!("case_sensitive_plugin"); | ||
| let root = &_context.root.to_string_lossy().to_string(); | ||
| if self.is_checkable(_param, root) { | ||
| let dist_path = self.check_case_sensitive(&_param.file.path, root); | ||
| if !dist_path.is_empty() { | ||
| return Err(anyhow!( | ||
| "{} does not match the corresponding path on disk [{}]", | ||
| _param.file.path.to_string_lossy().to_string(), | ||
| dist_path | ||
| )); | ||
| } | ||
| } | ||
| Ok(None) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::path::PathBuf; | ||
|
|
||
| use crate::ast::file::File; | ||
| use crate::plugin::Plugin; | ||
| use crate::plugins::case_sensitive::{CaseSensitivePlugin, PluginLoadParam}; | ||
| use crate::utils::test_helper::setup_compiler; | ||
|
|
||
| #[test] | ||
| fn test_case_sensitive_checker() { | ||
| let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test/build/case-sensitive"); | ||
| let compiler = setup_compiler("test/build/case-sensitive", false); | ||
| let plugin = CaseSensitivePlugin::new(); | ||
| let file = &File::new( | ||
| root.join("Assets/umi-logo.png") | ||
| .to_string_lossy() | ||
| .to_string(), | ||
| compiler.context.clone(), | ||
| ); | ||
| let result = plugin.load(&PluginLoadParam { file }, &compiler.context); | ||
| assert!(result.is_err()); | ||
| } | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| import UmiLogo from "./Assets/umi-logo.png"; | ||
| console.log(UmiLogo); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "duplicatePackageChecker": { | ||
| "verbose": true, | ||
| "showHelp": true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "name": "test", | ||
| "version": "1.0.0", | ||
| "dependencies": { | ||
| "a": "~1.0.0", | ||
| "b": "~1.0.0" | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
建议使用
Path方法来处理文件路径直接对文件路径进行字符串操作可能导致跨平台兼容性问题,建议使用
std::path::Path提供的方法(如starts_with、components等)来确保路径处理的正确性和跨平台兼容性。