Skip to content

Commit

Permalink
[[FIX]] Emit correct token value from "module" API
Browse files Browse the repository at this point in the history
* [[TEST]] Re-factor tests for "module" API

Separate the project's single unit test for the module API into a
standalone file. Beyond improving test discoverability, this
organization encapsulates new logic necessary for test hygiene (as
described in the in-line documentation).

* [[FIX]] Emit correct token value

Correct a typo in the source which caused the value `undefined` to be
emitted as the `from` property for all Identifier tokens, regardless of
the actual character position.
  • Loading branch information
jugglinmike authored and rwaldron committed Apr 3, 2016
1 parent c3b4d63 commit 4a43fb9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/lex.js
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ Lexer.prototype = {
this.triggerAsync("Identifier", {
line: this.line,
char: this.char,
from: this.form,
from: this.from,
name: token.value,
raw_name: token.text,
isProperty: state.tokens.curr.id === "."
Expand Down
108 changes: 108 additions & 0 deletions tests/unit/module-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* The JSHint API does not allow for un-registering "modules", and the Nodeunit
* API does not support per-group setup/teardown logic. These deficiencies
* necessitate additional logic in the test suite in order to test the JSHint
* "module" API hygienically:
*
* - Only one JSHint "module" should be added at any time, regardless of the
* number of tests executed
* - No JSHint "module" should run following the completion of this group of
* tests
*/
"use strict";

var JSHINT = require("../..").JSHINT;
var TestRun = require("../helpers/testhelper").setup.testRun;

var firstRun = true;
var testContext = null;
var suiteSetup = function (done) {
JSHINT.addModule(function (linter) {
if (!testContext) {
return;
}
testContext.onAddModule(linter);
});

done();
};
exports.setUp = function (done) {
testContext = this;

if (firstRun) {
firstRun = false;
suiteSetup(done);
return;
}

done();
};
exports.tearDown = function (done) {
testContext = null;
done();
};

exports["test for GH-1103"] = function (test) {
var code = [ "var ohnoes = 42;" ];

var run = TestRun(test);

var patch = true;

this.onAddModule = function (linter) {
if (!patch) {
return;
}
patch = false;

var ohnoes = "oh noes";
Array.prototype.ohnoes = function () {
linter.warn("E024", { line: 1, char: 1, data: [ ohnoes += "!" ] });
};
};

run.test(code);

test.done();

delete Array.prototype.ohnoes;
};

exports.identifiers = function (test) {
var src = [
"var x = {",
" y: 23,",
" 'z': 45",
"};"
];
var expected = [
{
line: 1,
char: 6,
from: 5,
name: 'x',
raw_name: 'x',
isProperty: false
},
{
line: 2,
char: 4,
from: 3,
name: 'y',
raw_name: 'y',
isProperty: false
}
];
var actual = [];
this.onAddModule = function (linter) {
linter.on("Identifier", function(x) {
actual.push(x);
});
};

JSHINT(src);

test.deepEqual(actual, expected);

test.done();
};
26 changes: 0 additions & 26 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6196,32 +6196,6 @@ exports["test for GH-1089"] = function (test) {
test.done();
};

exports["test for GH-1103"] = function (test) {
var code = [ "var ohnoes = 42;" ];

var run = TestRun(test);

var patch = true;

JSHINT.addModule(function (linter) {
if (!patch) {
return;
}
patch = false;

var ohnoes = "oh noes";
Array.prototype.ohnoes = function () {
linter.warn("E024", { line: 1, char: 1, data: [ ohnoes += "!" ] });
};
});

run.test(code);

test.done();

delete Array.prototype.ohnoes;
};

exports["test for GH-1105"] = function (test) {
var code = [
"while (true) {",
Expand Down

0 comments on commit 4a43fb9

Please sign in to comment.