2016-05-11 08:05:29 +09:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var esprima = require('esprima');
|
|
|
|
var escodegen = require('escodegen');
|
|
|
|
var vm = require('vm');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var PDFJS_PREPROCESSOR_NAME = 'PDFJSDev';
|
|
|
|
var ROOT_PREFIX = '$ROOT/';
|
|
|
|
|
|
|
|
function isLiteral(obj, value) {
|
|
|
|
return obj.type === 'Literal' && obj.value === value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isPDFJSPreprocessor(obj) {
|
|
|
|
return obj.type === 'Identifier' &&
|
|
|
|
obj.name === PDFJS_PREPROCESSOR_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evalWithDefines(code, defines, loc) {
|
|
|
|
if (!code || !code.trim()) {
|
|
|
|
throw new Error('No JavaScript expression given');
|
|
|
|
}
|
|
|
|
return vm.runInNewContext(code, defines, {displayErrors: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePreprocessorAction(ctx, actionName, args, loc) {
|
|
|
|
try {
|
|
|
|
var arg;
|
|
|
|
switch (actionName) {
|
|
|
|
case 'test':
|
|
|
|
arg = args[0];
|
|
|
|
if (!arg || arg.type !== 'Literal' ||
|
|
|
|
typeof arg.value !== 'string') {
|
|
|
|
throw new Error('No code for testing is given');
|
|
|
|
}
|
|
|
|
var isTrue = !!evalWithDefines(arg.value, ctx.defines);
|
|
|
|
return {type: 'Literal', value: isTrue, loc: loc};
|
|
|
|
case 'eval':
|
|
|
|
arg = args[0];
|
|
|
|
if (!arg || arg.type !== 'Literal' ||
|
|
|
|
typeof arg.value !== 'string') {
|
|
|
|
throw new Error('No code for eval is given');
|
|
|
|
}
|
|
|
|
var result = evalWithDefines(arg.value, ctx.defines);
|
|
|
|
if (typeof result === 'boolean' || typeof result === 'string' ||
|
|
|
|
typeof result === 'number') {
|
|
|
|
return {type: 'Literal', value: result, loc: loc};
|
|
|
|
}
|
|
|
|
if (typeof result === 'object') {
|
|
|
|
var parsedObj = esprima.parse('(' + JSON.stringify(result) + ')');
|
|
|
|
parsedObj.body[0].expression.loc = loc;
|
|
|
|
return parsedObj.body[0].expression;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'json':
|
|
|
|
arg = args[0];
|
|
|
|
if (!arg || arg.type !== 'Literal' ||
|
|
|
|
typeof arg.value !== 'string') {
|
|
|
|
throw new Error('Path to JSON is not provided');
|
|
|
|
}
|
|
|
|
var jsonPath = arg.value;
|
|
|
|
if (jsonPath.indexOf(ROOT_PREFIX) === 0) {
|
|
|
|
jsonPath = path.join(ctx.rootPath,
|
|
|
|
jsonPath.substring(ROOT_PREFIX.length));
|
|
|
|
}
|
|
|
|
var jsonContent = fs.readFileSync(jsonPath).toString();
|
2016-12-11 18:43:09 +09:00
|
|
|
var parsedJSON = esprima.parse('(' + jsonContent + ')');
|
2016-05-11 08:05:29 +09:00
|
|
|
parsedJSON.body[0].expression.loc = loc;
|
|
|
|
return parsedJSON.body[0].expression;
|
|
|
|
}
|
|
|
|
throw new Error('Unsupported action');
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Could not process ' + PDFJS_PREPROCESSOR_NAME + '.' +
|
|
|
|
actionName + ' at ' + JSON.stringify(loc) + '\n' +
|
|
|
|
e.name + ': ' + e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function postprocessNode(ctx, node) {
|
|
|
|
switch (node.type) {
|
|
|
|
case 'IfStatement':
|
|
|
|
if (isLiteral(node.test, true)) {
|
|
|
|
// if (true) stmt1; => stmt1
|
|
|
|
return node.consequent;
|
|
|
|
} else if (isLiteral(node.test, false)) {
|
|
|
|
// if (false) stmt1; else stmt2; => stmt2
|
|
|
|
return node.alternate || {type: 'EmptyStatement', loc: node.loc};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ConditionalExpression':
|
|
|
|
if (isLiteral(node.test, true)) {
|
|
|
|
// true ? stmt1 : stmt2 => stmt1
|
|
|
|
return node.consequent;
|
|
|
|
} else if (isLiteral(node.test, false)) {
|
|
|
|
// false ? stmt1 : stmt2 => stmt2
|
|
|
|
return node.alternate;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'UnaryExpression':
|
|
|
|
if (node.operator === 'typeof' &&
|
|
|
|
isPDFJSPreprocessor(node.argument)) {
|
|
|
|
// typeof PDFJSDev => 'object'
|
|
|
|
return {type: 'Literal', value: 'object', loc: node.loc};
|
|
|
|
}
|
|
|
|
if (node.operator === '!' &&
|
|
|
|
node.argument.type === 'Literal' &&
|
|
|
|
typeof node.argument.value === 'boolean') {
|
|
|
|
// !true => false, !false => true
|
|
|
|
return {type: 'Literal', value: !node.argument.value, loc: node.loc};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'LogicalExpression':
|
|
|
|
switch (node.operator) {
|
|
|
|
case '&&':
|
|
|
|
if (isLiteral(node.left, true)) {
|
|
|
|
return node.right;
|
|
|
|
}
|
|
|
|
if (isLiteral(node.left, false)) {
|
|
|
|
return node.left;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '||':
|
|
|
|
if (isLiteral(node.left, true)) {
|
|
|
|
return node.left;
|
|
|
|
}
|
|
|
|
if (isLiteral(node.left, false)) {
|
|
|
|
return node.right;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'BinaryExpression':
|
|
|
|
switch (node.operator) {
|
|
|
|
case '==':
|
|
|
|
case '===':
|
|
|
|
case '!=':
|
|
|
|
case '!==':
|
|
|
|
if (node.left.type === 'Literal' &&
|
|
|
|
node.right.type === 'Literal' &&
|
|
|
|
typeof node.left.value === typeof node.right.value) {
|
|
|
|
// folding two literals == and != check
|
|
|
|
switch (typeof node.left.value) {
|
|
|
|
case 'string':
|
|
|
|
case 'boolean':
|
|
|
|
case 'number':
|
|
|
|
var equal = node.left.value === node.right.value;
|
|
|
|
return {
|
|
|
|
type: 'Literal',
|
|
|
|
value: (node.operator[0] === '=') === equal,
|
|
|
|
loc: node.loc
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'CallExpression':
|
|
|
|
if (node.callee.type === 'MemberExpression' &&
|
|
|
|
isPDFJSPreprocessor(node.callee.object) &&
|
|
|
|
node.callee.property.type === 'Identifier') {
|
|
|
|
// PDFJSDev.xxxx(arg1, arg2, ...) => tranform
|
|
|
|
var action = node.callee.property.name;
|
|
|
|
return handlePreprocessorAction(ctx, action,
|
|
|
|
node.arguments, node.loc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'BlockStatement':
|
|
|
|
var subExpressionIndex = 0;
|
|
|
|
while (subExpressionIndex < node.body.length) {
|
2017-01-10 04:15:09 +09:00
|
|
|
switch (node.body[subExpressionIndex].type) {
|
|
|
|
case 'EmptyStatement':
|
|
|
|
// Removing empty statements from the blocks.
|
|
|
|
node.body.splice(subExpressionIndex, 1);
|
|
|
|
continue;
|
|
|
|
case 'BlockStatement':
|
|
|
|
// Block statements inside a block are moved to the parent one.
|
|
|
|
var subChildren = node.body[subExpressionIndex].body;
|
|
|
|
Array.prototype.splice.apply(node.body,
|
|
|
|
[subExpressionIndex, 1].concat(subChildren));
|
|
|
|
subExpressionIndex += Math.max(subChildren.length - 1, 0);
|
|
|
|
continue;
|
|
|
|
case 'ReturnStatement':
|
|
|
|
case 'ThrowStatement':
|
|
|
|
// Removing dead code after return or throw.
|
|
|
|
node.body.splice(subExpressionIndex + 1,
|
|
|
|
node.body.length - subExpressionIndex - 1);
|
|
|
|
break;
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
subExpressionIndex++;
|
|
|
|
}
|
|
|
|
break;
|
2017-01-10 04:15:09 +09:00
|
|
|
case 'FunctionDeclaration':
|
|
|
|
case 'FunctionExpression':
|
|
|
|
var block = node.body;
|
|
|
|
if (block.body.length > 0 &&
|
|
|
|
block.body[block.body.length - 1].type === 'ReturnStatement' &&
|
|
|
|
!block.body[block.body.length - 1].argument) {
|
|
|
|
// Function body ends with return without arg -- removing it.
|
|
|
|
block.body.pop();
|
|
|
|
}
|
|
|
|
break;
|
2017-02-09 07:35:58 +09:00
|
|
|
case 'Program':
|
|
|
|
// Checking for a function closure that looks like UMD header.
|
|
|
|
node.body.some(function (item, index) {
|
|
|
|
// Is it `(function (root, factory) { ? }(this, function (?) {?}));` ?
|
|
|
|
if (item.type !== 'ExpressionStatement' ||
|
|
|
|
item.expression.type !== 'CallExpression' ||
|
|
|
|
item.expression.callee.type !== 'FunctionExpression' ||
|
|
|
|
item.expression.callee.params.length !== 2 ||
|
|
|
|
item.expression.arguments.length !== 2 ||
|
|
|
|
item.expression.arguments[0].type !== 'ThisExpression' ||
|
|
|
|
item.expression.arguments[1].type !== 'FunctionExpression') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var init = item.expression.callee;
|
|
|
|
// Is init body looks like
|
|
|
|
// `if (?) { ? } else if (typeof exports !== 'undefined') { ? } ...`?
|
|
|
|
if (init.body.type !== 'BlockStatement' ||
|
|
|
|
init.body.body.length !== 1 ||
|
|
|
|
init.body.body[0].type !== 'IfStatement') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var initIf = init.body.body[0];
|
|
|
|
if (initIf.alternate.type !== 'IfStatement' ||
|
|
|
|
initIf.alternate.test.type !== 'BinaryExpression' ||
|
|
|
|
initIf.alternate.test.operator !== '!==' ||
|
|
|
|
initIf.alternate.test.left.type !== 'UnaryExpression' ||
|
|
|
|
initIf.alternate.test.left.operator !== 'typeof' ||
|
|
|
|
initIf.alternate.test.left.argument.type !== 'Identifier' ||
|
|
|
|
initIf.alternate.test.left.argument.name !== 'exports' ||
|
|
|
|
initIf.alternate.test.right.type !== 'Literal' ||
|
|
|
|
initIf.alternate.test.right.value !== 'undefined' ||
|
|
|
|
initIf.alternate.consequent.type !== 'BlockStatement') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var commonJsInit = initIf.alternate.consequent;
|
|
|
|
// Is commonJsInit `factory(exports, ...)` ?
|
|
|
|
if (commonJsInit.body.length !== 1 ||
|
|
|
|
commonJsInit.body[0].type !== 'ExpressionStatement' ||
|
|
|
|
commonJsInit.body[0].expression.type !== 'CallExpression' ||
|
|
|
|
commonJsInit.body[0].expression.callee.type !== 'Identifier') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var commonJsInitArgs = commonJsInit.body[0].expression.arguments;
|
|
|
|
if (commonJsInitArgs.length === 0 ||
|
|
|
|
commonJsInitArgs[0].type !== 'Identifier' ||
|
|
|
|
commonJsInitArgs[0].name !== 'exports') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var factory = item.expression.arguments[1];
|
|
|
|
// Is factory `function (exports, ....) { ? }` ?
|
|
|
|
if (factory.params.length === 0 ||
|
|
|
|
factory.params[0].type !== 'Identifier' ||
|
|
|
|
factory.params[0].name !== 'exports' ||
|
|
|
|
factory.body.type !== 'BlockStatement') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var factoryParams = factory.params;
|
|
|
|
var factoryBody = factory.body;
|
|
|
|
|
|
|
|
// Remove closure and function and replacing parameters with vars.
|
|
|
|
node.body.splice(index, 1);
|
|
|
|
for (var i = 1, ii = factoryParams.length; i < ii; i++) {
|
|
|
|
var varNode = {
|
|
|
|
type: 'VariableDeclaration',
|
|
|
|
'declarations': [{
|
|
|
|
type: 'VariableDeclarator',
|
|
|
|
id: factoryParams[i],
|
|
|
|
init: commonJsInitArgs[i] || null,
|
|
|
|
loc: factoryParams[i].loc
|
|
|
|
}],
|
|
|
|
kind: 'var'
|
|
|
|
};
|
|
|
|
node.body.splice(index++, 0, varNode);
|
|
|
|
}
|
|
|
|
factoryBody.body.forEach(function (item) {
|
|
|
|
node.body.splice(index++, 0, item);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
break;
|
2016-05-11 08:05:29 +09:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixComments(ctx, node) {
|
|
|
|
if (!ctx.saveComments) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Fixes double comments in the escodegen output.
|
|
|
|
delete node.trailingComments;
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
// Removes ESLint and other service comments.
|
2016-05-11 08:05:29 +09:00
|
|
|
if (node.leadingComments) {
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
var CopyrightRegExp = /\bcopyright\b/i;
|
|
|
|
var BlockCommentRegExp = /^\s*(globals|eslint|falls through|umdutils)\b/;
|
|
|
|
var LineCommentRegExp = /^\s*eslint\b/;
|
|
|
|
|
2016-05-11 08:05:29 +09:00
|
|
|
var i = 0;
|
|
|
|
while (i < node.leadingComments.length) {
|
|
|
|
var type = node.leadingComments[i].type;
|
|
|
|
var value = node.leadingComments[i].value;
|
2016-11-12 06:05:17 +09:00
|
|
|
|
|
|
|
if (ctx.saveComments === 'copyright') {
|
|
|
|
// Remove all comments, except Copyright notices and License headers.
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
if (!(type === 'Block' && CopyrightRegExp.test(value))) {
|
2016-11-12 06:05:17 +09:00
|
|
|
node.leadingComments.splice(i, 1);
|
|
|
|
continue;
|
|
|
|
}
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
} else if ((type === 'Block' && BlockCommentRegExp.test(value)) ||
|
|
|
|
(type === 'Line' && LineCommentRegExp.test(value))) {
|
2016-05-11 08:05:29 +09:00
|
|
|
node.leadingComments.splice(i, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function traverseTree(ctx, node) {
|
|
|
|
// generic node processing
|
|
|
|
for (var i in node) {
|
|
|
|
var child = node[i];
|
|
|
|
if (typeof child === 'object' && child !== null && child.type) {
|
|
|
|
var result = traverseTree(ctx, child);
|
|
|
|
if (result !== child) {
|
|
|
|
node[i] = result;
|
|
|
|
}
|
|
|
|
} else if (Array.isArray(child)) {
|
|
|
|
child.forEach(function (childItem, index) {
|
|
|
|
if (typeof childItem === 'object' && childItem !== null &&
|
|
|
|
childItem.type) {
|
|
|
|
var result = traverseTree(ctx, childItem);
|
|
|
|
if (result !== childItem) {
|
|
|
|
child[index] = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = postprocessNode(ctx, node) || node;
|
|
|
|
|
|
|
|
fixComments(ctx, node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
function preprocessPDFJSCode(ctx, code) {
|
|
|
|
var saveComments = !!ctx.saveComments;
|
|
|
|
var format = ctx.format || {
|
|
|
|
indent: {
|
2016-11-12 06:05:17 +09:00
|
|
|
style: ' ',
|
2016-05-11 08:05:29 +09:00
|
|
|
adjustMultilineComment: saveComments,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var parseComment = {
|
|
|
|
loc: true,
|
|
|
|
attachComment: saveComments
|
|
|
|
};
|
|
|
|
var codegenOptions = {
|
|
|
|
format: format,
|
|
|
|
comment: saveComments,
|
|
|
|
parse: esprima.parse
|
|
|
|
};
|
|
|
|
var syntax = esprima.parse(code, parseComment);
|
|
|
|
traverseTree(ctx, syntax);
|
|
|
|
return escodegen.generate(syntax, codegenOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.preprocessPDFJSCode = preprocessPDFJSCode;
|