mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
make default options be contingent on actual other options
This commit is contained in:
parent
7a117400be
commit
63be2c9968
@ -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,
|
||||
|
@ -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};
|
@ -1,9 +1,9 @@
|
||||
define('div', [
|
||||
'react/addons',
|
||||
'react',
|
||||
'lodash'
|
||||
], function (React, _) {
|
||||
'use strict';
|
||||
return function () {
|
||||
return React.createElement('div', {});
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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', {});
|
||||
};
|
||||
};
|
||||
|
@ -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', {});
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
define([
|
||||
'react/addons',
|
||||
'react',
|
||||
'lodash'
|
||||
], function (React, _) {
|
||||
'use strict';
|
||||
return function () {
|
||||
return React.createElement('div', {});
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import React = require('react/addons');
|
||||
import React = require('react');
|
||||
import _ = require('lodash');
|
||||
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
define([
|
||||
'react/addons',
|
||||
'react',
|
||||
'lodash'
|
||||
], function (React, _) {
|
||||
'use strict';
|
||||
|
@ -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', {});
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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', {});
|
||||
};
|
||||
};
|
||||
|
@ -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', {});
|
||||
}
|
||||
}
|
||||
|
@ -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');
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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
|
||||
export = fn
|
||||
|
@ -1,5 +1,5 @@
|
||||
define([
|
||||
'react/addons',
|
||||
'react',
|
||||
'lodash'
|
||||
], function (React, _) {
|
||||
'use strict';
|
||||
|
@ -1,5 +1,5 @@
|
||||
define([
|
||||
'react/addons',
|
||||
'react',
|
||||
'lodash'
|
||||
], function (React, _) {
|
||||
'use strict';
|
||||
@ -16,4 +16,4 @@ define([
|
||||
'renderRow': renderRow1.bind(this)
|
||||
}));
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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) });
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
define([
|
||||
'react/addons',
|
||||
'react',
|
||||
'lodash'
|
||||
], function (React, _) {
|
||||
'use strict';
|
||||
@ -13,4 +13,4 @@ define([
|
||||
return function () {
|
||||
return scopeName2.apply(this, []);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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) });
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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))
|
||||
]);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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');
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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');
|
||||
};
|
||||
};
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
@ -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');
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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
|
||||
export = fn
|
||||
|
@ -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));
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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, []));
|
||||
};
|
||||
});
|
||||
});
|
||||
|
@ -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]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user