mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
support react 13,
rename string utils method, warn on rt-if without key
This commit is contained in:
parent
e4bbd93844
commit
307084e11d
@ -1,13 +1,17 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {{line: number, col: number}} Pos
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} html
|
* @param {string} html
|
||||||
* @param node
|
* @param node
|
||||||
* @return {{line: number, col: number}}}
|
* @return {Pos}
|
||||||
*/
|
*/
|
||||||
function getLine(html, node) {
|
function getLine(html, node) {
|
||||||
if (!node) {
|
if (!node) {
|
||||||
return {col: 1, line: 1};
|
return {line: 1, col: 1};
|
||||||
}
|
}
|
||||||
var linesUntil = html.substring(0, node.startIndex).split('\n');
|
var linesUntil = html.substring(0, node.startIndex).split('\n');
|
||||||
return {line: linesUntil.length, col: linesUntil[linesUntil.length - 1].length + 1};
|
return {line: linesUntil.length, col: linesUntil[linesUntil.length - 1].length + 1};
|
||||||
@ -80,6 +84,16 @@ RTCodeError.prototype.toIssue = function () {
|
|||||||
* @return {RTCodeError}
|
* @return {RTCodeError}
|
||||||
*/
|
*/
|
||||||
function buildError(msg, context, node) {
|
function buildError(msg, context, node) {
|
||||||
|
var loc = getNodeLoc(context, node);
|
||||||
|
return new RTCodeError(msg, loc.start, loc.end, loc.pos.line, loc.pos.col);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param context
|
||||||
|
* @param node
|
||||||
|
* @return {{pos:Pos, start:number, end:number}}
|
||||||
|
*/
|
||||||
|
function getNodeLoc(context, node) {
|
||||||
var pos = getLine(context.html, node);
|
var pos = getLine(context.html, node);
|
||||||
var end;
|
var end;
|
||||||
if (node.data) {
|
if (node.data) {
|
||||||
@ -89,9 +103,14 @@ function buildError(msg, context, node) {
|
|||||||
} else {
|
} else {
|
||||||
end = context.html.length;
|
end = context.html.length;
|
||||||
}
|
}
|
||||||
return new RTCodeError(msg, node.startIndex, end, pos.line, pos.col);
|
return {
|
||||||
|
pos: pos,
|
||||||
|
start: node.startIndex,
|
||||||
|
end: end
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
RTCodeError: RTCodeError
|
RTCodeError: RTCodeError,
|
||||||
|
getNodeLoc: getNodeLoc
|
||||||
};
|
};
|
||||||
|
@ -9,8 +9,8 @@ var convertJSRTToJS = reactTemplates.convertJSRTToJS;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} source
|
* @param {string} source
|
||||||
* @param {{modules:string, dryRun:boolean}?} options
|
|
||||||
* @param {string} target
|
* @param {string} target
|
||||||
|
* @param {{modules:string, dryRun:boolean}?} options
|
||||||
* @param {CONTEXT} context
|
* @param {CONTEXT} context
|
||||||
*/
|
*/
|
||||||
function convertFile(source, target, options, context) {
|
function convertFile(source, target, options, context) {
|
||||||
@ -19,6 +19,7 @@ function convertFile(source, target, options, context) {
|
|||||||
// return;// only handle html files
|
// return;// only handle html files
|
||||||
// }
|
// }
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
options.fileName = source;
|
||||||
var fsUtil = require('./fsUtil');
|
var fsUtil = require('./fsUtil');
|
||||||
|
|
||||||
if (!options.force && !fsUtil.isStale(source, target)) {
|
if (!options.force && !fsUtil.isStale(source, target)) {
|
||||||
@ -35,7 +36,7 @@ function convertFile(source, target, options, context) {
|
|||||||
if (options.modules === 'jsrt') {
|
if (options.modules === 'jsrt') {
|
||||||
js = convertJSRTToJS(html, options);
|
js = convertJSRTToJS(html, options);
|
||||||
} else {
|
} else {
|
||||||
js = convertTemplateToReact(html, options);
|
js = convertTemplateToReact(html, context, options);
|
||||||
}
|
}
|
||||||
if (!options.dryRun) {
|
if (!options.dryRun) {
|
||||||
fs.writeFileSync(target, js);
|
fs.writeFileSync(target, js);
|
||||||
|
@ -37,8 +37,8 @@ var context = {
|
|||||||
info: function (msg, file, line, column) {
|
info: function (msg, file, line, column) {
|
||||||
context.issue(MESSAGE_LEVEL.INFO, msg, file, line, column);
|
context.issue(MESSAGE_LEVEL.INFO, msg, file, line, column);
|
||||||
},
|
},
|
||||||
warn: function (msg, file, line, column) {
|
warn: function (msg, file, line, column, startOffset, endOffset) {
|
||||||
context.issue(MESSAGE_LEVEL.WARN, msg, file, line, column);
|
context.issue(MESSAGE_LEVEL.WARN, msg, file, line, column, startOffset, endOffset);
|
||||||
},
|
},
|
||||||
error: function (msg, file, line, column, startOffset, endOffset) {
|
error: function (msg, file, line, column, startOffset, endOffset) {
|
||||||
context.issue(MESSAGE_LEVEL.ERROR, msg, file, line, column, startOffset, endOffset);
|
context.issue(MESSAGE_LEVEL.ERROR, msg, file, line, column, startOffset, endOffset);
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Chalk.ChalkModule}
|
* @type {Chalk.ChalkModule}
|
||||||
*/
|
*/
|
||||||
var chalk = require('chalk');
|
var chalk = require('chalk');
|
||||||
|
var _ = require('lodash');
|
||||||
|
var table = require('text-table');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {MESSAGE} message
|
* @param {MESSAGE} message
|
||||||
@ -19,78 +20,153 @@ function getMessageType(message) {
|
|||||||
return chalk.cyan('info');
|
return chalk.cyan('info');
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (warnings/*, config*/) {
|
/**
|
||||||
var _ = require('lodash');
|
* Given a word and a count, append an s if count is not one.
|
||||||
var table = require('text-table');
|
* @param {string} word A word in its singular form.
|
||||||
//var verbosity = false;
|
* @param {int} count A number controlling whether word should be pluralized.
|
||||||
var UNICODE_HEAVY_MULTIPLICATION_X = '\u2716';
|
* @returns {string} The original word with an s on the end if count is not one.
|
||||||
|
*/
|
||||||
|
function pluralize(word, count) {
|
||||||
|
return (count === 1 ? word : word + 's');
|
||||||
|
}
|
||||||
|
|
||||||
function pluralize(n, single, plural) {
|
//function pluralize(n, single, plural) {
|
||||||
return n === 1 ? single : plural;
|
// return n === 1 ? single : plural;
|
||||||
}
|
//}
|
||||||
|
|
||||||
function lineText(line) {
|
function lineText(line) {
|
||||||
return line < 1 ? '' : line;
|
return line < 1 ? '' : line;
|
||||||
}
|
}
|
||||||
|
|
||||||
// context.report(JSON.stringify(warnings, undefined, 2));
|
//module.exports = function (warnings/*, config*/) {
|
||||||
var output = table(
|
// var _ = require('lodash');
|
||||||
warnings.map(function (message) {
|
// var table = require('text-table');
|
||||||
return [
|
// //var verbosity = false;
|
||||||
'',
|
// var UNICODE_HEAVY_MULTIPLICATION_X = '\u2716';
|
||||||
message.file || '',
|
//
|
||||||
lineText(message.line || 0),
|
// // context.report(JSON.stringify(warnings, undefined, 2));
|
||||||
lineText(message.column || 0),
|
// var output = table(
|
||||||
getMessageType(message),
|
// warnings.map(function (message) {
|
||||||
// message.message.replace(/\.$/, ""),
|
// return [
|
||||||
message.msg || ''
|
// '',
|
||||||
// chalk.gray(message.ruleId)
|
// message.file || '',
|
||||||
];
|
// lineText(message.line || 0),
|
||||||
}),
|
// lineText(message.column || 0),
|
||||||
{
|
// getMessageType(message),
|
||||||
align: ['', 'r', 'l'],
|
// // message.message.replace(/\.$/, ''),
|
||||||
stringLength: function (str) {
|
// message.msg || ''
|
||||||
return chalk.stripColor(str).length;
|
// // chalk.gray(message.ruleId)
|
||||||
}
|
// ];
|
||||||
}
|
// }),
|
||||||
//}
|
// {
|
||||||
);
|
// align: ['', 'r', 'l'],
|
||||||
|
// stringLength: function (str) {
|
||||||
var buf = [];
|
// return chalk.stripColor(str).length;
|
||||||
|
// }
|
||||||
buf.push(output);
|
// }
|
||||||
|
// //}
|
||||||
var grouped = _.groupBy(warnings, 'level');
|
// );
|
||||||
|
//
|
||||||
var errCount = grouped.ERROR ? grouped.ERROR.length : 0;
|
// var buf = [];
|
||||||
var warnCount = grouped.WARN ? grouped.WARN.length : 0;
|
//
|
||||||
//var infoCount = grouped.INFO ? grouped.INFO.length : 0;
|
// buf.push(output);
|
||||||
|
//
|
||||||
// buf.push(errCount + ' ' + warnCount + ' ' + infoCount + '\n');
|
// var grouped = _.groupBy(warnings, 'level');
|
||||||
|
//
|
||||||
if (errCount === 0 && warnCount === 0) {
|
// var errCount = grouped.ERROR ? grouped.ERROR.length : 0;
|
||||||
buf.push(chalk.green('React templates done'));
|
// var warnCount = grouped.WARN ? grouped.WARN.length : 0;
|
||||||
} else {
|
// //var infoCount = grouped.INFO ? grouped.INFO.length : 0;
|
||||||
var msg = [];
|
//
|
||||||
if (errCount > 0) {
|
//// buf.push(errCount + ' ' + warnCount + ' ' + infoCount + '\n');
|
||||||
msg.push(errCount + ' ' + pluralize(errCount, 'error', 'errors'));
|
//
|
||||||
} else {
|
// if (errCount === 0 && warnCount === 0) {
|
||||||
msg.push(warnCount + ' ' + pluralize(warnCount, 'warning', 'warnings'));
|
// buf.push(chalk.green('React templates done'));
|
||||||
}
|
|
||||||
buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + msg.join(', ')));
|
|
||||||
if (errCount > 0) {
|
|
||||||
buf.push(chalk.red('React templates failed due to errors'));
|
|
||||||
} else {
|
|
||||||
buf.push(chalk.yellow('React templates done with warnings'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// context.report(JSON.stringify(grouped, undefined, 2));
|
|
||||||
// if (grouped.ERROR && grouped.ERROR.length > 0) {
|
|
||||||
//// throw new Error(errorMessages.VERIFY_FAILED.format(grouped.ERROR.length, pluralize(grouped.ERROR.length, 'error', 'errors')));
|
|
||||||
// } else {
|
// } else {
|
||||||
// buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + warnings.length + ' ' + pluralize(warnings.length, 'problem', 'problems')) + '\n');
|
// var msg = [];
|
||||||
// buf.push('React templates done with warnings\n');
|
// if (errCount > 0) {
|
||||||
|
// msg.push(errCount + ' ' + pluralize(errCount, 'error', 'errors'));
|
||||||
|
// } else {
|
||||||
|
// msg.push(warnCount + ' ' + pluralize(warnCount, 'warning', 'warnings'));
|
||||||
|
// }
|
||||||
|
// buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + msg.join(', ')));
|
||||||
|
// if (errCount > 0) {
|
||||||
|
// buf.push(chalk.red('React templates failed due to errors'));
|
||||||
|
// } else {
|
||||||
|
// buf.push(chalk.yellow('React templates done with warnings'));
|
||||||
|
// }
|
||||||
// }
|
// }
|
||||||
return buf.join('\n');
|
//
|
||||||
|
//// context.report(JSON.stringify(grouped, undefined, 2));
|
||||||
|
//// if (grouped.ERROR && grouped.ERROR.length > 0) {
|
||||||
|
////// throw new Error(errorMessages.VERIFY_FAILED.format(grouped.ERROR.length, pluralize(grouped.ERROR.length, 'error', 'errors')));
|
||||||
|
//// } else {
|
||||||
|
//// buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + warnings.length + ' ' + pluralize(warnings.length, 'problem', 'problems')) + '\n');
|
||||||
|
//// buf.push('React templates done with warnings\n');
|
||||||
|
//// }
|
||||||
|
// return buf.join('\n');
|
||||||
|
//};
|
||||||
|
|
||||||
|
module.exports = function (results) {
|
||||||
|
results = _.groupBy(results, 'file');
|
||||||
|
|
||||||
|
var output = '\n',
|
||||||
|
total = 0,
|
||||||
|
errors = 0,
|
||||||
|
warnings = 0,
|
||||||
|
summaryColor = 'yellow';
|
||||||
|
|
||||||
|
_.each(results, function (result, k) {
|
||||||
|
var messages = result;
|
||||||
|
|
||||||
|
if (messages.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
total += messages.length;
|
||||||
|
output += chalk.underline(k) + '\n';
|
||||||
|
|
||||||
|
output += table(
|
||||||
|
messages.map(function (message) {
|
||||||
|
var messageType;
|
||||||
|
|
||||||
|
if (message.fatal || message.severity === 2) {
|
||||||
|
messageType = chalk.red('error');
|
||||||
|
summaryColor = 'red';
|
||||||
|
errors++;
|
||||||
|
} else {
|
||||||
|
messageType = chalk.yellow('warning');
|
||||||
|
warnings++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'',
|
||||||
|
message.line || 0,
|
||||||
|
message.column || 0,
|
||||||
|
messageType,
|
||||||
|
message.msg.replace(/\.$/, ''),
|
||||||
|
chalk.gray(message.ruleId || '')
|
||||||
|
];
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
align: ['', 'r', 'l'],
|
||||||
|
stringLength: function (str) {
|
||||||
|
return chalk.stripColor(str).length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).split('\n').map(function (el) {
|
||||||
|
return el.replace(/(\d+)\s+(\d+)/, function (m, p1, p2) {
|
||||||
|
return chalk.gray(p1 + ':' + p2);
|
||||||
|
});
|
||||||
|
}).join('\n') + '\n\n';
|
||||||
|
});
|
||||||
|
|
||||||
|
if (total > 0) {
|
||||||
|
output += chalk[summaryColor].bold([
|
||||||
|
'\u2716 ', total, pluralize(' problem', total),
|
||||||
|
' (', errors, pluralize(' error', errors), ', ',
|
||||||
|
warnings, pluralize(' warning', warnings), ')\n'
|
||||||
|
].join(''));
|
||||||
|
}
|
||||||
|
|
||||||
|
return total > 0 ? output : '';
|
||||||
};
|
};
|
@ -74,7 +74,7 @@ module.exports = optionator({
|
|||||||
option: 'target-version',
|
option: 'target-version',
|
||||||
alias: 't',
|
alias: 't',
|
||||||
type: 'String',
|
type: 'String',
|
||||||
default: '0.12.2',
|
default: '0.13.1',
|
||||||
description: 'React version to generate code for (' + Object.keys(reactDOMSupport).join(', ') + ')'
|
description: 'React version to generate code for (' + Object.keys(reactDOMSupport).join(', ') + ')'
|
||||||
}, {
|
}, {
|
||||||
option: 'list-target-version',
|
option: 'list-target-version',
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
* Created by avim on 12/4/2014.
|
* Created by avim on 12/4/2014.
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ver0_12_0 = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr', 'circle', 'defs', 'ellipse', 'g', 'line', 'linearGradient', 'mask', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient', 'rect', 'stop', 'svg', 'text', 'tspan'];
|
var ver0_12_0 = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr', 'circle', 'defs', 'ellipse', 'g', 'line', 'linearGradient', 'mask', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient', 'rect', 'stop', 'svg', 'text', 'tspan'];
|
||||||
var ver0_11_2 = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr', 'circle', 'defs', 'ellipse', 'g', 'line', 'linearGradient', 'mask', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient', 'rect', 'stop', 'svg', 'text', 'tspan', 'injection'];
|
var ver0_11_2 = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr', 'circle', 'defs', 'ellipse', 'g', 'line', 'linearGradient', 'mask', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient', 'rect', 'stop', 'svg', 'text', 'tspan', 'injection'];
|
||||||
var ver0_11_0 = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr', 'circle', 'defs', 'ellipse', 'g', 'line', 'linearGradient', 'mask', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient', 'rect', 'stop', 'svg', 'text', 'tspan', 'injection'];
|
var ver0_11_0 = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr', 'circle', 'defs', 'ellipse', 'g', 'line', 'linearGradient', 'mask', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient', 'rect', 'stop', 'svg', 'text', 'tspan', 'injection'];
|
||||||
@ -9,6 +10,7 @@ var ver0_10_0 = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b
|
|||||||
|
|
||||||
|
|
||||||
var versions = {
|
var versions = {
|
||||||
|
'0.13.1': ver0_12_0,
|
||||||
'0.12.2': ver0_12_0,
|
'0.12.2': ver0_12_0,
|
||||||
'0.12.1': ver0_12_0,
|
'0.12.1': ver0_12_0,
|
||||||
'0.12.0': ver0_12_0,
|
'0.12.0': ver0_12_0,
|
||||||
|
@ -15,8 +15,11 @@ var repeatTemplate = _.template('_.map(<%= collection %>,<%= repeatFunction %>.b
|
|||||||
var ifTemplate = _.template('((<%= condition %>)?(<%= body %>):null)');
|
var ifTemplate = _.template('((<%= condition %>)?(<%= body %>):null)');
|
||||||
var propsTemplateSimple = _.template('_.assign({}, <%= generatedProps %>, <%= rtProps %>)');
|
var propsTemplateSimple = _.template('_.assign({}, <%= generatedProps %>, <%= rtProps %>)');
|
||||||
var propsTemplate = _.template('mergeProps( <%= generatedProps %>, <%= rtProps %>)');
|
var propsTemplate = _.template('mergeProps( <%= generatedProps %>, <%= rtProps %>)');
|
||||||
var propsMergeFunction = 'function mergeProps(inline,external) {\n var res = _.assign({},inline,external)\nif (inline.hasOwnProperty(\'style\')) {\n res.style = _.defaults(res.style, inline.style);\n}\n if (inline.hasOwnProperty(\'className\') && external.hasOwnProperty(\'className\')) {\n res.className = external.className + \' \' + inline.className;\n} return res;\n}\n';
|
var propsMergeFunction = 'function mergeProps(inline,external) {\n var res = _.assign({},inline,external)\nif (inline.hasOwnProperty(\'style\')) {\n res.style = _.defaults(res.style, inline.style);\n}\n' +
|
||||||
var classSetTemplate = _.template('React.addons.classSet(<%= classSet %>)');
|
' if (inline.hasOwnProperty(\'className\') && external.hasOwnProperty(\'className\')) {\n' +
|
||||||
|
' res.className = external.className + \' \' + inline.className;\n} return res;\n}\n';
|
||||||
|
//var classSetTemplate = _.template('React.addons.classSet(<%= classSet %>)');
|
||||||
|
var classSetTemplate = _.template('_.keys(_.pick(<%= classSet %>, _.identity)).join(" ")');
|
||||||
var simpleTagTemplate = _.template('<%= name %>(<%= props %><%= children %>)');
|
var simpleTagTemplate = _.template('<%= name %>(<%= props %><%= children %>)');
|
||||||
var tagTemplate = _.template('<%= name %>.apply(this, [<%= props %><%= children %>])');
|
var tagTemplate = _.template('<%= name %>.apply(this, [<%= props %><%= children %>])');
|
||||||
var simpleTagTemplateCreateElement = _.template('React.createElement(<%= name %>,<%= props %><%= children %>)');
|
var simpleTagTemplateCreateElement = _.template('React.createElement(<%= name %>,<%= props %><%= children %>)');
|
||||||
@ -51,7 +54,7 @@ var classSetProp = 'rt-class';
|
|||||||
var scopeProp = 'rt-scope';
|
var scopeProp = 'rt-scope';
|
||||||
var propsProp = 'rt-props';
|
var propsProp = 'rt-props';
|
||||||
|
|
||||||
var defaultOptions = {modules: 'amd', version: false, force: false, format: 'stylish', targetVersion: '0.12.2'};
|
var defaultOptions = {modules: 'amd', version: false, force: false, format: 'stylish', targetVersion: '0.13.1'};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Context} context
|
* @param {Context} context
|
||||||
@ -69,7 +72,12 @@ function shouldUseCreateElement(context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var reactSupportedAttributes = ['accept', 'acceptCharset', 'accessKey', 'action', 'allowFullScreen', 'allowTransparency', 'alt', 'async', 'autoComplete', 'autoPlay', 'cellPadding', 'cellSpacing', 'charSet', 'checked', 'classID', 'className', 'cols', 'colSpan', 'content', 'contentEditable', 'contextMenu', 'controls', 'coords', 'crossOrigin', 'data', 'dateTime', 'defer', 'dir', 'disabled', 'download', 'draggable', 'encType', 'form', 'formNoValidate', 'frameBorder', 'height', 'hidden', 'href', 'hrefLang', 'htmlFor', 'httpEquiv', 'icon', 'id', 'label', 'lang', 'list', 'loop', 'manifest', 'max', 'maxLength', 'media', 'mediaGroup', 'method', 'min', 'multiple', 'muted', 'name', 'noValidate', 'open', 'pattern', 'placeholder', 'poster', 'preload', 'radioGroup', 'readOnly', 'rel', 'required', 'role', 'rows', 'rowSpan', 'sandbox', 'scope', 'scrolling', 'seamless', 'selected', 'shape', 'size', 'sizes', 'span', 'spellCheck', 'src', 'srcDoc', 'srcSet', 'start', 'step', 'style', 'tabIndex', 'target', 'title', 'type', 'useMap', 'value', 'width', 'wmode'];
|
var reactSupportedAttributes = ['accept', 'acceptCharset', 'accessKey', 'action', 'allowFullScreen', 'allowTransparency', 'alt', 'async', 'autoComplete', 'autoPlay', 'cellPadding', 'cellSpacing', 'charSet', 'checked',
|
||||||
|
'classID', 'className', 'cols', 'colSpan', 'content', 'contentEditable', 'contextMenu', 'controls', 'coords', 'crossOrigin', 'data', 'dateTime', 'defer', 'dir', 'disabled', 'download',
|
||||||
|
'draggable', 'encType', 'form', 'formNoValidate', 'frameBorder', 'height', 'hidden', 'href', 'hrefLang', 'htmlFor', 'httpEquiv', 'icon', 'id', 'label', 'lang', 'list', 'loop', 'manifest',
|
||||||
|
'max', 'maxLength', 'media', 'mediaGroup', 'method', 'min', 'multiple', 'muted', 'name', 'noValidate', 'open', 'pattern', 'placeholder', 'poster', 'preload', 'radioGroup', 'readOnly', 'rel',
|
||||||
|
'required', 'role', 'rows', 'rowSpan', 'sandbox', 'scope', 'scrolling', 'seamless', 'selected', 'shape', 'size', 'sizes', 'span', 'spellCheck', 'src', 'srcDoc', 'srcSet', 'start', 'step',
|
||||||
|
'style', 'tabIndex', 'target', 'title', 'type', 'useMap', 'value', 'width', 'wmode'];
|
||||||
var attributesMapping = {'class': 'className', 'rt-class': 'className'};
|
var attributesMapping = {'class': 'className', 'rt-class': 'className'};
|
||||||
_.forEach(reactSupportedAttributes, function (attributeReactName) {
|
_.forEach(reactSupportedAttributes, function (attributeReactName) {
|
||||||
if (attributeReactName !== attributeReactName.toLowerCase()) {
|
if (attributeReactName !== attributeReactName.toLowerCase()) {
|
||||||
@ -291,7 +299,7 @@ function convertHtmlToReact(node, context) {
|
|||||||
}
|
}
|
||||||
var scopeName = scopeSubParts[1].trim();
|
var scopeName = scopeSubParts[1].trim();
|
||||||
validateJS(scopeName, node, context);
|
validateJS(scopeName, node, context);
|
||||||
stringUtils.addIfNotThere(context.boundParams, scopeName);
|
stringUtils.addIfMissing(context.boundParams, scopeName);
|
||||||
data.scopeName += stringUtils.capitalize(scopeName);
|
data.scopeName += stringUtils.capitalize(scopeName);
|
||||||
data.scopeMapping[scopeName] = scopeSubParts[0].trim();
|
data.scopeMapping[scopeName] = scopeSubParts[0].trim();
|
||||||
validateJS(data.scopeMapping[scopeName], node, context);
|
validateJS(data.scopeMapping[scopeName], node, context);
|
||||||
@ -307,8 +315,8 @@ function convertHtmlToReact(node, context) {
|
|||||||
data.collection = arr[1].trim();
|
data.collection = arr[1].trim();
|
||||||
validateJS(data.item, node, context);
|
validateJS(data.item, node, context);
|
||||||
validateJS(data.collection, node, context);
|
validateJS(data.collection, node, context);
|
||||||
stringUtils.addIfNotThere(context.boundParams, data.item);
|
stringUtils.addIfMissing(context.boundParams, data.item);
|
||||||
stringUtils.addIfNotThere(context.boundParams, data.item + 'Index');
|
stringUtils.addIfMissing(context.boundParams, data.item + 'Index');
|
||||||
}
|
}
|
||||||
data.props = generateProps(node, context);
|
data.props = generateProps(node, context);
|
||||||
if (node.attribs[propsProp]) {
|
if (node.attribs[propsProp]) {
|
||||||
@ -377,10 +385,15 @@ function isTag(node) {
|
|||||||
return node.type === 'tag';
|
return node.type === 'tag';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//function isEmptyText(node) {
|
||||||
|
// return node.type === 'text' && /^\s*$/g.test(node.data);
|
||||||
|
//}
|
||||||
|
|
||||||
function handleSelfClosingHtmlTags(nodes) {
|
function handleSelfClosingHtmlTags(nodes) {
|
||||||
return _(nodes)
|
return _(nodes)
|
||||||
.map(function (node) {
|
.map(function (node) {
|
||||||
var externalNodes = [];
|
var externalNodes = [];
|
||||||
|
//node.children = _.reject(node.children, isEmptyText);
|
||||||
node.children = handleSelfClosingHtmlTags(node.children);
|
node.children = handleSelfClosingHtmlTags(node.children);
|
||||||
if (node.type === 'tag' && _.contains(htmlSelfClosingTags, node.name)) {
|
if (node.type === 'tag' && _.contains(htmlSelfClosingTags, node.name)) {
|
||||||
externalNodes = _.filter(node.children, isTag);
|
externalNodes = _.filter(node.children, isTag);
|
||||||
@ -395,16 +408,34 @@ function handleSelfClosingHtmlTags(nodes) {
|
|||||||
.value();
|
.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param options
|
||||||
|
* @param {*} context
|
||||||
|
* @param {CONTEXT} reportContext
|
||||||
|
* @param node
|
||||||
|
*/
|
||||||
|
function validate(options, context, reportContext, node) {
|
||||||
|
if (node.type === 'tag' && node.attribs['rt-if'] && !node.attribs.key) {
|
||||||
|
var loc = rtError.getNodeLoc(context, node);
|
||||||
|
reportContext.warn('rt-if without a key', options.fileName, loc.pos.line, loc.pos.col, loc.start, loc.end);
|
||||||
|
}
|
||||||
|
if (node.children) {
|
||||||
|
node.children.forEach(validate.bind(this, options, context, reportContext));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} html
|
* @param {string} html
|
||||||
|
* @param {CONTEXT} reportContext
|
||||||
* @param {{modules:string,defines:*}?} options
|
* @param {{modules:string,defines:*}?} options
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
function convertTemplateToReact(html, options) {
|
function convertTemplateToReact(html, reportContext, options) {
|
||||||
var rootNode = cheerio.load(html, {lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: true, withStartIndices: true});
|
var rootNode = cheerio.load(html, {lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: true, withStartIndices: true});
|
||||||
options = _.defaults({}, options, defaultOptions);
|
options = _.defaults({}, options, defaultOptions);
|
||||||
var defines = options.defines ? options.defines : {'react/addons': 'React', lodash: '_'};
|
var defines = options.defines ? options.defines : {'react/addons': 'React', lodash: '_'};
|
||||||
var context = defaultContext(html, options);
|
var context = defaultContext(html, options);
|
||||||
|
validate(options, context, reportContext, rootNode.root()[0]);
|
||||||
var rootTags = _.filter(rootNode.root()[0].children, {type: 'tag'});
|
var rootTags = _.filter(rootNode.root()[0].children, {type: 'tag'});
|
||||||
rootTags = handleSelfClosingHtmlTags(rootTags);
|
rootTags = handleSelfClosingHtmlTags(rootTags);
|
||||||
if (!rootTags || rootTags.length === 0) {
|
if (!rootTags || rootTags.length === 0) {
|
||||||
@ -452,7 +483,7 @@ function convertTemplateToReact(html, options) {
|
|||||||
tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
|
tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
|
||||||
code = escodegen.generate(tree, {comment: true});
|
code = escodegen.generate(tree, {comment: true});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(code);
|
//console.log(code);
|
||||||
throw new RTCodeError(e.message, e.index, -1);
|
throw new RTCodeError(e.message, e.index, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ function capitalize(str) {
|
|||||||
* @param {Array.<*>} array
|
* @param {Array.<*>} array
|
||||||
* @param {*} obj
|
* @param {*} obj
|
||||||
*/
|
*/
|
||||||
function addIfNotThere(array, obj) {
|
function addIfMissing(array, obj) {
|
||||||
if (!_.contains(array, obj)) {
|
if (!_.contains(array, obj)) {
|
||||||
array.push(obj);
|
array.push(obj);
|
||||||
}
|
}
|
||||||
@ -30,5 +30,5 @@ function addIfNotThere(array, obj) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
convertToCamelCase: convertToCamelCase,
|
convertToCamelCase: convertToCamelCase,
|
||||||
capitalize: capitalize,
|
capitalize: capitalize,
|
||||||
addIfNotThere: addIfNotThere
|
addIfMissing: addIfMissing
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user