Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
test: add tests with year-based ecmaVersion (#83)
Browse files Browse the repository at this point in the history
Fixes #82
  • Loading branch information
mdjermanovic authored Dec 4, 2021
1 parent e57ffb0 commit 62e147b
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 400 deletions.
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
Loading

0 comments on commit 62e147b

Please sign in to comment.