Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So far it is pretty primitive--I reimplemented option 'regexp' but hopefully in future it will be able to catch more subtle regular expression oddities. Also Esprima blows up on invalid regular expression so we don't need to parse/verify it ourselves. However, this means that we really have to make Esprima more tolerant.
- Loading branch information
Showing
5 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"use strict"; | ||
|
||
var _ = require("underscore"); | ||
var utils = require("./utils.js"); | ||
var Events = require("./events.js").Events; | ||
|
||
function Tokens(exp) { | ||
this.exp = exp; | ||
this.pos = 0; | ||
} | ||
|
||
Tokens.prototype = { | ||
get current() { | ||
return this.peak(0); | ||
}, | ||
|
||
peak: function (offset) { | ||
var pos = this.pos + (offset || 1); | ||
var chr = this.exp.charAt(pos); | ||
|
||
return chr === "" ? null : chr; | ||
}, | ||
|
||
next: function () { | ||
var chr = this.peak(); | ||
|
||
if (chr !== null) | ||
this.pos += 1; | ||
|
||
return chr; | ||
} | ||
}; | ||
|
||
_.extend(Tokens.prototype, Events); | ||
|
||
exports.register = function (linter) { | ||
var report = linter.report; | ||
|
||
linter.on("Literal", function (literal) { | ||
var value = (literal.value || "").toString(); | ||
var range = literal.range; | ||
var tokens; | ||
|
||
value = value.match(/^\/(.+)\/[igm]?$/); | ||
if (value === null) | ||
return; | ||
|
||
tokens = new Tokens(value[1]); | ||
|
||
tokens.on("[", function () { | ||
tokens.next(); | ||
|
||
if (tokens.current === "^") { | ||
report.addWarning("W009", literal.range, { sym: tokens.current }); | ||
tokens.next(); | ||
} | ||
|
||
if (tokens.current === "]") { | ||
report.addWarning("W010", literal.range); | ||
} | ||
}); | ||
|
||
tokens.on(".", function () { | ||
if (tokens.peak(-1) !== "\\") { | ||
report.addWarning("W009", literal.range, { sym: tokens.current }); | ||
} | ||
}); | ||
|
||
while (tokens.current) { | ||
tokens.trigger(tokens.current); | ||
tokens.next(); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"use strict"; | ||
|
||
var _ = require("underscore"); | ||
var linter = require("../../src/jshint.js"); | ||
var helpers = require("../lib/helpers.js"); | ||
var runner = helpers.createRunner(__dirname, __filename); | ||
|
||
exports.testSuccess = function (test) { | ||
runner(test).test("/hello/g"); | ||
runner(test).test("/hello\s(world)/g"); | ||
test.done(); | ||
}; | ||
|
||
exports.testUnsafeSymbols = function (test) { | ||
runner(test) | ||
.addError(1, "W009") | ||
.test("/h[^...]/"); | ||
|
||
runner(test) | ||
.addError(1, "W009") | ||
.test("/h.ey/"); | ||
|
||
test.done(); | ||
}; | ||
|
||
exports.testEmptyClass = function (test) { | ||
runner(test) | ||
.addError(1, "W010") | ||
.test("/h[]/"); | ||
|
||
test.done(); | ||
}; |