The aim of this PR is to make the project more flexible and allow users to add new behaviors more easily.
I've created an AstAnalyser class that implements the logic originally contained in runASTAnalysis and runASTAnalysisOnFile. The class can be extended and has a dependency on any EStree-compatible parser that respects the following abstraction:
const jsAnalyser = new AstAnalyser(new JsSourceParser());
const tsAnalyser = new AstAnalyser(new TsSourceParser());
I removed the class SourceParser to favor composition over inheritance.
Responsibility for preparing source code (deleting html comments etc.) for analysis can be assigned to the new class AstAnalyser. It removes the need for this class from the workspaces, wich could be problematic to create the TsSourceParser (214).
I kept the functions runASTAnalysis and runASTAnalysisOnFile so my additions extend the possibilities without breaking the existing. Users can pass a custom parser to these functions without having to completely modify their code:
function runASTAnalysis(
str,
options = Object.create(null)
) {
const {
customParser = new JsSourceParser(),
...opts
} = options;
const analyser = new AstAnalyser(customParser);
return analyser.analyse(str, opts);
}
I've moved the tests for these functions to the new AstAnalyser class and replaced them with integration tests verifying that the functions call the AstAnalyser methods with the right arguments.
The aim of this PR is to make the project more flexible and allow users to add new behaviors more easily.
I've created an
AstAnalyser
class that implements the logic originally contained inrunASTAnalysis
andrunASTAnalysisOnFile
. The class can be extended and has a dependency on any EStree-compatible parser that respects the following abstraction:Example:
I removed the class
SourceParser
to favor composition over inheritance. Responsibility for preparing source code (deleting html comments etc.) for analysis can be assigned to the new classAstAnalyser
. It removes the need for this class from the workspaces, wich could be problematic to create theTsSourceParser
(214).I kept the functions
runASTAnalysis
andrunASTAnalysisOnFile
so my additions extend the possibilities without breaking the existing. Users can pass a custom parser to these functions without having to completely modify their code:I've moved the tests for these functions to the new
AstAnalyser
class and replaced them with integration tests verifying that the functions call theAstAnalyser
methods with the right arguments.