@recursive-robot/react-jsx-parser 2.0.0 → 2.0.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,4 +1,6 @@
1
1
  import { default as React, ComponentType, ExoticComponent } from 'react';
2
+ import { JsxParserError, SourceLocation } from '../helpers/errorUtilities';
3
+ import * as AcornJSX from 'acorn-jsx';
2
4
  export type TProps = {
3
5
  allowUnknownElements?: boolean;
4
6
  autoCloseVoidElements?: boolean;
@@ -12,8 +14,9 @@ export type TProps = {
12
14
  componentsOnly?: boolean;
13
15
  disableFragments?: boolean;
14
16
  disableKeyGeneration?: boolean;
17
+ fileName?: string;
15
18
  jsx?: string;
16
- onError?: (error: Error) => void;
19
+ onError?: (error: JsxParserError) => void;
17
20
  showWarnings?: boolean;
18
21
  renderError?: (props: {
19
22
  error: string;
@@ -21,6 +24,13 @@ export type TProps = {
21
24
  renderInWrapper?: boolean;
22
25
  renderUnrecognized?: (tagName: string) => React.JSX.Element | null;
23
26
  };
27
+ export interface SourceInfo {
28
+ fileName?: string;
29
+ source: string;
30
+ location: SourceLocation;
31
+ loopIndex: number | undefined;
32
+ astNode: AcornJSX.Expression;
33
+ }
24
34
  export default class JsxParser extends React.Component<TProps> {
25
35
  #private;
26
36
  static displayName: string;
@@ -0,0 +1,44 @@
1
+ export type JsxParserErrorType = 'parse' | 'unsupported-function' | 'function-parse' | 'function-runtime' | 'call' | 'chain' | 'member-access' | 'blacklisted-tag' | 'unrecognized-component' | 'unrecognized-tag';
2
+ export interface SourceLocation {
3
+ line: number;
4
+ column?: number;
5
+ startOffset?: number;
6
+ endOffset?: number;
7
+ }
8
+ export declare class JsxParserError extends Error {
9
+ type: JsxParserErrorType;
10
+ location?: SourceLocation;
11
+ fileName?: string;
12
+ snippet?: string;
13
+ source?: string;
14
+ cause?: unknown;
15
+ constructor(message: string, fields: {
16
+ type: JsxParserErrorType;
17
+ location?: SourceLocation;
18
+ fileName?: string;
19
+ snippet?: string;
20
+ source?: string;
21
+ cause?: unknown;
22
+ });
23
+ }
24
+ export declare function trimExcessLeadingWhitespaceFromCodeLines(lines: string[]): string[];
25
+ export declare function getLocationFromOffsets(source: string, start: number, end: number): SourceLocation;
26
+ export declare function buildErrorFromOffsets({ type, message, source, start, end, fileName, cause }: {
27
+ type: JsxParserErrorType;
28
+ message: string;
29
+ source: string;
30
+ start: number;
31
+ end: number;
32
+ fileName?: string;
33
+ cause?: unknown;
34
+ }): JsxParserError;
35
+ export declare function buildErrorFromLine(opts: {
36
+ type: JsxParserErrorType;
37
+ message: string;
38
+ bodyLines: string[];
39
+ line: number;
40
+ functionName?: string;
41
+ fileName?: string;
42
+ cause?: unknown;
43
+ }): JsxParserError;
44
+ export declare function sanitizeHtml(html: string): string;
@@ -1,7 +1,8 @@
1
+ import { JsxParserError } from './errorUtilities';
1
2
  import * as AcornJSX from 'acorn-jsx';
2
3
  export declare function isSpreadElement(node: AcornJSX.BaseExpression): node is AcornJSX.SpreadElement;
3
4
  export declare function getClosureBindings(fullExpression: AcornJSX.Expression): string[];
4
- export declare function getRenderFunction(jsx: string, bindings: Record<string, any>, parseExpression: (body: string, exp: AcornJSX.Expression, scope?: Record<string, any>) => any): (args: Record<string, any>) => any;
5
+ export declare function getRenderFunction(jsx: string, bindings: Record<string, any>, parseExpression: (body: string, exp: AcornJSX.Expression, scope?: Record<string, any>, baseOffset?: number) => any, sourceBaseOffset?: number): (args: Record<string, any>) => any;
5
6
  export declare function getAllJsxElements(code: string): (AcornJSX.JSXElement | AcornJSX.JSXFragment)[];
6
- export declare function transpileFunctionBody(body: string, bindings: Record<string, any>, parseExpression: (jsx: string, exp: AcornJSX.Expression, scope?: Record<string, any>) => any): [string, Record<string, any>];
7
- export declare function constructFunction(paramNames: string[], body: string, name?: string, onError?: (error: any) => void): (...args: any[]) => any;
7
+ export declare function transpileFunctionBody(body: string, bindings: Record<string, any>, parseExpression: (jsx: string, exp: AcornJSX.Expression, scope?: Record<string, any>, sourceBaseOffset?: number) => any, mapBodyOffsetToSource?: (bodyOffset: number) => number): [string, Record<string, any>];
8
+ export declare function constructFunction(paramNames: string[], body: string, name?: string, onError?: (error: JsxParserError) => void, fileName?: string): (...args: any[]) => any;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { default as JsxParser } from './components/JsxParser';
2
- export type { TProps } from './components/JsxParser';
2
+ export type { TProps, SourceInfo } from './components/JsxParser';
3
3
  export type { ScopedFunction, FunctionProxy } from './helpers/functionProxy';
4
+ export { JsxParserError } from './helpers/errorUtilities';
5
+ export type { JsxParserErrorType, SourceLocation } from './helpers/errorUtilities';
4
6
  export default JsxParser;