@orgajs/orgx 2.6.0 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"rehype-recma.d.ts","sourceRoot":"","sources":["rehype-recma.js"],"names":[],"mappings":"AAWA;;;;;;;GAOG;AACH,8CALW,OAAO,GAAG,IAAI,GAAG,SAAS,IAYlB,MALN,IAKU,KAHR,OAAO,CAmCrB;AA2BD,mDAAmD;AACnD,wBADW,OAAO,qBAAqB,EAAE,MAAM,CA+B9C;sBAjHY,OAAO,qBAAqB,EAAE,OAAO;6BADO,QAAQ;0BAD1C,MAAM"}
1
+ {"version":3,"file":"rehype-recma.d.ts","sourceRoot":"","sources":["rehype-recma.js"],"names":[],"mappings":"AAWA;;;;;;;GAOG;AACH,8CALW,OAAO,GAAG,IAAI,GAAG,SAAS,IAYnB,MALN,IAKU,KAHR,OAAO,CA+BpB;AA2BD,mDAAmD;AACnD,wBADW,OAAO,qBAAqB,EAAE,MAAM,CA8B9C;sBA5GY,OAAO,qBAAqB,EAAE,OAAO;6BADO,QAAQ;0BAD1C,MAAM"}
@@ -4,9 +4,9 @@
4
4
  * @typedef {import('hast-util-to-estree').Options} Options
5
5
  */
6
6
 
7
- import { toEstree } from 'hast-util-to-estree'
8
7
  import { Parser } from 'acorn'
9
8
  import jsx from 'acorn-jsx'
9
+ import { toEstree } from 'hast-util-to-estree'
10
10
  import renderError from '../util/render-error.js'
11
11
 
12
12
  /**
@@ -18,44 +18,40 @@ import renderError from '../util/render-error.js'
18
18
  * Transform.
19
19
  */
20
20
  export default function rehypeRecma(options) {
21
- /**
22
- * @param {Root} tree
23
- * Tree (hast).
24
- * @returns {Program}
25
- * Program (esast).
26
- */
27
- return function (tree) {
28
- const data = tree.data || {}
29
- /** @type {ModuleDeclaration[]} */
30
- const prepand = []
31
- Object.entries(data).forEach(([k, v]) => {
32
- if (k === 'layout') {
33
- prepand.push({
34
- type: 'ImportDeclaration',
35
- specifiers: [
36
- {
37
- type: 'ImportDefaultSpecifier',
38
- local: {
39
- type: 'Identifier',
40
- name: 'OrgLayout',
41
- },
42
- },
43
- ],
44
- source: {
45
- type: 'Literal',
46
- value: `${v}`,
47
- raw: `'${v}'`,
48
- },
49
- })
50
- } else {
51
- prepand.push(createExport(k, v))
52
- }
53
- })
21
+ /**
22
+ * @param {Root} tree
23
+ * Tree (hast).
24
+ * @returns {Program}
25
+ * Program (esast).
26
+ */
27
+ return function (tree) {
28
+ const data = tree.data || {}
29
+ /** @type {ModuleDeclaration[]} */
30
+ const prepend = []
31
+ Object.entries(data).forEach(([k, v]) => {
32
+ if (k === 'layout') {
33
+ prepend.push({
34
+ type: 'ImportDeclaration',
35
+ specifiers: [
36
+ {
37
+ type: 'ImportDefaultSpecifier',
38
+ local: {
39
+ type: 'Identifier',
40
+ name: 'OrgLayout'
41
+ }
42
+ }
43
+ ],
44
+ source: { type: 'Literal', value: `${v}` }
45
+ })
46
+ } else if (isValidName(k)) {
47
+ prepend.push(createExport(k, v))
48
+ }
49
+ })
54
50
 
55
- const estree = toEstree(tree, { ...options, handlers: { jsx: handleJsx } })
56
- estree.body.unshift(...prepand)
57
- return estree
58
- }
51
+ const estree = toEstree(tree, { ...options, handlers: { jsx: handleJsx } })
52
+ estree.body.unshift(...prepend)
53
+ return estree
54
+ }
59
55
  }
60
56
 
61
57
  const jsxParser = Parser.extend(jsx())
@@ -65,55 +61,54 @@ const jsxParser = Parser.extend(jsx())
65
61
  * @returns {node is Program}
66
62
  */
67
63
  function isProgram(node) {
68
- return node.type === 'Program'
64
+ return node.type === 'Program'
69
65
  }
70
66
 
71
67
  /**
72
68
  * @param {string} code
73
69
  */
74
70
  function parse(code) {
75
- try {
76
- return jsxParser.parse(code, {
77
- sourceType: 'module',
78
- ecmaVersion: 2020,
79
- })
80
- } catch (err) {
81
- // @ts-ignore
82
- return renderError(err)
83
- }
71
+ try {
72
+ return jsxParser.parse(code, {
73
+ sourceType: 'module',
74
+ ecmaVersion: 2020
75
+ })
76
+ } catch (err) {
77
+ // @ts-expect-error
78
+ return renderError(err)
79
+ }
84
80
  }
85
81
 
86
82
  /** @type {import("hast-util-to-estree").Handle} */
87
83
  export const handleJsx = (node, state) => {
88
- let skipImport = false
89
- const estree = parse(node.value)
84
+ const skipImport = false
85
+ const estree = parse(node.value)
90
86
 
91
- /** @type {Expression[]} */
92
- const expressions = []
87
+ /** @type {Expression[]} */
88
+ const expressions = []
93
89
 
94
- if (isProgram(estree)) {
95
- estree.body.forEach((child) => {
96
- if (child.type === 'ImportDeclaration') {
97
- if (!skipImport) {
98
- state.esm.push(child)
99
- }
100
- return false
101
- } else if (child.type === 'ExpressionStatement') {
102
- expressions.push(child.expression)
103
- } else if (
104
- child.type === 'ExportDefaultDeclaration' ||
105
- child.type === 'ExportNamedDeclaration'
106
- ) {
107
- state.esm.push(child)
108
- } else {
109
- throw new Error(`unexpected node: ${child.type}`)
110
- }
111
- })
112
- }
90
+ if (isProgram(estree)) {
91
+ estree.body.forEach((child) => {
92
+ if (child.type === 'ImportDeclaration') {
93
+ if (!skipImport) {
94
+ state.esm.push(child)
95
+ }
96
+ } else if (child.type === 'ExpressionStatement') {
97
+ expressions.push(child.expression)
98
+ } else if (
99
+ child.type === 'ExportDefaultDeclaration' ||
100
+ child.type === 'ExportNamedDeclaration'
101
+ ) {
102
+ state.esm.push(child)
103
+ } else {
104
+ throw new Error(`unexpected node: ${child.type}`)
105
+ }
106
+ })
107
+ }
113
108
 
114
- // @ts-expect-error: array works
115
- // https://github.com/syntax-tree/hast-util-to-estree/blob/c0c4bd33583abade25c4f4e248a06cb1ec8c3aff/lib/state.js#L215
116
- return expressions
109
+ // @ts-expect-error: array works
110
+ // https://github.com/syntax-tree/hast-util-to-estree/blob/c0c4bd33583abade25c4f4e248a06cb1ec8c3aff/lib/state.js#L215
111
+ return expressions
117
112
  }
118
113
 
119
114
  /**
@@ -121,7 +116,25 @@ export const handleJsx = (node, state) => {
121
116
  * @returns {string}
122
117
  */
123
118
  function removeQuotes(text) {
124
- return text.trim().replace(/^["'](.+(?=["']$))["']$/, '$1')
119
+ return text.trim().replace(/^["'](.+(?=["']$))["']$/, '$1')
120
+ }
121
+
122
+ /**
123
+ * Whether `name` can be declared with `export const`. Org keywords are `\w+`,
124
+ * so this only rejects names starting with a digit and reserved words.
125
+ *
126
+ * @param {string} name
127
+ */
128
+ function isValidName(name) {
129
+ try {
130
+ Parser.parse(`const ${name} = 0`, {
131
+ sourceType: 'module',
132
+ ecmaVersion: 'latest'
133
+ })
134
+ return true
135
+ } catch {
136
+ return false
137
+ }
125
138
  }
126
139
 
127
140
  /**
@@ -130,32 +143,31 @@ function removeQuotes(text) {
130
143
  * @returns {ModuleDeclaration}
131
144
  */
132
145
  function createExport(k, v) {
133
- /** @type {(text: string) => Expression} */
134
- const createLiteral = (text) => {
135
- const value = removeQuotes(`${text}`)
136
- return { type: 'Literal', value, raw: `'${value}'` }
137
- }
138
- /** @type {Expression} */
139
- const init = Array.isArray(v)
140
- ? {
141
- type: 'ArrayExpression',
142
- elements: v.map(createLiteral),
143
- }
144
- : createLiteral(v)
145
- return {
146
- type: 'ExportNamedDeclaration',
147
- declaration: {
148
- type: 'VariableDeclaration',
149
- declarations: [
150
- {
151
- type: 'VariableDeclarator',
152
- id: { type: 'Identifier', name: k },
153
- init,
154
- },
155
- ],
156
- kind: 'const',
157
- },
158
- specifiers: [],
159
- source: null,
160
- }
146
+ /** @type {(text: string) => Expression} */
147
+ const createLiteral = (text) => {
148
+ return { type: 'Literal', value: removeQuotes(`${text}`) }
149
+ }
150
+ /** @type {Expression} */
151
+ const init = Array.isArray(v)
152
+ ? {
153
+ type: 'ArrayExpression',
154
+ elements: v.map(createLiteral)
155
+ }
156
+ : createLiteral(v)
157
+ return {
158
+ type: 'ExportNamedDeclaration',
159
+ declaration: {
160
+ type: 'VariableDeclaration',
161
+ declarations: [
162
+ {
163
+ type: 'VariableDeclarator',
164
+ id: { type: 'Identifier', name: k },
165
+ init
166
+ }
167
+ ],
168
+ kind: 'const'
169
+ },
170
+ specifiers: [],
171
+ source: null
172
+ }
161
173
  }
package/lib/run.js CHANGED
@@ -12,7 +12,7 @@ const AsyncFunction = Object.getPrototypeOf(run).constructor
12
12
  * Anything.
13
13
  */
14
14
  export async function run(file, options) {
15
- return new AsyncFunction(String(file))(options)
15
+ return new AsyncFunction(String(file))(options)
16
16
  }
17
17
 
18
18
  /**
@@ -26,6 +26,6 @@ export async function run(file, options) {
26
26
  * Anything.
27
27
  */
28
28
  export function runSync(file, options) {
29
- // eslint-disable-next-line no-new-func
30
- return new Function(String(file))(options)
29
+ // eslint-disable-next-line no-new-func
30
+ return new Function(String(file))(options)
31
31
  }
@@ -11,17 +11,17 @@
11
11
  * Nothing.
12
12
  */
13
13
  export function create(from, to) {
14
- /** @type {Array<keyof Node>} */
15
- // @ts-expect-error: `start`, `end`, `comments` are custom Acorn fields.
16
- const fields = ['start', 'end', 'loc', 'range', 'comments']
17
- let index = -1
14
+ /** @type {Array<keyof Node>} */
15
+ // @ts-expect-error: `start`, `end`, `comments` are custom Acorn fields.
16
+ const fields = ['start', 'end', 'loc', 'range', 'comments']
17
+ let index = -1
18
18
 
19
- while (++index < fields.length) {
20
- const field = fields[index]
19
+ while (++index < fields.length) {
20
+ const field = fields[index]
21
21
 
22
- if (field in from) {
23
- // @ts-expect-error: assume they’re settable.
24
- to[field] = from[field]
25
- }
26
- }
22
+ if (field in from) {
23
+ // @ts-expect-error: assume they’re settable.
24
+ to[field] = from[field]
25
+ }
26
+ }
27
27
  }
@@ -16,17 +16,17 @@
16
16
  * Expression.
17
17
  */
18
18
  export function declarationToExpression(declaration) {
19
- if (declaration.type === 'FunctionDeclaration') {
20
- return {...declaration, type: 'FunctionExpression'}
21
- }
19
+ if (declaration.type === 'FunctionDeclaration') {
20
+ return { ...declaration, type: 'FunctionExpression' }
21
+ }
22
22
 
23
- if (declaration.type === 'ClassDeclaration') {
24
- return {...declaration, type: 'ClassExpression'}
25
- /* Internal utility so the next shouldn’t happen or a maintainer is making a
26
- * mistake. */
27
- /* c8 ignore next 4 */
28
- }
23
+ if (declaration.type === 'ClassDeclaration') {
24
+ return { ...declaration, type: 'ClassExpression' }
25
+ /* Internal utility so the next shouldn’t happen or a maintainer is making a
26
+ * mistake. */
27
+ /* c8 ignore next 4 */
28
+ }
29
29
 
30
- // Probably `VariableDeclaration`.
31
- throw new Error('Cannot turn `' + declaration.type + '` into an expression')
30
+ // Probably `VariableDeclaration`.
31
+ throw new Error(`Cannot turn \`${declaration.type}\` into an expression`)
32
32
  }
@@ -12,9 +12,9 @@
12
12
  * Whether `node` is a declaration.
13
13
  */
14
14
  export function isDeclaration(node) {
15
- return Boolean(
16
- node.type === 'FunctionDeclaration' ||
17
- node.type === 'ClassDeclaration' ||
18
- node.type === 'VariableDeclaration'
19
- )
15
+ return Boolean(
16
+ node.type === 'FunctionDeclaration' ||
17
+ node.type === 'ClassDeclaration' ||
18
+ node.type === 'VariableDeclaration'
19
+ )
20
20
  }
@@ -9,7 +9,7 @@
9
9
  * @typedef {import('estree-jsx').VariableDeclarator} VariableDeclarator
10
10
  */
11
11
 
12
- import {create} from './estree-util-create.js'
12
+ import { create } from './estree-util-create.js'
13
13
 
14
14
  /**
15
15
  * @param {Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier | ExportSpecifier>} specifiers
@@ -17,78 +17,78 @@ import {create} from './estree-util-create.js'
17
17
  * @returns {Array<VariableDeclarator>}
18
18
  */
19
19
  export function specifiersToDeclarations(specifiers, init) {
20
- let index = -1
21
- /** @type {Array<VariableDeclarator>} */
22
- const declarations = []
23
- /** @type {Array<ImportSpecifier | ImportDefaultSpecifier | ExportSpecifier>} */
24
- const otherSpecifiers = []
25
- // Can only be one according to JS syntax.
26
- /** @type {ImportNamespaceSpecifier | undefined} */
27
- let importNamespaceSpecifier
20
+ let index = -1
21
+ /** @type {Array<VariableDeclarator>} */
22
+ const declarations = []
23
+ /** @type {Array<ImportSpecifier | ImportDefaultSpecifier | ExportSpecifier>} */
24
+ const otherSpecifiers = []
25
+ // Can only be one according to JS syntax.
26
+ /** @type {ImportNamespaceSpecifier | undefined} */
27
+ let importNamespaceSpecifier
28
28
 
29
- while (++index < specifiers.length) {
30
- const specifier = specifiers[index]
29
+ while (++index < specifiers.length) {
30
+ const specifier = specifiers[index]
31
31
 
32
- if (specifier.type === 'ImportNamespaceSpecifier') {
33
- importNamespaceSpecifier = specifier
34
- } else {
35
- otherSpecifiers.push(specifier)
36
- }
37
- }
32
+ if (specifier.type === 'ImportNamespaceSpecifier') {
33
+ importNamespaceSpecifier = specifier
34
+ } else {
35
+ otherSpecifiers.push(specifier)
36
+ }
37
+ }
38
38
 
39
- if (importNamespaceSpecifier) {
40
- /** @type {VariableDeclarator} */
41
- const declarator = {
42
- type: 'VariableDeclarator',
43
- id: importNamespaceSpecifier.local,
44
- init
45
- }
46
- create(importNamespaceSpecifier, declarator)
47
- declarations.push(declarator)
48
- }
39
+ if (importNamespaceSpecifier) {
40
+ /** @type {VariableDeclarator} */
41
+ const declarator = {
42
+ type: 'VariableDeclarator',
43
+ id: importNamespaceSpecifier.local,
44
+ init
45
+ }
46
+ create(importNamespaceSpecifier, declarator)
47
+ declarations.push(declarator)
48
+ }
49
49
 
50
- declarations.push({
51
- type: 'VariableDeclarator',
52
- id: {
53
- type: 'ObjectPattern',
54
- properties: otherSpecifiers.map((specifier) => {
55
- /** @type {Identifier} */
56
- // @ts-expect-error: fine.
57
- let key =
58
- specifier.type === 'ImportSpecifier'
59
- ? specifier.imported
60
- : specifier.type === 'ExportSpecifier'
61
- ? specifier.exported
62
- : {type: 'Identifier', name: 'default'}
63
- let value = specifier.local
50
+ declarations.push({
51
+ type: 'VariableDeclarator',
52
+ id: {
53
+ type: 'ObjectPattern',
54
+ properties: otherSpecifiers.map((specifier) => {
55
+ /** @type {Identifier} */
56
+ // @ts-expect-error: fine.
57
+ let key =
58
+ specifier.type === 'ImportSpecifier'
59
+ ? specifier.imported
60
+ : specifier.type === 'ExportSpecifier'
61
+ ? specifier.exported
62
+ : { type: 'Identifier', name: 'default' }
63
+ let value = specifier.local
64
64
 
65
- // Switch them around if we’re exporting.
66
- if (specifier.type === 'ExportSpecifier') {
67
- value = key
68
- // @ts-expect-error: fine.
69
- key = specifier.local
70
- }
65
+ // Switch them around if we’re exporting.
66
+ if (specifier.type === 'ExportSpecifier') {
67
+ value = key
68
+ // @ts-expect-error: fine.
69
+ key = specifier.local
70
+ }
71
71
 
72
- /** @type {AssignmentProperty} */
73
- const property = {
74
- type: 'Property',
75
- kind: 'init',
76
- // @ts-expect-error: fine.
77
- shorthand: key.name === value.name,
78
- method: false,
79
- computed: false,
80
- key,
81
- // @ts-expect-error: fine.
82
- value
83
- }
84
- create(specifier, property)
85
- return property
86
- })
87
- },
88
- init: importNamespaceSpecifier
89
- ? {type: 'Identifier', name: importNamespaceSpecifier.local.name}
90
- : init
91
- })
72
+ /** @type {AssignmentProperty} */
73
+ const property = {
74
+ type: 'Property',
75
+ kind: 'init',
76
+ // @ts-expect-error: fine.
77
+ shorthand: key.name === value.name,
78
+ method: false,
79
+ computed: false,
80
+ key,
81
+ // @ts-expect-error: fine.
82
+ value
83
+ }
84
+ create(specifier, property)
85
+ return property
86
+ })
87
+ },
88
+ init: importNamespaceSpecifier
89
+ ? { type: 'Identifier', name: importNamespaceSpecifier.local.name }
90
+ : init
91
+ })
92
92
 
93
- return declarations
93
+ return declarations
94
94
  }
@@ -1 +1 @@
1
- {"version":3,"file":"estree-util-to-binary-addition.d.ts","sourceRoot":"","sources":["estree-util-to-binary-addition.js"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,8CAFW,KAAK,CAAC,UAAU,CAAC,+BAiB3B;yBArBY,OAAO,YAAY,EAAE,UAAU"}
1
+ {"version":3,"file":"estree-util-to-binary-addition.d.ts","sourceRoot":"","sources":["estree-util-to-binary-addition.js"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,8CAFW,KAAK,CAAC,UAAU,CAAC,+BAmB3B;yBAvBY,OAAO,YAAY,EAAE,UAAU"}
@@ -6,18 +6,20 @@
6
6
  * @param {Array<Expression>} expressions
7
7
  */
8
8
  export function toBinaryAddition(expressions) {
9
- let index = -1
10
- /** @type {Expression | undefined} */
11
- let left
9
+ let index = -1
10
+ /** @type {Expression | undefined} */
11
+ let left
12
12
 
13
- while (++index < expressions.length) {
14
- const right = expressions[index]
15
- left = left ? {type: 'BinaryExpression', left, operator: '+', right} : right
16
- }
13
+ while (++index < expressions.length) {
14
+ const right = expressions[index]
15
+ left = left
16
+ ? { type: 'BinaryExpression', left, operator: '+', right }
17
+ : right
18
+ }
17
19
 
18
- // Just for types.
19
- /* c8 ignore next */
20
- if (!left) throw new Error('Expected non-empty `expressions` to be passed')
20
+ // Just for types.
21
+ /* c8 ignore next */
22
+ if (!left) throw new Error('Expected non-empty `expressions` to be passed')
21
23
 
22
- return left
24
+ return left
23
25
  }
@@ -1 +1 @@
1
- {"version":3,"file":"estree-util-to-id-or-member-expression.d.ts","sourceRoot":"","sources":["estree-util-to-id-or-member-expression.js"],"names":[],"mappings":"AAsCE;;;GAGG;AACH,4CAHW,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,GACpB,UAAU,GAAG,gBAAgB,CAwCzC;AA5DH,wCAEa,CAAC,GAAG,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,aAAa,GAAG,mBAAmB,CAO9E;yBA5BU,OAAO,YAAY,EAAE,UAAU;4BAC/B,OAAO,YAAY,EAAE,aAAa;kCAClC,OAAO,YAAY,EAAE,mBAAmB;sBACxC,OAAO,YAAY,EAAE,OAAO;+BAC5B,OAAO,YAAY,EAAE,gBAAgB"}
1
+ {"version":3,"file":"estree-util-to-id-or-member-expression.d.ts","sourceRoot":"","sources":["estree-util-to-id-or-member-expression.js"],"names":[],"mappings":"AAsCC;;;GAGG;AACH,4CAHW,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,GACpB,UAAU,GAAG,gBAAgB,CA0CzC;AA9DF,wCAEY,CAAC,GAAG,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,aAAa,GAAG,mBAAmB,CAO9E;yBA5BW,OAAO,YAAY,EAAE,UAAU;4BAC/B,OAAO,YAAY,EAAE,aAAa;kCAClC,OAAO,YAAY,EAAE,mBAAmB;sBACxC,OAAO,YAAY,EAAE,OAAO;+BAC5B,OAAO,YAAY,EAAE,gBAAgB"}