Skip to content

Commit 37e8336

Browse files
committed
.
0 parents  commit 37e8336

File tree

11 files changed

+331
-0
lines changed

11 files changed

+331
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.github/workflows/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: main
2+
on:
3+
- pull_request
4+
- push
5+
jobs:
6+
main:
7+
name: ${{matrix.node}}
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: dcodeIO/setup-node-nvm@master
12+
with:
13+
node-version: ${{matrix.node}}
14+
- run: npm install
15+
- run: npm test
16+
- uses: codecov/codecov-action@v1
17+
strategy:
18+
matrix:
19+
node:
20+
- lts/erbium
21+
- node

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.nyc_output/
2+
coverage/
3+
node_modules/
4+
*.log
5+
.DS_Store
6+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
*.json
3+
*.md

example.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {parse} from 'acorn'
2+
import {positionFromEstree} from './index.js'
3+
4+
// Make acorn support line/column.
5+
var node = parse('function x() { console.log(1) }', {locations: true})
6+
7+
console.log(positionFromEstree(node))
8+
console.log(positionFromEstree(node.body[0].id))
9+
console.log(positionFromEstree(node.body[0].body.body[0].expression))

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function positionFromEstree(value) {
2+
var node = value || {}
3+
var loc = node.loc || {}
4+
var range = node.range || []
5+
var startOffset = range[0] || node.start
6+
7+
return {
8+
start: {
9+
line: loc.start && loc.start.line > -1 ? loc.start.line : null,
10+
column: loc.start && loc.start.column > -1 ? loc.start.column + 1 : null,
11+
offset: startOffset > -1 ? startOffset : null
12+
},
13+
end: {
14+
line: loc.end && loc.end.line > -1 ? loc.end.line : null,
15+
column: loc.end && loc.end.column > -1 ? loc.end.column + 1 : null,
16+
offset: range[1] || node.end || null
17+
}
18+
}
19+
}

license

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2021 Titus Wormer <tituswormer@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"name": "unist-util-position-from-estree",
3+
"version": "0.0.0",
4+
"description": "unist utility to get a position from an estree node",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"unist-util",
9+
"util",
10+
"utility",
11+
"recma",
12+
"esast",
13+
"estree",
14+
"javascript",
15+
"ecmascript",
16+
"node",
17+
"position"
18+
],
19+
"repository": "syntax-tree/unist-util-position-from-estree",
20+
"bugs": "https://github.com/syntax-tree/unist-util-position-from-estree/issues",
21+
"funding": {
22+
"type": "opencollective",
23+
"url": "https://opencollective.com/unified"
24+
},
25+
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
26+
"contributors": [
27+
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
28+
],
29+
"type": "module",
30+
"module": "./index.js",
31+
"files": [
32+
"index.js"
33+
],
34+
"dependencies": {},
35+
"devDependencies": {
36+
"acorn": "^8.0.0",
37+
"c8": "^7.5.0",
38+
"nyc": "^15.0.0",
39+
"prettier": "^2.0.0",
40+
"remark-cli": "^9.0.0",
41+
"remark-preset-wooorm": "^8.0.0",
42+
"tape": "^5.0.0",
43+
"xo": "^0.37.0"
44+
},
45+
"scripts": {
46+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
47+
"test-api": "node test",
48+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --experimental-modules test.js",
49+
"test": "npm run format && npm run test-coverage"
50+
},
51+
"prettier": {
52+
"tabWidth": 2,
53+
"useTabs": false,
54+
"singleQuote": true,
55+
"bracketSpacing": false,
56+
"semi": false,
57+
"trailingComma": "none"
58+
},
59+
"xo": {
60+
"prettier": true,
61+
"ignores": [],
62+
"rules": {
63+
"no-var": "off",
64+
"prefer-arrow-callback": "off"
65+
}
66+
},
67+
"remarkConfig": {
68+
"plugins": [
69+
"preset-wooorm"
70+
]
71+
}
72+
}

readme.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# unist-util-position-from-estree
2+
3+
[![Build][build-badge]][build]
4+
[![Coverage][coverage-badge]][coverage]
5+
[![Downloads][downloads-badge]][downloads]
6+
[![Size][size-badge]][size]
7+
[![Sponsors][sponsors-badge]][collective]
8+
[![Backers][backers-badge]][collective]
9+
[![Chat][chat-badge]][chat]
10+
11+
[**unist**][unist] utility to get a position from an [**estree**][estree] node.
12+
13+
## Install
14+
15+
[npm][]:
16+
17+
```sh
18+
npm install unist-util-position-from-estree
19+
```
20+
21+
Note that this package is ESM only: it must be imported instead of required.
22+
23+
## Use
24+
25+
```js
26+
import {parse} from 'acorn'
27+
import {positionFromEstree} from 'unist-util-position-from-estree'
28+
29+
// Make acorn support line/column.
30+
var node = parse('function x() { console.log(1) }', {locations: true})
31+
32+
console.log(positionFromEstree(node)) // `Program`
33+
console.log(positionFromEstree(node.body[0].id)) // `x`
34+
console.log(positionFromEstree(node.body[0].body.body[0].expression)) // Call
35+
```
36+
37+
Yields:
38+
39+
```js
40+
{
41+
start: {line: 1, column: 1, offset: 0},
42+
end: {line: 1, column: 32, offset: 31}
43+
}
44+
{
45+
start: {line: 1, column: 10, offset: 9},
46+
end: {line: 1, column: 11, offset: 10}
47+
}
48+
{
49+
start: {line: 1, column: 16, offset: 15},
50+
end: {line: 1, column: 30, offset: 29}
51+
}
52+
```
53+
54+
## API
55+
56+
`unist-util-position-from-estree` exports the following identifiers:
57+
[`positionFromEstree`](#positionfromestreenode).
58+
There is no default export.
59+
60+
### `positionFromEstree(node)`
61+
62+
Given a [`node`][estree], returns a [`position`][position].
63+
64+
## Related
65+
66+
## Contribute
67+
68+
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
69+
started.
70+
See [`support.md`][support] for ways to get help.
71+
72+
This project has a [code of conduct][coc].
73+
By interacting with this repository, organization, or community you agree to
74+
abide by its terms.
75+
76+
## License
77+
78+
[MIT][license] © [Titus Wormer][author]
79+
80+
<!-- Definition -->
81+
82+
[build-badge]: https://github.com/syntax-tree/unist-util-position-from-estree/workflows/main/badge.svg
83+
84+
[build]: https://github.com/syntax-tree/unist-util-position-from-estree/actions
85+
86+
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-position-from-estree.svg
87+
88+
[coverage]: https://codecov.io/github/syntax-tree/unist-util-position-from-estree
89+
90+
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-position-from-estree.svg
91+
92+
[downloads]: https://www.npmjs.com/package/unist-util-position-from-estree
93+
94+
[size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-position-from-estree.svg
95+
96+
[size]: https://bundlephobia.com/result?p=unist-util-position-from-estree
97+
98+
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
99+
100+
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
101+
102+
[collective]: https://opencollective.com/unified
103+
104+
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
105+
106+
[chat]: https://github.com/syntax-tree/unist/discussions
107+
108+
[npm]: https://docs.npmjs.com/cli/install
109+
110+
[license]: license
111+
112+
[author]: https://wooorm.com
113+
114+
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
115+
116+
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
117+
118+
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
119+
120+
[estree]: https://github.com/estree/estree
121+
122+
[unist]: https://github.com/syntax-tree/unist
123+
124+
[position]: https://github.com/syntax-tree/unist#position

test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
import test from 'tape'
4+
import {parse} from 'acorn'
5+
import {positionFromEstree} from './index.js'
6+
7+
test('unist-util-position-from-estree', function (t) {
8+
t.deepEqual(
9+
positionFromEstree(),
10+
{
11+
start: {line: null, column: null, offset: null},
12+
end: {line: null, column: null, offset: null}
13+
},
14+
'should support a missing node'
15+
)
16+
17+
t.deepEqual(
18+
positionFromEstree(parse('x')),
19+
{
20+
start: {line: null, column: null, offset: 0},
21+
end: {line: null, column: null, offset: 1}
22+
},
23+
'should support node w/o `loc`s'
24+
)
25+
26+
t.deepEqual(
27+
positionFromEstree(parse('x', {locations: true})),
28+
{
29+
start: {line: 1, column: 1, offset: 0},
30+
end: {line: 1, column: 2, offset: 1}
31+
},
32+
'should support node w/ `loc`s'
33+
)
34+
35+
t.deepEqual(
36+
positionFromEstree(parse('x', {ranges: true})),
37+
{
38+
start: {line: null, column: null, offset: 0},
39+
end: {line: null, column: null, offset: 1}
40+
},
41+
'should support node w/ `range`s'
42+
)
43+
44+
t.end()
45+
})

0 commit comments

Comments
 (0)