1 | 'use strict';
|
2 | const _ = require('lodash');
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | function 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 |
|
20 | const 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'];
|
26 | const classNameProp = 'className';
|
27 | const attributesMapping = {'class': classNameProp, 'rt-class': classNameProp, 'for': 'htmlFor'};
|
28 |
|
29 | _.forEach(reactSupportedAttributes, attributeReactName => {
|
30 | if (attributeReactName !== attributeReactName.toLowerCase()) {
|
31 | attributesMapping[attributeReactName.toLowerCase()] = attributeReactName;
|
32 | }
|
33 | });
|
34 |
|
35 | const htmlSelfClosingTags = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
36 |
|
37 |
|
38 | const templateAMDTemplate = _.template("define(<%= name ? '\"'+name + '\", ' : '' %>[<%= requirePaths %>], function (<%= requireNames %>) {\n'use strict';\n <%= injectedFunctions %>\nreturn function(<%= statelessProps %>){ return <%= body %>};\n});");
|
39 | const templateCommonJSTemplate = _.template("'use strict';\n<%= vars %>\n\n<%= injectedFunctions %>\nmodule.exports = function(<%= statelessProps %>){ return <%= body %>};\n");
|
40 | const templateES6Template = _.template('<%= vars %>\n\n<%= injectedFunctions %>\nexport default function(<%= statelessProps %>){ return <%= body %>}\n');
|
41 | const templatePJSTemplate = _.template(`var <%= name %> = function (<%= statelessProps %>) {
|
42 | <%= injectedFunctions %>
|
43 | return <%= body %>
|
44 | };
|
45 | `);
|
46 | const templateTypescriptTemplate = _.template('<%= vars %>\n\n<%= injectedFunctions %>\nvar fn = function() { return <%= body %> };\nexport = fn\n');
|
47 | const templateJSRTTemplate = _.template('(function () {\n <%= injectedFunctions %>\n return function(){\nreturn <%= body %>}}\n)()');
|
48 |
|
49 | const templates = {
|
50 | amd: templateAMDTemplate,
|
51 | commonjs: templateCommonJSTemplate,
|
52 | typescript: templateTypescriptTemplate,
|
53 | es6: templateES6Template,
|
54 | none: templatePJSTemplate,
|
55 | jsrt: templateJSRTTemplate
|
56 | };
|
57 |
|
58 | const isImportAsterisk = _.matches({member: '*'});
|
59 | const defaultCase = _.constant(true);
|
60 |
|
61 | const buildImportTypeScript = _.cond([
|
62 | [isImportAsterisk, d => `import ${d.alias} = require('${d.moduleName}');`],
|
63 | [defaultCase, d => `import ${d.alias} = require('${d.moduleName}').${d.member};`]
|
64 | ]);
|
65 |
|
66 | const 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 |
|
72 | const buildImportCommonJS = _.cond([
|
73 | [isImportAsterisk, d => `var ${d.alias} = require('${d.moduleName}');`],
|
74 | [defaultCase, d => `var ${d.alias} = require('${d.moduleName}').${d.member};`]
|
75 | ]);
|
76 |
|
77 | const buildImport = {
|
78 | typescript: buildImportTypeScript,
|
79 | es6: buildImportES6,
|
80 | commonjs: buildImportCommonJS,
|
81 | amd: buildImportCommonJS,
|
82 | none: buildImportCommonJS,
|
83 | jsrt: buildImportCommonJS
|
84 | };
|
85 |
|
86 | module.exports = {
|
87 | htmlSelfClosingTags,
|
88 | attributesMapping,
|
89 | classNameProp,
|
90 | shouldUseCreateElement,
|
91 | templates,
|
92 | buildImport
|
93 | };
|