@a-la/detect-jsx

1.0.2 • Public • Published

@a-la/detect-jsx

npm version

@a-la/detect-jsx Detects Position Of JSX Tag In JavaScript File.

yarn add -E @a-la/detect-jsx

Table Of Contents

API

The package is available by importing its default function:

import detectJsx from '@a-la/detect-jsx'

detectJsx(
  string: string,
): number

Returns the position of the first JSX tag by evaluating the code using Node's vm package: since JSX reference will throw an error, this error can be caught and used to find out the index of the opening < symbol in JSX.

For example, given the following JSX component:

const Component = ({ example }) => (
  <div class="Test" id={example}></div>
)

It is possible to detect where JSX starts:

/* yarn example/ */
import read from '@wrote/read'
import detectJSX from '@a-la/detect-jsx'

(async () => {
  const code = await read('example/Component.jsx')
  const res = detectJSX(code)
  console.log(res)
})()
39

Copyright

(c) À La Mode 2019

Readme

Keywords

Package Sidebar

Install

npm i @a-la/detect-jsx

Weekly Downloads

120

Version

1.0.2

License

MIT

Unpacked Size

7.42 kB

Total Files

13

Last publish

Collaborators

  • zvr