style: 完成所有文件的lint

This commit is contained in:
claude-code-best
2026-05-01 21:39:30 +08:00
parent d136872cc9
commit 6182015005
1333 changed files with 68255 additions and 77882 deletions

View File

@@ -1,60 +1,51 @@
import { relative } from 'path'
import React, { useMemo } from 'react'
import { useDiffInIDE } from '../../../hooks/useDiffInIDE.js'
import { Box, Text } from '@anthropic/ink'
import type { ToolUseContext } from '../../../Tool.js'
import { getLanguageName } from '../../../utils/cliHighlight.js'
import { getCwd } from '../../../utils/cwd.js'
import {
getFsImplementation,
safeResolvePath,
} from '../../../utils/fsOperations.js'
import { expandPath } from '../../../utils/path.js'
import type { CompletionType } from '../../../utils/unaryLogging.js'
import { Select } from '../../CustomSelect/index.js'
import { ShowInIDEPrompt } from '../../ShowInIDEPrompt.js'
import { usePermissionRequestLogging } from '../hooks.js'
import { PermissionDialog } from '../PermissionDialog.js'
import type { ToolUseConfirm } from '../PermissionRequest.js'
import type { WorkerBadgeProps } from '../WorkerBadge.js'
import type { IDEDiffSupport } from './ideDiffConfig.js'
import type {
FileOperationType,
PermissionOption,
} from './permissionOptions.js'
import {
type ToolInput,
useFilePermissionDialog,
} from './useFilePermissionDialog.js'
import { relative } from 'path';
import React, { useMemo } from 'react';
import { useDiffInIDE } from '../../../hooks/useDiffInIDE.js';
import { Box, Text } from '@anthropic/ink';
import type { ToolUseContext } from '../../../Tool.js';
import { getLanguageName } from '../../../utils/cliHighlight.js';
import { getCwd } from '../../../utils/cwd.js';
import { getFsImplementation, safeResolvePath } from '../../../utils/fsOperations.js';
import { expandPath } from '../../../utils/path.js';
import type { CompletionType } from '../../../utils/unaryLogging.js';
import { Select } from '../../CustomSelect/index.js';
import { ShowInIDEPrompt } from '../../ShowInIDEPrompt.js';
import { usePermissionRequestLogging } from '../hooks.js';
import { PermissionDialog } from '../PermissionDialog.js';
import type { ToolUseConfirm } from '../PermissionRequest.js';
import type { WorkerBadgeProps } from '../WorkerBadge.js';
import type { IDEDiffSupport } from './ideDiffConfig.js';
import type { FileOperationType, PermissionOption } from './permissionOptions.js';
import { type ToolInput, useFilePermissionDialog } from './useFilePermissionDialog.js';
export type FilePermissionDialogProps<T extends ToolInput = ToolInput> = {
// Required props from PermissionRequestProps
toolUseConfirm: ToolUseConfirm
toolUseContext: ToolUseContext
onDone: () => void
onReject: () => void
toolUseConfirm: ToolUseConfirm;
toolUseContext: ToolUseContext;
onDone: () => void;
onReject: () => void;
// Dialog customization
title: string
subtitle?: React.ReactNode
question?: string | React.ReactNode
content?: React.ReactNode // Can be general content or diff component
title: string;
subtitle?: React.ReactNode;
question?: string | React.ReactNode;
content?: React.ReactNode; // Can be general content or diff component
// Logging
completionType?: CompletionType
languageName?: string // override — derived from path when omitted
completionType?: CompletionType;
languageName?: string; // override — derived from path when omitted
// File/directory operations
path: string | null
parseInput: (input: unknown) => T
operationType?: FileOperationType
path: string | null;
parseInput: (input: unknown) => T;
operationType?: FileOperationType;
// IDE diff support
ideDiffSupport?: IDEDiffSupport<T>
ideDiffSupport?: IDEDiffSupport<T>;
// Worker badge for teammate permission requests
workerBadge: WorkerBadgeProps | undefined
}
workerBadge: WorkerBadgeProps | undefined;
};
export function FilePermissionDialog<T extends ToolInput = ToolInput>({
toolUseConfirm,
@@ -80,28 +71,28 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
const languageName = useMemo(
() => languageNameOverride ?? (path ? getLanguageName(path) : 'none'),
[languageNameOverride, path],
)
);
const unaryEvent = useMemo(
() => ({
completion_type: completionType,
language_name: languageName,
}),
[completionType, languageName],
)
usePermissionRequestLogging(toolUseConfirm, unaryEvent)
);
usePermissionRequestLogging(toolUseConfirm, unaryEvent);
const symlinkTarget = useMemo(() => {
if (!path || operationType === 'read') {
return null
return null;
}
const expandedPath = expandPath(path)
const fs = getFsImplementation()
const { resolvedPath, isSymlink } = safeResolvePath(fs, expandedPath)
const expandedPath = expandPath(path);
const fs = getFsImplementation();
const { resolvedPath, isSymlink } = safeResolvePath(fs, expandedPath);
if (isSymlink) {
return resolvedPath
return resolvedPath;
}
return null
}, [path, operationType])
return null;
}, [path, operationType]);
const fileDialogResult = useFilePermissionDialog({
filePath: path || '',
@@ -112,7 +103,7 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
onReject,
parseInput,
operationType,
})
});
// Use file dialog results for options
const {
@@ -124,22 +115,19 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
focusedOption,
yesInputMode,
noInputMode,
} = fileDialogResult
} = fileDialogResult;
// Parse input using the provided parser
const parsedInput = parseInput(toolUseConfirm.input)
const parsedInput = parseInput(toolUseConfirm.input);
// Set up IDE diff support if enabled. Memoized: getConfig may do disk I/O
// (FileWrite's getConfig calls readFileSync for the old-content diff).
// Keyed on the raw input — parseInput is a pure Zod parse whose result
// depends only on toolUseConfirm.input.
const ideDiffConfig = useMemo(
() =>
ideDiffSupport
? ideDiffSupport.getConfig(parseInput(toolUseConfirm.input))
: null,
() => (ideDiffSupport ? ideDiffSupport.getConfig(parseInput(toolUseConfirm.input)) : null),
[ideDiffSupport, toolUseConfirm.input],
)
);
// Create diff params based on whether IDE diff is available
const diffParams = ideDiffConfig
@@ -147,19 +135,16 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
onChange: (
option: PermissionOption,
input: {
file_path: string
file_path: string;
edits: Array<{
old_string: string
new_string: string
replace_all?: boolean
}>
old_string: string;
new_string: string;
replace_all?: boolean;
}>;
},
) => {
const transformedInput = ideDiffSupport!.applyChanges(
parsedInput,
input.edits,
)
fileDialogResult.onChange(option, transformedInput)
const transformedInput = ideDiffSupport!.applyChanges(parsedInput, input.edits);
fileDialogResult.onChange(option, transformedInput);
},
toolUseContext,
filePath: ideDiffConfig.filePath,
@@ -176,21 +161,19 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
filePath: '',
edits: [],
editMode: 'single' as const,
}
};
const { closeTabInIDE, showingDiffInIDE, ideName } = useDiffInIDE(diffParams)
const { closeTabInIDE, showingDiffInIDE, ideName } = useDiffInIDE(diffParams);
const onChange = (option: PermissionOption, feedback?: string) => {
closeTabInIDE?.()
fileDialogResult.onChange(option, parsedInput, feedback?.trim())
}
closeTabInIDE?.();
fileDialogResult.onChange(option, parsedInput, feedback?.trim());
};
if (showingDiffInIDE && ideDiffConfig && path) {
return (
<ShowInIDEPrompt
onChange={(option: PermissionOption, _input, feedback?: string) =>
onChange(option, feedback)
}
onChange={(option: PermissionOption, _input, feedback?: string) => onChange(option, feedback)}
options={options}
filePath={path}
input={parsedInput}
@@ -204,11 +187,10 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
yesInputMode={yesInputMode}
noInputMode={noInputMode}
/>
)
);
}
const isSymlinkOutsideCwd =
symlinkTarget != null && relative(getCwd(), symlinkTarget).startsWith('..')
const isSymlinkOutsideCwd = symlinkTarget != null && relative(getCwd(), symlinkTarget).startsWith('..');
const symlinkWarning = symlinkTarget ? (
<Box paddingX={1} marginBottom={1}>
@@ -218,16 +200,11 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
: `Symlink target: ${symlinkTarget}`}
</Text>
</Box>
) : null
) : null;
return (
<>
<PermissionDialog
title={title}
subtitle={subtitle}
innerPaddingX={0}
workerBadge={workerBadge}
>
<PermissionDialog title={title} subtitle={subtitle} innerPaddingX={0} workerBadge={workerBadge}>
{symlinkWarning}
{content}
<Box flexDirection="column" paddingX={1}>
@@ -236,21 +213,21 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
options={options}
inlineDescriptions
onChange={value => {
const selected = options.find(opt => opt.value === value)
const selected = options.find(opt => opt.value === value);
if (selected) {
// For reject option
if (selected.option.type === 'reject') {
const trimmedFeedback = rejectFeedback.trim()
onChange(selected.option, trimmedFeedback || undefined)
return
const trimmedFeedback = rejectFeedback.trim();
onChange(selected.option, trimmedFeedback || undefined);
return;
}
// For accept-once option, pass accept feedback if present
if (selected.option.type === 'accept-once') {
const trimmedFeedback = acceptFeedback.trim()
onChange(selected.option, trimmedFeedback || undefined)
return
const trimmedFeedback = acceptFeedback.trim();
onChange(selected.option, trimmedFeedback || undefined);
return;
}
onChange(selected.option)
onChange(selected.option);
}
}}
onCancel={() => onChange({ type: 'reject' })}
@@ -262,11 +239,10 @@ export function FilePermissionDialog<T extends ToolInput = ToolInput>({
<Box paddingX={1} marginTop={1}>
<Text dimColor>
Esc to cancel
{((focusedOption === 'yes' && !yesInputMode) ||
(focusedOption === 'no' && !noInputMode)) &&
{((focusedOption === 'yes' && !yesInputMode) || (focusedOption === 'no' && !noInputMode)) &&
' · Tab to amend'}
</Text>
</Box>
</>
)
);
}

View File

@@ -1,37 +1,31 @@
import { homedir } from 'os'
import { basename, join, sep } from 'path'
import React, { type ReactNode } from 'react'
import { getOriginalCwd } from '../../../bootstrap/state.js'
import { Text } from '@anthropic/ink'
import { getShortcutDisplay } from '../../../keybindings/shortcutFormat.js'
import type { ToolPermissionContext } from '../../../Tool.js'
import { expandPath, getDirectoryForPath } from '../../../utils/path.js'
import {
normalizeCaseForComparison,
pathInAllowedWorkingPath,
} from '../../../utils/permissions/filesystem.js'
import type { OptionWithDescription } from '../../CustomSelect/select.js'
import { homedir } from 'os';
import { basename, join, sep } from 'path';
import React, { type ReactNode } from 'react';
import { getOriginalCwd } from '../../../bootstrap/state.js';
import { Text } from '@anthropic/ink';
import { getShortcutDisplay } from '../../../keybindings/shortcutFormat.js';
import type { ToolPermissionContext } from '../../../Tool.js';
import { expandPath, getDirectoryForPath } from '../../../utils/path.js';
import { normalizeCaseForComparison, pathInAllowedWorkingPath } from '../../../utils/permissions/filesystem.js';
import type { OptionWithDescription } from '../../CustomSelect/select.js';
/**
* Check if a path is within the project's .claude/ folder.
* This is used to determine whether to show the special ".claude folder" permission option.
*/
export function isInClaudeFolder(filePath: string): boolean {
const absolutePath = expandPath(filePath)
const claudeFolderPath = expandPath(`${getOriginalCwd()}/.claude`)
const absolutePath = expandPath(filePath);
const claudeFolderPath = expandPath(`${getOriginalCwd()}/.claude`);
// Check if the path is within the project's .claude folder
const normalizedAbsolutePath = normalizeCaseForComparison(absolutePath)
const normalizedClaudeFolderPath =
normalizeCaseForComparison(claudeFolderPath)
const normalizedAbsolutePath = normalizeCaseForComparison(absolutePath);
const normalizedClaudeFolderPath = normalizeCaseForComparison(claudeFolderPath);
// Path must start with the .claude folder path (and be inside it, not just the folder itself)
return (
normalizedAbsolutePath.startsWith(
normalizedClaudeFolderPath + sep.toLowerCase(),
) ||
normalizedAbsolutePath.startsWith(normalizedClaudeFolderPath + sep.toLowerCase()) ||
// Also match case where sep is / on posix systems
normalizedAbsolutePath.startsWith(normalizedClaudeFolderPath + '/')
)
);
}
/**
@@ -40,32 +34,28 @@ export function isInClaudeFolder(filePath: string): boolean {
* for files in the user's home directory.
*/
export function isInGlobalClaudeFolder(filePath: string): boolean {
const absolutePath = expandPath(filePath)
const globalClaudeFolderPath = join(homedir(), '.claude')
const absolutePath = expandPath(filePath);
const globalClaudeFolderPath = join(homedir(), '.claude');
const normalizedAbsolutePath = normalizeCaseForComparison(absolutePath)
const normalizedGlobalClaudeFolderPath = normalizeCaseForComparison(
globalClaudeFolderPath,
)
const normalizedAbsolutePath = normalizeCaseForComparison(absolutePath);
const normalizedGlobalClaudeFolderPath = normalizeCaseForComparison(globalClaudeFolderPath);
return (
normalizedAbsolutePath.startsWith(
normalizedGlobalClaudeFolderPath + sep.toLowerCase(),
) ||
normalizedAbsolutePath.startsWith(normalizedGlobalClaudeFolderPath + sep.toLowerCase()) ||
normalizedAbsolutePath.startsWith(normalizedGlobalClaudeFolderPath + '/')
)
);
}
export type PermissionOption =
| { type: 'accept-once' }
| { type: 'accept-session'; scope?: 'claude-folder' | 'global-claude-folder' }
| { type: 'reject' }
| { type: 'reject' };
export type PermissionOptionWithLabel = OptionWithDescription<string> & {
option: PermissionOption
}
option: PermissionOption;
};
export type FileOperationType = 'read' | 'write' | 'create'
export type FileOperationType = 'read' | 'write' | 'create';
export function getFilePermissionOptions({
filePath,
@@ -76,20 +66,16 @@ export function getFilePermissionOptions({
yesInputMode = false,
noInputMode = false,
}: {
filePath: string
toolPermissionContext: ToolPermissionContext
operationType?: FileOperationType
onRejectFeedbackChange?: (value: string) => void
onAcceptFeedbackChange?: (value: string) => void
yesInputMode?: boolean
noInputMode?: boolean
filePath: string;
toolPermissionContext: ToolPermissionContext;
operationType?: FileOperationType;
onRejectFeedbackChange?: (value: string) => void;
onAcceptFeedbackChange?: (value: string) => void;
yesInputMode?: boolean;
noInputMode?: boolean;
}): PermissionOptionWithLabel[] {
const options: PermissionOptionWithLabel[] = []
const modeCycleShortcut = getShortcutDisplay(
'chat:cycleMode',
'Chat',
'shift+tab',
)
const options: PermissionOptionWithLabel[] = [];
const modeCycleShortcut = getShortcutDisplay('chat:cycleMode', 'Chat', 'shift+tab');
// When in input mode, show input field
if (yesInputMode && onAcceptFeedbackChange) {
@@ -101,23 +87,20 @@ export function getFilePermissionOptions({
onChange: onAcceptFeedbackChange,
allowEmptySubmitToCancel: true,
option: { type: 'accept-once' },
})
});
} else {
options.push({
label: 'Yes',
value: 'yes',
option: { type: 'accept-once' },
})
});
}
const inAllowedPath = pathInAllowedWorkingPath(
filePath,
toolPermissionContext,
)
const inAllowedPath = pathInAllowedWorkingPath(filePath, toolPermissionContext);
// Check if this is a .claude/ folder path (project or global)
const inClaudeFolder = isInClaudeFolder(filePath)
const inGlobalClaudeFolder = isInGlobalClaudeFolder(filePath)
const inClaudeFolder = isInClaudeFolder(filePath);
const inGlobalClaudeFolder = isInGlobalClaudeFolder(filePath);
// Option 2: For .claude/ folder, show special option instead of generic session option
// Note: Session-level options are always shown since they only affect in-memory state,
@@ -131,42 +114,40 @@ export function getFilePermissionOptions({
type: 'accept-session',
scope: inGlobalClaudeFolder ? 'global-claude-folder' : 'claude-folder',
},
})
});
} else {
// Option 2: Allow all changes/reads during session
let sessionLabel: ReactNode
let sessionLabel: ReactNode;
if (inAllowedPath) {
// Inside working directory
if (operationType === 'read') {
sessionLabel = 'Yes, during this session'
sessionLabel = 'Yes, during this session';
} else {
sessionLabel = (
<Text>
Yes, allow all edits during this session{' '}
<Text bold>({modeCycleShortcut})</Text>
Yes, allow all edits during this session <Text bold>({modeCycleShortcut})</Text>
</Text>
)
);
}
} else {
// Outside working directory - include directory name
const dirPath = getDirectoryForPath(filePath)
const dirName = basename(dirPath) || 'this directory'
const dirPath = getDirectoryForPath(filePath);
const dirName = basename(dirPath) || 'this directory';
if (operationType === 'read') {
sessionLabel = (
<Text>
Yes, allow reading from <Text bold>{dirName}/</Text> during this
session
Yes, allow reading from <Text bold>{dirName}/</Text> during this session
</Text>
)
);
} else {
sessionLabel = (
<Text>
Yes, allow all edits in <Text bold>{dirName}/</Text> during this
session <Text bold>({modeCycleShortcut})</Text>
Yes, allow all edits in <Text bold>{dirName}/</Text> during this session{' '}
<Text bold>({modeCycleShortcut})</Text>
</Text>
)
);
}
}
@@ -174,7 +155,7 @@ export function getFilePermissionOptions({
label: sessionLabel,
value: 'yes-session',
option: { type: 'accept-session' },
})
});
}
// When in input mode, show input field for reject
@@ -187,15 +168,15 @@ export function getFilePermissionOptions({
onChange: onRejectFeedbackChange,
allowEmptySubmitToCancel: true,
option: { type: 'reject' },
})
});
} else {
// Not in input mode - simple option
options.push({
label: 'No',
value: 'no',
option: { type: 'reject' },
})
});
}
return options
return options;
}