TypeScript 重构:移动到新文件不起作用

kcrjzv8t  于 3个月前  发布在  TypeScript
关注(0)|答案(5)|浏览(52)

这个问题在使用所有扩展都被禁用时是否会发生?:是的

  • VS Code 版本:版本:1.92.1(通用)
  • OS 版本:MacOS Sonoma 14.5 / Apple M1 Pro

重现步骤:

  1. 重构 > 移动到新文件
    我已经尝试过的方法:
  2. 使用 --disable-extensions 运行以确保它不是包的问题。
  3. typescript.tsserver.enableTrace
  4. 寻找最小示例
  5. 搜索类似问题:Refactor: move to new file does not work vscode#51586,Refactor > Move to a new file -- not working (update, nothing in Refactor menu working) vscode#58711
    在之前的几个版本中它可以正常工作。我不知道确切是哪个版本没问题。
    更新:不起作用:
import { useLatest } from 'react-use';

export function useUseLatest (data: string) {
    return useLatest(data);
}

不起作用:

import { selectStore, useSelector } from '@proterm/core';

export function useOrders() {
    return useSelector(selectStore);
}

不起作用:

import { selectStore } from '@proterm/core';

export function useOrders() {
    return selectStore;
}

起作用:

import { AnyBaseOrder } from '@terminal/scalp/types';

export function useOrders(symbolId: string): AnyBaseOrder[] {}

起作用:

import { useSelector } from 'react-redux';

function useOrders() {
    return useSelector((state) => console.log(state));
}

起作用:

import { selectStore, useSelector } from '@proterm/core';
export function useOrders(symbolId: string): AnyBaseOrder[] {}

我认为 @ import aliases 是问题所在,但这个也可以:

import { FancyAlertCircleIcon } from '@terminal/icons';

export function useOrders() {
    return FancyAlertCircleIcon;
}

错误:

2024-08-15 18:42:15.101 [error] <semantic> TypeScript Server Error (5.5.4)
Debug Failure.
Error: Debug Failure.
    at Object.addImportFromExportedSymbol (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:154243:32)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144288:19
    at Map.forEach (<anonymous>)
    at addTargetFileImports (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144282:17)
    at getNewStatementsAndRemoveFromOldFile (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:143512:3)
    at doChange4 (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144488:3)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144477:77
    at _ChangeTracker.with (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:174306:5)
    at Object.getRefactorEditsToMoveToNewFile [as getEditsForAction] (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:144477:60)
    at Object.getEditsForRefactor (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:142603:31)
    at Object.getEditsForRefactor2 [as getEditsForRefactor] (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:149939:32)
    at IpcIOSession.getEditsForRefactor (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191087:49)
    at getEditsForRefactor (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:189305:43)
    at /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191491:69
    at IpcIOSession.executeWithRequestId (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191483:14)
    at IpcIOSession.executeCommand (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191491:29)
    at IpcIOSession.onMessage (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/typescript.js:191533:51)
    at process.<anonymous> (/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/node_modules/typescript/lib/tsserver.js:523:14)
    at process.emit (node:events:519:28)
    at emit (node:internal/child_process:951:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21): Error: <semantic> TypeScript Server Error (5.5.4)
4smxwvx5

4smxwvx51#

tsconfig.json:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "resolveJsonModule": true,
        "outDir": "./dist/",
        "lib": ["es2018", "dom", "ES2020.Promise", "DOM.Iterable", "es2019.array", "ES2021.String"],
        "sourceMap": true,
        "strict": true,
        "module": "esnext",
        "jsx": "react-jsx",
        "target": "es2017",
        "rootDir": ".",
        "allowJs": true,
        "moduleResolution": "node",
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "downlevelIteration": true,
        "importHelpers": true,
        "experimentalDecorators": true,
        "preserveSymlinks": true,
        "useUnknownInCatchVariables": false,

        // COPY: /webpack/tsconfig.json
        // COPY: /webpack/webpack.base.config.ts#alias
        // COPY: /.storybook/main.ts
        "paths": {
            "@proterm/core": ["./src/core"],
            "@terminal/*": ["./src/*"]
        },
        "plugins": [
            {
                "name": "typescript-plugin-css-modules",
                "options": {
                    "customMatcher": "\\.less$"
                }
            }
        ]
    },
    "types": ["node", "jest", "@testing-library/jest-dom", "@testing-library/react"],
    "include": ["./src/"],
    "exclude": ["**/__tests__", "**/*.stories.*", "**/__mock__/*.ts", "**/node_modules", "**/lib"]
}
kqlmhetl

kqlmhetl2#

因此,@proterm/core是具有重新导出和重新导出的重新导出的大文件的别名。此外,selectStore被声明为奇怪的形式:

function selectStore<State>(store: ReducerStore<State>): (state: any) => State;
function selectStore<State>(store: ReducerStore<State>, state: any): State;
function selectStore<State>(store: ReducerStore<State>, state?: any) {
    if (state) {
        return select(store, state);
    }

    function selectStoreCurried(stateCurried: State) {
        return select(store, stateCurried);
    }

    (selectStoreCurried as any)[widgetScope.symbol] = true;

    return selectStoreCurried;
}

(selectStore as any)[widgetScope.symbol] = true;

export default selectStore;
isr3a4wc

isr3a4wc3#

看起来这是一个问题,因为

import { syncSettings } from '@proterm/core';

export function useOrders() {
    return syncSettings;
}

作为syncSettings没有多个声明。在保存的时候,我不能移动其他函数,除非参考 selectStore 或者其他类似的东西。例如,这个也不起作用:

import { useCallback, useRef, useState } from 'react';
import { useLatest } from 'react-use';

type Opts = { lastOnly?: boolean };
type Set<D> = (data: D) => void;
export function useSubscriptionProvider<T extends Set<D>, D>(name: string, data: D, opts?: Opts) {
    const { lastOnly = true } = opts || {};
    const [buffer, setBuffer] = useState<D[]>([]);
    const queueRef = useLatest(buffer);
    const saveBuffer = useCallback((data: D) => setBuffer((q) => [...q, data]), []);
    const ref = useRef<T>();
    console.log('hm', { lastOnly, buffer, queueRef, saveBuffer, ref, name, data });
}

目前的问题是来自 'react-use' 的 useLatest ,
最小不起作用的示例:

import { useLatest } from 'react-use';

export function useUseLatest (data: string) {
    return useLatest(data);
}
1cklez4t

1cklez4t4#

我也遇到了这个问题。当我尝试在vscode中移动到新文件时,扩展主机输出中会出现以下错误。

h7appiyu

h7appiyu5#

@navya9singh 有关这个的更新吗?

相关问题