refactor: replace 'let' with 'const' for better variable scoping and immutability

This commit is contained in:
Tunglies
2025-09-18 23:07:18 +08:00
parent 9d96ac0f6a
commit 324628dd3d
17 changed files with 154 additions and 121 deletions

View File

@@ -287,8 +287,8 @@ export const RulesEditorViewer = (props: Props) => {
const { active, over } = event;
if (over) {
if (active.id !== over.id) {
let activeIndex = prependSeq.indexOf(active.id.toString());
let overIndex = prependSeq.indexOf(over.id.toString());
const activeIndex = prependSeq.indexOf(active.id.toString());
const overIndex = prependSeq.indexOf(over.id.toString());
setPrependSeq(reorder(prependSeq, activeIndex, overIndex));
}
}
@@ -297,15 +297,15 @@ export const RulesEditorViewer = (props: Props) => {
const { active, over } = event;
if (over) {
if (active.id !== over.id) {
let activeIndex = appendSeq.indexOf(active.id.toString());
let overIndex = appendSeq.indexOf(over.id.toString());
const activeIndex = appendSeq.indexOf(active.id.toString());
const overIndex = appendSeq.indexOf(over.id.toString());
setAppendSeq(reorder(appendSeq, activeIndex, overIndex));
}
}
};
const fetchContent = async () => {
let data = await readProfileFile(property);
let obj = yaml.load(data) as ISeqProfileConfig | null;
const data = await readProfileFile(property);
const obj = yaml.load(data) as ISeqProfileConfig | null;
setPrependSeq(obj?.prepend || []);
setAppendSeq(obj?.append || []);
@@ -319,7 +319,7 @@ export const RulesEditorViewer = (props: Props) => {
if (currData === "") return;
if (visualization !== true) return;
let obj = yaml.load(currData) as ISeqProfileConfig | null;
const obj = yaml.load(currData) as ISeqProfileConfig | null;
setPrependSeq(obj?.prepend || []);
setAppendSeq(obj?.append || []);
setDeleteSeq(obj?.delete || []);
@@ -349,21 +349,21 @@ export const RulesEditorViewer = (props: Props) => {
}, [prependSeq, appendSeq, deleteSeq]);
const fetchProfile = async () => {
let data = await readProfileFile(profileUid); // 原配置文件
let groupsData = await readProfileFile(groupsUid); // groups配置文件
let mergeData = await readProfileFile(mergeUid); // merge配置文件
let globalMergeData = await readProfileFile("Merge"); // global merge配置文件
const data = await readProfileFile(profileUid); // 原配置文件
const groupsData = await readProfileFile(groupsUid); // groups配置文件
const mergeData = await readProfileFile(mergeUid); // merge配置文件
const globalMergeData = await readProfileFile("Merge"); // global merge配置文件
let rulesObj = yaml.load(data) as { rules: [] } | null;
const rulesObj = yaml.load(data) as { rules: [] } | null;
let originGroupsObj = yaml.load(data) as { "proxy-groups": [] } | null;
let originGroups = originGroupsObj?.["proxy-groups"] || [];
let moreGroupsObj = yaml.load(groupsData) as ISeqProfileConfig | null;
let morePrependGroups = moreGroupsObj?.["prepend"] || [];
let moreAppendGroups = moreGroupsObj?.["append"] || [];
let moreDeleteGroups =
const originGroupsObj = yaml.load(data) as { "proxy-groups": [] } | null;
const originGroups = originGroupsObj?.["proxy-groups"] || [];
const moreGroupsObj = yaml.load(groupsData) as ISeqProfileConfig | null;
const morePrependGroups = moreGroupsObj?.["prepend"] || [];
const moreAppendGroups = moreGroupsObj?.["append"] || [];
const moreDeleteGroups =
moreGroupsObj?.["delete"] || ([] as string[] | { name: string }[]);
let groups = morePrependGroups.concat(
const groups = morePrependGroups.concat(
originGroups.filter((group: any) => {
if (group.name) {
return !moreDeleteGroups.includes(group.name);
@@ -374,27 +374,37 @@ export const RulesEditorViewer = (props: Props) => {
moreAppendGroups,
);
let originRuleSetObj = yaml.load(data) as { "rule-providers": {} } | null;
let originRuleSet = originRuleSetObj?.["rule-providers"] || {};
let moreRuleSetObj = yaml.load(mergeData) as {
const originRuleSetObj = yaml.load(data) as { "rule-providers": {} } | null;
const originRuleSet = originRuleSetObj?.["rule-providers"] || {};
const moreRuleSetObj = yaml.load(mergeData) as {
"rule-providers": {};
} | null;
let moreRuleSet = moreRuleSetObj?.["rule-providers"] || {};
let globalRuleSetObj = yaml.load(globalMergeData) as {
const moreRuleSet = moreRuleSetObj?.["rule-providers"] || {};
const globalRuleSetObj = yaml.load(globalMergeData) as {
"rule-providers": {};
} | null;
let globalRuleSet = globalRuleSetObj?.["rule-providers"] || {};
let ruleSet = Object.assign({}, originRuleSet, moreRuleSet, globalRuleSet);
const globalRuleSet = globalRuleSetObj?.["rule-providers"] || {};
const ruleSet = Object.assign(
{},
originRuleSet,
moreRuleSet,
globalRuleSet,
);
let originSubRuleObj = yaml.load(data) as { "sub-rules": {} } | null;
let originSubRule = originSubRuleObj?.["sub-rules"] || {};
let moreSubRuleObj = yaml.load(mergeData) as { "sub-rules": {} } | null;
let moreSubRule = moreSubRuleObj?.["sub-rules"] || {};
let globalSubRuleObj = yaml.load(globalMergeData) as {
const originSubRuleObj = yaml.load(data) as { "sub-rules": {} } | null;
const originSubRule = originSubRuleObj?.["sub-rules"] || {};
const moreSubRuleObj = yaml.load(mergeData) as { "sub-rules": {} } | null;
const moreSubRule = moreSubRuleObj?.["sub-rules"] || {};
const globalSubRuleObj = yaml.load(globalMergeData) as {
"sub-rules": {};
} | null;
let globalSubRule = globalSubRuleObj?.["sub-rules"] || {};
let subRule = Object.assign({}, originSubRule, moreSubRule, globalSubRule);
const globalSubRule = globalSubRuleObj?.["sub-rules"] || {};
const subRule = Object.assign(
{},
originSubRule,
moreSubRule,
globalSubRule,
);
setProxyPolicyList(
builtinProxyPolicies.concat(groups.map((group: any) => group.name)),
);
@@ -554,7 +564,7 @@ export const RulesEditorViewer = (props: Props) => {
startIcon={<VerticalAlignTopRounded />}
onClick={() => {
try {
let raw = validateRule();
const raw = validateRule();
if (prependSeq.includes(raw)) return;
setPrependSeq([raw, ...prependSeq]);
} catch (err: any) {
@@ -572,7 +582,7 @@ export const RulesEditorViewer = (props: Props) => {
startIcon={<VerticalAlignBottomRounded />}
onClick={() => {
try {
let raw = validateRule();
const raw = validateRule();
if (appendSeq.includes(raw)) return;
setAppendSeq([...appendSeq, raw]);
} catch (err: any) {
@@ -601,7 +611,7 @@ export const RulesEditorViewer = (props: Props) => {
}
increaseViewportBy={256}
itemContent={(index) => {
let shift = filteredPrependSeq.length > 0 ? 1 : 0;
const shift = filteredPrependSeq.length > 0 ? 1 : 0;
if (filteredPrependSeq.length > 0 && index === 0) {
return (
<DndContext
@@ -632,7 +642,7 @@ export const RulesEditorViewer = (props: Props) => {
</DndContext>
);
} else if (index < filteredRuleList.length + shift) {
let newIndex = index - shift;
const newIndex = index - shift;
return (
<RuleItem
key={`${filteredRuleList[newIndex]}-${index}`}