Skip to content

Commit ef9a512

Browse files
gfmioljharb
authored andcommitted
[patch] relax JSX pragma regexp
Fixes #2642.
1 parent 78e48c8 commit ef9a512

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

Diff for: lib/util/pragma.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77

8-
const JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
8+
const JSX_ANNOTATION_REGEX = /@jsx\s+([^\s]+)/;
99
// Does not check for reserved keywords or unicode characters
1010
const JS_IDENTIFIER_REGEX = /^[_$a-zA-Z][_$a-zA-Z0-9]*$/;
1111

@@ -42,7 +42,7 @@ function getFromContext(context) {
4242
if (pragmaNode) {
4343
const matches = JSX_ANNOTATION_REGEX.exec(pragmaNode.value);
4444
pragma = matches[1].split('.')[0];
45-
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
45+
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
4646
} else if (context.settings.react && context.settings.react.pragma) {
4747
pragma = context.settings.react.pragma;
4848
}

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"eslint-config-airbnb-base": "^14.2.0",
5252
"eslint-plugin-eslint-plugin": "^2.2.2",
5353
"eslint-plugin-import": "^2.21.2",
54+
"espree": "^3.5.4",
5455
"istanbul": "^0.4.5",
5556
"markdown-magic": "^1.0.0",
5657
"mocha": "^5.2.0",

Diff for: tests/util/pragma.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const SourceCode = require('eslint').SourceCode;
5+
const espree = require('espree');
6+
7+
const getFromContext = require('../../lib/util/pragma').getFromContext;
8+
9+
const DEFAULT_CONFIG = {
10+
ecmaVersion: 6,
11+
comment: true,
12+
tokens: true,
13+
range: true,
14+
loc: true
15+
};
16+
17+
const DEFAULT_SETTINGS = {
18+
react: {
19+
pragma: 'React'
20+
}
21+
};
22+
23+
const fakeContext = (code) => {
24+
const ast = espree.parse(code, DEFAULT_CONFIG);
25+
return {
26+
getSourceCode: () => new SourceCode(code, ast),
27+
settings: DEFAULT_SETTINGS
28+
};
29+
};
30+
31+
describe('pragma', () => {
32+
describe('getFromContext', () => {
33+
it('finds the pragma in a block comment', () => {
34+
const code = '/* @jsx jsx */';
35+
assert.strictEqual(getFromContext(fakeContext(code)), 'jsx');
36+
});
37+
38+
it('finds the pragma in a docstring comment', () => {
39+
const code = '/** @jsx jsx */';
40+
assert.strictEqual(getFromContext(fakeContext(code)), 'jsx');
41+
});
42+
43+
it('finds the pragma in a line comment', () => {
44+
const code = '// @jsx jsx';
45+
assert.strictEqual(
46+
getFromContext(fakeContext(code)),
47+
'jsx'
48+
);
49+
});
50+
51+
it('defaults to the value of settings.react.pragma', () => {
52+
const code = '';
53+
assert.strictEqual(
54+
getFromContext(fakeContext(code)),
55+
DEFAULT_SETTINGS.react.pragma
56+
);
57+
});
58+
59+
it('throws an error if the pragma is invalid', () => {
60+
const code = '/* @jsx invalid-jsx-pragma */';
61+
assert.throws(() => getFromContext(fakeContext(code)));
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)