How to use esquery - 10 common examples

To help you get started, we’ve selected a few esquery examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TradeMe / tractor / plugins / mocha-specs / src / tractor / client / parsers / test-parser-service.js View on Github external
// Module:
import { MochaSpecsModule } from '../mocha-specs.module';

// Dependencies:
import { match, parse } from 'esquery';
import '../models/test';
import './step-parser-service';

// Queries:
const ONLY_QUERY = parse('ExpressionStatement > CallExpression > MemberExpression > Identifier[name="only"]');
const SKIP_QUERY = parse('ExpressionStatement > CallExpression > MemberExpression > Identifier[name="skip"]');
const STEP_QUERY = parse('FunctionExpression > BlockStatement > ExpressionStatement[expression.left.name="step"]');
const FLAKEY_QUERY = parse('FunctionExpression > BlockStatement > ExpressionStatement > CallExpression:has(MemberExpression[object.type="ThisExpression"][property.name="retries"])');

function TestParserService (
    TestModel,
    astCompareService,
    stepParserService
) {
    return { parse };

    function parse (spec, astObject, meta) {
        let test = new TestModel(spec);

        if (meta && meta.name) {
            test.name = meta.name;
        }
github DefinitelyTyped / DefinitelyTyped / types / esquery / esquery-tests.ts View on Github external
return false;
}`);

const s = 'FunctionDeclaration !VariableDeclaration > VariableDeclarator[init.value > 3]';

// $ExpectType Selector
const selector = esquery.parse(s);

// $ExpectType Node[]
const nodes = esquery.query(AST, s);

// $ExpectType Node[]
esquery(AST, s);

// $ExpectError
esquery.parse(3);

// $ExpectType Node[]
esquery.match(AST, selector);

// $ExpectError
esquery.match(AST, 'VariableDeclarator');

// $ExpectType boolean
esquery.matches(nodes[0], selector, esquery(AST, 'FunctionDeclaration'));

// $ExpectError
esquery.match(3, selector);

switch (selector.type) {
    case 'adjacent':
        // $ExpectType SubjectSelector
github TradeMe / tractor / plugins / mocha-specs / src / tractor / client / parsers / test-parser-service.js View on Github external
// Module:
import { MochaSpecsModule } from '../mocha-specs.module';

// Dependencies:
import { match, parse } from 'esquery';
import '../models/test';
import './step-parser-service';

// Queries:
const ONLY_QUERY = parse('ExpressionStatement > CallExpression > MemberExpression > Identifier[name="only"]');
const SKIP_QUERY = parse('ExpressionStatement > CallExpression > MemberExpression > Identifier[name="skip"]');
const STEP_QUERY = parse('FunctionExpression > BlockStatement > ExpressionStatement[expression.left.name="step"]');
const FLAKEY_QUERY = parse('FunctionExpression > BlockStatement > ExpressionStatement > CallExpression:has(MemberExpression[object.type="ThisExpression"][property.name="retries"])');

function TestParserService (
    TestModel,
    astCompareService,
    stepParserService
) {
    return { parse };

    function parse (spec, astObject, meta) {
        let test = new TestModel(spec);

        if (meta && meta.name) {
            test.name = meta.name;
        }

        let [only] = match(astObject, ONLY_QUERY);
github DefinitelyTyped / DefinitelyTyped / types / esquery / esquery-tests.ts View on Github external
const nodes = esquery.query(AST, s);

// $ExpectType Node[]
esquery(AST, s);

// $ExpectError
esquery.parse(3);

// $ExpectType Node[]
esquery.match(AST, selector);

// $ExpectError
esquery.match(AST, 'VariableDeclarator');

// $ExpectType boolean
esquery.matches(nodes[0], selector, esquery(AST, 'FunctionDeclaration'));

// $ExpectError
esquery.match(3, selector);

switch (selector.type) {
    case 'adjacent':
        // $ExpectType SubjectSelector
        selector.left;
        // $ExpectType SubjectSelector
        selector.right;
        break;

    case 'attribute':
        // $ExpectType string
        selector.name;
}
github DefinitelyTyped / DefinitelyTyped / types / esquery / esquery-tests.ts View on Github external
import * as esquery from 'esquery';

const AST = esprima.parseScript(`const x = 2;
function f (n) {
    const y = 4;
    if (n > y) return true;
    return false;
}`);

const s = 'FunctionDeclaration !VariableDeclaration > VariableDeclarator[init.value > 3]';

// $ExpectType Selector
const selector = esquery.parse(s);

// $ExpectType Node[]
const nodes = esquery.query(AST, s);

// $ExpectType Node[]
esquery(AST, s);

// $ExpectError
esquery.parse(3);

// $ExpectType Node[]
esquery.match(AST, selector);

// $ExpectError
esquery.match(AST, 'VariableDeclarator');

// $ExpectType boolean
esquery.matches(nodes[0], selector, esquery(AST, 'FunctionDeclaration'));
github TradeMe / tractor / plugins / mocha-specs / src / tractor / client / parsers / mock-request-parser-service.js View on Github external
[, mockRequest.action] = action.name.match(MOCK_REQUEST_ACTION_REGEX);
        }

        let [url, options] = astObject.arguments;
        if (url && options) {
            // We escape parts of the RegExp when we save it, so we need to un-escape
            // it before it goes back into the UI:
            mockRequest.url = url.regex.pattern.replace(/\\\?/g, '?');

            let [passThrough] = match(options, MOCK_REQUEST_PASSTHROUGH_QUERY);
            if (passThrough) {
                mockRequest.passThrough = passThrough.value;
                return;
            }
    
            let [data] = match(options, MOCK_REQUEST_DATA_QUERY);
            if (data) {
                mockRequest.data = mockRequest.test.spec.availableMockRequests.find(mockRequest => mockRequest.variableName === data.name);
            }
    
            let [status] = match(options, MOCK_REQUEST_STATUS_QUERY);
            if (status) {
                mockRequest.status = status.value;
            }
    
            let headers = match(options, MOCK_REQUEST_HEADERS_QUERY);
            headers.forEach(headerASTObject => {
                let header = headerParserService.parse(mockRequest, headerASTObject);
                mockRequest.headers.push(header);
            });
        }
    }
github TradeMe / tractor / plugins / mocha-specs / src / tractor / client / parsers / mock-request-parser-service.js View on Github external
// Module:
import { MochaSpecsModule } from '../mocha-specs.module';

// Constants:
const MOCK_REQUEST_ACTION_REGEX = /when(.*)/;

// Dependencies:
import { match, parse } from 'esquery';
import '../models/mock-request';
import './header-parser-service';

// Queries:
const MOCK_REQUEST_ACTION_QUERY = parse('CallExpression > MemberExpression > Identifier[name=/^when/]');
const MOCK_REQUEST_PASSTHROUGH_QUERY = parse('Property[key.name="passThrough"] > Literal');
const MOCK_REQUEST_DATA_QUERY = parse('Property[key.name="body"] > Identifier[name!="body"]');
const MOCK_REQUEST_STATUS_QUERY = parse('Property[key.name="status"] > Literal');
const MOCK_REQUEST_HEADERS_QUERY = parse('Property[key.name="headers"] > ObjectExpression > Property');

function MockRequestParserService (
    SpecMockRequestModel,
    headerParserService
) {
    return { parse };

    function parse (test, astObject) {
        let mockRequest = new SpecMockRequestModel(test);
        _parseMockRequest(mockRequest, astObject);
        return mockRequest;
    }
github TradeMe / tractor / plugins / page-objects / src / tractor / client / parsers / interaction-parser.service.js View on Github external
import { PageObjectsModule } from '../page-objects.module';

// Constants:
import { ELEMENT_GROUP_SELECTOR_ARGUMENT } from '../models/meta/element-group-selector-argument';

// Dependencies:
import { match, parse } from 'esquery';
import '../models/interaction';
import './action-argument-parser.service';

// Queries:
const ACTION_CALL_EXPRESSION_QUERY = 'CallExpression[callee.object.name="result"] > FunctionExpression > BlockStatement >';
const ACTION_CALL_EXPRESSION_NOT_OPTIONAL_QUERY = parse(`${ACTION_CALL_EXPRESSION_QUERY} ReturnStatement > CallExpression[callee.property.name!="then"]`);
const ACTION_CALL_EXPRESSION_OPTIONAL_QUERY = parse(`${ACTION_CALL_EXPRESSION_QUERY} VariableDeclaration CallExpression`);
const ACTION_MEMBER_EXPRESSION_QUERY = parse('MemberExpression[object.name!="self"][property.type="Identifier"]');
const ELEMENT_MEMBER_EXPRESSION_QUERY = parse('MemberExpression > MemberExpression[object.type="Identifier"][property.type="Identifier"]');
const ELEMENT_GROUP_MEMBER_EXPRESSION_QUERY = parse('MemberExpression > CallExpression > MemberExpression[object.type="Identifier"][property.type="Identifier"]');
const ELEMENT_GROUP_SELECTOR_QUERY = parse('CallExpression > MemberExpression > CallExpression');
const PLUGIN_MEMBER_EXPRESSION_QUERY = parse('MemberExpression[object.type="Identifier"][property.type="Identifier"]');

function InteractionParserService (
    InteractionModel,
    astCompareService,
    actionArgumentParserService
) {
    const QUERY_SELECTOR = {
        notOptional: ACTION_CALL_EXPRESSION_NOT_OPTIONAL_QUERY,
        optional: ACTION_CALL_EXPRESSION_OPTIONAL_QUERY
    };
    const QUERY_HANDLER = {
        notOptional: _interactionParser,
        optional: _optionalInteractionParser
github TradeMe / tractor / plugins / mocha-specs / src / tractor / client / parsers / assertion-parser-service.js View on Github external
// Module:
import { MochaSpecsModule } from '../mocha-specs.module';

// Dependencies:
import { match, parse } from 'esquery';
import '../models/assertion';

// Queries:
const ASSERTION_ARGUMENT_QUERY = parse('ReturnStatement > CallExpression > Literal');
const ASSERTION_CONDITION_QUERY = parse('ReturnStatement > CallExpression > MemberExpression > Identifier');

function AssertionParserService (
    SpecAssertionModel,
) {
    return { parse };

    function parse (test, astObject) {
        let assertion = new SpecAssertionModel(test);
        _parseAssertion(assertion, astObject);
        return assertion;
    }


    function _parseAssertion (assertion, astObject) {
        let [argument] = match(astObject, ASSERTION_ARGUMENT_QUERY);
        assertion.expectedResult.value = argument && argument.value != null ? argument.value.toString() : null;
github TradeMe / tractor / plugins / mocha-specs / src / tractor / client / parsers / step-parser-service.js View on Github external
// Module:
import { MochaSpecsModule } from '../mocha-specs.module';

// Dependencies:
import { match, parse } from 'esquery';
import '../models/interaction';
import '../models/step';
import '../models/step-argument';
import './assertion-parser-service';
import './mock-request-parser-service';

// Queries:
const STEP_QUERY = parse('AssignmentExpression > CallExpression[callee.object.name="step"][callee.property.name="then"] > FunctionExpression > BlockStatement');
const PAGE_OBJECT_QUERY = parse('AssignmentExpression > Identifier[name!="element"]');
const SELECTOR_QUERY = parse('AssignmentExpression:has(MemberExpression[object.name="element"])');
const GROUP_SELECTOR_QUERY = parse('AssignmentExpression > CallExpression[callee.object.name="element"]');
const ELEMENT_SELECTOR_QUERY = parse('AssignmentExpression > MemberExpression[object.name="element"]');
const ASSERTION_QUERY = parse(`ReturnStatement > CallExpression > MemberExpression[property.name=/equal|contain/] > MemberExpression[property.name="eventually"] > MemberExpression[property.name="to"] > CallExpression[callee.name="expect"] > CallExpression`);
const INTERACTION_QUERY = parse('ReturnStatement > CallExpression[callee.object.name="element"]');
const MOCK_REQUEST_QUERY = parse('ReturnStatement > CallExpression[callee.object.name="mockRequests"]');

function StepParserService (
    SpecInteractionModel,
    SpecStepModel,
    StepArgumentModel,
    assertionParserService,
    mockRequestParserService,
    astCompareService
) {
    return { parse };

esquery

A query library for ECMAScript AST using a CSS selector like query language.

BSD-3-Clause
Latest version published 5 months ago

Package Health Score

84 / 100
Full package analysis