Skip to content

Commit

Permalink
[patch] relax JSX pragma regexp
Browse files Browse the repository at this point in the history
Fixes #2642.
  • Loading branch information
gfmio authored and ljharb committed May 17, 2020
1 parent 78e48c8 commit ef9a512
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/util/pragma.js
Expand Up @@ -5,7 +5,7 @@

'use strict';

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

Expand Down Expand Up @@ -42,7 +42,7 @@ function getFromContext(context) {
if (pragmaNode) {
const matches = JSX_ANNOTATION_REGEX.exec(pragmaNode.value);
pragma = matches[1].split('.')[0];
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
// .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings)
} else if (context.settings.react && context.settings.react.pragma) {
pragma = context.settings.react.pragma;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -51,6 +51,7 @@
"eslint-config-airbnb-base": "^14.2.0",
"eslint-plugin-eslint-plugin": "^2.2.2",
"eslint-plugin-import": "^2.21.2",
"espree": "^3.5.4",
"istanbul": "^0.4.5",
"markdown-magic": "^1.0.0",
"mocha": "^5.2.0",
Expand Down
64 changes: 64 additions & 0 deletions tests/util/pragma.js
@@ -0,0 +1,64 @@
'use strict';

const assert = require('assert');
const SourceCode = require('eslint').SourceCode;
const espree = require('espree');

const getFromContext = require('../../lib/util/pragma').getFromContext;

const DEFAULT_CONFIG = {
ecmaVersion: 6,
comment: true,
tokens: true,
range: true,
loc: true
};

const DEFAULT_SETTINGS = {
react: {
pragma: 'React'
}
};

const fakeContext = (code) => {
const ast = espree.parse(code, DEFAULT_CONFIG);
return {
getSourceCode: () => new SourceCode(code, ast),
settings: DEFAULT_SETTINGS
};
};

describe('pragma', () => {
describe('getFromContext', () => {
it('finds the pragma in a block comment', () => {
const code = '/* @jsx jsx */';
assert.strictEqual(getFromContext(fakeContext(code)), 'jsx');
});

it('finds the pragma in a docstring comment', () => {
const code = '/** @jsx jsx */';
assert.strictEqual(getFromContext(fakeContext(code)), 'jsx');
});

it('finds the pragma in a line comment', () => {
const code = '// @jsx jsx';
assert.strictEqual(
getFromContext(fakeContext(code)),
'jsx'
);
});

it('defaults to the value of settings.react.pragma', () => {
const code = '';
assert.strictEqual(
getFromContext(fakeContext(code)),
DEFAULT_SETTINGS.react.pragma
);
});

it('throws an error if the pragma is invalid', () => {
const code = '/* @jsx invalid-jsx-pragma */';
assert.throws(() => getFromContext(fakeContext(code)));
});
});
});

0 comments on commit ef9a512

Please sign in to comment.