Operate on AST for 🐊 Putout
npm install @putout/operate[NPMURL]: https://npmjs.org/package/putout "npm"
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/operate.svg?style=flat&longCache=true
[CoverageURL]: https://coveralls.io/github/coderaiser/putout?branch=master
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/putout/badge.svg?branch=master&service=github
Manipulate with path nodes and keep comments and loc information.
```
npm i @putout/operate
If you write plugin for putout you already have operator in putout, all examples will get operator from putout, but you can use direct require('@putout/operate') as well.
Let's suppose you have code
`js`
const {hello} = one;
hello();
You want to change to:
`js`
const {world} = one;
world();
Use:
`js`
rename(path, 'hello', 'world');
Let's suppose you have code
`js`
const {hello: world} = one;
You want to change to:
`js`
const {world} = one;
Use:
`js`
renameProperty(path, 'hello', 'world');
Set raw and value of a literal.
Check if path is:
- Literal;Identifier
- ;MemberExpression
- ;OptionalMemberExpression
- ;
Extract node value according to it's type::
- if it is Identifier or JSXIdentifier or JSXAttribute return name;Literal
- if it is any type of or JSXText return value;RegExp
- if it is return pattern;TemplateLiteral
- if it is return qusis[0].value.raw;TemplateElement
- if it is return value.raw;ClassMethod
- if it is return key;TSTypeReference
- if it is return typeName.name;TSTypeParameter
- if it is return name;MemberExpression
- if it is return object.property;ArrayExpression
- if it is return element1,element2,...,elementN;TSAsExpression
- if it is return key.expression;throw
- in other cases
Safe way to insert node after path without duplicating comments.
Safe way to insert node before path.
`js
const {operator, types} = require('putout');
const {replaceWith} = operator;
const {ContinueStatement} = types;
replaceWith(path, continueStatement());
`
`js
const {operator, types} = require('putout');
const {replaceWithMultiple} = operator;
const {
expressionStatement,
continueStatement,
} = types;
replaceWithMultiple(path, [
expressionStatement(path.node.argument),
ContinueStatement,
]);
`
Check if currentPath is module.exports expression.
Can be used to convert node to expression when building new nodes.
Remove node, preserve comments.
`js
path.toString();
// returns const [a, b] = c;
remove(path.get('declarations.0.id.0'));
path.toString(); // returns const [, b] = c;
`
Get raw or extra.raw, which can be received from template methods.
Get next path after latest require:
`js`
const programPath = path.scope.getProgramParent().path;
const afterRequirePath = getPathAfterRequires(programPath.get('body'));
Get next path after latest ImportDeclaration:
`js`
const programPath = path.scope.getProgramParent().path;
const afterImportsPath = getPathAfterImports(programPath.get('body'));
Get binding (declaration of variable) by name using starting from path and move up.
`js`
getBinding(path, 'hello');
Get binding path by name using starting from path and move up.
`js
const bindingPath = getBindingPath(path, 'hello');
module.exports.match = () => ({
'typeof __a === "__b"': ({__a}, path) => {
// when __a declared proceed to replace
return getBindingPath(path, __a);
},
});
`
Computes value of expression:
For code like this:
`jstypeof __a === 'function'
const bodies = {
function: ,
};
module.exports.replace = () => ({
[bodies.function]: 'isFn(__a)',
});
`
You can compute value of bodies.function:
`js
const {parse, operator} = require('putout');
const {traverse, compute} = operator;
traverse({
'__a.__b': (path) => {
const [computed, value] = compute(path);
// returns
[true, typeof __a === 'function'];`
},
});
Get export default or null.
Check if given source is ESM search for ImportDeclaration and ExportDeclaration nodes.
Get property from ObjectExpression path:
`js`
const homepagePath = getProperty(__aPath, 'homepage');
Get properties from ObjectExpression path and add a Path suffix to each result:
`js`
const {homepagePath} = getProperties(__aPath, ['homepage']);
Traverse list of properties from ObjectExpression.
`js``
const object = template.ast('x({"a": "b"})');
const [propertyPath] = traverseProperties(object, 'a');
MIT