1
0
mirror of https://github.com/bobwen-dev/react-templates synced 2025-04-12 00:56:39 +02:00

enable rule prefer-arrow-callback and fix errors

This commit is contained in:
Omer Ganim 2016-04-27 14:05:42 +03:00
parent 5b664b9904
commit 4bbbfec3e8
10 changed files with 31 additions and 121 deletions

View File

@ -3,7 +3,6 @@
"plugins": ["lodash", "wix-editor"], "plugins": ["lodash", "wix-editor"],
"rules": { "rules": {
"semi": [2, "always"], "semi": [2, "always"],
"prefer-arrow-callback": 0,
"prefer-spread": 0, "prefer-spread": 0,
"prefer-template": 0, "prefer-template": 0,
"consistent-return": 0, "consistent-return": 0,

View File

@ -105,7 +105,7 @@ module.exports = function (grunt) {
grunt.registerTask('lint', ['eslint:all']); grunt.registerTask('lint', ['eslint:all']);
grunt.registerTask('test', ['tape']); grunt.registerTask('test', ['tape']);
grunt.registerTask('rt', function () { grunt.registerTask('rt', () => {
const reactTemplates = require('./src/cli'); const reactTemplates = require('./src/cli');
const files = grunt.file.expand('playground/*.rt'); const files = grunt.file.expand('playground/*.rt');
const ret = reactTemplates.execute({modules: 'amd', force: true, _: files}); const ret = reactTemplates.execute({modules: 'amd', force: true, _: files});

View File

@ -2,7 +2,8 @@
"rules": { "rules": {
"strict": [2, "function"], "strict": [2, "function"],
"no-var": 0, "no-var": 0,
"object-shorthand": 0 "object-shorthand": 0,
"prefer-arrow-callback": 0
}, },
"env": { "env": {
"browser": true, "browser": true,

View File

@ -2,7 +2,8 @@
"rules": { "rules": {
"strict": [2, "function"], "strict": [2, "function"],
"no-var": 0, "no-var": 0,
"object-shorthand": 0 "object-shorthand": 0,
"prefer-arrow-callback": 0
}, },
"env": { "env": {
"browser": true, "browser": true,

View File

@ -6,20 +6,6 @@ const chalk = require('chalk');
const _ = require('lodash'); const _ = require('lodash');
const table = require('text-table'); const table = require('text-table');
///**
// * @param {MESSAGE} message
// * @return {string}
// */
//function getMessageType(message) {
// if (message.level === 'WARN') {
// return chalk.yellow('warning');
// }
// if (message.level === 'ERROR') {
// return chalk.red('error');
// }
// return chalk.cyan('info');
//}
/** /**
* Given a word and a count, append an s if count is not one. * Given a word and a count, append an s if count is not one.
* @param {string} word A word in its singular form. * @param {string} word A word in its singular form.
@ -29,11 +15,6 @@ const table = require('text-table');
function pluralize(word, count) { function pluralize(word, count) {
return count === 1 ? word : word + 's'; return count === 1 ? word : word + 's';
} }
//function pluralize(n, single, plural) {
// return n === 1 ? single : plural;
//}
/** /**
* @param {number} line * @param {number} line
* @return {string} * @return {string}
@ -42,74 +23,6 @@ function lineText(line) {
return line < 1 ? '' : line; return line < 1 ? '' : line;
} }
//module.exports = function (warnings/*, config*/) {
// const _ = require('lodash');
// const table = require('text-table');
// //const verbosity = false;
// const UNICODE_HEAVY_MULTIPLICATION_X = '\u2716';
//
// // context.report(JSON.stringify(warnings, undefined, 2));
// const output = table(
// warnings.map(function (message) {
// return [
// '',
// message.file || '',
// lineText(message.line || 0),
// lineText(message.column || 0),
// getMessageType(message),
// // message.message.replace(/\.$/, ''),
// message.msg || ''
// // chalk.gray(message.ruleId)
// ];
// }),
// {
// align: ['', 'r', 'l'],
// stringLength: function (str) {
// return chalk.stripColor(str).length;
// }
// }
// //}
// );
//
// const buf = [];
//
// buf.push(output);
//
// const grouped = _.groupBy(warnings, 'level');
//
// const errCount = grouped.ERROR ? grouped.ERROR.length : 0;
// const warnCount = grouped.WARN ? grouped.WARN.length : 0;
// //const infoCount = grouped.INFO ? grouped.INFO.length : 0;
//
//// buf.push(errCount + ' ' + warnCount + ' ' + infoCount + '\n');
//
// if (errCount === 0 && warnCount === 0) {
// buf.push(chalk.green('React templates done'));
// } else {
// const msg = [];
// 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'));
// }
// }
//
//// 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) { module.exports = function (results) {
results = _.groupBy(results, 'file'); results = _.groupBy(results, 'file');
@ -120,7 +33,7 @@ module.exports = function (results) {
let infos = 0; let infos = 0;
let summaryColor = 'cyan'; let summaryColor = 'cyan';
_.forEach(results, function (result, k) { _.forEach(results, (result, k) => {
const messages = result; const messages = result;
if (messages.length === 0) { if (messages.length === 0) {
@ -131,7 +44,7 @@ module.exports = function (results) {
output += chalk.underline(k) + '\n'; output += chalk.underline(k) + '\n';
output += table( output += table(
messages.map(function (message) { messages.map(message => {
let messageType; let messageType;
if (message.level === 'ERROR') { if (message.level === 'ERROR') {
@ -160,11 +73,7 @@ module.exports = function (results) {
align: ['', 'r', 'l'], align: ['', 'r', 'l'],
stringLength: str => chalk.stripColor(str).length stringLength: str => chalk.stripColor(str).length
} }
).split('\n').map(function (el) { ).split('\n').map(el => el.replace(/(\d+)\s+(\d+)/, (m, p1, p2) => chalk.gray(p1 + ':' + p2))).join('\n') + '\n\n';
return el.replace(/(\d+)\s+(\d+)/, function (m, p1, p2) {
return chalk.gray(p1 + ':' + p2);
});
}).join('\n') + '\n\n';
}); });
if (total > 0) { if (total > 0) {

View File

@ -26,7 +26,7 @@ const reactSupportedAttributes = ['accept', 'acceptCharset', 'accessKey', 'actio
const classNameProp = 'className'; const classNameProp = 'className';
const attributesMapping = {'class': classNameProp, 'rt-class': classNameProp, 'for': 'htmlFor'}; //eslint-disable-line quote-props const attributesMapping = {'class': classNameProp, 'rt-class': classNameProp, 'for': 'htmlFor'}; //eslint-disable-line quote-props
_.forEach(reactSupportedAttributes, function (attributeReactName) { _.forEach(reactSupportedAttributes, attributeReactName => {
if (attributeReactName !== attributeReactName.toLowerCase()) { if (attributeReactName !== attributeReactName.toLowerCase()) {
attributesMapping[attributeReactName.toLowerCase()] = attributeReactName; attributesMapping[attributeReactName.toLowerCase()] = attributeReactName;
} }

View File

@ -170,7 +170,7 @@ function generateInjectedFunc(context, namePrefix, body, params) {
function generateTemplateProps(node, context) { function generateTemplateProps(node, context) {
const propTemplateDefinition = context.options.propTemplates[node.name]; const propTemplateDefinition = context.options.propTemplates[node.name];
const propertiesTemplates = _(node.children) const propertiesTemplates = _(node.children)
.map(function (child, index) { .map((child, index) => {
let templateProp = null; let templateProp = null;
if (child.name === templateNode) { // Generic explicit template tag if (child.name === templateNode) { // Generic explicit template tag
if (!_.has(child.attribs, 'prop')) { if (!_.has(child.attribs, 'prop')) {
@ -198,7 +198,7 @@ function generateTemplateProps(node, context) {
.compact() .compact()
.value(); .value();
return _.transform(propertiesTemplates, function (props, templateProp) { return _.transform(propertiesTemplates, (props, templateProp) => {
const functionParams = _.values(context.boundParams).concat(templateProp.arguments); const functionParams = _.values(context.boundParams).concat(templateProp.arguments);
const oldBoundParams = context.boundParams; const oldBoundParams = context.boundParams;
@ -222,7 +222,7 @@ function generateTemplateProps(node, context) {
*/ */
function generateProps(node, context) { function generateProps(node, context) {
const props = {}; const props = {};
_.forOwn(node.attribs, function (val, key) { _.forOwn(node.attribs, (val, key) => {
const propKey = reactSupport.attributesMapping[key.toLowerCase()] || key; const propKey = reactSupport.attributesMapping[key.toLowerCase()] || key;
if (props.hasOwnProperty(propKey) && propKey !== reactSupport.classNameProp) { if (props.hasOwnProperty(propKey) && propKey !== reactSupport.classNameProp) {
throw RTCodeError.build(context, node, `duplicate definition of ${propKey} ${JSON.stringify(node.attribs)}`); throw RTCodeError.build(context, node, `duplicate definition of ${propKey} ${JSON.stringify(node.attribs)}`);
@ -400,7 +400,7 @@ function convertHtmlToReact(node, context) {
} }
} }
const children = _.map(node.children, function (child) { const children = _.map(node.children, child => {
const code = convertHtmlToReact(child, context); const code = convertHtmlToReact(child, context);
validateJS(code, child, context); validateJS(code, child, context);
return code; return code;
@ -494,7 +494,7 @@ function isTag(node) {
} }
function handleSelfClosingHtmlTags(nodes) { function handleSelfClosingHtmlTags(nodes) {
return _.flatMap(nodes, function (node) { return _.flatMap(nodes, node => {
let externalNodes = []; let externalNodes = [];
node.children = handleSelfClosingHtmlTags(node.children); node.children = handleSelfClosingHtmlTags(node.children);
if (node.type === 'tag' && (_.includes(reactSupport.htmlSelfClosingTags, node.name) || if (node.type === 'tag' && (_.includes(reactSupport.htmlSelfClosingTags, node.name) ||
@ -521,7 +521,7 @@ function parseAndConvertHtmlToReact(html, context) {
throw new RTCodeError('Document should have a root element'); throw new RTCodeError('Document should have a root element');
} }
let firstTag = null; let firstTag = null;
_.forEach(rootTags, function (tag) { _.forEach(rootTags, tag => {
if (tag.name === 'rt-require') { if (tag.name === 'rt-require') {
if (!tag.attribs.dependency || !tag.attribs.as) { if (!tag.attribs.dependency || !tag.attribs.as) {
throw RTCodeError.build(context, tag, "rt-require needs 'dependency' and 'as' attributes"); throw RTCodeError.build(context, tag, "rt-require needs 'dependency' and 'as' attributes");

View File

@ -51,7 +51,7 @@ function addIfMissing(array, obj) {
*/ */
function concatChildren(children) { function concatChildren(children) {
let res = ''; let res = '';
_.forEach(children, function (child) { _.forEach(children, child => {
if (child && !_.startsWith(child, ' /*')) { if (child && !_.startsWith(child, ' /*')) {
res += ','; res += ',';
} }

View File

@ -4,7 +4,7 @@ const rtStyle = require('../../src/rtStyle');
const text = '.text { background-color: #00346E; padding: 3px; }'; const text = '.text { background-color: #00346E; padding: 3px; }';
const textEp = '{\n "text": {\n "backgroundColor": "#00346E",\n "padding": 3\n }\n}'; const textEp = '{\n "text": {\n "backgroundColor": "#00346E",\n "padding": 3\n }\n}';
test('html tests', function (t) { test('html tests', t => {
const res = rtStyle.convertBody(text); const res = rtStyle.convertBody(text);
t.equal(res, textEp); t.equal(res, textEp);
t.end(); t.end();

View File

@ -31,7 +31,7 @@ const invalidFiles = [
{file: 'invalid-virtual.rt', issue: new RTCodeError('Document should not have <rt-virtual> as root element', 0, 60, 1, 1)} {file: 'invalid-virtual.rt', issue: new RTCodeError('Document should not have <rt-virtual> as root element', 0, 60, 1, 1)}
]; ];
test('invalid tests', function (t) { test('invalid tests', t => {
t.plan(invalidFiles.length); t.plan(invalidFiles.length);
invalidFiles.forEach(check); invalidFiles.forEach(check);
@ -62,7 +62,7 @@ function normalizeError(err) {
return err; return err;
} }
test('invalid tests json', function (t) { test('invalid tests json', t => {
const cli = require('../../src/cli'); const cli = require('../../src/cli');
t.plan(invalidFiles.length); t.plan(invalidFiles.length);
@ -99,17 +99,17 @@ function errorEqualMessage(err, file) {
}; };
} }
test('rt-if with rt-scope test', function (t) { test('rt-if with rt-scope test', t => {
const files = ['if-with-scope/valid-if-scope.rt']; const files = ['if-with-scope/valid-if-scope.rt'];
testFiles(t, files); testFiles(t, files);
}); });
test('conversion test', function (t) { test('conversion test', t => {
const files = ['div.rt', 'test.rt', 'repeat.rt', 'inputs.rt', 'require.rt']; const files = ['div.rt', 'test.rt', 'repeat.rt', 'inputs.rt', 'require.rt'];
testFiles(t, files); testFiles(t, files);
}); });
test('prop template conversion test', function (t) { test('prop template conversion test', t => {
const options = { const options = {
propTemplates: { propTemplates: {
List: { List: {
@ -134,7 +134,7 @@ function testFiles(t, files, options) {
files.forEach(checkFile.bind(this, t, options)); files.forEach(checkFile.bind(this, t, options));
} }
test('conversion test - native', function (t) { test('conversion test - native', t => {
const options = { const options = {
propTemplates: { propTemplates: {
MyComp: { MyComp: {
@ -147,7 +147,7 @@ test('conversion test - native', function (t) {
testFiles(t, files, options); testFiles(t, files, options);
}); });
test('convert div with all module types', function (t) { test('convert div with all module types', t => {
const files = [ const files = [
{source: 'div.rt', expected: 'div.rt.commonjs.js', options: {modules: 'commonjs'}}, {source: 'div.rt', expected: 'div.rt.commonjs.js', options: {modules: 'commonjs'}},
{source: 'div.rt', expected: 'div.rt.amd.js', options: {modules: 'amd', name: 'div'}}, {source: 'div.rt', expected: 'div.rt.amd.js', options: {modules: 'amd', name: 'div'}},
@ -167,7 +167,7 @@ test('convert div with all module types', function (t) {
} }
}); });
test('convert jsrt and test source results', function (t) { test('convert jsrt and test source results', t => {
const files = ['simple.jsrt']; const files = ['simple.jsrt'];
t.plan(files.length); t.plan(files.length);
files.forEach(check); files.forEach(check);
@ -181,7 +181,7 @@ test('convert jsrt and test source results', function (t) {
} }
}); });
test('html tests', function (t) { test('html tests', t => {
const files = [ const files = [
'scope.rt', 'scope.rt',
'scope-trailing-semicolon.rt', 'scope-trailing-semicolon.rt',
@ -231,7 +231,7 @@ test('html tests', function (t) {
} }
}); });
test('test context', function (t) { test('test context', t => {
context.clear(); context.clear();
t.equal(context.hasErrors(), false); t.equal(context.hasErrors(), false);
context.error('hi', '', 1, 1); context.error('hi', '', 1, 1);
@ -242,7 +242,7 @@ test('test context', function (t) {
t.end(); t.end();
}); });
test('test shell', function (t) { test('test shell', t => {
const shell = require('../../src/shell'); const shell = require('../../src/shell');
const newContext = _.cloneDeep(context); const newContext = _.cloneDeep(context);
let outputJSON = ''; let outputJSON = '';
@ -268,7 +268,7 @@ test('test shell', function (t) {
t.end(); t.end();
}); });
test('test shell', function (t) { test('test shell', t => {
const filename = path.join(dataPath, 'div.rt'); const filename = path.join(dataPath, 'div.rt');
const cli = require('../../src/cli'); const cli = require('../../src/cli');
const r = cli.execute(`${filename} -r --dry-run`); const r = cli.execute(`${filename} -r --dry-run`);
@ -276,7 +276,7 @@ test('test shell', function (t) {
t.end(); t.end();
}); });
test('test convertText', function (t) { test('test convertText', t => {
const texts = [ const texts = [
{input: '{}', expected: '()'}, {input: '{}', expected: '()'},
{input: "a {'b'}", expected: '"a "+(\'b\')'} {input: "a {'b'}", expected: '"a "+(\'b\')'}
@ -289,7 +289,7 @@ test('test convertText', function (t) {
} }
}); });
test('util.isStale', function (t) { test('util.isStale', t => {
const a = path.join(dataPath, 'a.tmp'); const a = path.join(dataPath, 'a.tmp');
const b = path.join(dataPath, 'b.tmp'); const b = path.join(dataPath, 'b.tmp');