Skip to content

Commit

Permalink
Chore: remove internal Linter#getAncestors helper (refs #9161) (#9222)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Sep 4, 2017
1 parent a567499 commit 171962a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
10 changes: 1 addition & 9 deletions lib/linter.js
Expand Up @@ -900,7 +900,7 @@ class Linter {
Object.assign(
Object.create(BASE_TRAVERSAL_CONTEXT),
{
getAncestors: this.getAncestors.bind(this),
getAncestors: () => this.traverser.parents(),
getDeclaredVariables: this.getDeclaredVariables.bind(this),
getFilename: this.getFilename.bind(this),
getScope: this.getScope.bind(this),
Expand Down Expand Up @@ -1050,14 +1050,6 @@ class Linter {
return this.sourceCode;
}

/**
* Gets nodes that are ancestors of current node.
* @returns {ASTNode[]} Array of objects representing ancestors.
*/
getAncestors() {
return this.traverser.parents();
}

/**
* Gets the scope for the current node.
* @returns {Object} An object representing the current node's scope.
Expand Down
33 changes: 20 additions & 13 deletions tests/lib/linter.js
Expand Up @@ -302,36 +302,43 @@ describe("Linter", () => {

});

describe("when calling getAncestors", () => {
describe("when calling context.getAncestors", () => {
const code = TEST_CODE;

it("should retrieve all ancestors when used", () => {

const config = { rules: { checker: "error" } };
const spy = sandbox.spy(() => {
const ancestors = linter.getAncestors();
let spy;

assert.equal(ancestors.length, 3);
});
linter.defineRule("checker", context => {
spy = sandbox.spy(() => {
const ancestors = context.getAncestors();

linter.defineRule("checker", () => ({ BinaryExpression: spy }));
assert.equal(ancestors.length, 3);
});
return { BinaryExpression: spy };
});

linter.verify(code, config, filename, true);
assert(spy.calledOnce);
assert(spy && spy.calledOnce);
});

it("should retrieve empty ancestors for root node", () => {
const config = { rules: { checker: "error" } };
const spy = sandbox.spy(() => {
const ancestors = linter.getAncestors();
let spy;

assert.equal(ancestors.length, 0);
});
linter.defineRule("checker", context => {
spy = sandbox.spy(() => {
const ancestors = context.getAncestors();

linter.defineRule("checker", () => ({ Program: spy }));
assert.equal(ancestors.length, 0);
});

return { Program: spy };
});

linter.verify(code, config);
assert(spy.calledOnce);
assert(spy && spy.calledOnce);
});
});

Expand Down

0 comments on commit 171962a

Please sign in to comment.