商業源代碼中的JSLint錯誤

[英]JSLint error in commercial source code


JS lint gives the following error in commercial source code (therefore code that is tested on all browsers without issue). Is there a way to suppress this warning by editing the code? I would rather not upgrade jslint if possible.

JS lint在商業源代碼中給出以下錯誤(因此在所有瀏覽器上測試的代碼沒有問題)。有沒有辦法通過編輯代碼來抑制此警告?如果可能的話,我寧願不升級jslint。

**** redactor/redactor917.js ****

redactor917.js
/var/moteevate/js/redactor/redactor917.js(1471): SyntaxError: unterminated character class ^
if (current.remove && current.nodeType === 3 && current.nodeValue.match(/[^/\u200B]/g) == null)
........................................................................................................^


1 error(s), 0 warning(s)

2 个解决方案

#1


2  

/[^/\u200B]/g

is a regex that does contain a slash, which is a regex literal delimiter actually, but is also valid inside a character class. However, JSLint does parse the regex as /[^/ and states for it that the character class is unterminated.

是一個包含斜杠的正則表達式,實際上是一個正則表達式文字分隔符,但在字符類中也是有效的。但是,JSLint將正則表達式解析為/ [^ /並聲明字符類未終止。

You should escape that slash:

你應該逃避斜線:

/[^\/\u200B]/g

#2


3  

Interesting regular expression. If I'm reading that right, it looks like if you're removing, and your nodeType is 3, and your nodeValue isn't made up (matches are null) of anything that's not a zero-width space, take the branch.

有趣的正則表達式。如果我正確地閱讀,看起來如果你要移除,並且你的nodeType是3,並且你的nodeValue沒有組成(匹配為空)任何不是零寬度空間的東西,那么取分支。

The problem really isn't escaping ^, as blueberryfields suggests. The ^ is fine. It's just a regexp not operator when it's inside of the brackets.

正如blueberryfields所暗示的,這個問題確實沒有逃脫^。 ^很好。當它在括號內時,它只是一個正則表達式而不是運算符。

The problem here is that Crockford (JSLint's author & maintainer) doesn't want you to say what shouldn't match a regular expression (here, you're looking for everything that's not a zero-width space).

這里的問題是Crockford(JSLint的作者和維護者)不希望你說出與正則表達式不匹配的東西(在這里,你正在尋找不是零寬度空間的所有東西)。

He wants your regexp to be specific: Instead, say what should be valid so that items you weren't thinking about when setting up the negative case don't slip through. That's why the error you're seeing should be Insecure '^'.. It's insecure because you could let in patterns you didn't think of.

他希望你的正則表達式是具體的:相反,說出什么應該是有效的,這樣你在設置否定案例時沒有考慮的項目不會漏掉。這就是為什么你看到的錯誤應該是不安全的'^'。這是不安全的,因為你可以讓你沒有想到的模式。

"I'd like a hamburger without onions, sauerkraut, or cheese."

“我想要一個沒有洋蔥,酸菜或奶酪的漢堡包。”

"Sure thing! Here ya' go!"

“當然可以!在這里,你走吧!”

"But this has motor oil on it!"

“但這里有機油!”

Perhaps that's not an issue in this case, but as a general rule, not too shabby.

也許在這種情況下這不是一個問題,但作為一般規則,不要太破舊。

Though I think that's a useful motivation, as it's catching that weird "double not" logic from the original regexp, let's not worry about the merits of if we agree with that or not for now and just see what needs to happen to fix it. ;^)

雖然我認為這是一個有用的動機,因為它正在捕捉原始正則表達式中那種奇怪的“雙重不”邏輯,但我們不要擔心我們現在是否同意這一點的優點,只是看看需要發生什么來修復它。 ; ^)

So if I'm doing this right, the current regexp's "nodeValue isn't made up of anything that's not a zero-width space" equates to "nodeValue is made up of zero or more zero-width spaces".

因此,如果我這樣做,當前正則表達式的“nodeValue不是由任何不是零寬度空間組成”等於“nodeValue由零個或多個零寬度空間組成”。

To make this JSLint happy, then, rearrange the regexp to check for "between the start of the string and the end of the string, we want zero or more zero-width spaces. That looks like this (fiddle)...

為了使這個JSLint滿意,然后重新排列regexp以檢查“在字符串的開頭和字符串的結尾之間,我們想要零或更多的零寬度空間。看起來像這樣(小提琴)...

/*jslint browser: true, white:true, sloppy:true */
var current = {
    remove: true,
    nodeType: 3,
    nodeValue: "\u200b\u200b\u200b"
};

if (current.remove && current.nodeType === 3 
    && current.nodeValue.match(/^[\u200B]*$/g) !== null)
{
    window.alert("YOU CAN DO IT");
}

And yes, "".match(/^[\u200B]*$/g); gives one match as well.

是的,“”。匹配(/ ^ [\ u200B] * $ / g);也給出了一場比賽。

Admittedly, I didn't know what character \u200B was initially. Good info on that here.

不可否認,我不知道最初是什么角色。關於這方面的好消息。

==============================================================

EDIT: Responding to the OP's comment.

編輯:回應OP的評論。

Bergi's edit shouldn't pass JSLint.

Bergi的編輯不應該通過JSLint。

So try this code:

所以試試這個代碼:

/*jslint browser: true, white:true, sloppy:true */
var current = {
    remove: true,
    nodeType: 3,
    nodeValue: "\u200b\u200b\u200b"
};

if (current.remove && current.nodeType === 3 
    && current.nodeValue.match(/[^\/\u200B]/g) === null) // with Bergi's edit.
{
    window.alert("YOU CAN DO IT");
}    else    {
    window.alert("you can't do");
}

insecure carot

So it's not JSLint compliant.

所以它不符合JSLint。

But if your tool isn't complaining about the insecure ^, I'm not sure what's going on.

但如果你的工具沒有抱怨不安全^,我不確定發生了什么。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/03/26/72983b7f6860b60d824bf2547185ecb4.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com