Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added nonbsp option to warn about non-breaking spaces #1458

Closed
wants to merge 1 commit into from

3 participants

@prayerslayer

Emits warnings if non-breaking spaces should be disallowed (set nonbsp to true). See issue #1456.

@prayerslayer prayerslayer Added nonbsp option
if non-breaking spaces should be disallowed (true | false)
6b446df
@nschonni

This might be a better fit for https://github.com/mdevils/node-jscs as JSHint isn't taking "style" PRs anymore.

@prayerslayer

Yes, maybe. However I argue that this option is not only about style as the combination of non-breaking spaces and no UTF-8 encoding will (or may) cause errors on your page :)

@valueof valueof referenced this pull request from a commit
@prayerslayer prayerslayer Issue #1456: New option - nonbsp (PR #1458)
Signed-off-by: Anton Kovalyov <anton@kovalyov.net>
3cb02e3
@valueof valueof closed this
@jugglinmike jugglinmike referenced this pull request from a commit in jugglinmike/jshint
@prayerslayer prayerslayer Issue #1456: New option - nonbsp (PR #1458)
Signed-off-by: Anton Kovalyov <anton@kovalyov.net>
199d762
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 4, 2014
  1. @prayerslayer

    Added nonbsp option

    prayerslayer authored
    if non-breaking spaces should be disallowed (true | false)
This page is out of date. Refresh to see the latest.
View
6 package.json
@@ -1,8 +1,8 @@
{
"name": "jshint",
- "version": "2.4.1",
+ "version": "2.4.1-prayerslayer",
"homepage": "http://jshint.com/",
- "description": "Static analysis tool for JavaScript",
+ "description": "Static analysis tool for JavaScript (prayerslayer build)",
"author": {
"name": "Anton Kovalyov",
@@ -12,7 +12,7 @@
"repository": {
"type": "git",
- "url": "https://github.com/jshint/jshint.git"
+ "url": "https://github.com/prayerslayer/jshint.git"
},
"bugs": {
View
1  src/jshint.js
@@ -116,6 +116,7 @@ var JSHINT = (function () {
node : true, // if the Node.js environment globals should be
// predefined
noempty : true, // if empty blocks should be disallowed
+ nonbsp : false,// if non-breaking spaces should be disallowed
nonew : true, // if using `new` for side-effects should be disallowed
nonstandard : true, // if non-standard (but widely adopted) globals should
// be predefined
View
22 src/lex.js
@@ -1242,6 +1242,23 @@ Lexer.prototype = {
},
/*
+ * Scan for any occurance of a non-breaking space.
+ */
+ scanNonBreakingSpace: function () {
+ var nbsp = -1;
+
+ if (state.option.nonbsp) {
+ nbsp = this.input.search(reg.nonBreakingSpace);
+ }
+
+ if (state.option.nonbsp === true || state.option.nonbsp === false) {
+ console.log(state.option.nonbsp, nbsp);
+ }
+
+ return nbsp;
+ },
+
+ /*
* Scan for characters that get silently deleted by one or more browsers.
*/
scanUnsafeChars: function () {
@@ -1334,6 +1351,11 @@ Lexer.prototype = {
}
}
+ char = this.scanNonBreakingSpace();
+ if (char >= 0) {
+ this.trigger("warning", { code: "W125", line: this.line, character: char + 1 });
+ }
+
char = this.scanMixedSpacesAndTabs();
if (char >= 0) {
this.trigger("warning", { code: "W099", line: this.line, character: char + 1 });
View
3  src/messages.js
@@ -195,7 +195,8 @@ var warnings = {
W121: "Extending prototype of native object: '{a}'.",
W122: "Invalid typeof value '{a}'",
W123: "'{a}' is already defined in outer scope.",
- W124: "A generator function shall contain a yield statement."
+ W124: "A generator function shall contain a yield statement.",
+ W125: "Illegal non-breaking space."
};
var info = {
View
3  src/reg.js
@@ -10,6 +10,9 @@
exports.unsafeString =
/@cc|<\/?|script|\]\s*\]|<\s*!|&lt/i;
+// Non-breaking space
+exports.nonBreakingSpace = /(\u00A0)/;
+
// Unsafe characters that are silently deleted by one or more browsers (cx)
exports.unsafeChars =
/[\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/;
View
1  tests/unit/fixtures/gh1456.js
@@ -0,0 +1 @@
+var someVar = obj[ "key" ] || 0;
View
18 tests/unit/options.js
@@ -117,6 +117,24 @@ exports.latedef = function (test) {
test.done();
};
+/** Option `nonbsp` warns you about non-breaking spaces.*/
+
+exports.nonbsp = function (test) {
+ // load fixture
+ var src = fs.readFileSync(__dirname + '/fixtures/gh1456.js', 'utf8');
+
+ // if `nonbsp` is set to false we're fine with the code
+ TestRun( test )
+ .test( src, { nonbsp: false, sub: true } );
+
+ // if `nonbsp` is set to true we add an error message
+ TestRun( test )
+ .addError( 1, "Illegal non-breaking space." )
+ .test( src, { nonbsp: true, sub: true } );
+
+ test.done();
+};
+
exports.notypeof = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/typeofcomp.js', 'utf8');
Something went wrong with that request. Please try again.