@putout/operate
Operate on AST for 🐊 Putout
Last updated 7 months ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/operate 
SYNC missed versions from official npm registry.

Operate NPM version Coverage Status

Manipulate with 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

rename

Let's suppose you have code

const {hello} = one;
hello();

You want to change to:

const {world} = one;
world();

Use:

rename(path, 'hello', 'world');

renameProperty

Let's suppose you have code

const {hello: world} = one;

You want to change to:

const {world} = one;

Use:

renameProperty(path, 'hello', 'world');

setLiteralValue(path: Path | Node, value: string)

Set raw and value of a literal.

isSimple(path: Path | Node)

Check if path is:

  • Literal;
  • Identifier;
  • MemberExpression;
  • OptionalMemberExpression;

extract(path)

Extract node value according to it's type::

  • if it is Identifier or JSXIdentifier or JSXAttribute return name;
  • if it is any type of Literal or JSXText 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;
  • if it is TSTypeReference return typeName.name;
  • if it is MemberExpression return object.property;
  • if it is ArrayExpression return element1,element2,...,elementN;
  • throw in other cases

insertAfter(path, node)

Safe way to insert node after path without duplicating comments.

insertBefore(path, node)

Safe way to insert node before path.

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.

path.toString();
// returns const [a, b] = c;
remove(path.get('declarations.0.id.0'));

path.toString(); // returns const [, b] = c;

getLiteralRaw(path: Path|Node)

Get raw or extra.raw, which can be received from template methods.

getPathAfterRequires(body)

Get next path after latest require:

const programPath = path.scope.getProgramParent().path;
const afterRequirePath = getPathAfterRequires(programPath.get('body'));

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'`];
    },
});

getExportDefault(path)

Get export default or null.

isESM(path)

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

getProperty(path: Path, name: string)

Get property from ObjectExpression path:

const homepagePath = getProperties(__aPath, 'homepage');

getProperties(path: Path, names: string[])

Get properties from ObjectExpression path and add a Path suffix to each result:

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

traverseProperties(path: Path | Node, name: string, {firstLevel?: false})

Traverse list of properties from ObjectExpression.

const object = template.ast('x({"a": "b"})');
const [propertyPath] = traverseProperties(object, 'a');

License

MIT

Current Tags

  • 12.9.2                                ...           latest (5 months ago)

91 Versions

  • 12.9.2                                ...           5 months ago
  • 12.9.1                                ...           5 months ago
  • 12.9.0                                ...           5 months ago
  • 12.8.0                                ...           5 months ago
  • 12.7.1                                ...           6 months ago
  • 12.7.0                                ...           6 months ago
  • 12.6.0                                ...           7 months ago
  • 12.5.0                                ...           7 months ago
  • 12.4.0                                ...           7 months ago
  • 12.3.0                                ...           9 months ago
  • 12.2.0                                ...           9 months ago
  • 12.1.0                                ...           a year ago
  • 12.0.0                                ...           a year ago
  • 11.4.0                                ...           a year ago
  • 11.3.0                                ...           a year ago
  • 11.2.0                                ...           a year ago
  • 11.1.2                                ...           a year ago
  • 11.1.1                                ...           a year ago
  • 11.1.0                                ...           a year ago
  • 11.0.0                                ...           a year ago
  • 10.3.0                                ...           a year ago
  • 10.2.0                                ...           a year ago
  • 10.1.0                                ...           a year ago
  • 10.0.2                                ...           a year ago
  • 10.0.1                                ...           a year ago
  • 10.0.0                                ...           a year ago
  • 9.3.0                                ...           a year ago
  • 9.2.0                                ...           a year ago
  • 9.1.0                                ...           a year ago
  • 9.0.0                                ...           a year ago
  • 8.15.0                                ...           a year ago
  • 8.14.0                                ...           a year ago
  • 8.13.0                                ...           a year ago
  • 8.12.0                                ...           a year ago
  • 8.11.0                                ...           a year ago
  • 8.10.0                                ...           2 years ago
  • 8.9.1                                ...           2 years ago
  • 8.9.0                                ...           2 years ago
  • 8.8.0                                ...           2 years ago
  • 8.7.0                                ...           2 years ago
  • 8.6.1                                ...           2 years ago
  • 8.6.0                                ...           2 years ago
  • 8.5.0                                ...           2 years ago
  • 8.4.0                                ...           2 years ago
  • 8.3.0                                ...           3 years ago
  • 8.2.0                                ...           3 years ago
  • 8.1.0                                ...           3 years ago
  • 8.0.0                                ...           3 years ago
  • 7.1.0                                ...           3 years ago
  • 7.0.0                                ...           3 years ago
  • 6.15.0                                ...           3 years ago
  • 6.14.0                                ...           3 years ago
  • 6.13.0                                ...           3 years ago
  • 6.12.2                                ...           3 years ago
  • 6.12.0                                ...           3 years ago
  • 6.11.0                                ...           3 years ago
  • 6.10.0                                ...           3 years ago
  • 6.9.1                                ...           3 years ago
  • 6.9.0                                ...           3 years ago
  • 6.8.0                                ...           3 years ago
  • 6.7.0                                ...           3 years ago
  • 6.6.0                                ...           3 years ago
  • 6.5.2                                ...           3 years ago
  • 6.5.1                                ...           3 years ago
  • 6.5.0                                ...           3 years ago
  • 6.4.0                                ...           3 years ago
  • 6.3.0                                ...           3 years ago
  • 6.2.0                                ...           3 years ago
  • 6.1.4                                ...           3 years ago
  • 6.1.3                                ...           3 years ago
  • 6.1.2                                ...           3 years ago
  • 6.1.1                                ...           4 years ago
  • 6.1.0                                ...           4 years ago
  • 6.0.0                                ...           4 years ago
  • 5.0.1                                ...           4 years ago
  • 5.0.0                                ...           4 years ago
  • 4.1.1                                ...           4 years ago
  • 4.1.0                                ...           4 years ago
  • 4.0.0                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.8.0                                ...           5 years ago
  • 1.7.0                                ...           5 years ago
  • 1.6.0                                ...           5 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.0                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE