Repository: eslint/eslint
File: tests/lib/source-code/token-store.js (JavaScript)

Code before:
                filter() {
                    assert.fail("Unexpected call to filter callback");
                }
            });

            assert.strictEqual(token, null);
        });

        it("should return null if the source is empty", () => {
            const code = "";
            const ast = espree.parse(code, { loc: true, range: true, tokens: true, comment: true });
            const tokenStore = new TokenStore(ast.tokens, ast.comments);
            const token = tokenStore.getFirstToken(ast);

            assert.strictEqual(token, null);
        });

        it("should retrieve a parent node's token if a node has no tokens", () => {
            const code = "foo``";
            const ast = espree.parse(code, { loc: true, range: true, tokens: true, comment: true, ecmaVersion: 6 });
            const tokenStore = new TokenStore(ast.tokens, ast.comments);
            const emptyTemplateElementNode = ast.body[0].expression.quasi.quasis[0];
            const token = tokenStore.getFirstToken(emptyTemplateElementNode);

            assert.strictEqual(token.value, "``");
        });

    });

    describe("when calling getLastTokens", () => {

        it("should retrieve zero tokens from the end of a node's token stream", () => {
            check(
                store.getLastTokens(BinaryExpression, 0),
                []
            );
        });

        it("should retrieve one token from the end of a node's token stream", () => {
            check(
                store.getLastTokens(BinaryExpression, 1),
                ["b"]
            );
        });

        it("should retrieve more than one token from the end of a node's token stream", () => {
            check(
                store.getLastTokens(BinaryExpression, 2),
                ["*", "b"]
            );
        });

Review comment (suggestion):
If a node has no tokens, I think `getFirstToken` should return `null`. Either way, this doesn't seem to be a case where a node has no tokens, because that token does belong to the TemplateElement node (`ast.body[0].expression.quasi.quasis[0]` node).

Code after:
            const token = tokenStore.getFirstToken(ast);

            assert.strictEqual(token, ast.tokens[0]);
        });

        it("should return null if the source contains only comments", () => {
            const code = "// comment";
            const ast = espree.parse(code, { loc: true, range: true, tokens: true, comment: true });
            const tokenStore = new TokenStore(ast.tokens, ast.comments);
            const token = tokenStore.getFirstToken(ast, {
                filter() {
                    assert.fail("Unexpected call to filter callback");
                }
            });

            assert.strictEqual(token, null);
        });

        it("should return null if the source is empty", () => {
            const code = "";
            const ast = espree.parse(code, { loc: true, range: true, tokens: true, comment: true });
            const tokenStore = new TokenStore(ast.tokens, ast.comments);
            const token = tokenStore.getFirstToken(ast);

            assert.strictEqual(token, null);
        });

    });

    describe("when calling getLastTokens", () => {

        it("should retrieve zero tokens from the end of a node's token stream", () => {
            check(
                store.getLastTokens(BinaryExpression, 0),
                []
            );
        });

        it("should retrieve one token from the end of a node's token stream", () => {
            check(
                store.getLastTokens(BinaryExpression, 1),
                ["b"]
            );
        });

        it("should retrieve more than one token from the end of a node's token stream", () => {
            check(
                store.getLastTokens(BinaryExpression, 2),
                ["*", "b"]
            );
        });