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

Relax JSX pragma regexp #2643

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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');
Copy link
Member

Choose a reason for hiding this comment

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

is there any way to do this with eslint itself, rather than adding a dev dep on espree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not as far as I could tell, unfortunately. I tried using eslint first, but eslint does not expose the API to generate the AST object needed to create the SourceCode object.


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)));
});
});
});