|
@@ -3,8 +3,6 @@ import acornImportMeta from 'acorn-import-meta';
|
|
|
import * as ESTree from 'estree';
|
|
|
import chalk, { Chalk } from 'chalk';
|
|
|
|
|
|
-import { IASTParserFacadeInputData } from './interfaces/IASTParserFacadeInputData';
|
|
|
-
|
|
|
/**
|
|
|
* Facade over AST parser `acorn`
|
|
|
*/
|
|
@@ -28,23 +26,23 @@ export class ASTParserFacade {
|
|
|
];
|
|
|
|
|
|
/**
|
|
|
- * @param {string} inputData
|
|
|
+ * @param {string} sourceCode
|
|
|
* @param {Options} config
|
|
|
* @returns {Program}
|
|
|
*/
|
|
|
- public static parse (inputData: IASTParserFacadeInputData, config: acorn.Options): ESTree.Program | never {
|
|
|
+ public static parse (sourceCode: string, config: acorn.Options): ESTree.Program | never {
|
|
|
const sourceTypeLength: number = ASTParserFacade.sourceTypes.length;
|
|
|
|
|
|
for (let i: number = 0; i < sourceTypeLength; i++) {
|
|
|
try {
|
|
|
- return ASTParserFacade.parseType(inputData, config, ASTParserFacade.sourceTypes[i]);
|
|
|
+ return ASTParserFacade.parseType(sourceCode, config, ASTParserFacade.sourceTypes[i]);
|
|
|
} catch (error) {
|
|
|
if (i < sourceTypeLength - 1) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
throw new Error(ASTParserFacade.processParsingError(
|
|
|
- inputData,
|
|
|
+ sourceCode,
|
|
|
error.message,
|
|
|
error.loc
|
|
|
));
|
|
@@ -55,17 +53,16 @@ export class ASTParserFacade {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param {IASTParserFacadeInputData} inputData
|
|
|
+ * @param {string} sourceCode
|
|
|
* @param {acorn.Options} inputConfig
|
|
|
* @param {acorn.Options["sourceType"]} sourceType
|
|
|
* @returns {Program}
|
|
|
*/
|
|
|
private static parseType (
|
|
|
- inputData: IASTParserFacadeInputData,
|
|
|
+ sourceCode: string,
|
|
|
inputConfig: acorn.Options,
|
|
|
sourceType: acorn.Options['sourceType']
|
|
|
): ESTree.Program {
|
|
|
- const { sourceCode } = inputData;
|
|
|
const comments: ESTree.Comment[] = [];
|
|
|
const config: acorn.Options = {
|
|
|
...inputConfig,
|
|
@@ -85,13 +82,13 @@ export class ASTParserFacade {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param {IASTParserFacadeInputData} inputData
|
|
|
+ * @param {string} sourceCode
|
|
|
* @param {string} errorMessage
|
|
|
* @param {Position | null} position
|
|
|
* @returns {never}
|
|
|
*/
|
|
|
private static processParsingError (
|
|
|
- inputData: IASTParserFacadeInputData,
|
|
|
+ sourceCode: string,
|
|
|
errorMessage: string,
|
|
|
position: ESTree.Position | null
|
|
|
): never {
|
|
@@ -99,8 +96,6 @@ export class ASTParserFacade {
|
|
|
throw new Error(errorMessage);
|
|
|
}
|
|
|
|
|
|
- const { sourceCode, inputFilePath } = inputData;
|
|
|
-
|
|
|
const sourceCodeLines: string[] = sourceCode.split(/\r?\n/);
|
|
|
const errorLine: string | undefined = sourceCodeLines[position.line - 1];
|
|
|
|
|
@@ -108,10 +103,6 @@ export class ASTParserFacade {
|
|
|
throw new Error(errorMessage);
|
|
|
}
|
|
|
|
|
|
- const formattedInputFilePath: string = inputFilePath
|
|
|
- ? `${inputFilePath}, `
|
|
|
- : '';
|
|
|
-
|
|
|
const startErrorIndex: number = Math.max(0, position.column - ASTParserFacade.nearestSymbolsCount);
|
|
|
const endErrorIndex: number = Math.min(errorLine.length, position.column + ASTParserFacade.nearestSymbolsCount);
|
|
|
|
|
@@ -121,7 +112,7 @@ export class ASTParserFacade {
|
|
|
}...`;
|
|
|
|
|
|
throw new Error(
|
|
|
- `ERROR in ${formattedInputFilePath}line ${position.line}: ${errorMessage}\n${formattedPointer} ${formattedCodeSlice}`
|
|
|
+ `ERROR at line ${position.line}: ${errorMessage}\n${formattedPointer} ${formattedCodeSlice}`
|
|
|
);
|
|
|
}
|
|
|
}
|