UNPKG

4.53 kBJavaScriptView Raw
1'use strict';
2const _ = require('lodash');
3
4/**
5 * @param {Context} context
6 * @return {boolean}
7 */
8function shouldUseCreateElement(context) {
9 switch (context.options.targetVersion) {
10 case '0.11.2':
11 case '0.11.1':
12 case '0.11.0':
13 case '0.10.0':
14 return false;
15 default:
16 return true;
17 }
18}
19
20const reactSupportedAttributes = ['accept', 'acceptCharset', 'accessKey', 'action', 'allowFullScreen', 'allowTransparency', 'alt', 'async', 'autoComplete', 'autoPlay', 'cellPadding', 'cellSpacing', 'charSet', 'checked',
21 'classID', 'className', 'cols', 'colSpan', 'content', 'contentEditable', 'contextMenu', 'controls', 'coords', 'crossOrigin', 'data', 'dateTime', 'defer', 'dir', 'disabled', 'download',
22 'draggable', 'encType', 'form', 'formNoValidate', 'frameBorder', 'height', 'hidden', 'href', 'hrefLang', 'htmlFor', 'httpEquiv', 'icon', 'id', 'label', 'lang', 'list', 'loop', 'manifest',
23 'max', 'maxLength', 'media', 'mediaGroup', 'method', 'min', 'multiple', 'muted', 'name', 'noValidate', 'open', 'pattern', 'placeholder', 'poster', 'preload', 'radioGroup', 'readOnly', 'rel',
24 'required', 'role', 'rows', 'rowSpan', 'sandbox', 'scope', 'scrolling', 'seamless', 'selected', 'shape', 'size', 'sizes', 'span', 'spellCheck', 'src', 'srcDoc', 'srcSet', 'start', 'step',
25 'style', 'tabIndex', 'target', 'title', 'type', 'useMap', 'value', 'width', 'wmode'];
26const classNameProp = 'className';
27const attributesMapping = {'class': classNameProp, 'rt-class': classNameProp, 'for': 'htmlFor'}; //eslint-disable-line quote-props
28
29_.forEach(reactSupportedAttributes, attributeReactName => {
30 if (attributeReactName !== attributeReactName.toLowerCase()) {
31 attributesMapping[attributeReactName.toLowerCase()] = attributeReactName;
32 }
33});
34
35const htmlSelfClosingTags = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
36
37
38const templateAMDTemplate = _.template("define(<%= name ? '\"'+name + '\", ' : '' %>[<%= requirePaths %>], function (<%= requireNames %>) {\n'use strict';\n <%= injectedFunctions %>\nreturn function(<%= statelessProps %>){ return <%= body %>};\n});");
39const templateCommonJSTemplate = _.template("'use strict';\n<%= vars %>\n\n<%= injectedFunctions %>\nmodule.exports = function(<%= statelessProps %>){ return <%= body %>};\n");
40const templateES6Template = _.template('<%= vars %>\n\n<%= injectedFunctions %>\nexport default function(<%= statelessProps %>){ return <%= body %>}\n');
41const templatePJSTemplate = _.template(`var <%= name %> = function (<%= statelessProps %>) {
42<%= injectedFunctions %>
43return <%= body %>
44};
45`);
46const templateTypescriptTemplate = _.template('<%= vars %>\n\n<%= injectedFunctions %>\nvar fn = function() { return <%= body %> };\nexport = fn\n');
47const templateJSRTTemplate = _.template('(function () {\n <%= injectedFunctions %>\n return function(){\nreturn <%= body %>}}\n)()');
48
49const templates = {
50 amd: templateAMDTemplate,
51 commonjs: templateCommonJSTemplate,
52 typescript: templateTypescriptTemplate,
53 es6: templateES6Template,
54 none: templatePJSTemplate,
55 jsrt: templateJSRTTemplate
56};
57
58const isImportAsterisk = _.matches({member: '*'});
59const defaultCase = _.constant(true);
60
61const buildImportTypeScript = _.cond([
62 [isImportAsterisk, d => `import ${d.alias} = require('${d.moduleName}');`],
63 [defaultCase, d => `import ${d.alias} = require('${d.moduleName}').${d.member};`]
64]);
65
66const buildImportES6 = _.cond([
67 [isImportAsterisk, d => `import * as ${d.alias} from '${d.moduleName}';`],
68 [_.matches({member: 'default'}), d => `import ${d.alias} from '${d.moduleName}';`],
69 [defaultCase, d => `import { ${d.member} as ${d.alias} } from '${d.moduleName}';`]
70]);
71
72const buildImportCommonJS = _.cond([
73 [isImportAsterisk, d => `var ${d.alias} = require('${d.moduleName}');`],
74 [defaultCase, d => `var ${d.alias} = require('${d.moduleName}').${d.member};`]
75]);
76
77const buildImport = {
78 typescript: buildImportTypeScript,
79 es6: buildImportES6,
80 commonjs: buildImportCommonJS,
81 amd: buildImportCommonJS,
82 none: buildImportCommonJS,
83 jsrt: buildImportCommonJS
84};
85
86module.exports = {
87 htmlSelfClosingTags,
88 attributesMapping,
89 classNameProp,
90 shouldUseCreateElement,
91 templates,
92 buildImport
93};