Clone engine262 in /engine262

This commit is contained in:
2020-09-07 10:19:00 +05:30
parent 7688de2e5c
commit 66c1aeb9e0
313 changed files with 46014 additions and 0 deletions
@@ -0,0 +1,9 @@
'use strict';
module.exports = {
rules: {
'no-use-in-def': require('./no-use-in-def'),
'valid-feature': require('./valid-feature'),
'valid-throw': require('./valid-throw'),
},
};
@@ -0,0 +1,68 @@
'use strict';
// https://github.com/eslint/eslint/blob/master/lib/rules/no-use-before-define.js
const SENTINEL_TYPE = /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/u;
const FOR_IN_OF_TYPE = /^For(?:In|Of)Statement$/u;
function isInRange(node, location) {
return node && node.range[0] <= location && location <= node.range[1];
}
function isUsedInDef(reference) {
const variable = reference.resolved;
if (!variable || variable.scope !== reference.from) {
return false;
}
let node = variable.identifiers[0].parent;
const location = reference.identifier.range[1];
while (node) {
if (node.type === 'VariableDeclarator') {
if (isInRange(node.init, location)) {
return true;
}
if (FOR_IN_OF_TYPE.test(node.parent.parent.type) && isInRange(node.parent.parent.right, location)) {
return true;
}
break;
}
if (node.type === 'AssignmentPattern' && isInRange(node.right, location)) {
return true;
}
if (SENTINEL_TYPE.test(node.type)) {
break;
}
node = node.parent;
}
return false;
}
module.exports = {
create(context) {
function findVariablesInScope(scope) {
scope.references.forEach((reference) => {
if (isUsedInDef(reference)) {
context.report({
node: reference.identifier,
message: '{{name}} was used in its own definition',
data: reference.identifier,
});
}
});
scope.childScopes.forEach((s) => findVariablesInScope(s));
}
return {
Program() {
findVariablesInScope(context.getScope());
},
};
},
};
@@ -0,0 +1,32 @@
'use strict';
let features;
try {
features = require('../..').FEATURES.map((f) => f.flag);
} catch {}
function isFeatureCall(node) {
return node.callee.type === 'MemberExpression'
&& node.callee.computed === false
&& node.callee.property.name === 'feature';
}
module.exports = {
create(context) {
return {
CallExpression(node) {
if (!isFeatureCall(node)) {
return;
}
if (node.arguments.length !== 1) {
context.report(node, 'Invalid arguments passed to feature()');
return;
}
const featureName = node.arguments[0].value;
if (!features.includes(featureName)) {
context.report(node.arguments[0], `'${featureName}' is not a valid feature. Check src/engine.mjs.`);
}
},
};
},
};
@@ -0,0 +1,63 @@
'use strict';
const fs = require('fs');
const path = require('path');
const acorn = require('acorn');
function isThrowCall(node) {
return node.callee.type === 'MemberExpression'
&& node.callee.computed === false
&& node.callee.object.type === 'Identifier'
&& node.callee.object.name === 'surroundingAgent'
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name === 'Throw';
}
const templates = {};
{
const source = fs.readFileSync(path.join(__dirname, '../../src/messages.mjs'), 'utf8');
const ast = acorn.parse(source, { ecmaVersion: 2020, sourceType: 'module' });
ast.body.forEach((n) => {
if (n.type !== 'ExportNamedDeclaration') {
return;
}
const [v] = n.declaration.declarations;
const name = v.id.name;
const length = v.init.params.length;
templates[name] = length;
});
}
module.exports = {
create(context) {
return {
CallExpression(node) {
if (!isThrowCall(node)) {
return;
}
if (node.arguments.length === 1 && node.arguments[0].type !== 'Literal') {
return;
}
const [type, template, ...templateArgs] = node.arguments;
if (!type || type.type !== 'Literal') {
context.report(node, 'Throw must use a valid error constructor');
return;
}
if (!template || template.type !== 'Literal') {
context.report(node, 'Throw must use a valid message template');
return;
}
const tfn = templates[template.value];
if (tfn === undefined) {
context.report(template, `'${template.value}' is not a valid message template`);
return;
}
if (tfn !== templateArgs.length) {
context.report(node, `Template expects ${tfn} args`);
}
},
};
},
};