Skip to content

Commit 8d3c957

Browse files
committed
.
0 parents  commit 8d3c957

File tree

15 files changed

+1057
-0
lines changed

15 files changed

+1057
-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/erbium
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage/
2+
*.md

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @typedef {import('./lib/index.js').Options} Options
3+
* @typedef {import('./lib/types.js').Handler} Handler
4+
* @typedef {import('./lib/types.js').Handlers} Handlers
5+
* @typedef {import('./lib/types.js').State} State
6+
*/
7+
8+
export {toJs} from './lib/index.js'
9+
export {jsx} from './lib/jsx.js'

lib/index.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @typedef {import('estree-jsx').Program} Program
3+
* @typedef {typeof import('source-map').SourceMapGenerator} SourceMapGenerator
4+
* @typedef {import('./types.js').Handlers} Handlers
5+
*
6+
* @typedef BaseFields
7+
* @property {Handlers} [handlers]
8+
* Object mapping node types to functions handling the corresponding nodes.
9+
*
10+
* @typedef SourceMapFieldsWithoutSourceMapGenerator
11+
* @property {undefined} [SourceMapGenerator]
12+
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
13+
* in.
14+
* @property {undefined} [filePath]
15+
* Path to input file.
16+
*
17+
* @typedef SourceMapFieldsWithSourceMapGenerator
18+
* @property {SourceMapGenerator} SourceMapGenerator
19+
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
20+
* in.
21+
* @property {string} [filePath]
22+
* Path to input file.
23+
*
24+
* @typedef {BaseFields & SourceMapFieldsWithoutSourceMapGenerator} OptionsWithoutSourceMapGenerator
25+
* @typedef {BaseFields & SourceMapFieldsWithSourceMapGenerator} OptionsWithSourceMapGenerator
26+
*
27+
* @typedef {OptionsWithoutSourceMapGenerator|OptionsWithSourceMapGenerator} Options
28+
* Configuration (optional).
29+
*
30+
* @typedef Map
31+
* Raw source map, see:
32+
* <https://github.com/mozilla/source-map/blob/58819f0/source-map.d.ts#L15-L23>.
33+
* @property {number} version
34+
* @property {Array<string>} sources
35+
* @property {Array<string>} names
36+
* @property {string|undefined} [sourceRoot]
37+
* @property {Array<string>|undefined} [sourcesContent]
38+
* @property {string} mappings
39+
* @property {string} file
40+
*
41+
* @typedef BaseResultFields
42+
* @property {string} value
43+
* Serialized JavaScript.
44+
*
45+
* @typedef ResultFieldsWithoutSourceMapGenerator
46+
* @property {undefined} map
47+
* Source map as (parsed) JSON, if `SourceMapGenerator` is passed.
48+
*
49+
* @typedef ResultFieldsWithSourceMapGenerator
50+
* @property {Map} map
51+
* Source map as (parsed) JSON, if `SourceMapGenerator` is passed.
52+
*
53+
* @typedef ResultFieldsRegardlessOfSourceMapGenerator
54+
* @property {Map|undefined} map
55+
* Source map as (parsed) JSON, if `SourceMapGenerator` is passed.
56+
*
57+
* @typedef {BaseResultFields & ResultFieldsWithoutSourceMapGenerator} ResultWithoutSourceMapGenerator
58+
* @typedef {BaseResultFields & ResultFieldsWithSourceMapGenerator} ResultWithSourceMapGenerator
59+
* @typedef {BaseResultFields & ResultFieldsRegardlessOfSourceMapGenerator} ResultRegardlessOfSourceMapGenerator
60+
*
61+
* @typedef {ResultRegardlessOfSourceMapGenerator} Result
62+
*/
63+
64+
import {GENERATOR, generate} from 'astring'
65+
66+
/**
67+
* Estree (and esast) utility to serialize estrees as JavaScript.
68+
*
69+
* @param value
70+
* Estree (esast).
71+
* @param options
72+
* Configuration (optional).
73+
* @returns
74+
* An object with two fields:
75+
* * `value` (`string`) — serialized JavaScript
76+
* * `map` (`Object?`) — source map as (parsed) JSON, if
77+
* `SourceMapGenerator` is passed
78+
*/
79+
export const toJs =
80+
/**
81+
* @type {(
82+
* ((value: Program, options: OptionsWithSourceMapGenerator) => ResultWithSourceMapGenerator) &
83+
* ((value: Program, options?: OptionsWithoutSourceMapGenerator) => ResultWithoutSourceMapGenerator)
84+
* )}
85+
*/
86+
(
87+
/**
88+
* @param {Program} tree
89+
* @param {Options} [options={}]
90+
* @returns {Result}
91+
*/
92+
function (tree, options = {}) {
93+
const {SourceMapGenerator, filePath, handlers} = options
94+
const sourceMap = SourceMapGenerator
95+
? new SourceMapGenerator({file: filePath || '<unknown>.js'})
96+
: undefined
97+
98+
const value = generate(tree, {
99+
comments: true,
100+
// @ts-expect-error: `generator` is fine!
101+
generator: {...GENERATOR, ...handlers},
102+
sourceMap
103+
})
104+
const map = sourceMap ? sourceMap.toJSON() : undefined
105+
106+
return {value, map}
107+
}
108+
)

0 commit comments

Comments
 (0)