Skip to content

Commit

Permalink
chore: make the $parser non-enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Jun 24, 2021
1 parent 5b13582 commit 0e3bbb5
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -239,6 +239,21 @@ function defineStartEndAsError(objName, node) {
});
}

/**
* define a property $parser to be original parser
* use `defineProperty` to make it non-enumerable
* @param {Parser} wrappedParser the wrapped parser
* @param {Parser} originalParser the original parser
* @returns {void}
*/
function defineOriginalParser(wrappedParser, originalParser) {
Object.defineProperty(wrappedParser, "$parser", {
value: originalParser,
writable: true,
enumerable: false
});
}

/**
* Define `start`/`end` properties of all nodes of the given AST as throwing error.
* @param {ASTNode} ast The root node to errorize `start`/`end` properties.
Expand All @@ -258,8 +273,10 @@ function defineStartEndAsErrorInTree(ast, visitorKeys) {
* @returns {Parser} Wrapped parser object.
*/
function wrapParser(parser) {
let wrappedParser;

if (typeof parser.parseForESLint === "function") {
return {
wrappedParser = {
$parser: parser,
parseForESLint(...args) {
const ret = parser.parseForESLint(...args);
Expand All @@ -268,16 +285,22 @@ function wrapParser(parser) {
return ret;
}
};
} else {
wrappedParser = {
$parser: parser,
parse(...args) {
const ast = parser.parse(...args);

defineStartEndAsErrorInTree(ast);
return ast;
}
};
}
return {
$parser: parser,
parse(...args) {
const ast = parser.parse(...args);

defineStartEndAsErrorInTree(ast);
return ast;
}
};
defineOriginalParser(wrappedParser, parser);

return wrappedParser;

}

//------------------------------------------------------------------------------
Expand Down

0 comments on commit 0e3bbb5

Please sign in to comment.