feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@ -21,27 +21,24 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-undefined */
import estraverse from "estraverse";
const Syntax = require("estraverse").Syntax;
import Reference from "./reference.js";
import Variable from "./variable.js";
import { Definition } from "./definition.js";
import { assert } from "./assert.js";
const Reference = require("./reference");
const Variable = require("./variable");
const Definition = require("./definition").Definition;
const assert = require("assert");
const { Syntax } = estraverse;
/**
* Test if scope is struct
* @param {Scope} scope - scope
* @param {Block} block - block
* @param {boolean} isMethodDefinition - is method definition
* @param {boolean} useDirective - use directive
* @param {Scope} scope scope
* @param {Block} block block
* @param {boolean} isMethodDefinition is method definition
* @returns {boolean} is strict scope
*/
function isStrictScope(scope, block, isMethodDefinition, useDirective) {
function isStrictScope(scope, block, isMethodDefinition) {
let body;
// When upper scope is exists and strict, inner scope is also strict.
@ -81,48 +78,36 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) {
return false;
}
// Search 'use strict' directive.
if (useDirective) {
for (let i = 0, iz = body.body.length; i < iz; ++i) {
const stmt = body.body[i];
// Search for a 'use strict' directive.
for (let i = 0, iz = body.body.length; i < iz; ++i) {
const stmt = body.body[i];
if (stmt.type !== Syntax.DirectiveStatement) {
break;
}
if (stmt.raw === "\"use strict\"" || stmt.raw === "'use strict'") {
return true;
}
/*
* Check if the current statement is a directive.
* If it isn't, then we're past the directive prologue
* so stop the search because directives cannot
* appear after this point.
*
* Some parsers set `directive:null` on non-directive
* statements, so the `typeof` check is safer than
* checking for property existence.
*/
if (typeof stmt.directive !== "string") {
break;
}
} else {
for (let i = 0, iz = body.body.length; i < iz; ++i) {
const stmt = body.body[i];
if (stmt.type !== Syntax.ExpressionStatement) {
break;
}
const expr = stmt.expression;
if (expr.type !== Syntax.Literal || typeof expr.value !== "string") {
break;
}
if (expr.raw !== null && expr.raw !== undefined) {
if (expr.raw === "\"use strict\"" || expr.raw === "'use strict'") {
return true;
}
} else {
if (expr.value === "use strict") {
return true;
}
}
if (stmt.directive === "use strict") {
return true;
}
}
return false;
}
/**
* Register scope
* @param {ScopeManager} scopeManager - scope manager
* @param {Scope} scope - scope
* @param {ScopeManager} scopeManager scope manager
* @param {Scope} scope scope
* @returns {void}
*/
function registerScope(scopeManager, scope) {
@ -139,7 +124,7 @@ function registerScope(scopeManager, scope) {
/**
* Should be statically
* @param {Object} def - def
* @param {Object} def def
* @returns {boolean} should be statically
*/
function shouldBeStatically(def) {
@ -150,14 +135,15 @@ function shouldBeStatically(def) {
}
/**
* @class Scope
* @constructor Scope
*/
class Scope {
constructor(scopeManager, type, upperScope, block, isMethodDefinition) {
/**
* One of 'module', 'block', 'switch', 'function', 'catch', 'with', 'function', 'class', 'global'.
* @member {String} Scope#type
* One of "global", "module", "function", "function-expression-name", "block", "switch", "catch", "with", "for",
* "class", "class-field-initializer", "class-static-block".
* @member {string} Scope#type
*/
this.type = type;
@ -171,7 +157,8 @@ class Scope {
/**
* The tainted variables of this scope, as <code>{ Variable.name :
* boolean }</code>.
* @member {Map} Scope#taints */
* @member {Map} Scope#taints
*/
this.taints = new Map();
/**
@ -224,7 +211,13 @@ class Scope {
* @member {Scope} Scope#variableScope
*/
this.variableScope =
(this.type === "global" || this.type === "function" || this.type === "module") ? this : upperScope.variableScope;
this.type === "global" ||
this.type === "module" ||
this.type === "function" ||
this.type === "class-field-initializer" ||
this.type === "class-static-block"
? this
: upperScope.variableScope;
/**
* Whether this scope is created by a FunctionExpression.
@ -255,7 +248,9 @@ class Scope {
* Whether 'use strict' is in effect in this scope.
* @member {boolean} Scope#isStrict
*/
this.isStrict = isStrictScope(this, block, isMethodDefinition, scopeManager.__useDirective());
this.isStrict = scopeManager.isStrictModeSupported()
? isStrictScope(this, block, isMethodDefinition)
: false;
/**
* List of nested {@link Scope}s.
@ -342,7 +337,7 @@ class Scope {
// To override by function scopes.
// References in default parameters isn't resolved to variables which are in their function body.
__isValidResolution(ref, variable) { // eslint-disable-line class-methods-use-this, no-unused-vars
__isValidResolution(ref, variable) { // eslint-disable-line class-methods-use-this, no-unused-vars -- Desired as instance method with signature
return true;
}
@ -376,17 +371,17 @@ class Scope {
}
__addDeclaredVariablesOfNode(variable, node) {
if (node === null || node === undefined) {
if (node === null || node === void 0) {
return;
}
let variables = this.__declaredVariables.get(node);
if (variables === null || variables === undefined) {
if (variables === null || variables === void 0) {
variables = [];
this.__declaredVariables.set(node, variables);
}
if (variables.indexOf(variable) === -1) {
if (!variables.includes(variable)) {
variables.push(variable);
}
}
@ -461,8 +456,8 @@ class Scope {
/**
* returns resolved {Reference}
* @method Scope#resolve
* @param {Espree.Identifier} ident - identifier to be resolved.
* @function Scope#resolve
* @param {Espree.Identifier} ident identifier to be resolved.
* @returns {Reference} reference
*/
resolve(ident) {
@ -481,7 +476,7 @@ class Scope {
/**
* returns this scope is static
* @method Scope#isStatic
* @function Scope#isStatic
* @returns {boolean} static
*/
isStatic() {
@ -490,19 +485,19 @@ class Scope {
/**
* returns this scope has materialized arguments
* @method Scope#isArgumentsMaterialized
* @function Scope#isArgumentsMaterialized
* @returns {boolean} arguemnts materialized
*/
isArgumentsMaterialized() { // eslint-disable-line class-methods-use-this
isArgumentsMaterialized() { // eslint-disable-line class-methods-use-this -- Desired as instance method
return true;
}
/**
* returns this scope has materialized `this` reference
* @method Scope#isThisMaterialized
* @function Scope#isThisMaterialized
* @returns {boolean} this materialized
*/
isThisMaterialized() { // eslint-disable-line class-methods-use-this
isThisMaterialized() { // eslint-disable-line class-methods-use-this -- Desired as instance method
return true;
}
@ -519,6 +514,9 @@ class Scope {
}
}
/**
* Global scope.
*/
class GlobalScope extends Scope {
constructor(scopeManager, block) {
super(scopeManager, "global", null, block, false);
@ -527,10 +525,10 @@ class GlobalScope extends Scope {
variables: [],
/**
* List of {@link Reference}s that are left to be resolved (i.e. which
* need to be linked to the variable they refer to).
* @member {Reference[]} Scope#implicit#left
*/
* List of {@link Reference}s that are left to be resolved (i.e. which
* need to be linked to the variable they refer to).
* @member {Reference[]} Scope#implicit#left
*/
left: []
};
}
@ -580,12 +578,18 @@ class GlobalScope extends Scope {
}
}
/**
* Module scope.
*/
class ModuleScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "module", upperScope, block, false);
}
}
/**
* Function expression name scope.
*/
class FunctionExpressionNameScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "function-expression-name", upperScope, block, false);
@ -602,12 +606,18 @@ class FunctionExpressionNameScope extends Scope {
}
}
/**
* Catch scope.
*/
class CatchScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "catch", upperScope, block, false);
}
}
/**
* With statement scope.
*/
class WithScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "with", upperScope, block, false);
@ -630,18 +640,27 @@ class WithScope extends Scope {
}
}
/**
* Block scope.
*/
class BlockScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "block", upperScope, block, false);
}
}
/**
* Switch scope.
*/
class SwitchScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "switch", upperScope, block, false);
}
}
/**
* Function scope.
*/
class FunctionScope extends Scope {
constructor(scopeManager, upperScope, block, isMethodDefinition) {
super(scopeManager, "function", upperScope, block, isMethodDefinition);
@ -719,19 +738,43 @@ class FunctionScope extends Scope {
}
}
/**
* Scope of for, for-in, and for-of statements.
*/
class ForScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "for", upperScope, block, false);
}
}
/**
* Class scope.
*/
class ClassScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "class", upperScope, block, false);
}
}
module.exports = {
/**
* Class field initializer scope.
*/
class ClassFieldInitializerScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "class-field-initializer", upperScope, block, true);
}
}
/**
* Class static block scope.
*/
class ClassStaticBlockScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "class-static-block", upperScope, block, true);
}
}
export {
Scope,
GlobalScope,
ModuleScope,
@ -742,7 +785,9 @@ module.exports = {
SwitchScope,
FunctionScope,
ForScope,
ClassScope
ClassScope,
ClassFieldInitializerScope,
ClassStaticBlockScope
};
/* vim: set sw=4 ts=4 et tw=80 : */