Skip to content

Commit 181d247

Browse files
committed
add linting to ci
1 parent 834d0e9 commit 181d247

File tree

5 files changed

+230
-224
lines changed

5 files changed

+230
-224
lines changed

.eslintrc

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
"es6": true,
77
"node": true
88
},
9-
"extends": "strongloop"
9+
"extends": "strongloop",
10+
"rules": {
11+
"max-len": [2, 120, 8],
12+
"no-console": 2
13+
}
1014
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"lint": "eslint src/**/*.es6",
1212
"build": "babel src --ignore *.test.*,*.sol --copy-files --source-maps --out-dir dist",
1313
"compile": "babel src -d build --copy-files",
14+
"pretest": "npm run lint",
1415
"test": "babel-tape-runner \"test/**/*.test.es6\" | node_modules/.bin/tap-spec",
1516
"coverage": "NODE_ENV=test nyc ./node_modules/.bin/babel-tape-runner \"test/**/*.test.es6\"",
1617
"coveralls": "nyc report --reporter=text-lcov | coveralls",

src/index.es6

+22-22
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@ import parser from 'solidity-parser-antlr';
22
import {ContractFile} from './lib/contract/contract-file';
33
import {Contract} from './lib/contract/contract';
44
import ContractParts from './lib/contract-parts.es6';
5-
import {ContractComment} from './lib/contract/contract-comment'
5+
import {ContractComment} from './lib/contract/contract-comment';
66

77
export function generateCommentsFromText(text, config = {}) {
8-
return generate(new Contract(text), config);
8+
return generate(new Contract(text), config);
99
}
1010

1111
export function generateCommentsFromFile(path, config = {}) {
12-
let contract = new ContractFile(path);
13-
generate(contract, config);
14-
contract.save();
12+
let contract = new ContractFile(path);
13+
generate(contract, config);
14+
contract.save();
1515
}
1616

1717
function generate(contract, config) {
18-
let ast = parser.parse(
19-
contract.getText(),
20-
{tolerant: true, loc: true, range: true}
21-
);
22-
const contractComment = new ContractComment(contract);
23-
const visitors = getVisitors(contractComment);
24-
parser.visit(ast, visitors);
25-
return contract.getText();
18+
let ast = parser.parse(
19+
contract.getText(),
20+
{tolerant: true, loc: true, range: true}
21+
);
22+
const contractComment = new ContractComment(contract);
23+
const visitors = getVisitors(contractComment);
24+
parser.visit(ast, visitors);
25+
return contract.getText();
2626
}
2727

2828
function getVisitors(contractComment) {
29-
let visitors = {};
30-
for (let prop in ContractParts) {
31-
if (ContractParts.hasOwnProperty(prop)) {
32-
visitors[ContractParts[prop]] = function (node) {
33-
contractComment.insertComment(node);
34-
};
35-
}
29+
let visitors = {};
30+
for (let prop in ContractParts) {
31+
if (ContractParts.hasOwnProperty(prop)) {
32+
visitors[ContractParts[prop]] = function(node) {
33+
contractComment.insertComment(node);
34+
};
3635
}
37-
return visitors;
38-
}
36+
}
37+
return visitors;
38+
}

src/lib/contract/contract-comment.es6

+110-109
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,130 @@
1-
import {pad} from "../utils/string-utils";
2-
import CommentsGenerator from "../generators/comments-generator";
1+
import { pad } from '../utils/string-utils';
2+
import CommentsGenerator from '../generators/comments-generator';
33

44
// regexps
55
const generatedCommentRegex = new RegExp('/// @([a-zA-Z]*)\\b');
66
const parameterCommentRegex = '/// @param';
77

88
export class ContractComment {
99

10-
constructor(contract) {
11-
this.contract = contract;
12-
this.generator = new CommentsGenerator();
13-
}
10+
constructor(contract) {
11+
this.contract = contract;
12+
this.generator = new CommentsGenerator();
13+
}
1414

15-
insertComment(node) {
16-
const comment = this.generator.generate(node).trim();
17-
if (!comment) return;
18-
let commentLines = comment.split('\n');
19-
if (this.updateComment(commentLines, node.loc)) return;
20-
commentLines = pad(
21-
node.loc.start.column,
22-
commentLines,
23-
this.isTab(this.contract.getOriginalLineAt(node.loc.start.line - 1))
24-
);
25-
this.contract.insertLinesBefore(commentLines, node.loc.start.line - 1);
26-
}
15+
insertComment(node) {
16+
const comment = this.generator.generate(node).trim();
17+
if (!comment) return;
18+
let commentLines = comment.split('\n');
19+
if (this.updateComment(commentLines, node.loc)) return;
20+
commentLines = pad(
21+
node.loc.start.column,
22+
commentLines,
23+
this.isTab(this.contract.getOriginalLineAt(node.loc.start.line - 1))
24+
);
25+
this.contract.insertLinesBefore(commentLines, node.loc.start.line - 1);
26+
}
2727

28-
updateComment(commentLines, location) {
29-
let line = location.start.line;
30-
if (this.hasComment(line)) {
31-
// extract old comments
32-
let oldCommentsParams = [];
33-
let oldCommentsMap = {};
34-
let oldCommentPosition = line - 2;
35-
while (true) {
36-
let comment = this.contract.getLineAt(oldCommentPosition).trim();
37-
if (comment.startsWith(parameterCommentRegex)) {
38-
oldCommentsParams.push({line: oldCommentPosition, value: comment})
39-
} else if (comment.startsWith('//')) {
40-
oldCommentsMap[comment.match(generatedCommentRegex)[0]] = comment
41-
} else if (!comment.startsWith('function')) {
42-
break;
43-
}
44-
oldCommentPosition--;
45-
}
46-
// check if old comment is generated comment
47-
if (this.isEmptyObject(oldCommentsMap)) {
48-
return true;
49-
}
50-
// extract new comments
51-
let newCommentsParams = [];
52-
let newCommentsMap = commentLines.reduce(function (map, obj) {
53-
let key = obj.match(generatedCommentRegex)[0];
54-
if (key === parameterCommentRegex) {
55-
newCommentsParams.push(obj);
56-
} else map[key] = obj;
57-
return map;
58-
}, {});
59-
// update params if changed
60-
if (newCommentsParams.length) {
61-
for (let k in oldCommentsMap) {
62-
if (!k in newCommentsMap) {
63-
return true;
64-
}
65-
}
66-
let firstCommentLine = oldCommentsParams
67-
.reduce((min, b) => Math.min(min, b.line), oldCommentsParams[0].line);
68-
// remove old params comments and save additional information about params
69-
let savedComments = {};
70-
for (let oldComment of oldCommentsParams) {
71-
this.contract.removeLine(firstCommentLine);
72-
// save old right part of comment
73-
let c = oldComment.value.toString().trim().split(' ');
74-
if (c.length > 3) {
75-
savedComments[c[2]] = c.slice(3).join(' ');
76-
}
77-
}
78-
// insert new params comments
79-
newCommentsParams = pad(
80-
location.start.column,
81-
newCommentsParams,
82-
this.isTab(this.contract.getOriginalLineAt(location.start.line - 1))
83-
);
84-
for (let newComment of newCommentsParams.reverse()) {
85-
let oldCommentParamName = newComment.trim().split(' ')[2];
86-
let savedComment = savedComments[oldCommentParamName];
87-
if (typeof savedComment !== "undefined") {
88-
newComment = newComment + " " + savedComment;
89-
}
90-
this.contract.insertLinesBeforeWithoutCalculatingAndAddingOffset(newComment.split(), firstCommentLine);
91-
}
92-
this.contract.addOffset(firstCommentLine, newCommentsParams.length - oldCommentsParams.length);
93-
return true;
94-
}
28+
updateComment(commentLines, location) {
29+
let line = location.start.line;
30+
if (this.hasComment(line)) {
31+
// extract old comments
32+
let oldCommentsParams = [];
33+
let oldCommentsMap = {};
34+
let oldCommentPosition = line - 2;
35+
while (true) {
36+
let comment = this.contract.getLineAt(oldCommentPosition).trim();
37+
if (comment.startsWith(parameterCommentRegex)) {
38+
oldCommentsParams.push({ line: oldCommentPosition, value: comment });
39+
} else if (comment.startsWith('//')) {
40+
oldCommentsMap[comment.match(generatedCommentRegex)[0]] = comment;
41+
} else if (!comment.startsWith('function')) {
42+
break;
43+
}
44+
oldCommentPosition--;
45+
}
46+
// check if old comment is generated comment
47+
if (this.isEmptyObject(oldCommentsMap)) {
48+
return true;
49+
}
50+
// extract new comments
51+
let newCommentsParams = [];
52+
let newCommentsMap = commentLines.reduce(function(map, obj) {
53+
let key = obj.match(generatedCommentRegex)[0];
54+
if (key === parameterCommentRegex) {
55+
newCommentsParams.push(obj);
56+
} else map[key] = obj;
57+
return map;
58+
}, {});
59+
// update params if changed
60+
if (newCommentsParams.length) {
61+
for (let k in oldCommentsMap) {
62+
const c = !k;
63+
if (c in newCommentsMap) {
9564
return true;
65+
}
9666
}
97-
return false;
98-
}
99-
100-
hasComment(line) {
101-
let counter = 1;
102-
while (true) {
103-
counter++;
104-
let lineText = this.contract.getOriginalLineAt(line - counter);
105-
if (lineText.trim().startsWith('function')) {
106-
lineText = this.contract.getOriginalLineAt(line - counter - 1);
107-
}
108-
if (lineText === undefined) return false;
109-
lineText = lineText.trim();
110-
if (lineText.startsWith('*') || lineText.startsWith('//')) return true;
111-
if (!lineText.replace(/\s/g, '').length) continue;
112-
return false;
67+
let firstCommentLine = oldCommentsParams
68+
.reduce((min, b) => Math.min(min, b.line), oldCommentsParams[0].line);
69+
// remove old params comments and save additional information about
70+
// params
71+
let savedComments = {};
72+
for (let oldComment of oldCommentsParams) {
73+
this.contract.removeLine(firstCommentLine);
74+
// save old right part of comment
75+
let c = oldComment.value.toString().trim().split(' ');
76+
if (c.length > 3) {
77+
savedComments[c[2]] = c.slice(3).join(' ');
78+
}
11379
}
114-
}
115-
116-
isEmptyObject(obj) {
117-
for (let name in obj) {
118-
return false;
80+
// insert new params comments
81+
newCommentsParams = pad(
82+
location.start.column,
83+
newCommentsParams,
84+
this.isTab(this.contract.getOriginalLineAt(location.start.line - 1))
85+
);
86+
for (let newComment of newCommentsParams.reverse()) {
87+
let oldCommentParamName = newComment.trim().split(' ')[2];
88+
let savedComment = savedComments[oldCommentParamName];
89+
if (typeof savedComment !== 'undefined') {
90+
newComment = newComment + ' ' + savedComment;
91+
}
92+
this.contract.insertLinesBeforeWithoutCalculatingAndAddingOffset(newComment.split(), firstCommentLine);
11993
}
94+
this.contract.addOffset(firstCommentLine, newCommentsParams.length - oldCommentsParams.length);
12095
return true;
96+
}
97+
return true;
12198
}
99+
return false;
100+
}
122101

123-
isTab(originalLineAt) {
124-
return originalLineAt.startsWith('\t');
102+
hasComment(line) {
103+
let counter = 1;
104+
while (true) {
105+
counter++;
106+
let lineText = this.contract.getOriginalLineAt(line - counter);
107+
if (lineText.trim().startsWith('function')) {
108+
lineText = this.contract.getOriginalLineAt(line - counter - 1);
109+
}
110+
if (lineText === undefined) return false;
111+
lineText = lineText.trim();
112+
if (lineText.startsWith('*') || lineText.startsWith('//')) return true;
113+
if (!lineText.replace(/\s/g, '').length) continue;
114+
return false;
125115
}
126-
}
116+
}
127117

118+
isEmptyObject(obj) {
119+
for (let name in obj) {
120+
return false;
121+
}
122+
return true;
123+
}
124+
125+
isTab(originalLineAt) {
126+
return originalLineAt.startsWith('\t');
127+
}
128+
}
128129

129130

0 commit comments

Comments
 (0)