JSPM

  • Created
  • Published
  • Downloads 27134
  • Score
    100M100P100Q149222F
  • License MIT

operate on ast

Package Exports

  • @putout/operate
  • @putout/operate/lib/operate.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@putout/operate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Operate NPM version Build Status Coverage Status

operate provide you a way to manipulate path nodes and keep comments and loc information.

Install

npm i @putout/operate

If you write plugin for putout you already have operator in putout, all exampes will get operator from putout, but you can use direct require('@putout/operate') as well.

API

extract(path)

Extract node value according to it's type::

  • if it is Identifier return name;
  • if it is any type of Literal return value;
  • if it is RegExp return pattern;
  • if it is TemplateLiteral return qusis[0].value.raw;
  • if it is TemplateElement return value.raw;
  • if it is ClassMethod return key;
  • throw in other cases

replaceWith(path, node)

const {
    operator,
    types,
} = require('putout');

const {replaceWith} = operator;
const {ContinueStatement} = types;

replaceWith(path, ContinueStatement());

replaceWithMultiple(path, nodes)

const {
    operator,
    types,
} = require('putout');

const {replaceWithMultiple} = operator;
const {
    ExpressionStatement,
    ContinueStatement,
} = types;

replaceWithMultiple(path, [
    ExpressionStatement(path.node.argument),
    ContinueStatement,
]);

isModuleExports(path)

Check if currentPath is module.exports expression.

toExpression(node)

Can be used to convert node to expression when building new nodes.

remove(path)

Remove node, preserve comments.

getPathAfterImports(body)

Get next path after latest ImportDeclaration:

const programPath =  path.scope.getProgramParent().path;
const afterImportsPath = getPathAfterImports(programPath.get('body'));

getBinding(path, name: string | Node)

Get binding (declaration of variable) by name using starting from path and move up.

getBinding(path, 'hello');

getBindingPath(path, name: string | Node)

Get binding path by name using starting from path and move up.

const bindingPath = getBindingPath(path, 'hello');

module.exports.match = () => ({
    'typeof __a === "__b"': ({__a}, path) => {
        // when __a declared proceed to replace
        return getBindingPath(path, __a);
    },
});

compute(path)

Computes value of expression:

For code like this:

const bodies = {
    function: `typeof __a === 'function'`,
};

module.exports.replace = () => ({
    [bodies.function]: 'isFn(__a)',
});

You can compute value of bodies.function:

const {parse, operator} = require('putout');
const {traverse, compute} = operator;

traverse({
    '__a.__b': (path) => {
        const [computed, value] = compute(path);
        // returns
        [true, "typeof __a === 'function'"];
    },
});

isESM(path)

Check if given source is ESM search for ImportDeclaration and ExportDeclaration nodes.

findProperties(path: Path, names: string[]

Find properties in ObjectExpression path and add suffix Path when found:

const {homepagePath} = findProperties(__aPath, ['homepage']);

License

MIT