Skip to content

Commit 4049ba5

Browse files
committed
fix orgx tests
1 parent d1d4ccb commit 4049ba5

File tree

7 files changed

+36
-21
lines changed

7 files changed

+36
-21
lines changed

packages/orgx/src/evaluate.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { VFileCompatible } from 'vfile'
22
import { compile, compileSync } from './compile.js'
33
import { ProcessorOptions } from './processor.js'
44
import { OrgaContent } from './types.js'
5+
import { run, runSync } from './run'
56

67
export interface ExportMap extends Record<string, unknown> {
78
default: OrgaContent
@@ -45,15 +46,13 @@ export const evaluate = async (
4546
options: EvaluateOptions
4647
): Promise<ExportMap> => {
4748
const { compiletime, runtime } = resolveOptions(options)
48-
const code = await compile(file, compiletime)
49-
return new Function(String(code))(runtime)
49+
return run(await compile(file, compiletime), runtime)
5050
}
5151

5252
export const evaluateSync = (
5353
file: VFileCompatible,
5454
options: EvaluateOptions
5555
): ExportMap => {
5656
const { compiletime, runtime } = resolveOptions(options)
57-
const code = compileSync(file, compiletime)
58-
return new Function(String(code))(runtime)
57+
return runSync(compileSync(file, compiletime), runtime)
5958
}

packages/orgx/src/plugin/estree-document.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export const estreeDocument: Plugin<
5858
const baseUrl = options_.baseUrl || undefined
5959
const useDynamicImport = options_.useDynamicImport || undefined
6060
const outputFormat = options_.outputFormat || 'program'
61-
const pragma =
62-
options_.pragma === undefined ? 'React.createElement' : options_.pragma
61+
const pragma = options_.pragma || 'React.createElement'
6362
const pragmaFrag =
6463
options_.pragmaFrag === undefined ? 'React.Fragment' : options_.pragmaFrag
6564
const pragmaImportSource = options_.pragmaImportSource || 'react'

packages/orgx/src/plugin/estree-jsx-build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { buildJsx } from 'estree-util-build-jsx'
22
import type { Program } from 'estree'
33
import type { Plugin } from 'unified'
4-
import { specifiersToDeclarations } from '../estree/specifiers-to-declarations'
5-
import { toIdOrMemberExpression } from '../estree/to-id-or-member-expression'
4+
import { specifiersToDeclarations } from '../estree/specifiers-to-declarations.js'
5+
import { toIdOrMemberExpression } from '../estree/to-id-or-member-expression.js'
66

77
export interface Options {
88
outputFormat?: 'program' | 'function-body'

packages/orgx/src/processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function createProcessor(
8787
.use(reorgRehype, options)
8888
.use(options.rehypePlugins)
8989
.use(rehypeEstree, { ...options, handlers })
90-
.use(estreeDocument, options)
90+
.use(estreeDocument, { ...options, outputFormat })
9191
.use(options.estreePlugins)
9292
.use(estreeJsxRewrite, options)
9393

packages/orgx/src/run.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** @type {new (code: string, ...args: Array<unknown>) => Function} **/
2+
const AsyncFunction = Object.getPrototypeOf(run).constructor
3+
4+
/**
5+
* Asynchronously run code.
6+
*/
7+
export async function run(file: { toString(): string }, options: unknown) {
8+
return new AsyncFunction(String(file))(options)
9+
}
10+
11+
/**
12+
* Synchronously run code.
13+
*/
14+
export function runSync(file: { toString(): string }, options: unknown) {
15+
return new Function(String(file))(options)
16+
}

packages/orgx/tests/compile.test.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ const fixture = `
66
#+title: Hello World
77
* Hi
88
`
9-
const code = `/*@jsxRuntime classic @jsx createElement @jsxFrag Fragment*/
10-
import {createElement, Fragment} from "react";
9+
const code = `
10+
/*@jsxRuntime classic @jsx React.createElement @jsxFrag React.Fragment*/
11+
import React from "react";
1112
export const title = 'Hello World';
12-
function OrgaContent(props = {}) {
13+
function _createOrgaContent(props) {
1314
const _components = Object.assign({
1415
div: "div",
1516
h1: "h1"
16-
}, props.components), {wrapper: OrgaLayout} = _components;
17-
const _content = <><_components.div className="section"><_components.h1>{"Hi"}{" "}</_components.h1></_components.div></>;
18-
return OrgaLayout ? <OrgaLayout title={title} {...props}>{_content}</OrgaLayout> : _content;
17+
}, props.components);
18+
return <_components.div className="section"><_components.h1>{"Hi"}{" "}</_components.h1></_components.div>;
19+
}
20+
function OrgaContent(props = {}) {
21+
const {wrapper: OrgaLayout} = props.components || ({});
22+
return OrgaLayout ? <OrgaLayout {...props}><_createOrgaContent {...props} /></OrgaLayout> : _createOrgaContent(props);
1923
}
2024
export default OrgaContent;
2125
`
@@ -26,10 +30,8 @@ describe('compile', () => {
2630
jsxRuntime: 'classic',
2731
jsx: true,
2832
outputFormat: 'program',
29-
pragma: { name: 'createElement', source: 'react' },
30-
pragmaFrag: { name: 'Fragment', source: 'react' },
3133
})
3234

33-
assert.strictEqual(`${result}`, code)
35+
assert.strictEqual(`${result}`.trim(), code.trim())
3436
})
3537
})

packages/orgx/tests/evaluate.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import * as runtime from 'react/jsx-runtime'
88
describe('evaluate', () => {
99
it('can evaluate org file', async () => {
1010
const text = `
11-
#+title: hello
12-
* hello
11+
* hi
1312
`
1413
const Content = (await evaluate(text, runtime)).default
1514
const rendered = renderToStaticMarkup(createElement(Content))
16-
assert.equal(rendered, '<div class="section"><h1>hello </h1></div>')
15+
assert.equal(rendered, '<div class="section"><h1>hi </h1></div>')
1716
})
1817
})

0 commit comments

Comments
 (0)