UNPKG

@codeque/core

Version:

Multiline code search for every language. Structural code search for JavaScript, TypeScript, HTML and CSS

191 lines (157 loc) 5.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.esprimaParserSettings = exports.default = void 0; var _esprima = require("esprima"); var _utils = require("../../utils"); var _JSFamilyCommon = require("../_common/JSFamilyCommon"); var _afterWildcardsComparators = require("./afterWildcardsComparators"); var _beforeWildcardsComparators = require("./beforeWildcardsComparators"); var _common = require("./common"); const getProgramNodeFromRootNode = rootNode => rootNode; // root node is program node const getProgramBodyFromRootNode = rootNode => { return getProgramNodeFromRootNode(rootNode).body; }; const unwrapExpressionStatement = node => { if (typeof node !== 'object') { return node; } if (node.type === 'ExpressionStatement') { return node.expression; } return node; }; const createBlockStatementNode = (body, position) => ({ type: 'BlockStatement', body, loc: position.loc, range: [position.start, position.end] }); const isNode = maybeNode => { return typeof maybeNode?.type === 'string'; }; const astPropsToSkip = ['loc', 'range', 'raw', 'trailingComments', 'leadingComments', 'comments', 'tail', // Support for partial matching of template literals 'parent', // in eslint there is parent prop in node { type: 'ArrowFunctionExpression', key: 'expression' } // flag on ArrowFunctionExpression ]; const parseCode = (code, filePath = '') => { const settings = { jsx: true, range: true, loc: true }; const maybeWrappedJSON = /\.json$/.test(filePath) ? `(${code})` : code; const ast = (0, _esprima.parseModule)(maybeWrappedJSON, settings); return ast; }; const sanitizeTemplateElementValue = ({ raw, cooked }) => { return { raw: (0, _utils.normalizeText)(raw), cooked: (0, _utils.normalizeText)(cooked) }; }; const nodeValuesSanitizers = { ['JSXText']: { value: _utils.normalizeText, raw: _utils.normalizeText }, ['TemplateElement']: { value: sanitizeTemplateElementValue } }; const getSanitizedNodeValue = (nodeType, valueKey, value) => { const valueSanitizer = nodeValuesSanitizers?.[nodeType]?.[valueKey]; if (valueSanitizer) { return valueSanitizer(value); } return value; }; const shouldCompareNode = node => { if (node.type === 'JSXText') { const value = getSanitizedNodeValue('JSXText', 'value', node.value); return value.length > 0; } return true; }; const compareNodesBeforeWildcardsComparison = (...nodeComparatorParams) => { return (0, _utils.runNodesComparators)(_beforeWildcardsComparators.beforeWildcardsComparators, nodeComparatorParams); }; const compareNodesAfterWildcardsComparison = (...nodeComparatorParams) => { return (0, _utils.runNodesComparators)(_afterWildcardsComparators.afterWildcardsComparators, nodeComparatorParams); }; const isIdentifierNode = node => _common.identifierNodeTypes.includes((0, _common.getNodeType)(node)); const isFirstCharStringStart = str => str.charAt(0) === `'` || str.charAt(0) === `"`; const stringLikeLiteralUtils = { isStringLikeLiteralNode: node => node.type === 'Literal' && isFirstCharStringStart(node.raw) || node.type === 'TemplateElement' || node.type === 'JSXText', getStringLikeLiteralValue: node => { if (node.type === 'TemplateElement') { const { raw } = sanitizeTemplateElementValue(node.value); return raw; } // (node.type === 'Literal' || node.type === 'JSXText' return (0, _utils.normalizeText)(node.value); } }; const numericLiteralUtils = { isNumericLiteralNode: node => node.type === 'Literal' && !isFirstCharStringStart(node.raw), getNumericLiteralValue: node => node.raw }; const programNodeAndBlockNodeUtils = { isProgramNode: node => node.type === 'Program', isBlockNode: node => node.type === 'BlockStatement', programNodeBodyKey: 'body', blockNodeBodyKey: 'body' }; const getNodePosition = node => ({ start: node.range[0], end: node.range[1], loc: node.loc }); const getParseErrorLocation = e => ({ line: e.lineNumber ?? 0, column: e.column ?? 0 }); const alternativeNodeTypes = { Identifier: _common.identifierNodeTypes, ChainExpression: ['MemberExpression'], MemberExpression: ['ChainExpression'], BlockStatement: ['Program'] }; const esprimaParserSettings = { supportedExtensions: _JSFamilyCommon.supportedExtensions, parseCode, isNode, isIdentifierNode, identifierNodeTypes: _common.identifierNodeTypes, astPropsToSkip, getProgramBodyFromRootNode, getProgramNodeFromRootNode, getIdentifierNodeName: _common.getIdentifierNodeName, setIdentifierNodeName: _common.setIdentifierNodeName, getNodeType: _common.getNodeType, unwrapExpressionStatement, createBlockStatementNode, getSanitizedNodeValue, shouldCompareNode, wildcardUtils: _common.wildcardUtils, compareNodesBeforeWildcardsComparison, compareNodesAfterWildcardsComparison, identifierTypeAnnotationFieldName: 'typeAnnotation', stringLikeLiteralUtils, numericLiteralUtils, programNodeAndBlockNodeUtils, getNodePosition, getParseErrorLocation, alternativeNodeTypes }; exports.esprimaParserSettings = esprimaParserSettings; var _default = esprimaParserSettings; exports.default = _default;