首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有意外保留字的自定义eslint规则抛出

带有意外保留字的自定义eslint规则抛出
EN

Stack Overflow用户
提问于 2016-03-08 01:38:37
回答 1查看 587关注 0票数 0

我有这个代码

代码语言:javascript
复制
var astUtils = require("eslint/lib/ast-utils")

module.exports = function(context) {
  const selfConfigRegEx = /\bno-best-before-comments\b/
  const now = new Date()
  const regex = /BEST-?BEFORE (\d{4}-\d{2}-\d{2})/ig

  function checkBefore(node) {
    if (astUtils.isDirectiveComment(node) && selfConfigRegEx.test(node.value)) {
      return
    }

    const bestBeforeDate = new Date(regex.match(node.value)[0])
    if (bestBeforeDate > now) {
      context.report(node, "BEST-BEFORE is expired since " + bestBeforeDate)
    }
  }

  return {
    "BlockComment": checkBefore,
    "LineComment": checkBefore
  }
}

作为本地文件包正确安装。eslint加载它,但失败的原因是

代码语言:javascript
复制
SyntaxError: Failed to load plugin my-internal: Unexpected reserved word
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:413:25)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (c:\dev\projects\app\node_modules\eslint-plugin-my-internal\index.js:2:18)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)

堆栈跟踪不是很有帮助。使用eslint可以很好地验证规则本身。

EN

回答 1

Stack Overflow用户

发布于 2016-03-08 22:19:33

const标识符处的错误。以下是正确的规则文件:

代码语言:javascript
复制
/** eslint-disable semi */
"use strict"

var astUtils = require("eslint/lib/ast-utils")

module.exports = function(context) {
  var selfConfigRegEx = /\bno-best-before-comments\b/
  var now = new Date()
  var regex = /BEST-?BEFORE:?\s*(\d{4}-\d{1,2}-\d{1,2})/ig

  function checkBefore(node) {
    if (astUtils.isDirectiveComment(node) && selfConfigRegEx.test(node.value)) {
      return
    }

    var dateString = regex.exec(node.value)[1]
    var bestBeforeDate = new Date(dateString)
    if (bestBeforeDate < now) {
      context.report(node, "BEST-BEFORE expired since " + dateString)
    }
  }

  return {
    "BlockComment": checkBefore,
    "LineComment": checkBefore
  }
}

module.exports.schema = [
    // JSON Schema for rule options goes here
]

但是,eslint的错误报告可能会更好。会在那里提交一份报告。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35850285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档