Skip to content

Commit

Permalink
Merge pull request #532 from eslint/comments
Browse files Browse the repository at this point in the history
Add support for comment events (fixes #531)
  • Loading branch information
nzakas committed Jan 18, 2014
2 parents 7d613f5 + 328a846 commit f0e661b
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 30 deletions.
35 changes: 35 additions & 0 deletions lib/eslint.js
Expand Up @@ -125,6 +125,7 @@ function addDeclaredGlobals(program, globalScope, config) {
});
}

// TODO: Figures out if we can eliminate this initial sweep of comments
program.comments.forEach(function(comment) {
parseComment(comment, declaredGlobals);
});
Expand Down Expand Up @@ -316,10 +317,44 @@ module.exports = (function() {
*/
controller.traverse(ast, {
enter: function(node) {
var comments = api.getComments(node),
leadingComments = comments.leading,
trailingComments = comments.trailing;

if (leadingComments) {
leadingComments.forEach(function(node) {
api.emit(node.type + "Comment", node);
});
}

api.emit(node.type, node);

if (trailingComments) {
trailingComments.forEach(function(node) {
api.emit(node.type + "Comment", node);
});
}

},
leave: function(node) {

var comments = api.getComments(node),
leadingComments = comments.leading,
trailingComments = comments.trailing;

if (trailingComments) {
trailingComments.forEach(function(node) {
api.emit(node.type + "Comment:after", node);
});
}

api.emit(node.type + ":after", node);

if (leadingComments) {
leadingComments.forEach(function(node) {
api.emit(node.type + "Comment:after", node);
});
}
}
});

Expand Down
149 changes: 119 additions & 30 deletions tests/lib/eslint.js
Expand Up @@ -40,7 +40,8 @@ function getVariable(scope, name) {

describe("eslint", function() {
describe("when using events", function() {
var code = TEST_CODE;
var code = TEST_CODE,
sandbox;

it("an error should be thrown when an error occurs inside of an event handler", function() {
var config = { rules: {} };
Expand All @@ -59,28 +60,44 @@ describe("eslint", function() {
describe("when calling toSource()", function() {
var code = TEST_CODE;

it("should retrieve all text when used without parameters", function() {
var config = { rules: {} };
beforeEach(function() {
sandbox = sinon.sandbox.create();
});

eslint.reset();
eslint.on("Program", function() {
afterEach(function() {
sandbox.verifyAndRestore();
});

it("should retrieve all text when used without parameters", function() {
function handler() {
var source = eslint.getSource();
assert.equal(source, TEST_CODE);
});
}

var config = { rules: {} },
spy = sandbox.spy(handler);

eslint.reset();
eslint.on("Program", spy);

eslint.verify(code, config, true);
assert(spy.calledOnce);
});

it("should retrieve all text for root node", function() {
var config = { rules: {} };

eslint.reset();
eslint.on("Program", function(node) {
function handler(node) {
var source = eslint.getSource(node);
assert.equal(source, TEST_CODE);
});
}

var config = { rules: {} },
spy = sandbox.spy(handler);

eslint.reset();
eslint.on("Program", spy);

eslint.verify(code, config, true);
assert(spy.calledOnce);
});

it("should retrieve all text for binary expression", function() {
Expand Down Expand Up @@ -197,22 +214,34 @@ describe("eslint", function() {
});

describe("when retrieving comments", function() {
var code = [
"// my line comment",
"var a = 42;",
"/* my block comment */"
].join("\n");
var sandbox,
code = [
"// my line comment",
"var a = 42;",
"/* my block comment */"
].join("\n");

it("should retrieve all comments", function() {
var config = { rules: {} };
beforeEach(function() {
sandbox = sinon.sandbox.create();
});

eslint.reset();
eslint.on("Program", function(/*node*/) {
afterEach(function() {
sandbox.verifyAndRestore();
});

it("should retrieve all comments", function() {
function handler() {
var comments = eslint.getAllComments();
assert.equal(comments.length, 2);
});
}
var config = { rules: {} },
spy = sandbox.spy(handler);

eslint.reset();
eslint.on("Program", spy);

eslint.verify(code, config, true);
assert(spy.calledOnce, "Handler should be called.");
});

it("should attach them to all nodes", function() {
Expand All @@ -236,20 +265,80 @@ describe("eslint", function() {
eslint.verify(code, config, true);
});

it("should attach them lazily", function() {
var config = { rules: {} };
it("should fire LineComment event", function() {

function handler(node) {
var code = eslint.getSource(node);
assert.equal(node.value, " my line comment");
assert.equal(code, "// my line comment");
}

var config = { rules: {} },
spy = sandbox.spy(handler);

eslint.reset();
eslint.on("VariableDeclaration", function (node) {
assert.equal(node.hasOwnProperty("leadingComments"), false);
assert.equal(node.hasOwnProperty("trailingComments"), false);
eslint.getComments(node);
assert.equal(node.hasOwnProperty("leadingComments"), false);
assert.equal(node.hasOwnProperty("trailingComments"), true);
});
eslint.on("LineComment", spy);

eslint.verify(code, config, true);
assert(spy.calledOnce, "Handler should be called.");
});

it("should fire LineComment and LineComment:after events", function() {

function handler(node) {
var code = eslint.getSource(node);
assert.equal(node.value, " my line comment");
assert.equal(code, "// my line comment");
}

var config = { rules: {} },
spy = sandbox.spy(handler);

eslint.reset();
eslint.on("LineComment", spy);
eslint.on("LineComment:after", spy);

eslint.verify(code, config, true);
assert(spy.calledTwice, "Handler should be called.");
});

it("should fire BlockComment event", function() {

function handler(node) {
var code = eslint.getSource(node);
assert.equal(node.value, " my block comment ");
assert.equal(code, "/* my block comment */");
}

var config = { rules: {} },
spy = sandbox.spy(handler);

eslint.reset();
eslint.on("BlockComment", spy);

eslint.verify(code, config, true);
assert(spy.calledOnce, "Handler should be called.");
});

it("should fire BlockComment:after event", function() {

function handler(node) {
var code = eslint.getSource(node);
assert.equal(node.value, " my block comment ");
assert.equal(code, "/* my block comment */");
}

var config = { rules: {} },
spy = sandbox.spy(handler);

eslint.reset();
eslint.on("BlockComment", spy);
eslint.on("BlockComment:after", spy);

eslint.verify(code, config, true);
assert(spy.calledTwice, "Handler should be called.");
});

});

describe("when calling getAncestors", function() {
Expand Down

0 comments on commit f0e661b

Please sign in to comment.