diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 3a76982..522e377 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -6,7 +6,6 @@ const escodegen = require('escodegen'); const reactDOMSupport = require('./reactDOMSupport'); const reactNativeSupport = require('./reactNativeSupport'); const reactPropTemplates = require('./reactPropTemplates'); -const stringUtils = require('./stringUtils'); const rtError = require('./RTCodeError'); const reactSupport = require('./reactSupport'); const templates = reactSupport.templates; @@ -65,19 +64,18 @@ const reactTemplatesSelfClosingTags = [includeNode]; function getOptions(options) { options = options || {}; const defaultOptions = { - modules: options.native ? 'commonjs' : 'amd', version: false, force: false, format: 'stylish', targetVersion: reactDOMSupport.default, - reactImportPath: reactImport(options), lodashImportPath: 'lodash', native: false, - nativeTargetVersion: reactNativeSupport.default, - flow: options.flow + nativeTargetVersion: reactNativeSupport.default }; const finalOptions = _.defaults({}, options, defaultOptions); + finalOptions.reactImportPath = finalOptions.reactImportPath || reactImport(finalOptions); + finalOptions.modules = finalOptions.modules || (finalOptions.native ? 'commonjs' : 'amd'); const defaultPropTemplates = finalOptions.native ? reactPropTemplates.native[finalOptions.nativeTargetVersion] : @@ -321,8 +319,11 @@ function convertHtmlToReact(node, context) { data.collection = arr[1].trim(); validateJS(data.item, node, context); validateJS(`(${data.collection})`, node, context); - stringUtils.addIfMissing(context.boundParams, data.item); - stringUtils.addIfMissing(context.boundParams, `${data.item}Index`); + [data.item, `${data.item}Index`].forEach(param => { + if (!_.includes(context.boundParams, param)) { + context.boundParams.push(param); + } + }); } if (node.attribs[scopeAttr]) { @@ -383,7 +384,7 @@ function convertHtmlToReact(node, context) { // Order matters here. Each rt-repeat iteration wraps over the rt-scope, so // the scope variables are evaluated in context of the current iteration. if (node.attribs[repeatAttr]) { - data.repeatFunction = generateInjectedFunc(context, 'repeat' + stringUtils.capitalize(data.item), 'return ' + data.body); + data.repeatFunction = generateInjectedFunc(context, 'repeat' + _.upperFirst(data.item), 'return ' + data.body); data.repeatBinds = ['this'].concat(_.reject(context.boundParams, p => p === data.item || p === data.item + 'Index' || data.innerScope && p in data.innerScope.innerMapping)); data.body = repeatTemplate(data); } @@ -419,9 +420,11 @@ function handleScopeAttribute(node, context, data) { // this adds both parameters to the list of parameters passed further down // the scope chain, as well as variables that are locally bound before any // function call, as with the ones we generate for rt-scope. - stringUtils.addIfMissing(context.boundParams, alias); + if (!_.includes(context.boundParams, alias)) { + context.boundParams.push(alias); + } - data.innerScope.scopeName += stringUtils.capitalize(alias); + data.innerScope.scopeName += _.upperFirst(alias); data.innerScope.innerMapping[alias] = `var ${alias} = ${value};`; validateJS(data.innerScope.innerMapping[alias], node, context); }); @@ -445,23 +448,15 @@ function validateIfAttribute(node, context, data) { } } -/** - * @param node - * @return {boolean} - */ -function isTag(node) { - return node.type === 'tag'; -} - function handleSelfClosingHtmlTags(nodes) { return _.flatMap(nodes, node => { let externalNodes = []; node.children = handleSelfClosingHtmlTags(node.children); if (node.type === 'tag' && (_.includes(reactSupport.htmlSelfClosingTags, node.name) || _.includes(reactTemplatesSelfClosingTags, node.name))) { - externalNodes = _.filter(node.children, isTag); + externalNodes = _.filter(node.children, {type: 'tag'}); _.forEach(externalNodes, i => {i.parent = node;}); - node.children = _.reject(node.children, isTag); + node.children = _.reject(node.children, {type: 'tag'}); } return [node].concat(externalNodes); }); @@ -522,7 +517,7 @@ function parseAndConvertHtmlToReact(html, context) { withStartIndices: true }); utils.validate(context.options, context, context.reportContext, rootNode.root()[0]); - let rootTags = _.filter(rootNode.root()[0].children, isTag); + let rootTags = _.filter(rootNode.root()[0].children, {type: 'tag'}); rootTags = handleSelfClosingHtmlTags(rootTags); if (!rootTags || rootTags.length === 0) { throw new RTCodeError('Document should have a root element'); @@ -573,7 +568,7 @@ function convertRT(html, reportContext, options) { vars, name: options.name }; - let code = generate(data, options); + let code = templates[options.modules](data); if (options.modules !== 'typescript' && options.modules !== 'jsrt') { code = parseJS(code); } @@ -599,11 +594,6 @@ function convertJSRTToJS(text, reportContext, options) { return parseJS(code); } -function generate(data, options) { - const template = templates[options.modules]; - return template(data); -} - module.exports = { convertTemplateToReact, convertRT, diff --git a/src/stringUtils.js b/src/stringUtils.js deleted file mode 100644 index 3d1b933..0000000 --- a/src/stringUtils.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; -const _ = require('lodash'); - -/** - * @param {Array.<*>} array - * @param {*} obj - */ -function addIfMissing(array, obj) { - if (!_.includes(array, obj)) { - array.push(obj); - } -} - -/** - * @param {string} str - * @return {string} - */ -function capitalize(str) { - return str[0].toUpperCase() + str.slice(1); -} - -module.exports = {addIfMissing, capitalize}; diff --git a/test/data/div.rt.amd.js b/test/data/div.rt.amd.js index 56ca4a4..076b2c2 100644 --- a/test/data/div.rt.amd.js +++ b/test/data/div.rt.amd.js @@ -1,9 +1,9 @@ define('div', [ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; return function () { return React.createElement('div', {}); }; -}); \ No newline at end of file +}); diff --git a/test/data/div.rt.commonjs.js b/test/data/div.rt.commonjs.js index d40bb9b..f822228 100644 --- a/test/data/div.rt.commonjs.js +++ b/test/data/div.rt.commonjs.js @@ -1,6 +1,6 @@ 'use strict'; -var React = require('react/addons'); +var React = require('react'); var _ = require('lodash'); module.exports = function () { return React.createElement('div', {}); -}; \ No newline at end of file +}; diff --git a/test/data/div.rt.es6.js b/test/data/div.rt.es6.js index cfe6110..8a6d4ea 100644 --- a/test/data/div.rt.es6.js +++ b/test/data/div.rt.es6.js @@ -1,5 +1,5 @@ -import * as React from 'react/addons'; +import * as React from 'react'; import * as _ from 'lodash'; export default function () { return React.createElement('div', {}); -} \ No newline at end of file +} diff --git a/test/data/div.rt.js b/test/data/div.rt.js index 96ccfe6..8a94252 100644 --- a/test/data/div.rt.js +++ b/test/data/div.rt.js @@ -1,9 +1,9 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; return function () { return React.createElement('div', {}); }; -}); \ No newline at end of file +}); diff --git a/test/data/div.rt.typescript.ts b/test/data/div.rt.typescript.ts index e370605..0a81cf2 100644 --- a/test/data/div.rt.typescript.ts +++ b/test/data/div.rt.typescript.ts @@ -1,4 +1,4 @@ -import React = require('react/addons'); +import React = require('react'); import _ = require('lodash'); diff --git a/test/data/if-with-scope/valid-if-scope.rt.js b/test/data/if-with-scope/valid-if-scope.rt.js index 00a7ffb..f4fa052 100644 --- a/test/data/if-with-scope/valid-if-scope.rt.js +++ b/test/data/if-with-scope/valid-if-scope.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; diff --git a/test/data/import.rt.amd.js b/test/data/import.rt.amd.js index 9760959..c049d4a 100644 --- a/test/data/import.rt.amd.js +++ b/test/data/import.rt.amd.js @@ -1,5 +1,5 @@ define('div', [ - 'react/addons', + 'react', 'lodash', 'module-name', 'module-name', @@ -10,4 +10,4 @@ define('div', [ return function () { return React.createElement('div', {}); }; -}); \ No newline at end of file +}); diff --git a/test/data/import.rt.commonjs.js b/test/data/import.rt.commonjs.js index 471f833..22fcf63 100644 --- a/test/data/import.rt.commonjs.js +++ b/test/data/import.rt.commonjs.js @@ -1,5 +1,5 @@ 'use strict'; -var React = require('react/addons'); +var React = require('react'); var _ = require('lodash'); var member = require('module-name').member; var alias2 = require('module-name').member; @@ -7,4 +7,4 @@ var alias3 = require('module-name'); var alias4 = require('module-name').default; module.exports = function () { return React.createElement('div', {}); -}; \ No newline at end of file +}; diff --git a/test/data/import.rt.es6.js b/test/data/import.rt.es6.js index 668c137..30586d0 100644 --- a/test/data/import.rt.es6.js +++ b/test/data/import.rt.es6.js @@ -1,4 +1,4 @@ -import * as React from 'react/addons'; +import * as React from 'react'; import * as _ from 'lodash'; import { member } from 'module-name'; import { member as alias2 } from 'module-name'; @@ -6,4 +6,4 @@ import * as alias3 from 'module-name'; import alias4 from 'module-name'; export default function () { return React.createElement('div', {}); -} \ No newline at end of file +} diff --git a/test/data/import.rt.js b/test/data/import.rt.js index b1564de..2d54429 100644 --- a/test/data/import.rt.js +++ b/test/data/import.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash', 'comps/myComp', 'utils/utils' @@ -8,4 +8,4 @@ define([ return function () { return React.createElement(myComp, {}, '\n', utils.translate('Hello', 'es'), '\n'); }; -}); \ No newline at end of file +}); diff --git a/test/data/import.rt.typescript.ts b/test/data/import.rt.typescript.ts index f2f0662..68bb063 100644 --- a/test/data/import.rt.typescript.ts +++ b/test/data/import.rt.typescript.ts @@ -1,4 +1,4 @@ -import React = require('react/addons'); +import React = require('react'); import _ = require('lodash'); import member = require('module-name').member; import alias2 = require('module-name').member; @@ -7,4 +7,4 @@ import alias4 = require('module-name').default; var fn = function() { return React.createElement('div',{}) }; -export = fn \ No newline at end of file +export = fn diff --git a/test/data/inputs.rt.js b/test/data/inputs.rt.js index e49380f..67548d4 100644 --- a/test/data/inputs.rt.js +++ b/test/data/inputs.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; diff --git a/test/data/propTemplates/implicitTemplate.rt.js b/test/data/propTemplates/implicitTemplate.rt.js index 3703a66..1d32a9e 100644 --- a/test/data/propTemplates/implicitTemplate.rt.js +++ b/test/data/propTemplates/implicitTemplate.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; @@ -16,4 +16,4 @@ define([ 'renderRow': renderRow1.bind(this) })); }; -}); \ No newline at end of file +}); diff --git a/test/data/propTemplates/simpleTemplate.rt.js b/test/data/propTemplates/simpleTemplate.rt.js index 387ba75..2299438 100644 --- a/test/data/propTemplates/simpleTemplate.rt.js +++ b/test/data/propTemplates/simpleTemplate.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; @@ -9,4 +9,4 @@ define([ return function () { return React.createElement('div', { 'templateProp': templateProp1.bind(this) }); }; -}); \ No newline at end of file +}); diff --git a/test/data/propTemplates/templateInScope.rt.js b/test/data/propTemplates/templateInScope.rt.js index 7812efe..5474f7c 100644 --- a/test/data/propTemplates/templateInScope.rt.js +++ b/test/data/propTemplates/templateInScope.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; @@ -13,4 +13,4 @@ define([ return function () { return scopeName2.apply(this, []); }; -}); \ No newline at end of file +}); diff --git a/test/data/propTemplates/twoTemplates.rt.js b/test/data/propTemplates/twoTemplates.rt.js index b96cfad..6c2a457 100644 --- a/test/data/propTemplates/twoTemplates.rt.js +++ b/test/data/propTemplates/twoTemplates.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; @@ -12,4 +12,4 @@ define([ return function () { return React.createElement('div', { 'templateProp': templateProp2.bind(this) }); }; -}); \ No newline at end of file +}); diff --git a/test/data/repeat.rt.js b/test/data/repeat.rt.js index 1d2d336..5ed9875 100644 --- a/test/data/repeat.rt.js +++ b/test/data/repeat.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; @@ -28,4 +28,4 @@ define([ _.map(this.props.things, repeatItems3.bind(this)) ]); }; -}); \ No newline at end of file +}); diff --git a/test/data/require.rt.amd.js b/test/data/require.rt.amd.js index eb7d876..af73988 100644 --- a/test/data/require.rt.amd.js +++ b/test/data/require.rt.amd.js @@ -1,5 +1,5 @@ define('div', [ - 'react/addons', + 'react', 'lodash', 'comps/myComp', 'utils/utils' @@ -8,4 +8,4 @@ define('div', [ return function () { return React.createElement(myComp, {}, '\n', utils.translate('Hello', 'es'), '\n'); }; -}); \ No newline at end of file +}); diff --git a/test/data/require.rt.commonjs.js b/test/data/require.rt.commonjs.js index e1a499a..ce218ad 100644 --- a/test/data/require.rt.commonjs.js +++ b/test/data/require.rt.commonjs.js @@ -1,8 +1,8 @@ 'use strict'; -var React = require('react/addons'); +var React = require('react'); var _ = require('lodash'); var myComp = require('comps/myComp'); var utils = require('utils/utils'); module.exports = function () { return React.createElement(myComp, {}, '\n', utils.translate('Hello', 'es'), '\n'); -}; \ No newline at end of file +}; diff --git a/test/data/require.rt.es6.js b/test/data/require.rt.es6.js index 1e05ae0..1a65da1 100644 --- a/test/data/require.rt.es6.js +++ b/test/data/require.rt.es6.js @@ -1,7 +1,7 @@ -import * as React from 'react/addons'; +import * as React from 'react'; import * as _ from 'lodash'; import * as myComp from 'comps/myComp'; import * as utils from 'utils/utils'; export default function () { return React.createElement(myComp, {}, '\n', utils.translate('Hello', 'es'), '\n'); -} \ No newline at end of file +} diff --git a/test/data/require.rt.js b/test/data/require.rt.js index b1564de..2d54429 100644 --- a/test/data/require.rt.js +++ b/test/data/require.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash', 'comps/myComp', 'utils/utils' @@ -8,4 +8,4 @@ define([ return function () { return React.createElement(myComp, {}, '\n', utils.translate('Hello', 'es'), '\n'); }; -}); \ No newline at end of file +}); diff --git a/test/data/require.rt.typescript.ts b/test/data/require.rt.typescript.ts index a0cc558..ade57b0 100644 --- a/test/data/require.rt.typescript.ts +++ b/test/data/require.rt.typescript.ts @@ -1,8 +1,8 @@ -import React = require('react/addons'); +import React = require('react'); import _ = require('lodash'); import myComp = require('comps/myComp'); import utils = require('utils/utils'); var fn = function() { return React.createElement(myComp,{},"\n",(utils.translate('Hello','es')),"\n") }; -export = fn \ No newline at end of file +export = fn diff --git a/test/data/test.rt.js b/test/data/test.rt.js index 891d8ec..2c7dedd 100644 --- a/test/data/test.rt.js +++ b/test/data/test.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; @@ -27,4 +27,4 @@ define([ } }))), React.createElement('div', {}, 'editor\n ', !this.props.editorState.previewMode ? React.createElement('div', { 'key': '440' }, 'left bar') : null)); }; -}); \ No newline at end of file +}); diff --git a/test/data/virtual.rt.js b/test/data/virtual.rt.js index 3cf3122..8ddb654 100644 --- a/test/data/virtual.rt.js +++ b/test/data/virtual.rt.js @@ -1,5 +1,5 @@ define([ - 'react/addons', + 'react', 'lodash' ], function (React, _) { 'use strict'; @@ -23,4 +23,4 @@ define([ return function () { return React.createElement('div', {}, scopeVerb2.apply(this, [])); }; -}); \ No newline at end of file +}); diff --git a/test/src/testUtils.js b/test/src/testUtils.js index 1eb3e47..e85ff52 100644 --- a/test/src/testUtils.js +++ b/test/src/testUtils.js @@ -62,7 +62,7 @@ function rtToHtml(rt) { } function codeToHtml(code) { - const defineMap = {'react/addons': React, lodash: _}; + const defineMap = {react: React, lodash: _}; //noinspection JSUnusedLocalSymbols const define = function (requirementsNames, content) { //eslint-disable-line no-unused-vars,func-style const requirements = _.map(requirementsNames, reqName => defineMap[reqName]);