@putout/compare
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -8,2 +8,8 @@ 'use strict'; | ||
isExpressionStatement, | ||
isArrayPattern, | ||
isArrayExpression, | ||
isObjectPattern, | ||
isObjectExpression, | ||
isClassBody, | ||
isBlock, | ||
} = require('@babel/types'); | ||
@@ -13,3 +19,24 @@ | ||
const isObject = (a) => typeof a === 'object'; | ||
const {isArray} = Array; | ||
const isObject = (a) => { | ||
if (!a) | ||
return false; | ||
if (isArray(a)) | ||
return false; | ||
return typeof a === 'object'; | ||
}; | ||
const isArrays = (a, b) => { | ||
if (!isArray(a) && !isArray(b)) | ||
return false; | ||
if (a.length !== b.length) | ||
return false; | ||
return true; | ||
}; | ||
const isStr = (a) => typeof a === 'string'; | ||
@@ -39,2 +66,5 @@ const compareType = (type) => (path) => path.type === type; | ||
const isAnyObject = (a) => isIdentifier(a, {name: '__object'}); | ||
const isAnyArray = (a) => isIdentifier(a, {name: '__array'}); | ||
function compare(path, base) { | ||
@@ -47,2 +77,8 @@ const node = parseNode(path); | ||
if (isEqualAnyObject(node, baseNode)) | ||
return true; | ||
if (isEqualAnyArray(node, baseNode)) | ||
return true; | ||
if (isPath && node.type !== type) { | ||
@@ -90,3 +126,3 @@ const newPathNode = findParent(path, type); | ||
log (value, pathValue); | ||
log(value, pathValue); | ||
@@ -99,2 +135,5 @@ if (value == '__') | ||
if (isClassBody(value) || isBlock(value)) | ||
continue; | ||
const id = isExpressionStatement(value) ? value.expression : value; | ||
@@ -105,2 +144,8 @@ | ||
if (isEqualAnyArray(pathValue, id)) | ||
continue; | ||
if (isEqualAnyObject(pathValue, id)) | ||
continue; | ||
if (isIdentifier(id) && /^__[a-z]$/.test(id.name)) | ||
@@ -112,5 +157,11 @@ continue; | ||
if (value && isObject(value) && superCompare(pathValue, value)) | ||
if (isObject(value) && superCompare(pathValue, value)) | ||
continue; | ||
if (isArrays(value, pathValue) && superCompare(pathValue, value)) | ||
continue; | ||
if (isArray(id) && isIdentifier(id[0], {name: '__args'})) | ||
continue; | ||
return false; | ||
@@ -122,1 +173,21 @@ } | ||
function isEqualAnyArray(node, id) { | ||
if (!isAnyArray(id)) | ||
return false; | ||
if (!isArrayPattern(node) && !isArrayExpression(node)) | ||
return false; | ||
return true; | ||
} | ||
function isEqualAnyObject(node, id) { | ||
if (!isAnyObject(id)) | ||
return false; | ||
if (!isObjectPattern(node) && !isObjectExpression(node)) | ||
return false; | ||
return true; | ||
} | ||
{ | ||
"name": "@putout/compare", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", | ||
@@ -5,0 +5,0 @@ "description": "compare ast-nodes", |
@@ -21,12 +21,61 @@ # putout-compare [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] | ||
- `node` - `AST-node` or `code` that will be generated; | ||
- `baseNode` `AST-node` with support of `template variables`. | ||
#### Supported template variables: | ||
##### __ | ||
Any node. | ||
```js | ||
const {compare} = require('@putout/compare'); | ||
compare('const a = {}', 'const a = {}'); | ||
compare('const a = {}', 'const __ = {}'); | ||
compare('const hello = {}', 'const __a = {}'); | ||
compare('const a = "hello"', 'const __ = "__"'); | ||
// returns | ||
true | ||
compare('const x = data', 'const __ = __'); | ||
compare('const {x} = data', 'const __ = __'); | ||
compare('const x = {data}', 'const __ = __'); | ||
// returns | ||
true | ||
``` | ||
##### __object | ||
`ObjectPattern` or `ObjectExpression` with any count of `properties`. | ||
```js | ||
compare('const {} = data', 'const __object = __') | ||
compare('const {hello} = data', 'const __object = __') | ||
// returns | ||
true | ||
``` | ||
##### __array | ||
`ArrayPattern` or `ArrayExpression` with any count of `elements`. | ||
```js | ||
compare('const [] = data', 'const __array = __'); | ||
compare('const [hello] = data', 'const __array = __'); | ||
compare('const hello = [data]', 'const __ = __array'); | ||
// returns | ||
true | ||
``` | ||
##### __args | ||
Any count of `arguments`: | ||
```js | ||
compare('(a, b, c) => {}', '(__args) => {}'); | ||
compare('(a, b) => {}', '(__args) => {}'); | ||
compare('() => {}', '(__args) => {}'); | ||
// returns | ||
true | ||
``` | ||
##### "__" | ||
Any string literal. | ||
```js | ||
compare('const a = "hello"', 'const __ = "__"'); | ||
``` | ||
## License | ||
@@ -33,0 +82,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9161
156
84