[ReactNative] Bring back support for FB-specific transforms for lint

This commit is contained in:
Philipp von Weitershausen 2015-02-26 11:45:20 +00:00
parent ea6bfbb7d6
commit 75865f31aa
2 changed files with 78 additions and 75 deletions

76
lint/linterTransform.js Normal file
View File

@ -0,0 +1,76 @@
'use strict';
var eslint = require('eslint');
var ignoredStylisticRules = {
'key-spacing': false,
'comma-spacing': true,
'no-multi-spaces': true,
'brace-style': true,
'camelcase': true,
'consistent-this': true,
'eol-last': true,
'func-names': true,
'func-style': true,
'new-cap': true,
'new-parens': true,
'no-nested-ternary': true,
'no-array-constructor': true,
'no-lonely-if': true,
'no-new-object': true,
'no-spaced-func': true,
'no-space-before-semi': true,
'no-ternary': true,
'no-trailing-spaces': true,
'no-underscore-dangle': true,
'no-wrap-func': true,
'no-mixed-spaces-and-tabs': true,
'quotes': true,
'quote-props': true,
'semi': true,
'sort-vars': true,
'space-after-keywords': true,
'space-in-brackets': true,
'space-in-parens': true,
'space-infix-ops': true,
'space-return-throw-case': true,
'space-unary-word-ops': true,
'max-nested-callbacks': true,
'one-var': true,
'wrap-regex': true,
'curly': true,
'no-mixed-requires': true,
};
function setLinterTransform(transformSource) {
var originalVerify = eslint.linter.verify;
eslint.linter.verify = function(text, config, filename, saveState) {
var transformedText;
try {
transformedText = transformSource(text);
} catch (e) {
return [{
severity: 2,
line: e.lineNumber,
message: e.message,
source: text
}];
}
var originalLines = text.split('\n');
var transformedLines = transformedText.split('\n');
var warnings = originalVerify.call(eslint.linter, transformedText, config, filename, saveState);
// JSX and ES6 transforms usually generate pretty ugly code. Let's skip lint warnings
// about code style for lines that have been changed by transform step.
// Note that more important issues, like use of undefined vars, will still be reported.
return warnings.filter(function(error) {
var lineHasBeenTransformed = originalLines[error.line - 1] !== transformedLines[error.line - 1];
var shouldIgnore = ignoredStylisticRules[error.ruleId] && lineHasBeenTransformed;
return !shouldIgnore;
});
};
}
module.exports = {
setLinterTransform: setLinterTransform,
};

View File

@ -1,83 +1,10 @@
// Copyright 2012-present Facebook. All Rights Reserved.
'use strict';
var eslint = require('eslint');
var transformSource = require('./jestSupport/scriptPreprocess.js').transformSource;
var linterTransform = require('./lint/linterTransform');
var ignoredStylisticRules = {
'key-spacing': false,
'comma-spacing': true,
'no-multi-spaces': true,
'brace-style': true,
'camelcase': true,
'consistent-this': true,
'eol-last': true,
'func-names': true,
'func-style': true,
'new-cap': true,
'new-parens': true,
'no-nested-ternary': true,
'no-array-constructor': true,
'no-lonely-if': true,
'no-new-object': true,
'no-spaced-func': true,
'no-space-before-semi': true,
'no-ternary': true,
'no-trailing-spaces': true,
'no-underscore-dangle': true,
'no-wrap-func': true,
'no-mixed-spaces-and-tabs': true,
'quotes': true,
'quote-props': true,
'semi': true,
'sort-vars': true,
'space-after-keywords': true,
'space-in-brackets': true,
'space-in-parens': true,
'space-infix-ops': true,
'space-return-throw-case': true,
'space-unary-word-ops': true,
'max-nested-callbacks': true,
'one-var': true,
'wrap-regex': true,
'curly': true,
'no-mixed-requires': true,
};
/*
* Currently ESLint does not understand ES6+React-flavoured syntax.
* To make it work on our codebase, we monkey-patch `verify` function
* to do a transform before running lint rules.
*
* If future, as ESLint's support for ES6 expands, we can get rid of this
* hack
*/
var originalVerify = eslint.linter.verify;
eslint.linter.verify = function(text, config, filename, saveState) {
var transformedText;
try {
transformedText = transformSource(text);
} catch (e) {
return [{
severity: 2,
line: e.lineNumber,
message: e.message,
source: text
}];
}
var originalLines = text.split('\n');
var transformedLines = transformedText.split('\n');
var warnings = originalVerify.call(eslint.linter, transformedText, config, filename, saveState);
// JSX and ES6 transforms usually generate pretty ugly code. Let's skip lint warnings
// about code style for lines that have been changed by transform step.
// Note that more important issues, like use of undefined vars, will still be reported.
return warnings.filter(function(error) {
var lineHasBeenTransformed = originalLines[error.line - 1] !== transformedLines[error.line - 1];
var shouldIgnore = ignoredStylisticRules[error.ruleId] && lineHasBeenTransformed;
return !shouldIgnore;
});
};
linterTransform.setLinterTransform(transformSource);
// Run the original CLI
require('eslint/bin/eslint');