eslint-loader / index.js无法读取null的属性“范围”

时间:2018-11-29 17:49:04

标签: webpack eslint

我正在尝试确定以下问题是否是由于配置文件中的错误或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的第55行上的eslint/lib/token-store/forward-token-cursor.js,该函数的外观如下:

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

由于某种原因,index的{​​{1}}值(ForwardTokenCursor)超出了this的范围,这就是在eslint返回错误之前,ForwardTokenCursor的样子: this.indexEnd

如您所见,getOneToken函数稍后将向null返回getTokenBefore,当访问此null对象的假定属性时,它将导致linter错误。为什么会这样呢?尽管我不知道为什么这会导致这种错误,但我认为这与我的陪同设置方式有关。无论如何,我已在此处包括我的配置信息。

webpack.config.dev.js:

{current: null, tokens: Array(1188), index: 1092, indexEnd: 1090 }

.eslintrc

'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()
  ]
});

在评论中让我知道是否需要任何其他配置或构建文件。感谢您的帮助,我在eslint github上发现了一些与该问题有关的bug票,但它们已关闭且似乎未解决。 https://github.com/eslint/eslint/issues/9872

1 个答案:

答案 0 :(得分:0)

在“ .eslintrc”配置文件中添加

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

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