Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests with year-based ecmaVersion #83

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
268 changes: 139 additions & 129 deletions tests/es6-block-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,162 +25,172 @@

import { expect } from "chai";
import espree from "./util/espree.js";
import { getSupportedEcmaVersions } from "./util/ecma-version.js";
import { analyze } from "../lib/index.js";

describe("ES6 block scope", () => {
it("let is materialized in ES6 block scope#1", () => {
const ast = espree(`
{
let i = 20;
i;
}
`);
getSupportedEcmaVersions({ min: 6 }).forEach(ecmaVersion => {
const ast = espree(`
{
let i = 20;
i;
}
`);

const scopeManager = analyze(ast, { ecmaVersion: 6 });
const scopeManager = analyze(ast, { ecmaVersion });

expect(scopeManager.scopes).to.have.length(2); // Program and BlockStatement scope.
expect(scopeManager.scopes).to.have.length(2); // Program and BlockStatement scope.

let scope = scopeManager.scopes[0];
let scope = scopeManager.scopes[0];

expect(scope.type).to.be.equal("global");
expect(scope.variables).to.have.length(0); // No variable in Program scope.
expect(scope.type).to.be.equal("global");
expect(scope.variables).to.have.length(0); // No variable in Program scope.

scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1); // `i` in block scope.
expect(scope.variables[0].name).to.be.equal("i");
expect(scope.references).to.have.length(2);
expect(scope.references[0].identifier.name).to.be.equal("i");
expect(scope.references[1].identifier.name).to.be.equal("i");
scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1); // `i` in block scope.
expect(scope.variables[0].name).to.be.equal("i");
expect(scope.references).to.have.length(2);
expect(scope.references[0].identifier.name).to.be.equal("i");
expect(scope.references[1].identifier.name).to.be.equal("i");
});
});

it("function delaration is materialized in ES6 block scope", () => {
const ast = espree(`
{
function test() {
getSupportedEcmaVersions({ min: 6 }).forEach(ecmaVersion => {
const ast = espree(`
{
function test() {
}
test();
}
test();
}
`);
`);

const scopeManager = analyze(ast, { ecmaVersion: 6 });
const scopeManager = analyze(ast, { ecmaVersion });

expect(scopeManager.scopes).to.have.length(3);
expect(scopeManager.scopes).to.have.length(3);

let scope = scopeManager.scopes[0];
let scope = scopeManager.scopes[0];

expect(scope.type).to.be.equal("global");
expect(scope.variables).to.have.length(0);
expect(scope.type).to.be.equal("global");
expect(scope.variables).to.have.length(0);

scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("test");
expect(scope.references).to.have.length(1);
expect(scope.references[0].identifier.name).to.be.equal("test");
scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("test");
expect(scope.references).to.have.length(1);
expect(scope.references[0].identifier.name).to.be.equal("test");

scope = scopeManager.scopes[2];
expect(scope.type).to.be.equal("function");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("arguments");
expect(scope.references).to.have.length(0);
scope = scopeManager.scopes[2];
expect(scope.type).to.be.equal("function");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("arguments");
expect(scope.references).to.have.length(0);
});
});

it("let is not hoistable#1", () => {
const ast = espree(`
var i = 42; (1)
{
i; // (2) ReferenceError at runtime.
let i = 20; // (2)
i; // (2)
}
`);

const scopeManager = analyze(ast, { ecmaVersion: 6 });

expect(scopeManager.scopes).to.have.length(2);

const globalScope = scopeManager.scopes[0];

expect(globalScope.type).to.be.equal("global");
expect(globalScope.variables).to.have.length(1);
expect(globalScope.variables[0].name).to.be.equal("i");
expect(globalScope.references).to.have.length(1);

const scope = scopeManager.scopes[1];

expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("i");
expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(scope.variables[0]);
expect(scope.references[1].resolved).to.be.equal(scope.variables[0]);
expect(scope.references[2].resolved).to.be.equal(scope.variables[0]);
getSupportedEcmaVersions({ min: 6 }).forEach(ecmaVersion => {
const ast = espree(`
var i = 42; (1)
{
i; // (2) ReferenceError at runtime.
let i = 20; // (2)
i; // (2)
}
`);

const scopeManager = analyze(ast, { ecmaVersion });

expect(scopeManager.scopes).to.have.length(2);

const globalScope = scopeManager.scopes[0];

expect(globalScope.type).to.be.equal("global");
expect(globalScope.variables).to.have.length(1);
expect(globalScope.variables[0].name).to.be.equal("i");
expect(globalScope.references).to.have.length(1);

const scope = scopeManager.scopes[1];

expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("i");
expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(scope.variables[0]);
expect(scope.references[1].resolved).to.be.equal(scope.variables[0]);
expect(scope.references[2].resolved).to.be.equal(scope.variables[0]);
});

});

it("let is not hoistable#2", () => {
const ast = espree(`
(function () {
var i = 42; // (1)
i; // (1)
{
i; // (3)
getSupportedEcmaVersions({ min: 6 }).forEach(ecmaVersion => {
const ast = espree(`
(function () {
var i = 42; // (1)
i; // (1)
{
i; // (2)
let i = 20; // (2)
i; // (2)
i; // (3)
{
i; // (2)
let i = 20; // (2)
i; // (2)
}
let i = 30; // (3)
i; // (3)
}
let i = 30; // (3)
i; // (3)
}
i; // (1)
}());
`);

const scopeManager = analyze(ast, { ecmaVersion: 6 });

expect(scopeManager.scopes).to.have.length(4);

const globalScope = scopeManager.scopes[0];

expect(globalScope.type).to.be.equal("global");
expect(globalScope.variables).to.have.length(0);
expect(globalScope.references).to.have.length(0);

let scope = scopeManager.scopes[1];

expect(scope.type).to.be.equal("function");
expect(scope.variables).to.have.length(2);
expect(scope.variables[0].name).to.be.equal("arguments");
expect(scope.variables[1].name).to.be.equal("i");
const v1 = scope.variables[1];

expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(v1);
expect(scope.references[1].resolved).to.be.equal(v1);
expect(scope.references[2].resolved).to.be.equal(v1);

scope = scopeManager.scopes[2];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("i");
const v3 = scope.variables[0];

expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(v3);
expect(scope.references[1].resolved).to.be.equal(v3);
expect(scope.references[2].resolved).to.be.equal(v3);

scope = scopeManager.scopes[3];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("i");
const v2 = scope.variables[0];

expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(v2);
expect(scope.references[1].resolved).to.be.equal(v2);
expect(scope.references[2].resolved).to.be.equal(v2);
i; // (1)
}());
`);

const scopeManager = analyze(ast, { ecmaVersion });

expect(scopeManager.scopes).to.have.length(4);

const globalScope = scopeManager.scopes[0];

expect(globalScope.type).to.be.equal("global");
expect(globalScope.variables).to.have.length(0);
expect(globalScope.references).to.have.length(0);

let scope = scopeManager.scopes[1];

expect(scope.type).to.be.equal("function");
expect(scope.variables).to.have.length(2);
expect(scope.variables[0].name).to.be.equal("arguments");
expect(scope.variables[1].name).to.be.equal("i");
const v1 = scope.variables[1];

expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(v1);
expect(scope.references[1].resolved).to.be.equal(v1);
expect(scope.references[2].resolved).to.be.equal(v1);

scope = scopeManager.scopes[2];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("i");
const v3 = scope.variables[0];

expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(v3);
expect(scope.references[1].resolved).to.be.equal(v3);
expect(scope.references[2].resolved).to.be.equal(v3);

scope = scopeManager.scopes[3];
expect(scope.type).to.be.equal("block");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("i");
const v2 = scope.variables[0];

expect(scope.references).to.have.length(3);
expect(scope.references[0].resolved).to.be.equal(v2);
expect(scope.references[1].resolved).to.be.equal(v2);
expect(scope.references[2].resolved).to.be.equal(v2);
});
});
});

Expand Down