UNPKG

@sourcegraph/eslint-plugin-sourcegraph

Version:
36 lines 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getWrappedNode = exports.getNodeWrapper = exports.getSourceFileFromContext = void 0; const experimental_utils_1 = require("@typescript-eslint/experimental-utils"); const ts_morph_1 = require("ts-morph"); function getSourceFileFromContext(context) { const { program } = experimental_utils_1.ESLintUtils.getParserServices(context); const currentFilePath = program.getRootFileNames().find(filePath => { return filePath.includes(context.getFilename()); }) || context.getFilename(); const sourceFile = program.getSourceFile(currentFilePath); if (!sourceFile) { throw new Error('Source file not found!'); } return sourceFile; } exports.getSourceFileFromContext = getSourceFileFromContext; function getNodeWrapper(context) { const sourceFile = getSourceFileFromContext(context); const wrappedSourceFile = (0, ts_morph_1.createWrappedNode)(sourceFile); const { esTreeNodeToTSNodeMap } = experimental_utils_1.ESLintUtils.getParserServices(context); return function wrapNode(node, ctor) { const originalNode = esTreeNodeToTSNodeMap.get(node); return new ctor(wrappedSourceFile._context, originalNode, wrappedSourceFile); }; } exports.getNodeWrapper = getNodeWrapper; function getWrappedNode(context, node) { const parserServices = experimental_utils_1.ESLintUtils.getParserServices(context); const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node); return (0, ts_morph_1.createWrappedNode)(originalNode, { sourceFile: originalNode.getSourceFile(), }); } exports.getWrappedNode = getWrappedNode; //# sourceMappingURL=getWrappedNode.js.map