eslint-loader/index.js 无法读取 null 的属性'range'



我正在尝试确定以下问题是由配置文件中的错误还是ESLint中的某种错误引起的;以及最终如何修复。

ESLint版本:5.9.0

错误文本

Module build failed (from ./node_modules/eslint-loader/index.js): TypeError: Cannot read property 'range' of null at Array.forEach (<anonymous>)

错误原因

eslint/lib/rules/template-curly-spacing的线路117上,调用一个称为getFirstToken的函数。这个函数调用getOneToken,它位于eslint/lib/token-store/forward-token-cursor.js的第55行。下面是这个函数的样子:

getOneToken() { return (this.index <= this.indexEnd) ? this.tokens[this.index] : null; }

由于某些原因,this(ForwardTokenCursor(的index值超出了this.indexEnd的范围,这就是在esint返回错误之前ForwardToken Cursor的样子:{current: null, tokens: Array(1188), index: 1092, indexEnd: 1090 }

正如您所看到的,getOneToken函数将向getTokenBefore返回null。稍后,当访问该null对象的假定属性时,它会导致linter出错。为什么会发生这种情况?我认为这是我设置esint的方式的问题,尽管我不知道为什么会导致这种错误。无论如何,我已经在这里包含了我的配置信息。

webpack.config.dev.js:

'use strict';
const autoprefixer = require('autoprefixer');
const path = require('path');
const paths = require('../paths');
const webpack = require('webpack');
const merge = require('webpack-merge');
const common = require('./webpack.config.common.js');
const InterpolateHtmlPlugin = require('../../scripts/create_react_app_utils/InterpolateHtmlPlugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const getClientEnvironment = require('../env');
const fs = require('fs');
// process.env.API_ENV = 'mock';
const env = getClientEnvironment('');
module.exports = merge(common, {
entry: [
require.resolve('../../scripts/create_react_app_utils/webpackHotDevClient'),
require.resolve('../polyfills'),
/* require.resolve('react-error-overlay'), */
paths.appIndexJs
],
output: {
pathinfo: true,
filename: 'static/js/bundle.js',
chunkFilename: 'static/js/main-react.chunk.js',
publicPath: '/',
devtoolModuleFilenameTemplate: info =>
path.resolve(info.absoluteResourcePath),
},
mode: 'development',
devtool: 'inline-source-map',
module: {
strictExportPresence: true,
rules: [
{
test: /.(js|jsx)$/,
enforce: 'pre',
use: [
{
options: {
formatter: eslintFormatter,
},
loader: require.resolve('eslint-loader'),
},
],
include: paths.appSrc,
},
{
exclude: [
/.html$/,
/.(js|jsx)$/,
/.css$/,
/.json$/,
/.bmp$/,
/.gif$/,
/.jpe?g$/,
/.png$/,
/.scss$/,
],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
{
test: /.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
sourceMap: true
},
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
],
},
{
test: /.scss$/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
}, 
{
loader: "css-loader", // translates CSS into CommonJS
options: {
sourceMap: true,
}
}, 
{
loader: "sass-loader", // compiles Sass to CSS
options: {
sourceMap: true,
data: '@import "globals";',
includePaths: [path.resolve(paths.appSrc, "./styles")]
}
}]
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
}),
new webpack.DefinePlugin(env.stringified),
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
new CaseSensitivePathsPlugin(),
new webpack.IgnorePlugin(/^./locale$/, /moment$/),
// new BundleAnalyzerPlugin()
]
});

.eslintrc

{
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
},
"globals": {
"React":true
},
"plugins": [
"react"
],
"extends": "airbnb",
"rules": {
"max-len": 0,
"one-var": 0,
"no-console": ["error", { "allow": ["warn", "error"]  }],
"no-mixed-operators": 0,
"no-multiple-empty-lines": ["error", {"max": 2}],
"no-prototype-builtins": 0,
"class-methods-use-this": 0,
"padded-blocks": 0,
"no-return-assign": 0,
"no-unused-expressions": ["error", { "allowTernary": true }],
"camelcase": 0,
"no-plusplus": ["error", { "allowForLoopAfterthoughts": true }],
"prefer-destructuring": ["off"],

"no-tabs": 0,
"no-mixed-spaces-and-tabs": "error",
"indent": ["error", "tab", { "SwitchCase": 1 }],
"new-cap": 0,
"spaced-comment": ["error", "always", { "exceptions": ["*"] }],
"import/no-named-as-default": 0,
"import/no-unresolved": 0, // Linting will not interpret aliases if this rule is turned on
"import/no-extraneous-dependencies": 0,
"import/extensions": 0,
"jsx-a11y/href-no-hash": 0,
"jsx-a11y/no-static-element-interactions": 0,
"jsx-a11y/no-noninteractive-element-interactions": 0, //Allow us to put event handlers on any HTML element
"jsx-a11y/no-noninteractive-tabindex": 0,

"react/forbid-prop-types": [2, { "forbid": ["any"] }],
"react/jsx-indent": [2, "tab"],
"react/jsx-indent-props": ["error", "tab"],
"react/jsx-filename-extension": 0,
"react/jsx-no-bind": 1,
"react/prefer-stateless-function": [1, { "ignorePureComponents": true }],
"react/sort-comp": 0
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true,
"modules": true
}
}
}

如果需要任何其他配置或构建文件,请在评论中告诉我。我很感谢你的帮助,我在eslint-github上发现了几个似乎与此问题有关的错误通知单,但它们已关闭,似乎尚未解决。https://github.com/eslint/eslint/issues/9872

在您的"中;。eslintrc";配置文件添加

.."rules": {
"indent": ["error", 2, {
"ignoredNodes": ["TemplateLiteral"]
}]
or
"index": "off" 

https://www.javascripttutorial.net/es6/javascript-template-literals/

最新更新