* Prevent parseForESLint() behavior from changing after parse() is called (fixes https://github.com/babel/babel-eslint/issues/558, fixes https://github.com/eslint/eslint/issues/9767) * Avoid using the enhanced referencer after monkeypatching * Chore: add test for babel/babel-eslint#558 * Pass correct scope analyzer options * fix escope patch and improve tests * remove process.exit(1)
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
// Checks if the source ast implements the target ast. Ignores extra keys on source ast
|
|
module.exports = function assertImplementsAST(target, source, path) {
|
|
if (!path) {
|
|
path = [];
|
|
}
|
|
|
|
function error(text) {
|
|
var err = new Error(`At ${path.join(".")}: ${text}:`);
|
|
err.depth = path.length + 1;
|
|
throw err;
|
|
}
|
|
|
|
var typeA = target === null ? "null" : typeof target;
|
|
var typeB = source === null ? "null" : typeof source;
|
|
if (typeA !== typeB) {
|
|
error(
|
|
`have different types (${typeA} !== ${typeB}) (${target} !== ${source})`
|
|
);
|
|
} else if (
|
|
typeA === "object" &&
|
|
["RegExp"].indexOf(target.constructor.name) !== -1 &&
|
|
target.constructor.name !== source.constructor.name
|
|
) {
|
|
error(
|
|
`object have different constructors (${target.constructor
|
|
.name} !== ${source.constructor.name}`
|
|
);
|
|
} else if (typeA === "object") {
|
|
var keysTarget = Object.keys(target);
|
|
for (var i in keysTarget) {
|
|
var key = keysTarget[i];
|
|
path.push(key);
|
|
assertImplementsAST(target[key], source[key], path);
|
|
path.pop();
|
|
}
|
|
} else if (target !== source) {
|
|
error(
|
|
`are different (${JSON.stringify(target)} !== ${JSON.stringify(source)})`
|
|
);
|
|
}
|
|
};
|