Skip to content

Commit

Permalink
fix: jsx state for self close tag (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Nov 25, 2023
1 parent 3434915 commit 15dff14
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/app/live-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const customizableColors = Object.entries(SugarHigh.TokenTypes)
.filter(([, tokenTypeName]) => tokenTypeName !== 'break' && tokenTypeName !== 'space')
.sort((a, b) => a - b)

const defaultLiveCode = `\
export default function App() {
const defaultLiveCode =
`export default function App() {
return <p>hello world</p>
}`

Expand Down
17 changes: 14 additions & 3 deletions lib/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ function isSpaces(str) {
return /^[^\S\r\n]+$/g.test(str)
}

function isSign(ch) {
return signs.has(ch)
}

function encode(str) {
return str
.replace(/&/g, '&amp;')
Expand Down Expand Up @@ -190,6 +194,7 @@ function tokenize(code) {
let __jsxEnter = false
/**
* @type {0 | 1 | 2}
* @example
* 0 for not in jsx;
* 1 for open jsx tag;
* 2 for closing jsx tag;
Expand Down Expand Up @@ -237,7 +242,7 @@ function tokenize(code) {
return T_BREAK
} else if (isSpaces(token)) {
return T_SPACE
} else if (token.split('').every(ch => signs.has(ch))) {
} else if (token.split('').every(isSign)) {
return T_SIGN
} else if (isCls(token)) {
return inJsxTag() ? T_IDENTIFIER : T_CLS_NUMBER
Expand Down Expand Up @@ -352,7 +357,8 @@ function tokenize(code) {
if (__jsxTag) {
// >: open tag close sign or closing tag closing sign
// and it's not `=>` or `/>`
if (curr === '>' && !'/='.includes(prev)) {
// `curr` could be `>` or `/`
if ((curr === '>' && !'/='.includes(prev))) {
append()
if (__jsxTag === 1) {
__jsxTag = 0
Expand All @@ -368,15 +374,20 @@ function tokenize(code) {
// >: tag self close sign or close tag sign
if (c_n === '/>' || c_n === '</') {
// if current token is not part of close tag sign, push it first
if (current !== '<' || current !== '<') {
if (current !== '<' && current !== '/') {
append()
}

if (c_n === '/>') {
__jsxTag = 0
} else {
// is '</'
__jsxStack--
}

if (!__jsxStack)
__jsxEnter = false

current = c_n
i++
append(T_SIGN)
Expand Down
32 changes: 29 additions & 3 deletions test/ast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ describe('jsx', () => {
it('parse fold jsx', () => {
const tokens = tokenize(`// jsx
const element = (
<div>Hello World <Food /><div/>
<div>Hello World <Food /></div>
)`);
expect(extractTokenValues(tokens)).toEqual([
"// jsx", "const", "element", "=", "(", "<", "div", ">", "Hello World", "<", "Food", "/>", "<", "div", "/>", ")"
"// jsx", "const", "element", "=", "(", "<", "div", ">", "Hello World", "<", "Food", "/>", "</", "div", ">", ")"
])
expect(extractTokenArray(tokens)).toEqual([
["// jsx", "comment"], ["const", "keyword"], ["element", "identifier"], ["=", "sign"], ["(", "sign"], ["<", "sign"],
["div", "identifier"], [">", "sign"], ["Hello World", "jsxliterals"], ["<", "sign"], ["Food", "identifier"],
["/>", "sign"], ["<", "sign"], ["div", "identifier"], ["/>", "sign"], ["", "jsxliterals"], [")", "jsxliterals"]
["/>", "sign"], ["</", "sign"], ["div", "identifier"], [">", "sign"], [")", "sign"]
])
})

Expand Down Expand Up @@ -261,6 +261,32 @@ describe('jsx', () => {
["</", "sign"], [">", "sign"]
])
})

it('should not affect the function param after closed jsx tag', () => {
// issue: (str was treated as string
const code =
`<a k={v} />
function p(str) {}
`
const tokens = tokenize(code)
expect(extractTokenArray(tokens)).toEqual([
['<', 'sign'],
['a', 'identifier'],
['k', 'identifier'],
['=', 'sign'],
['{', 'sign'],
['v', 'identifier'],
['}', 'sign'],
['/>', 'sign'],
['function', 'keyword'],
['p', 'identifier'],
['(', 'sign'],
['str', 'identifier'],
[')', 'sign'],
['{', 'sign'],
['}', 'sign'],
])
})
})

describe('comments', () => {
Expand Down

1 comment on commit 15dff14

@vercel
Copy link

@vercel vercel bot commented on 15dff14 Nov 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sugar-high – ./

sugar-high-git-main-huozhi.vercel.app
sugar-high-huozhi.vercel.app
sugar-high.vercel.app

Please sign in to comment.