Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function parseTypeScriptSourceFile(
fileContent: string,
filePath: string,
tsConfigs?: string[],
): SourceCode | ParsingError {
try {
checkTypeScriptVersionCompatibility(require("typescript").version);
// we load the typescript parser dynamically, so we don't need typescript dependency when analyzing pure JS project
const tsParser = require("@typescript-eslint/parser");
const result = tsParser.parseForESLint(fileContent, {
...PARSER_CONFIG_MODULE,
filePath: filePath,
project: tsConfigs,
loggerFn: console.log,
});
return new SourceCode({ ...result, parserServices: result.services, text: fileContent });
} catch (exception) {
return {
line: exception.lineNumber,
message: exception.message,
code: parseExceptionCodeOf(exception.message),
};
}
}
function runTSParser(directory, files) {
for (const file of files) {
const result = tsParser.parse(
fs.readFileSync(path.join(__dirname, file), 'utf8'),
{
comment: true,
tokens: true
}
);
if (result.type !== 'Program') {
throw new Error('something went wrong');
}
}
}
import defaultParserInterface from './utils/defaultESTreeParserInterface';
import pkg from '@typescript-eslint/parser/package.json';
const ID = '@typescript-eslint/parser';
export default {
...defaultParserInterface,
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage || 'https://typescript-eslint.io/',
locationProps: new Set(['loc', 'start', 'end', 'range']),
loadParser(callback) {
require(['@typescript-eslint/parser'], callback);
},
parse(parser, code, options) {
return parser.parse(code, options);
},
getDefaultOptions() {
return {
range: true,
loc: false,
tokens: false,
comment: false,
import defaultParserInterface from './utils/defaultESTreeParserInterface';
import pkg from '@typescript-eslint/parser/package.json';
const ID = '@typescript-eslint/parser';
export default {
...defaultParserInterface,
id: ID,
displayName: ID,
version: pkg.version,
homepage: pkg.homepage || 'https://typescript-eslint.io/',
locationProps: new Set(['loc', 'start', 'end', 'range']),
loadParser(callback) {
require(['@typescript-eslint/parser'], callback);
},
parse(parser, code, options) {
return parser.parse(code, options);
},
getDefaultOptions() {
return {
range: true,
loc: false,
tokens: false,