Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Use eslint-config-babel
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Jul 30, 2016
1 parent 9bea6fb commit 1882a87
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 333 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures
26 changes: 7 additions & 19 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
{
"root": true,
"extends": "babel",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"rules": {
"strict": 0,
"no-underscore-dangle": 0,
"curly": 0,
"no-multi-spaces": 0,
"key-spacing": 0,
"no-return-assign": 0,
"consistent-return": 0,
"no-shadow": 0,
"comma-dangle": 0,
"no-use-before-define": 0,
"no-empty": 0,
"new-parens": 0,
"no-cond-assign": 0,
"no-fallthrough": 0,
"new-cap": 0,
"no-loop-func": 0,
"no-unreachable": 0,
"no-process-exit": 0,
"quotes": [1, "double", "avoid-escape"]
"no-var": 0,
"max-len": 0
},
"env": {
"node": true,
Expand Down
2 changes: 1 addition & 1 deletion babylon-to-espree/convertTemplateType.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ module.exports = function (tokens, tt) {
}
startingToken++;
}
}
};
2 changes: 1 addition & 1 deletion babylon-to-espree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ exports.convertComments = function (comments) {
comment.range = [comment.start, comment.end];
}
}
}
};
32 changes: 14 additions & 18 deletions babylon-to-espree/toAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ function changeToLiteral(node) {
}
}

function changeComments(nodeComments) {
for (var i = 0; i < nodeComments.length; i++) {
var comment = nodeComments[i];
if (comment.type === "CommentLine") {
comment.type = "Line";
} else if (comment.type === "CommentBlock") {
comment.type = "Block";
}
comment.range = [comment.start, comment.end];
}
}

var astTransformVisitor = {
noScope: true,
enter: function (path) {
Expand All @@ -33,27 +45,11 @@ var astTransformVisitor = {
}

if (node.trailingComments) {
for (var i = 0; i < node.trailingComments.length; i++) {
var comment = node.trailingComments[i];
if (comment.type === "CommentLine") {
comment.type = "Line";
} else if (comment.type === "CommentBlock") {
comment.type = "Block";
}
comment.range = [comment.start, comment.end];
}
changeComments(node.trailingComments);
}

if (node.leadingComments) {
for (var i = 0; i < node.leadingComments.length; i++) {
var comment = node.leadingComments[i];
if (comment.type === "CommentLine") {
comment.type = "Line";
} else if (comment.type === "CommentBlock") {
comment.type = "Block";
}
comment.range = [comment.start, comment.end];
}
changeComments(node.leadingComments);
}

// make '_paths' non-enumerable (babel-eslint #200)
Expand Down
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ exports.parse = function (code, options) {
}

return exports.parseNoPatch(code, options);
}
};

exports.parseNoPatch = function (code, options) {
var opts = {
Expand All @@ -397,20 +397,20 @@ exports.parseNoPatch = function (code, options) {
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
plugins: [
"flow",
"jsx",
"asyncFunctions",
"asyncGenerators",
"classConstructorCall",
"classProperties",
"decorators",
"doExpressions",
"exponentiationOperator",
"exportExtensions",
"functionBind",
"functionSent",
"objectRestSpread",
"trailingFunctionCommas"
"flow",
"jsx",
"asyncFunctions",
"asyncGenerators",
"classConstructorCall",
"classProperties",
"decorators",
"doExpressions",
"exponentiationOperator",
"exportExtensions",
"functionBind",
"functionSent",
"objectRestSpread",
"trailingFunctionCommas"
]
};

Expand Down Expand Up @@ -458,4 +458,4 @@ exports.parseNoPatch = function (code, options) {
babylonToEspree.attachComments(ast, ast.comments, ast.tokens);

return ast;
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"homepage": "https://github.com/babel/babel-eslint",
"devDependencies": {
"eslint": "^2.4.0",
"eslint-config-babel": "^1.0.1",
"espree": "^3.0.0",
"mocha": "^2.3.3"
}
Expand Down
108 changes: 54 additions & 54 deletions test/babel-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ function lookup(obj, keypath, backwardsDepth) {
if (!keypath) { return obj; }

return keypath.split(".").slice(0, -1 * backwardsDepth)
.reduce(function (base, segment) { return base && base[segment], obj });
.reduce(function (base, segment) { return base && base[segment], obj; });
}

function parseAndAssertSame(code) {
var esAST = espree.parse(code, {
ecmaFeatures: {
// enable JSX parsing
jsx: true,
jsx: true,
// enable return in global scope
globalReturn: true,
globalReturn: true,
// enable implied strict mode (if ecmaVersion >= 5)
impliedStrict: true,
impliedStrict: true,
// allow experimental object rest/spread
experimentalObjectRestSpread: true
experimentalObjectRestSpread: true
},
tokens: true,
loc: true,
Expand All @@ -62,7 +62,7 @@ function parseAndAssertSame(code) {
var babylonAST = babelEslint.parse(code);
try {
assertImplementsAST(esAST, babylonAST);
} catch(err) {
} catch (err) {
var traversal = err.message.slice(3, err.message.indexOf(":"));
if (esAST.tokens) {
delete esAST.tokens;
Expand Down Expand Up @@ -145,10 +145,10 @@ describe("babylon-to-esprima", function () {
it("template with destructuring #31", function () {
parseAndAssertSame([
"module.exports = {",
"render() {",
"var {name} = this.props;",
"return Math.max(null, `Name: ${name}, Name: ${name}`);",
"}",
"render() {",
"var {name} = this.props;",
"return Math.max(null, `Name: ${name}, Name: ${name}`);",
"}",
"};"
].join("\n"));
});
Expand Down Expand Up @@ -199,19 +199,19 @@ describe("babylon-to-esprima", function () {
});

it("default import", function () {
parseAndAssertSame('import foo from "foo";');
parseAndAssertSame("import foo from \"foo\";");
});

it("import specifier", function () {
parseAndAssertSame('import { foo } from "foo";');
parseAndAssertSame("import { foo } from \"foo\";");
});

it("import specifier with name", function () {
parseAndAssertSame('import { foo as bar } from "foo";');
parseAndAssertSame("import { foo as bar } from \"foo\";");
});

it("import bare", function () {
parseAndAssertSame('import "foo";');
parseAndAssertSame("import \"foo\";");
});

it("export default class declaration", function () {
Expand All @@ -231,7 +231,7 @@ describe("babylon-to-esprima", function () {
});

it("export all", function () {
parseAndAssertSame('export * from "foo";');
parseAndAssertSame("export * from \"foo\";");
});

it("export named", function () {
Expand Down Expand Up @@ -272,11 +272,11 @@ describe("babylon-to-esprima", function () {
it("block comments #124", function () {
parseAndAssertSame([
"React.createClass({",
"render() {",
"// return (",
"// <div />",
"// ); // <-- this is the line that is reported",
"}",
"render() {",
"// return (",
"// <div />",
"// ); // <-- this is the line that is reported",
"}",
"});"
].join("\n"));
});
Expand Down Expand Up @@ -308,24 +308,24 @@ describe("babylon-to-esprima", function () {
it("jsdoc", function () {
parseAndAssertSame([
"/**",
"* @param {object} options",
"* @return {number}",
"*/",
"* @param {object} options",
"* @return {number}",
"*/",
"const test = function({ a, b, c }) {",
"return a + b + c;",
"return a + b + c;",
"};",
"module.exports = test;"
].join("\n"));
})
});

it("empty block with comment", function () {
parseAndAssertSame([
"function a () {",
"try {",
"b();",
"} catch (e) {",
"// asdf",
"}",
"try {",
"b();",
"} catch (e) {",
"// asdf",
"}",
"}"
].join("\n"));
});
Expand All @@ -334,7 +334,7 @@ describe("babylon-to-esprima", function () {
it("MethodDefinition", function () {
parseAndAssertSame([
"export default class A {",
"a() {}",
"a() {}",
"}"
].join("\n"));
});
Expand All @@ -348,17 +348,17 @@ describe("babylon-to-esprima", function () {
it("ClassMethod", function () {
parseAndAssertSame([
"class A {",
"constructor() {",
"}",
"constructor() {",
"}",
"}"
].join("\n"));
});

it("ClassMethod multiple params", function () {
parseAndAssertSame([
"class A {",
"constructor(a, b, c) {",
"}",
"constructor(a, b, c) {",
"}",
"}"
].join("\n"));
});
Expand All @@ -385,16 +385,16 @@ describe("babylon-to-esprima", function () {
it("ObjectMethod", function () {
parseAndAssertSame([
"var a = {",
"b(c) {",
"}",
"b(c) {",
"}",
"}"
].join("\n"));
});

it("do not allow import export everywhere", function() {
assert.throws(function () {
parseAndAssertSame("function F() { import a from \"a\"; }");
}, /SyntaxError: 'import' and 'export' may only appear at the top level/)
}, /SyntaxError: 'import' and 'export' may only appear at the top level/);
});

it("return outside function", function () {
Expand All @@ -415,31 +415,31 @@ describe("babylon-to-esprima", function () {
parseAndAssertSame("class A { get x ( ) { ; } }");
parseAndAssertSame([
"class A {",
"get x(",
")",
"{",
";",
"}",
"get x(",
")",
"{",
";",
"}",
"}"
].join("\n"));
parseAndAssertSame("class A { set x (a) { ; } }");
parseAndAssertSame([
"class A {",
"set x(a",
")",
"{",
";",
"}",
"set x(a",
")",
"{",
";",
"}",
"}"
].join("\n"));
parseAndAssertSame([
"var B = {",
"get x () {",
"return this.ecks;",
"},",
"set x (ecks) {",
"this.ecks = ecks;",
"}",
"get x () {",
"return this.ecks;",
"},",
"set x (ecks) {",
"this.ecks = ecks;",
"}",
"};"
].join("\n"));
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/rules/strict/global-with.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";;
"use strict";
/*
The empty statement is intentional. As of now, ESLint won't enforce
string: [2, "global"] on a program with an empty body. A test for that without
Expand Down
Loading

0 comments on commit 1882a87

Please sign in to comment.