-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support case sensitive (#1714)
* feat: add CaseSensitivePlugin * FIX: update path check * fix: clippy error * fix: 调整判断 * fix: 修复目录读取 * fix: 修改参数名称 * fix: 调整测试方式添加e2e断言,并增加配置项只有mac系统下才需要开启相关配置 * fix: 删除过滤条件 * fix: 删除多余测试文件,增加更新内容 * fix: 删除多余log * fix: 测试执行 * fix: 添加测试系统 * fix: 调整顺序 * fix: 添加测试系统 * fix: 非mac环境断言 * fix: 引用遗漏 * fix: 断言 * fix: 删除默认 * fix: 默认配置 * fix: 测试覆盖 * Revert "fix: 测试覆盖" This reverts commit b0a2e66. --------- Co-authored-by: shikuan.sk <shikuan.sk@antgroup.com>
- Loading branch information
Showing
14 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,95 @@ | ||
use std::collections::HashMap; | ||
use std::fs; | ||
use std::path::Path; | ||
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, load_param: &PluginLoadParam, root: &String) -> bool { | ||
let file_path = &load_param.file.path; | ||
if !file_path.starts_with(root) { | ||
return false; | ||
} | ||
for component in file_path.iter() { | ||
if component.eq_ignore_ascii_case("node_modules") { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
|
||
pub fn check_case_sensitive(&self, file: &Path, root: &str) -> String { | ||
// 可变变量,在循环内会被修改 | ||
let mut file_path = file.to_path_buf(); | ||
let mut case_name = String::new(); | ||
// 缓存map,file path做为key存在对应路径下的文件名和文件夹名 | ||
let mut cache_map = self.cache_map.lock().unwrap_or_else(|e| e.into_inner()); | ||
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(dir) { | ||
files.for_each(|entry| { | ||
entries.push(entry.unwrap().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.to_string(); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
case_name | ||
} | ||
} | ||
|
||
impl Plugin for CaseSensitivePlugin { | ||
fn name(&self) -> &str { | ||
"case_sensitive_plugin" | ||
} | ||
|
||
fn load( | ||
&self, | ||
load_param: &PluginLoadParam, | ||
context: &Arc<Context>, | ||
) -> Result<Option<Content>> { | ||
let root = &context.root.to_string_lossy().to_string(); | ||
if self.is_checkable(load_param, root) { | ||
let dist_path = self.check_case_sensitive(load_param.file.path.as_path(), root); | ||
if !dist_path.is_empty() { | ||
return Err(anyhow!( | ||
"{} does not match the corresponding path on disk [{}]", | ||
load_param.file.path.to_string_lossy().to_string(), | ||
dist_path | ||
)); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
} |
This file contains 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 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
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 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,20 @@ | ||
const assert = require("assert"); | ||
const os = require("os"); | ||
|
||
module.exports = (err) => { | ||
if (os.platform() === "darwin") { | ||
assert( | ||
err.stderr.includes( | ||
`/Assets/umi-logo.png does not match the corresponding path on disk [assets]` | ||
), | ||
"should throw error" | ||
); | ||
} else { | ||
assert( | ||
err.stderr.includes( | ||
`Module not found: Can't resolve './Assets/umi-logo.png'` | ||
), | ||
"should throw error" | ||
); | ||
} | ||
}; |
This file contains 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,4 @@ | ||
import UmiLogo from "./Assets/umi-logo.png"; | ||
console.log(UmiLogo); | ||
|
||
export default UmiLogo; |
This file contains 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,4 @@ | ||
{ | ||
"mode": "production", | ||
"caseSensitiveCheck": true | ||
} |
This file contains 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" | ||
} | ||
} |