Javascript无法验证对导入的命名空间的计算引用

Bru*_*ith 4 javascript reactjs eslint

不确定我是否需要添加另一个jshint库,或者我是否应该以不同的方式执行此操作.

我有一个文件(为了解释原因,我们称之为stuff-functions.js)导出这样的函数...

export function a() {
    return 'stuff';
}

export function b() {
    return 'more stuff';
}

export function c() {
    return 'even more stuff';
}
Run Code Online (Sandbox Code Playgroud)

在另一个文件中,我正在导入该文件并通过参数调用该函数...

import * as stuffFunctions from './stuff-functions'

export default class someReactClass {
    myFunc(functionToCall) {
        return stuffFunctions[functionToCall]();
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,但在控制台我得到一个eslint错误...

Unable to validate computed reference to imported namespace 'stuffFunctions'
Run Code Online (Sandbox Code Playgroud)

那么,我应该采取不同的方式或追捕某种允许这种情况的eslint库吗?

编辑...

我添加了这一行来阻止错误// eslint-disable-line

如果有更好的方法,我只是好奇.也许像...

import {a, b, c} from './stuff-functions';

export default class someReactClass {
    myFunc(functionToCall) {
        const myStuffFunctions = {
            a: a,
            b: b,
            c: c
        };

        return myStuffFunctions[functionToCall]();
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

虽然似乎多余.:/

car*_*ant 11

eslint-plugin-import插件中的import/namespace规则报告了该错误.之所以发生这种情况,是因为您决定在运行时调用哪个导入的函数:

stuffFunctions[functionToCall]();
Run Code Online (Sandbox Code Playgroud)

插件的静态分析无法验证这是否为有效导入,并将其报告为错误.

最简单的解决方案是添加ESLint注释以重新配置规则以允许计算引用:

/*eslint import/namespace: ['error', { allowComputed: true }]*/
import * as stuffFunctions from './stuff-functions'

export default class someReactClass {
    myFunc(functionToCall) {
        return stuffFunctions[functionToCall]();
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)