【问题标题】:Fix - expected an assignment or function call and instead saw an expression修复 - 期望一个赋值或函数调用,而是看到一个表达式
【发布时间】:2022-01-24 16:25:54
【问题描述】:

我正在尝试修复这个 Eslint 错误:

Expected an assignment or function call an instead saw an expression

关于如何容纳代码以使其在以下示例代码中不会出现错误的任何想法?

window.matchMedia || (window.matchMedia = function() {
 
  if (!styleMedia) {
    const style = document.createElement('style');

    const script = document.getElementsByTagName('script')[0];

    let info = null;

    style.type  = 'text/css';
    style.id    = 'matchmediajs-test';

    script.parentNode.insertBefore(style, script);

    info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle;

    styleMedia = {
      matchMedium: function(media) {
        const text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }';

     
        if (style.styleSheet) {
          style.styleSheet.cssText = text;
        } else {
          style.textContent = text;
        }

        return info.width === '1px';
      },
    };
  }

  return function(media) {
    return {
      matches: styleMedia.matchMedium(media || 'all'),
      media: media || 'all',
    };
  };
}());

知道为什么我会收到此 lint 错误吗?

谢谢

【问题讨论】:

    标签: javascript reactjs eslint lint


    【解决方案1】:

    您的第一行(实际上包含了整个脚本)

    window.matchMedia || (window.matchMedia = function() {
    

    是一个表达式,而不是赋值或函数调用。你需要类似的东西:

    if (!window.matchMedia) {
      window.matchMedia = function() {
        // ..
    }
    

    而不是在条件内赋值。

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 2020-02-28
      • 1970-01-01
      • 2019-04-22
      • 2020-02-12
      • 2019-10-15
      • 2020-11-08
      • 1970-01-01
      相关资源
      最近更新 更多