14.2.0 • Published 29 days ago

@putout/compare v14.2.0

Weekly downloads
83
License
MIT
Repository
github
Last release
29 days ago

@putout/compare NPM version

Compare AST-nodes according to 🦎PutoutScript.

Install

npm i @putout/compare

API

getTemplateValues(node, template)

Get template values from node according to 🦎PutouScript template.

  • node - AST-node or code that will be generated;
  • template - 🦎PutouScript;
const {operator} = require('putout');
const {template} = operator;
const node = template.ast('const [] = a');

getTemplateValues(node, 'const __array = array');
// returns
({
    __array: {
        type: 'ArrayPattern',
    },
});

compare(node: string | Node, template: string | Node , options: Options)

  • node - AST-node or code that will be generated;
  • template - AST-node with support of template variables.
  • options - (optional) - object with properties:
    • findUp (default: true) - find up template node;

compareAll(node: string | Node, templates: string[] | Node|Nodes[], , options: Options)

Compare nodes feats templates.

compareAny(node: string | Node, templates: string[] | Node|Nodes[], , options: Options)

Compare any nodes that feats one of templates

compareAny(path, 'const __a = __b', {
    findUp: false,
});

Supported template variables:

__

Any node.

compare('const x = data', 'const __ = __');
compare('const {x} = data', 'const __ = __');
compare('const x = {data}', 'const __ = __');
compare('<h1>hello</h1>', '<h1>__</h1>');
// returns
true;
__object

ObjectPattern or ObjectExpression with any count of properties.

compare('const {} = data', 'const __object = __');
compare('const {hello} = data', 'const __object = __');
// returns
true;
__array

ArrayPattern or ArrayExpression with any count of elements.

compare('const [] = data', 'const __array = __');
compare('const [hello] = data', 'const __array = __');
compare('const hello = [data]', 'const __ = __array');
// returns
true;
args, args__a

Any count of arguments:

compare('(a, b, c) => {}', '(__args) => {}');
compare('(a, b) => {}', '(__args) => {}');
compare('() => {}', '(__args) => {}');
// returns
true;

Or linked arguments:

compare('((a) => fn(a))(value)', '((__args__a) => __c(__args__a))(__args__b)');
// returns
true;

compare('((a) => fn(42))(value)', '((__args__a) => __c(__args__a))(__args__b)');
// returns
false;
__imports

Any count of import specifiers:

compare('import React, {Component} from "react"', 'import __imports from "react"');
// returns
true;
__exports

Any count of export specifiers:

compare('export {scan, fix, report}', 'export {__exports}');
// returns
true;
"__"

Any string literal.

compare('const a = "hello"', 'const __ = "__"');
__a

Linked node.

compare('const __a = "hello"', 'const __a = "hello"');
"__a"

Linked string literal.

compare('const a = "hello"', 'const __a = "__b"');
__a

Linked template literal.

compare('const a = `hello`', 'const __a = `__b`');
// returns
true;
__body

Any BlockStatement.

compare('const a = () => 0', 'const a = () => __body');
// returns
false;

compare('const a = () => {}', 'const a = () => __body');
// returns
true;
__jsx_children

Any count of children of JSXElement:

compare('<div hello="world"></div>', '<div hello="world">__jsx_children</div>');
// returns
true;

compare('<div hello="world"><span>hi</span></div>', '<div hello="world">__jsx_children</div>');
// returns
true;
__jsx_attributes

Any count of attributes of JSXElement:

compare('<div hello="world"></div>', '<__a __jsx_attributes/>');
// returns
true;
__nop

Any Function with no arguments and empty body;

compare('const a = () => {}', 'const __a = __nop');
// returns
true;

compare('const a = async () => {}', 'const a = __nop');
// returns
true;
__identifier

Any Identifier

compare('const a = 5', 'const __identifier = 5');
// returns
true;
__bool

Any Boolean

compare('const a = true', 'const a = __bool');
// returns
true;
/__a/

Any regexp

compare('const a = /hello/g', 'const a = /__a/');
// returns
true;

License

MIT

14.2.0

29 days ago

14.1.0

4 months ago

14.0.1

5 months ago

14.0.2

5 months ago

14.0.0

5 months ago

12.0.3

9 months ago

12.0.4

9 months ago

12.0.0

9 months ago

12.0.1

9 months ago

12.0.2

9 months ago

11.2.0

9 months ago

13.0.0

8 months ago

13.0.1

8 months ago

11.1.0

10 months ago

13.1.0

7 months ago

12.2.1

9 months ago

12.2.0

9 months ago

11.0.0

10 months ago

13.2.0

6 months ago

12.1.0

9 months ago

10.0.0

11 months ago

9.15.1

11 months ago

9.15.2

11 months ago

9.15.0

11 months ago

10.2.0

11 months ago

9.14.0

12 months ago

10.1.0

11 months ago

9.13.0

1 year ago

9.12.0

1 year ago

9.12.1

1 year ago

9.10.0

1 year ago

9.9.0

1 year ago

9.11.0

1 year ago

9.8.0

1 year ago

9.6.2

1 year ago

9.6.1

1 year ago

9.6.0

1 year ago

9.7.0

1 year ago

9.5.0

1 year ago

9.4.1

2 years ago

9.4.0

2 years ago

9.3.0

2 years ago

9.2.1

2 years ago

9.1.0

2 years ago

9.2.0

2 years ago

8.6.0

2 years ago

8.5.0

2 years ago

8.7.0

2 years ago

9.0.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.13.0

3 years ago

7.12.1

3 years ago

7.11.0

3 years ago

7.12.0

3 years ago

7.10.0

3 years ago

7.9.0

3 years ago

7.8.0

3 years ago

7.7.0

3 years ago

7.6.1

3 years ago

7.6.0

3 years ago

7.5.0

3 years ago

7.4.0

3 years ago

7.3.0

3 years ago

7.2.1

3 years ago

7.2.0

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.2

3 years ago

7.0.1

3 years ago

7.0.0

3 years ago

6.1.1

3 years ago

6.1.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.2.1

4 years ago

5.2.0

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.7.0

4 years ago

4.6.1

4 years ago

4.6.0

4 years ago

4.5.0

4 years ago

4.4.1

4 years ago

4.4.0

4 years ago

4.3.1

4 years ago

4.3.0

4 years ago

4.2.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.8.0

4 years ago

3.7.0

4 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.3

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.0.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