Skip to content

Commit 76985b0

Browse files
committed
fix double comments
1 parent 0f1948e commit 76985b0

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nodefactory/solidity-comments-core",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "dist/index.js",
66
"engines": {

src/index.es6

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,23 @@ function isTab(originalLineAt) {
4343
return originalLineAt.startsWith('\t');
4444
}
4545

46+
function hasComment(contract, line) {
47+
let counter = 1;
48+
while (true) {
49+
counter++;
50+
let lineText = contract.getOriginalLineAt(line - counter);
51+
if (lineText === undefined) return false;
52+
lineText = lineText.trim();
53+
if (lineText.startsWith('*') || lineText.startsWith('//')) return true;
54+
if (!lineText.replace(/\s/g, '').length) continue;
55+
return false;
56+
}
57+
}
58+
4659
function insertComment(contract, node) {
4760
let comment = generator.generate(node);
4861
if (!comment) return;
62+
if (hasComment(contract, node.loc.start.line)) return;
4963
let commentLines = comment.split('\n');
5064
commentLines = pad(
5165
node.loc.start.column,

test/index.test.es6

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ test('Assert generate comments from text', (t) => {
1111
fs.readFileSync('./test/Metacoin.commented.sol', 'utf-8')
1212
);
1313
});
14+
15+
test('Assert generate comments from text second time wont duplicate', (t) => {
16+
t.plan(1);
17+
let commentedContract =
18+
generateCommentsFromText(fs.readFileSync('./test/Metacoin.sol', 'utf-8'));
19+
commentedContract =
20+
generateCommentsFromText(commentedContract, 'utf-8');
21+
t.equal(
22+
commentedContract,
23+
fs.readFileSync('./test/Metacoin.commented.sol', 'utf-8')
24+
);
25+
});

0 commit comments

Comments
 (0)