Skip to content

Commit

Permalink
Upgrade: eslint & eslint-config-eslint (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add authored Nov 4, 2018
1 parent d3f123e commit 6ebc219
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 251 deletions.
45 changes: 14 additions & 31 deletions Makefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @fileoverview Build file
* @author nzakas
*/
/* global cp, echo, exit, find, mkdir, rm, target, test */
/* global cp, echo, exit, mkdir, rm, target, test */

"use strict";

Expand All @@ -13,41 +13,24 @@

require("shelljs/make");

var nodeCLI = require("shelljs-nodecli");
const nodeCLI = require("shelljs-nodecli");
const path = require("path");

//------------------------------------------------------------------------------
// Data
//------------------------------------------------------------------------------

var NODE_MODULES = "./node_modules/",
const NODE_MODULES = "./node_modules/",
TEMP_DIR = "./tmp/",
BUILD_DIR = "./build/",

// Utilities - intentional extra space at the end of each string
MOCHA = NODE_MODULES + "mocha/bin/_mocha ",
MOCHA = `${NODE_MODULES}mocha/bin/_mocha `,

// Files
MAKEFILE = "./Makefile.js",
/* eslint-disable no-use-before-define */
JS_FILES = find("lib/").filter(fileType("js")).join(" ") + " espree.js",
TEST_FILES = find("tests/lib/").filter(fileType("js")).join(" ");
/* eslint-enable no-use-before-define */

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Generates a function that matches files with a particular extension.
* @param {string} extension The file extension (i.e. "js")
* @returns {Function} The function to pass into a filter method.
* @private
*/
function fileType(extension) {
return function(filename) {
return filename.substring(filename.lastIndexOf(".") + 1) === extension;
};
}
JS_FILES = "\"lib/**/*.js\" \"espree.js\"",
TEST_FILES = "tests/lib/**/*.js";

//------------------------------------------------------------------------------
// Tasks
Expand All @@ -58,7 +41,7 @@ target.all = function() {
};

target.lint = function() {
var errors = 0,
let errors = 0,
lastReturn;

echo("Validating Makefile.js");
Expand All @@ -85,12 +68,12 @@ target.lint = function() {
};

target.test = function() {

// target.lint();

var errors = 0,
lastReturn;
let errors = 0;

lastReturn = nodeCLI.exec("nyc", MOCHA, "--color", "--reporter progress", "--timeout 30000", TEST_FILES);
const lastReturn = nodeCLI.exec("nyc", MOCHA, "--color", "--reporter progress", "--timeout 30000", TEST_FILES);

if (lastReturn.code !== 0) {
errors++;
Expand All @@ -112,21 +95,21 @@ target.browserify = function() {
// 1. create temp and build directory
if (!test("-d", TEMP_DIR)) {
mkdir(TEMP_DIR);
mkdir(TEMP_DIR + "/lib");
mkdir(path.join(TEMP_DIR, "lib"));
}

if (!test("-d", BUILD_DIR)) {
mkdir(BUILD_DIR);
}

// 2. copy files into temp directory
cp("-r", "lib/*", TEMP_DIR + "/lib");
cp("-r", "lib/*", path.join(TEMP_DIR, "lib"));
cp("espree.js", TEMP_DIR);
cp("package.json", TEMP_DIR);


// 3. browserify the temp directory
nodeCLI.exec("browserify", TEMP_DIR + "espree.js", "-o", BUILD_DIR + "espree.js", "-s espree");
nodeCLI.exec("browserify", path.join(TEMP_DIR, "espree.js"), "-o", path.join(BUILD_DIR, "espree.js"), "-s espree");

// 4. remove temp directory
rm("-r", TEMP_DIR);
Expand Down
7 changes: 5 additions & 2 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const parsers = {
options.ecmaFeatures &&
options.ecmaFeatures.jsx
);

return useJsx ? this.jsx : this.regular;
}
};
Expand All @@ -109,7 +110,7 @@ function tokenize(code, options) {

// Ensure to collect tokens.
if (!options || options.tokens !== true) {
options = Object.assign({}, options, { tokens: true });
options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign
}

return new Parser(options, code).tokenize();
Expand All @@ -128,6 +129,7 @@ function tokenize(code, options) {
*/
function parse(code, options) {
const Parser = parsers.get(options);

return new Parser(options, code).parse();
}

Expand All @@ -144,7 +146,8 @@ exports.parse = parse;
// Deep copy.
/* istanbul ignore next */
exports.Syntax = (function() {
var name, types = {};
let name,
types = {};

if (typeof Object.create === "function") {
types = Object.create(null);
Expand Down
13 changes: 7 additions & 6 deletions lib/comment-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
// Requirements
//------------------------------------------------------------------------------

var astNodeTypes = require("./ast-node-types");
const astNodeTypes = require("./ast-node-types");

//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------

var extra = {
const extra = {
trailingComments: [],
leadingComments: [],
bottomRightStack: [],
Expand All @@ -28,20 +28,20 @@ var extra = {

module.exports = {

reset: function() {
reset() {
extra.trailingComments = [];
extra.leadingComments = [];
extra.bottomRightStack = [];
extra.previousNode = null;
},

addComment: function(comment) {
addComment(comment) {
extra.trailingComments.push(comment);
extra.leadingComments.push(comment);
},

processComment: function(node) {
var lastChild,
processComment(node) {
let lastChild,
trailingComments,
i,
j;
Expand Down Expand Up @@ -95,6 +95,7 @@ module.exports = {
node.leadingComments = lastChild.leadingComments;
delete lastChild.leadingComments;
} else {

// A leading comment for an anonymous class had been stolen by its first MethodDefinition,
// so this takes back the leading comment.
// See Also: https://github.com/eslint/espree/issues/158
Expand Down
18 changes: 13 additions & 5 deletions lib/espree.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

/* eslint-disable no-param-reassign*/
const acorn = require("acorn");
const jsx = require("acorn-jsx");
const commentAttachment = require("./comment-attachment");
Expand Down Expand Up @@ -89,9 +90,9 @@ module.exports = () => Parser => class Espree extends Parser {
const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion);
const isModule = options.sourceType === "module";
const tokenTranslator =
options.tokens === true ?
new TokenTranslator(tokTypes, code) :
null;
options.tokens === true
? new TokenTranslator(tokTypes, code)
: null;

// Initialize acorn parser.
super({
Expand All @@ -104,8 +105,9 @@ module.exports = () => Parser => class Espree extends Parser {
allowReturnOutsideFunction: Boolean(ecmaFeatures.globalReturn),

// Collect tokens
onToken: (token) => {
onToken: token => {
if (tokenTranslator) {

// Use `tokens`, `ecmaVersion`, and `jsxAttrValueToken` in the state.
tokenTranslator.onToken(token, this[STATE]);
}
Expand All @@ -118,6 +120,7 @@ module.exports = () => Parser => class Espree extends Parser {
onComment: (block, text, start, end, startLoc, endLoc) => {
if (this[STATE].comments) {
const comment = convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc);

this[STATE].comments.push(comment);

if (options.attachComment === true) {
Expand Down Expand Up @@ -159,11 +162,13 @@ module.exports = () => Parser => class Espree extends Parser {

finishNode(...args) {
const result = super.finishNode(...args);

return this[ESPRIMA_FINISH_NODE](result);
}

finishNodeAt(...args) {
const result = super.finishNodeAt(...args);

return this[ESPRIMA_FINISH_NODE](result);
}

Expand Down Expand Up @@ -216,6 +221,7 @@ module.exports = () => Parser => class Espree extends Parser {
raise(pos, message) {
const loc = acorn.getLineInfo(this.input, pos);
const err = new SyntaxError(message);

err.index = pos;
err.lineNumber = loc.line;
err.column = loc.column + 1; // acorn uses 0-based columns
Expand Down Expand Up @@ -256,7 +262,7 @@ module.exports = () => Parser => class Espree extends Parser {
}

if (this.end > this.start) {
message += " " + this.input.slice(this.start, this.end);
message += ` ${this.input.slice(this.start, this.end)}`;
}

this.raise(this.start, message);
Expand All @@ -271,6 +277,7 @@ module.exports = () => Parser => class Espree extends Parser {
*/
jsx_readString(quote) { // eslint-disable-line camelcase
const result = super.jsx_readString(quote);

if (this.type === tokTypes.string) {
this[STATE].jsxAttrValueToken = true;
}
Expand All @@ -283,6 +290,7 @@ module.exports = () => Parser => class Espree extends Parser {
* @returns {ASTNode} The finished node.
*/
[ESPRIMA_FINISH_NODE](result) {

// Acorn doesn't count the opening and closing backticks as part of templates
// so we have to adjust ranges/locations appropriately.
if (result.type === "TemplateElement") {
Expand Down
28 changes: 16 additions & 12 deletions lib/token-translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


// Esprima Token Types
var Token = {
const Token = {
Boolean: "Boolean",
EOF: "<end>",
Identifier: "Identifier",
Expand All @@ -41,10 +41,10 @@ var Token = {
* @private
*/
function convertTemplatePart(tokens, code) {
var firstToken = tokens[0],
const firstToken = tokens[0],
lastTemplateToken = tokens[tokens.length - 1];

var token = {
const token = {
type: Token.Template,
value: code.slice(firstToken.start, lastTemplateToken.end)
};
Expand Down Expand Up @@ -99,9 +99,9 @@ TokenTranslator.prototype = {
* @param {Object} extra Espree extra object.
* @returns {EsprimaToken} The Esprima version of the token.
*/
translate: function(token, extra) {
translate(token, extra) {

var type = token.type,
const type = token.type,
tt = this._acornTokTypes;

if (type === tt.name) {
Expand Down Expand Up @@ -157,12 +157,13 @@ TokenTranslator.prototype = {
token.value = this._code.slice(token.start, token.end);
} else if (type === tt.regexp) {
token.type = Token.RegularExpression;
var value = token.value;
const value = token.value;

token.regex = {
flags: value.flags,
pattern: value.pattern
};
token.value = "/" + value.pattern + "/" + value.flags;
token.value = `/${value.pattern}/${value.flags}`;
}

return token;
Expand All @@ -174,9 +175,9 @@ TokenTranslator.prototype = {
* @param {Object} extra The Espree extra object.
* @returns {void}
*/
onToken: function(token, extra) {
onToken(token, extra) {

var that = this,
const that = this,
tt = this._acornTokTypes,
tokens = extra.tokens,
templateTokens = this._tokens;
Expand Down Expand Up @@ -218,11 +219,13 @@ TokenTranslator.prototype = {
}

return;
} else if (token.type === tt.dollarBraceL) {
}
if (token.type === tt.dollarBraceL) {
templateTokens.push(token);
translateTemplateTokens();
return;
} else if (token.type === tt.braceR) {
}
if (token.type === tt.braceR) {

// if there's already a curly, it's not part of the template
if (this._curlyBrace) {
Expand All @@ -232,7 +235,8 @@ TokenTranslator.prototype = {
// store new curly for later
this._curlyBrace = token;
return;
} else if (token.type === tt.template || token.type === tt.invalidTemplate) {
}
if (token.type === tt.template || token.type === tt.invalidTemplate) {
if (this._curlyBrace) {
templateTokens.push(this._curlyBrace);
this._curlyBrace = null;
Expand Down
Loading

0 comments on commit 6ebc219

Please sign in to comment.