用于生成import节点,首先会检查是否已经存在这个路径,如果有则直接合并
反之生成一个新的import节点并插入在最后一行import后面
// 已经存在这个路径的导入用例
test_inline!(
Default::default(),
|_| visit_mut_pass(TransformVisitor::with_confg(PluginConfig {
default_import: "a".to_string(),
name_import: vec![
"throttle".to_string(),
"debounce".to_string(),
"deepClone".to_string()
],
path: "lodash".to_string()
})),
// Test case name
merge_exist_default_with_name_import,
// Input code (with existing imports)
r#"
import a,{deepClone,isEqual} from 'lodash';
import b ,{ isEqual } from 'lodash';
"#,
// Expected output (with new import added)
r#"
import a,{deepClone,isEqual,throttle,debounce } from 'lodash';
"#
);
// 存在as重命名的导入
test_inline!(
Default::default(),
|_| visit_mut_pass(TransformVisitor::with_confg(PluginConfig {
default_import: "a".to_string(),
name_import: vec![
"throttle".to_string(),
"debounce".to_string(),
"deepClone".to_string()
],
path: "lodash".to_string()
})),
// Test case name
merge_exist_default_with_as_import,
// Input code (with existing imports)
r#"
import a,{deepClone,isEqual} from 'lodash';
import b ,{ isEqual as iE} from 'lodash';
"#,
// Expected output (with new import added)
r#"
import a,{deepClone,isEqual,isEqual as iE, throttle, debounce } from 'lodash';
"#
);
// 存在as重命名的导入并且插件配置传入了这个具名导入
test_inline!(
Default::default(),
|_| visit_mut_pass(TransformVisitor::with_confg(PluginConfig {
default_import: "a".to_string(),
name_import: vec![
"throttle".to_string(),
"debounce".to_string(),
"deepClone".to_string()
],
path: "lodash".to_string()
})),
// Test case name
merge_exist_with_as_import_plugin,
// Input code (with existing imports)
r#"
import a,{deepClone as dC,isEqual} from 'lodash';
import b ,{ isEqual as iE} from 'lodash';
"#,
// Expected output (with new import added)
r#"
import a,{deepClone as dC,isEqual,isEqual as iE, throttle, debounce } from 'lodash';
"#
);
test_inline!(
Default::default(),
|_| visit_mut_pass(TransformVisitor::with_confg(PluginConfig {
default_import: "a".to_string(),
name_import: vec![],
path: "lodash".to_string()
})),
// Test case name
no_default_import,
// Input code (with existing imports)
r#"
import { isEqual } from 'lodash';
"#,
// Expected output (with new import added)
r#"
import a,{isEqual } from 'lodash';
"#
);
//之前不存在已经导入的
//默认导入
test_inline!(
Default::default(),
|_| visit_mut_pass(TransformVisitor::with_confg(PluginConfig {
default_import: "a".to_string(),
name_import: vec![],
path: "lodash".to_string()
})),
// Test case name
add_default_import,
// Input code (with existing imports)
r#"
import { ElMessage } from 'element-plus'
import { useThrottleFn } from '@vueuse/core'
import useUser from '@/hooks/useUser'
import { useCart } from '@/store/useCart'
const router = useRouter()
const search = ref('')
"#,
// Expected output (with new import added)
r#"
import { ElMessage } from 'element-plus'
import { useThrottleFn } from '@vueuse/core'
import useUser from '@/hooks/useUser'
import { useCart } from '@/store/useCart'
import a from 'lodash';
const router = useRouter()
const search = ref('')
"#
);
//具名导入
test_inline!(
Default::default(),
|_| visit_mut_pass(TransformVisitor::with_confg(PluginConfig {
default_import: "a".to_string(),
name_import: vec![
"throttle".to_string(),
"debounce".to_string()
],
path: "lodash".to_string()
})),
// Test case name
add_all_import,
// Input code (with existing imports)
r#"
import { ElMessage } from 'element-plus'
import { useThrottleFn } from '@vueuse/core'
import useUser from '@/hooks/useUser'
import { useCart } from '@/store/useCart'
const router = useRouter()
const search = ref('')
"#,
// Expected output (with new import added)
r#"
import { ElMessage } from 'element-plus'
import { useThrottleFn } from '@vueuse/core'
import useUser from '@/hooks/useUser'
import { useCart } from '@/store/useCart'
import a,{throttle,debounce} from 'lodash';
const router = useRouter()
const search = ref('')
"#
);
-
Notifications
You must be signed in to change notification settings - Fork 0
dddssw/add-import-plugin
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published