| 1 | /**
|
|---|
| 2 | * @fileoverview Utility functions for React pragma configuration
|
|---|
| 3 | * @author Yannick Croissant
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | const getSourceCode = require('./eslint').getSourceCode;
|
|---|
| 9 |
|
|---|
| 10 | const JSX_ANNOTATION_REGEX = /@jsx\s+([^\s]+)/;
|
|---|
| 11 | // Does not check for reserved keywords or unicode characters
|
|---|
| 12 | const JS_IDENTIFIER_REGEX = /^[_$a-zA-Z][_$a-zA-Z0-9]*$/;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * @param {Context} context
|
|---|
| 16 | * @returns {string}
|
|---|
| 17 | */
|
|---|
| 18 | function getCreateClassFromContext(context) {
|
|---|
| 19 | let pragma = 'createReactClass';
|
|---|
| 20 | // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
|
|---|
| 21 | if (context.settings.react && context.settings.react.createClass) {
|
|---|
| 22 | pragma = context.settings.react.createClass;
|
|---|
| 23 | }
|
|---|
| 24 | if (!JS_IDENTIFIER_REGEX.test(pragma)) {
|
|---|
| 25 | throw new Error(`createClass pragma ${pragma} is not a valid function name`);
|
|---|
| 26 | }
|
|---|
| 27 | return pragma;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * @param {Context} context
|
|---|
| 32 | * @returns {string}
|
|---|
| 33 | */
|
|---|
| 34 | function getFragmentFromContext(context) {
|
|---|
| 35 | let pragma = 'Fragment';
|
|---|
| 36 | // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
|
|---|
| 37 | if (context.settings.react && context.settings.react.fragment) {
|
|---|
| 38 | pragma = context.settings.react.fragment;
|
|---|
| 39 | }
|
|---|
| 40 | if (!JS_IDENTIFIER_REGEX.test(pragma)) {
|
|---|
| 41 | throw new Error(`Fragment pragma ${pragma} is not a valid identifier`);
|
|---|
| 42 | }
|
|---|
| 43 | return pragma;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * @param {Context} context
|
|---|
| 48 | * @returns {string}
|
|---|
| 49 | */
|
|---|
| 50 | function getFromContext(context) {
|
|---|
| 51 | let pragma = 'React';
|
|---|
| 52 |
|
|---|
| 53 | const sourceCode = getSourceCode(context);
|
|---|
| 54 | const pragmaNode = sourceCode.getAllComments().find((node) => JSX_ANNOTATION_REGEX.test(node.value));
|
|---|
| 55 |
|
|---|
| 56 | if (pragmaNode) {
|
|---|
| 57 | const matches = JSX_ANNOTATION_REGEX.exec(pragmaNode.value);
|
|---|
| 58 | pragma = matches[1].split('.')[0];
|
|---|
| 59 | // .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings)
|
|---|
| 60 | } else if (context.settings.react && context.settings.react.pragma) {
|
|---|
| 61 | pragma = context.settings.react.pragma;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | if (!JS_IDENTIFIER_REGEX.test(pragma)) {
|
|---|
| 65 | console.warn(`React pragma ${pragma} is not a valid identifier`);
|
|---|
| 66 | return 'React';
|
|---|
| 67 | }
|
|---|
| 68 | return pragma;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | module.exports = {
|
|---|
| 72 | getCreateClassFromContext,
|
|---|
| 73 | getFragmentFromContext,
|
|---|
| 74 | getFromContext,
|
|---|
| 75 | };
|
|---|