Skip to content

Commit ce1bb66

Browse files
author
Conrad Sollitt
committed
⚛️ Update JSX Loader for Issues 20 and 21
1 parent 0f29951 commit ce1bb66

5 files changed

Lines changed: 87 additions & 11 deletions

File tree

docs/to-do-list.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ TODO List
2222
|
2323
--------------------------------------------------------------------------
2424

25+
**) As of 2022-01-06 updates are being tested for the following issues:
26+
https://github.com/dataformsjs/dataformsjs/issues/20
27+
https://github.com/dataformsjs/dataformsjs/issues/21
28+
This will likely be published later this week
2529

2630
**) Update the Getting Started CSS Template used here:
2731
https://www.dataformsjs.com/en/getting-started

js/react/jsxLoader.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
{ find: /export default class/g, replace: 'class' },
182182
{ find: /import React from 'react';/g, replace: '' },
183183
{ find: /import React from"react";/g, replace: '' },
184+
{ find: /=>,/g, replace:'=>' }, // Work-around for edge-case JSX, Issue 21 on GitHub
184185
],
185186

186187
/**
@@ -588,6 +589,41 @@
588589
return false;
589590
},
590591

592+
/**
593+
* Helper function that gets called when a '<' charater is
594+
* found to determine if it's likely an element or not.
595+
*
596+
* @param {string} input
597+
* @param {number} current
598+
* @param {number} length
599+
* @returns {bool}
600+
*/
601+
isExpression: function(input, current, length) {
602+
var pos = current + 2;
603+
var foundName = false;
604+
while (pos < length) {
605+
var nextChar = input[pos];
606+
pos++;
607+
if (/[a-zA-Z0-9_/]/.test(nextChar)) {
608+
if (foundName) {
609+
break;
610+
} else {
611+
continue;
612+
}
613+
} else if (nextChar === '>') {
614+
break;
615+
} else if (nextChar === ' ') {
616+
foundName = true;
617+
continue;
618+
} else if (nextChar === ')' || nextChar === '&' || nextChar === '|' || nextChar === '?' || nextChar === ';') {
619+
// This happens if an less than expression uses no spaces and the
620+
// right-hand side value is a variable. Issue #20 on GitHub.
621+
return true;
622+
}
623+
}
624+
return false;
625+
},
626+
591627
/**
592628
* Compiler Step 1 - Remove Comments from the Code
593629
*
@@ -709,7 +745,7 @@
709745
break;
710746
case '<':
711747
charNext = peekNext();
712-
if (/[a-zA-Z>]/.test(charNext)) {
748+
if (/[a-zA-Z>]/.test(charNext) && !this.isExpression(input, current, length)) {
713749
state.elementCount++;
714750
} else if (charNext === '/') {
715751
state.elementCount--;
@@ -785,7 +821,7 @@
785821
state.inStringMultiLine = true;
786822
break;
787823
case '<':
788-
if (/[a-zA-Z>]/.test(input[c + 1])) {
824+
if (/[a-zA-Z>]/.test(input[c + 1]) && !jsxLoader.compiler.isExpression(input, c, length)) {
789825
return c; // Start of Element found
790826
}
791827
break;
@@ -1103,7 +1139,7 @@
11031139
}
11041140
break;
11051141
case '<':
1106-
if (/[a-zA-Z>/]/.test(peekNext())) {
1142+
if (/[a-zA-Z>/]/.test(peekNext()) && !jsxLoader.compiler.isExpression(input, current, length)) {
11071143
state.addChild = true;
11081144
state.inElement = true;
11091145
}

0 commit comments

Comments
 (0)