Skip to content

Commit c776a79

Browse files
committed
.
0 parents  commit c776a79

27 files changed

+2949
-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/bb.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: bb
2+
on:
3+
issues:
4+
types: [opened, reopened, edited, closed, labeled, unlabeled]
5+
pull_request_target:
6+
types: [opened, reopened, edited, closed, labeled, unlabeled]
7+
jobs:
8+
main:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: unifiedjs/beep-boop-beta@main
12+
with:
13+
repo-token: ${{secrets.GITHUB_TOKEN}}

.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/fermium
21+
- node

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.d.ts
3+
*.log
4+
coverage/
5+
node_modules/
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+
*.md
3+
test/fixtures/

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @typedef {import('./lib/index.js').Options} Options
3+
* @typedef {import('./lib/index.js').Version} Version
4+
* @typedef {import('./lib/index.js').Plugin} Plugin
5+
* @typedef {import('./lib/index.js').Value} Value
6+
*/
7+
8+
export {fromJs} from './lib/index.js'

lib/index.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @typedef {typeof import('acorn').Parser} ParserClass
3+
* @typedef {import('acorn').Position} Position
4+
* @typedef {import('estree-jsx').Comment} Comment
5+
* @typedef {import('estree-jsx').Program} Program
6+
* @typedef {import('buffer').Buffer} Buffer
7+
* @typedef {any extends Buffer ? never : Buffer} MaybeBuffer
8+
* This is the same as `Buffer` if node types are included, `never` otherwise.
9+
* @typedef {string|MaybeBuffer} Value
10+
*
11+
*
12+
* @typedef AcornErrorFields
13+
* @property {number} pos
14+
* @property {Position} loc
15+
*
16+
* @typedef {Error & AcornErrorFields} AcornError
17+
*
18+
* @callback Plugin
19+
* Acorn plugin.
20+
* @param {ParserClass} Parser
21+
* Base parser class.
22+
* @returns {ParserClass}
23+
* Resulting parser class.
24+
*
25+
* @typedef {2015|2016|2017|2018|2019|2020|2021|2022|'latest'} Version
26+
*
27+
* @typedef Options
28+
* Configuration (optional).
29+
* @property {Version} [version='latest']
30+
* JavaScript version (year between 2015 and 2022 or `'latest'`).
31+
* @property {boolean} [module=false]
32+
* Whether this is a module (ESM) or a script.
33+
* @property {boolean} [allowReturnOutsideFunction=false]
34+
* Whether a return statement is allowed in the top scope.
35+
* @property {boolean} [allowImportExportEverywhere=false]
36+
* Whether import/export statements are allowed in the every scope.
37+
* @property {boolean} [allowAwaitOutsideFunction]
38+
* Whether `await` is allowed in the top scope.
39+
* Defaults to `version >= 2022`.
40+
* @property {boolean} [allowSuperOutsideMethod=false]
41+
* Whether `super` is allowed outside methods.
42+
* @property {boolean} [allowHashBang=false]
43+
* Whether a shell hasbang is allowed.
44+
* @property {Array<Plugin>} [plugins=[]]
45+
* List of acorn plugins.
46+
*/
47+
48+
import {Parser} from 'acorn'
49+
import {fromEstree} from 'esast-util-from-estree'
50+
import {VFileMessage} from 'vfile-message'
51+
52+
/**
53+
* @param {Value} value
54+
* @param {Options} [options={}]
55+
*/
56+
export function fromJs(value, options = {}) {
57+
/** @type {ParserClass} */
58+
let parser = Parser
59+
/** @type {Array<Comment>} */
60+
const comments = []
61+
/** @type {Program} */
62+
let tree
63+
64+
if (options.plugins) {
65+
parser = parser.extend(...options.plugins)
66+
}
67+
68+
try {
69+
// @ts-expect-error: Acorn looks enough like estree.
70+
tree = parser.parse(value, {
71+
ecmaVersion: options.version || 'latest',
72+
sourceType: options.module ? 'module' : 'script',
73+
allowReturnOutsideFunction: options.allowReturnOutsideFunction,
74+
allowImportExportEverywhere: options.allowImportExportEverywhere,
75+
allowAwaitOutsideFunction: options.allowAwaitOutsideFunction,
76+
allowHashBang: options.allowHashBang,
77+
allowSuperOutsideMethod: options.allowSuperOutsideMethod,
78+
locations: true,
79+
onComment: comments
80+
})
81+
} catch (error) {
82+
const exception = /** @type {AcornError} */ (error)
83+
exception.message = exception.message.replace(/ \((\d+):(\d+)\)$/, '')
84+
85+
throw new VFileMessage(
86+
exception,
87+
{
88+
line: exception.loc.line,
89+
column: exception.loc.column + 1,
90+
offset: exception.pos
91+
},
92+
'esast-util-from-js:acorn'
93+
)
94+
}
95+
96+
tree.comments = comments
97+
98+
return fromEstree(tree)
99+
}

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) 2022 Titus Wormer <[email protected]>
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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"name": "esast-util-from-js",
3+
"version": "0.0.0",
4+
"description": "estree (and esast) utility to parse from JavaScript",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"estree",
9+
"estree-util",
10+
"esast",
11+
"esast-util",
12+
"util",
13+
"utility",
14+
"js",
15+
"parse",
16+
"tokenize",
17+
"acorn"
18+
],
19+
"repository": "syntax-tree/esast-util-from-js",
20+
"bugs": "https://github.com/syntax-tree/esast-util-from-js/issues",
21+
"funding": {
22+
"type": "opencollective",
23+
"url": "https://opencollective.com/unified"
24+
},
25+
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
26+
"contributors": [
27+
"Titus Wormer <[email protected]> (https://wooorm.com)"
28+
],
29+
"sideEffects": false,
30+
"type": "module",
31+
"main": "index.js",
32+
"types": "index.d.ts",
33+
"files": [
34+
"lib/",
35+
"index.d.ts",
36+
"index.js"
37+
],
38+
"dependencies": {
39+
"@types/estree-jsx": "^0.0.1",
40+
"acorn": "^8.0.0",
41+
"esast-util-from-estree": "^1.0.0",
42+
"vfile-message": "^3.0.0"
43+
},
44+
"devDependencies": {
45+
"@types/tape": "^4.0.0",
46+
"acorn-stage3": "^4.0.0",
47+
"c8": "^7.0.0",
48+
"prettier": "^2.0.0",
49+
"remark-cli": "^10.0.0",
50+
"remark-preset-wooorm": "^9.0.0",
51+
"rimraf": "^3.0.0",
52+
"tape": "^5.0.0",
53+
"type-coverage": "^2.0.0",
54+
"typescript": "^4.0.0",
55+
"xo": "^0.49.0"
56+
},
57+
"scripts": {
58+
"prepack": "npm run build && npm run format",
59+
"build": "rimraf \"{lib,test}/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage",
60+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
61+
"test-api": "node --conditions development test/index.js",
62+
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
63+
"test": "npm run build && npm run format && npm run test-coverage"
64+
},
65+
"prettier": {
66+
"tabWidth": 2,
67+
"useTabs": false,
68+
"singleQuote": true,
69+
"bracketSpacing": false,
70+
"semi": false,
71+
"trailingComma": "none"
72+
},
73+
"xo": {
74+
"prettier": true,
75+
"rules": {
76+
"no-await-in-loop": "off"
77+
},
78+
"ignore": [
79+
"test/fixtures/"
80+
]
81+
},
82+
"remarkConfig": {
83+
"plugins": [
84+
"preset-wooorm"
85+
]
86+
},
87+
"typeCoverage": {
88+
"atLeast": 100,
89+
"detail": true,
90+
"strict": true,
91+
"ignoreFiles": [
92+
"lib/types.d.ts"
93+
]
94+
}
95+
}

0 commit comments

Comments
 (0)