Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(javascript) fix jsx self-closing tag issues #2322

Merged
merged 6 commits into from Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGES.md
Expand Up @@ -10,19 +10,23 @@ New themes:

Core Changes:

- none yet.
- (javascript) fix JSX self-closing tag issues (#2322) [Josh Goebel][]

Language Improvements:

- none yet.

[Josh Goebel]: https://github.com/yyyc514


## Version 9.17.1

Fixes:

- fix(parser): resolve IE 11 issue with Object.freeze() (#2319) [Josh Goebel][]

[Josh Goebel]: https://github.com/yyyc514


## Version 9.17.0

Expand Down
14 changes: 6 additions & 8 deletions src/languages/javascript.js
Expand Up @@ -6,6 +6,8 @@ Website: https://developer.mozilla.org/en-US/docs/Web/JavaScript
*/

function(hljs) {
var TAG_START = /<[A-Za-z0-9\\._:-]+/;
var TAG_FINISH = /\/[A-Za-z0-9\\._:-]+>|\/>/;

Choose a reason for hiding this comment

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

Hey guys, React now support fragments which are usually written as <>...</>.

So maybe this could be

var TAG_START = /<[A-Za-z0-9\\._:-]*/;
var TAG_FINISH = /\/[A-Za-z0-9\\._:-]*>|\/>/;

What do you think? Do you see any problem?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I need an actual real example of what you're talking about.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well now if that ain't just ridiculous. :-)

https://reactjs.org/docs/fragments.html#short-syntax

Could you open an actual issue for this, please?

Choose a reason for hiding this comment

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

Sure: #2327

var IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
var KEYWORDS = {
keyword:
Expand Down Expand Up @@ -187,17 +189,13 @@ function(hljs) {
end: /\s*/,
skip: true,
},
{ // E4X / JSX
begin: /</, end: /(\/[A-Za-z0-9\\._:-]+|[A-Za-z0-9\\._:-]+\/)>/,
{ // JSX
begin: TAG_START, end: TAG_FINISH,
subLanguage: 'xml',
contains: [
{ begin: /<[A-Za-z0-9\\._:-]+\s*\/>/, skip: true },
{
begin: /<[A-Za-z0-9\\._:-]+/, end: /(\/[A-Za-z0-9\\._:-]+|[A-Za-z0-9\\._:-]+\/)>/, skip: true,
contains: [
{ begin: /<[A-Za-z0-9\\._:-]+\s*\/>/, skip: true },
'self'
]
begin: TAG_START, end: TAG_FINISH, skip: true,
contains: ['self']
}
]
}
Expand Down
20 changes: 20 additions & 0 deletions test/markup/javascript/jsx.expect.txt
Expand Up @@ -3,5 +3,25 @@
<span class="hljs-keyword">var</span> jsx = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">node</span>&gt;</span>...<span class="hljs-tag">&lt;<span class="hljs-name">child</span>&gt;</span>...<span class="hljs-tag">&lt;/<span class="hljs-name">child</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">node</span>&gt;</span></span>;
<span class="hljs-keyword">var</span> jsx = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
<span class="hljs-keyword">var</span> jsx = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">pre-node</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">child</span> /&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">pre-node</span>&gt;</span></span>;

<span class="hljs-keyword">var</span> x = <span class="hljs-number">5</span>;

<span class="hljs-keyword">return</span> (<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">node</span> <span class="hljs-attr">attr</span>=<span class="hljs-string">"value"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">node</span>&gt;</span></span>);

<span class="hljs-keyword">const</span> n = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">X</span> /&gt;</span></span>
<span class="hljs-keyword">const</span> m = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">X</span> <span class="hljs-attr">x</span>=<span class="hljs-string">""</span> /&gt;</span></span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
render() {
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">BrowserRouter</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/about"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{About}</span> /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/contact"</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Contact}</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">BrowserRouter</span>&gt;</span></span>
);
}
}

<span class="hljs-keyword">var</span> x = <span class="hljs-number">5</span>;
20 changes: 20 additions & 0 deletions test/markup/javascript/jsx.txt
Expand Up @@ -3,5 +3,25 @@ var jsx = <node><child/></node>;
var jsx = <node>...<child>...</child></node>;
var jsx = <div><span><br /></span></div>;
var jsx = <pre-node><child /></pre-node>;

var x = 5;

return (<node attr="value"></node>);

const n = () => <X />
const m = () => <X x="" />

class App extends Component {
render() {
return (
<BrowserRouter>
<div>
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</div>
</BrowserRouter>
);
}
}

var x = 5;