unist-util-position-from-estree
Advanced tools
Comparing version 1.1.2 to 2.0.0
/** | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Point} UnistPoint | ||
* @typedef {import('unist').Position} UnistPosition | ||
*/ | ||
/** | ||
* @typedef {[start: number | null | undefined, end: number | null | undefined]} RangeLike | ||
* | ||
* @typedef {[number, number]} RangeLike | ||
* | ||
* @typedef PointLike | ||
@@ -25,8 +27,14 @@ * @property {number | null | undefined} [line] | ||
* estree node. | ||
* @returns {Position} | ||
* @returns {UnistPosition | undefined} | ||
* unist position. | ||
*/ | ||
export function positionFromEstree(node?: NodeLike | null | undefined): Position | ||
export type Position = import('unist').Position | ||
export type RangeLike = [number, number] | ||
export function positionFromEstree( | ||
node?: NodeLike | null | undefined | ||
): UnistPosition | undefined | ||
export type UnistPoint = import('unist').Point | ||
export type UnistPosition = import('unist').Position | ||
export type RangeLike = [ | ||
start: number | null | undefined, | ||
end: number | null | undefined | ||
] | ||
export type PointLike = { | ||
@@ -33,0 +41,0 @@ line?: number | null | undefined |
/** | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Point} UnistPoint | ||
* @typedef {import('unist').Position} UnistPosition | ||
*/ | ||
/** | ||
* @typedef {[start: number | null | undefined, end: number | null | undefined]} RangeLike | ||
* | ||
* @typedef {[number, number]} RangeLike | ||
* | ||
* @typedef PointLike | ||
@@ -26,3 +29,3 @@ * @property {number | null | undefined} [line] | ||
* estree node. | ||
* @returns {Position} | ||
* @returns {UnistPosition | undefined} | ||
* unist position. | ||
@@ -33,22 +36,34 @@ */ | ||
const loc = nodeLike.loc || {} | ||
const range = nodeLike.range || [0, 0] | ||
const startColumn = loc.start | ||
? numberOrUndefined(loc.start.column) | ||
: undefined | ||
const endColumn = loc.end ? numberOrUndefined(loc.end.column) : undefined | ||
const range = nodeLike.range || [undefined, undefined] | ||
const start = pointOrUndefined(loc.start, range[0] || nodeLike.start) | ||
const end = pointOrUndefined(loc.end, range[1] || nodeLike.end) | ||
return { | ||
start: { | ||
// @ts-expect-error: return no point / no position next major. | ||
line: loc.start ? numberOrUndefined(loc.start.line) : undefined, | ||
// @ts-expect-error: return no point / no position next major. | ||
column: startColumn === undefined ? undefined : startColumn + 1, | ||
offset: numberOrUndefined(range[0] || nodeLike.start) | ||
}, | ||
end: { | ||
// @ts-expect-error: return no point / no position next major. | ||
line: loc.end ? numberOrUndefined(loc.end.line) : undefined, | ||
// @ts-expect-error: return no point / no position next major. | ||
column: endColumn === undefined ? undefined : endColumn + 1, | ||
offset: numberOrUndefined(range[1] || nodeLike.end) | ||
if (start && end) { | ||
return {start, end} | ||
} | ||
} | ||
/** | ||
* @param {unknown} estreePoint | ||
* estree point. | ||
* @param {unknown} estreeOffset | ||
* estree offset. | ||
* @returns {UnistPoint | undefined} | ||
* unist point. | ||
*/ | ||
function pointOrUndefined(estreePoint, estreeOffset) { | ||
if (estreePoint && typeof estreePoint === 'object') { | ||
const line = | ||
'line' in estreePoint ? numberOrUndefined(estreePoint.line) : undefined | ||
const column = | ||
'column' in estreePoint | ||
? numberOrUndefined(estreePoint.column) | ||
: undefined | ||
if (line && column !== undefined) { | ||
return { | ||
line, | ||
column: column + 1, | ||
offset: numberOrUndefined(estreeOffset) | ||
} | ||
} | ||
@@ -59,4 +74,3 @@ } | ||
/** | ||
* | ||
* @param {number | null | undefined} value | ||
* @param {unknown} value | ||
* @returns {number | undefined} | ||
@@ -63,0 +77,0 @@ */ |
{ | ||
"name": "unist-util-position-from-estree", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "unist utility to get a position from an estree node", | ||
@@ -31,4 +31,3 @@ "license": "MIT", | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"exports": "./index.js", | ||
"files": [ | ||
@@ -40,9 +39,9 @@ "lib/", | ||
"dependencies": { | ||
"@types/unist": "^2.0.0" | ||
"@types/unist": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/acorn": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"@types/node": "^20.0.0", | ||
"acorn": "^8.0.0", | ||
"c8": "^7.0.0", | ||
"c8": "^8.0.0", | ||
"prettier": "^2.0.0", | ||
@@ -52,4 +51,4 @@ "remark-cli": "^11.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.53.0" | ||
"typescript": "^5.0.0", | ||
"xo": "^0.54.0" | ||
}, | ||
@@ -61,19 +60,16 @@ "scripts": { | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test-coverage": "c8 --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": false | ||
}, | ||
"xo": { | ||
"prettier": true | ||
}, | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
"remark-preset-wooorm" | ||
] | ||
@@ -84,4 +80,8 @@ }, | ||
"detail": true, | ||
"ignoreCatch": true, | ||
"strict": true | ||
}, | ||
"xo": { | ||
"prettier": true | ||
} | ||
} |
@@ -39,3 +39,3 @@ # unist-util-position-from-estree | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
In Node.js (version 16+), install with [npm][]: | ||
@@ -49,3 +49,3 @@ ```sh | ||
```js | ||
import {positionFromEstree} from 'https://esm.sh/unist-util-position-from-estree@1' | ||
import {positionFromEstree} from 'https://esm.sh/unist-util-position-from-estree@2' | ||
``` | ||
@@ -57,3 +57,3 @@ | ||
<script type="module"> | ||
import {positionFromEstree} from 'https://esm.sh/unist-util-position-from-estree@1?bundle' | ||
import {positionFromEstree} from 'https://esm.sh/unist-util-position-from-estree@2?bundle' | ||
</script> | ||
@@ -112,3 +112,3 @@ ``` | ||
unist position ([`Position`][position]). | ||
unist position, if valid ([`Position`][position] or `undefined`). | ||
@@ -122,7 +122,10 @@ ## Types | ||
Projects maintained by the unified collective are compatible with all maintained | ||
Projects maintained by the unified collective are compatible with maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
When we cut a new major release, we drop support for unmaintained versions of | ||
Node. | ||
This means we try to keep the current release line, | ||
`unist-util-position-from-estree@^2`, compatible with Node.js 16. | ||
## Contribute | ||
@@ -156,5 +159,5 @@ | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-position-from-estree.svg | ||
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-position-from-estree | ||
[size]: https://bundlephobia.com/result?p=unist-util-position-from-estree | ||
[size]: https://bundlejs.com/?q=unist-util-position-from-estree | ||
@@ -161,0 +164,0 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11927
125
197
0
+ Added@types/unist@3.0.3(transitive)
- Removed@types/unist@2.0.11(transitive)
Updated@types/unist@^3.0.0