eslint未检测到解构变量

发布于 2025-01-23 23:08:52 字数 1759 浏览 2 评论 0原文

我有以下代码:

    for ([uri, { socket, settings }] of map) {

        // parse websocket urls
        let url1 = new url.URL(ws.url);
        let url2 = new url.URL(uri);

        // override http(s) with ws(s)
        url2.protocol = url1.protocol;

        console.log(`Bridge "%s" <-> ${socket}://${host}:${port}`, url2);

        bridge(url2, settings, socket);

    }

哪些解构变量,但是ESLINT并未检测到变量:

/home/.../handler.js
  81:11  error  'uri' is not defined       no-undef
  81:18  error  'socket' is not defined    no-undef
  81:26  error  'settings' is not defined  no-undef
  85:32  error  'uri' is not defined       no-undef
  90:40  error  'socket' is not defined    no-undef
  90:52  error  'host' is not defined      no-undef
  90:60  error  'port' is not defined      no-undef
  92:22  error  'settings' is not defined  no-undef
  92:32  error  'socket' is not defined    no-undef

✖ 9 problems (9 errors, 0 warnings)

如何告诉linter linter的良好和变量被正确检测到?

.eslintrc.json

{
    "env": {
        "node": true,
        "commonjs": true,
        "es2021": true
    },
    "parserOptions": {
        "ecmaVersion": 12
    },
    "ignorePatterns": [
        "node_modules"
    ],
    "extends": [
        "eslint:recommended"
    ],
    "rules": {
        "semi": [
            "error",
            "always"
        ],
        "quotes": [
            "error",
            "double",
            {
                "avoidEscape": true, // Does not work https://eslint.org/docs/rules/quotes#avoidescape
                "allowTemplateLiterals": true
            }
        ]
    }
}

I have the following code:

    for ([uri, { socket, settings }] of map) {

        // parse websocket urls
        let url1 = new url.URL(ws.url);
        let url2 = new url.URL(uri);

        // override http(s) with ws(s)
        url2.protocol = url1.protocol;

        console.log(`Bridge "%s" <-> ${socket}://${host}:${port}`, url2);

        bridge(url2, settings, socket);

    }

which deconstruct variables, but eslint does not detect the variables:

/home/.../handler.js
  81:11  error  'uri' is not defined       no-undef
  81:18  error  'socket' is not defined    no-undef
  81:26  error  'settings' is not defined  no-undef
  85:32  error  'uri' is not defined       no-undef
  90:40  error  'socket' is not defined    no-undef
  90:52  error  'host' is not defined      no-undef
  90:60  error  'port' is not defined      no-undef
  92:22  error  'settings' is not defined  no-undef
  92:32  error  'socket' is not defined    no-undef

✖ 9 problems (9 errors, 0 warnings)

How can i tell the linter that its fine and the variables are detect properly?

.eslintrc.json

{
    "env": {
        "node": true,
        "commonjs": true,
        "es2021": true
    },
    "parserOptions": {
        "ecmaVersion": 12
    },
    "ignorePatterns": [
        "node_modules"
    ],
    "extends": [
        "eslint:recommended"
    ],
    "rules": {
        "semi": [
            "error",
            "always"
        ],
        "quotes": [
            "error",
            "double",
            {
                "avoidEscape": true, // Does not work https://eslint.org/docs/rules/quotes#avoidescape
                "allowTemplateLiterals": true
            }
        ]
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2025-01-30 23:08:52

对于循环需要const,(const

const map = [
  [
    "uri",
    {
      socket: "",
      settings: ''
    },
  ],
];

for (const [uri, { socket, settings }] of map) {
    console.log(uri, socket, settings);
}

for of loop requires const, for(const

const map = [
  [
    "uri",
    {
      socket: "",
      settings: ''
    },
  ],
];

for (const [uri, { socket, settings }] of map) {
    console.log(uri, socket, settings);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文