[英]Custom eslint rule throws with unexpected reserved word
I have this code 我有这个代码
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
}
}
Properly installed as local file package. 正确安装为本地文件包。
eslint
loads it but fails with eslint
加载它,但失败
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)
The stacktrace is not very helpful. stacktrace不是很有帮助。 The rule itself validates just fine with eslint.
规则本身可以通过eslint进行验证。
The errors where the const
identifiers. const
标识符所在的错误。 This is the correct rule file: 这是正确的规则文件:
/** 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
]
However, the error reporting of eslint
could be better. 但是,
eslint
的错误报告可能会更好。 Will file a report over there. 将在那边提交报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.