source: imaps-frontend/node_modules/eslint-plugin-react/lib/util/pragma.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[d565449]1/**
2 * @fileoverview Utility functions for React pragma configuration
3 * @author Yannick Croissant
4 */
5
6'use strict';
7
8const getSourceCode = require('./eslint').getSourceCode;
9
10const JSX_ANNOTATION_REGEX = /@jsx\s+([^\s]+)/;
11// Does not check for reserved keywords or unicode characters
12const JS_IDENTIFIER_REGEX = /^[_$a-zA-Z][_$a-zA-Z0-9]*$/;
13
14/**
15 * @param {Context} context
16 * @returns {string}
17 */
18function 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 */
34function 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 */
50function 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 throw new Error(`React pragma ${pragma} is not a valid identifier`);
66 }
67 return pragma;
68}
69
70module.exports = {
71 getCreateClassFromContext,
72 getFragmentFromContext,
73 getFromContext,
74};
Note: See TracBrowser for help on using the repository browser.