Skip to content

Commit

Permalink
Fix comment extraction issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgeiter committed Sep 28, 2019
1 parent a47dfc8 commit a59fdef
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/js/extractors/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ export abstract class JsCommentUtils {
return false;
}

let lineNumber = sourceFile.getLineAndCharacterOfPosition(nodes[index].getFullStart()).line;
let lineNumber = sourceFile.getLineAndCharacterOfPosition(nodes[index].getStart()).line;
if (index > 0) {
if (lineNumber === sourceFile.getLineAndCharacterOfPosition(nodes[index - 1].getEnd()).line) {
return false;
}
}
if (index + 1 < nodes.length) {
if (lineNumber === sourceFile.getLineAndCharacterOfPosition(nodes[index + 1].getFullStart()).line) {
if (lineNumber === sourceFile.getLineAndCharacterOfPosition(nodes[index + 1].getStart()).line) {
return false;
}
}
Expand Down
91 changes: 91 additions & 0 deletions tests/js/extractors/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ describe('JS: comments', () => {
});
});

test('first property - trailing comma', () => {
check(`(
// Irrelevant leading line comment
{
// Irrelevant leading line comment
foo: getText('Foo'), // Relevant trailing line comment
bar: 42, // Irrelevant trailing line comment
} // Irrelevant trailing line comment
)`, {
sameLineTrailing: ['Relevant trailing line comment']
});
});

test('first property with extra whitespace', () => {
check(`(
// Irrelevant leading line comment
Expand All @@ -362,6 +375,19 @@ describe('JS: comments', () => {
});
});

test('first property with extra whitespace - trailing comma', () => {
check(`(
// Irrelevant leading line comment
{
// Irrelevant leading line comment
foo: getText('Foo') , // Relevant trailing line comment
bar: 42, // Irrelevant trailing line comment
} // Irrelevant trailing line comment
)`, {
sameLineTrailing: ['Relevant trailing line comment']
});
});

test('first property with directly relevant block comment', () => {
check(`(
// Irrelevant leading line comment
Expand All @@ -375,6 +401,19 @@ describe('JS: comments', () => {
});
});

test('first property with directly relevant block comment - trailing comma', () => {
check(`(
// Irrelevant leading line comment
{
// Irrelevant leading line comment
foo: getText('Foo') /* Relevant trailing block comment */, // Irrelevant trailing line comment
bar: 42, // Irrelevant trailing line comment
} // Irrelevant trailing line comment
)`, {
sameLineTrailing: ['Relevant trailing block comment']
});
});

test('second property', () => {
check(`(
// Irrelevant leading line comment
Expand All @@ -388,6 +427,19 @@ describe('JS: comments', () => {
});
});

test('second property - trailing comma', () => {
check(`(
// Irrelevant leading line comment
{
bar: 42, // Irrelevant trailing line comment
// Irrelevant leading line comment
foo: getText('Foo'), // Relevant trailing line comment
} // Irrelevant trailing line comment
)`, {
sameLineTrailing: ['Relevant trailing line comment']
});
});

test('second property with extra whitespace', () => {
check(`(
// Irrelevant leading line comment
Expand All @@ -401,6 +453,19 @@ describe('JS: comments', () => {
});
});

test('second property with extra whitespace - trailing comma', () => {
check(`(
// Irrelevant leading line comment
{
bar: 42, // Irrelevant trailing line comment
// Irrelevant leading line comment
foo: getText('Foo') , // Relevant trailing line comment
} // Irrelevant trailing line comment
)`, {
sameLineTrailing: ['Relevant trailing line comment']
});
});

test('second property with directly relevant block comment', () => {
check(`(
// Irrelevant leading line comment
Expand All @@ -414,6 +479,19 @@ describe('JS: comments', () => {
});
});

test('second property with directly relevant block comment - trailing comma', () => {
check(`(
// Irrelevant leading line comment
{
bar: 42, // Irrelevant trailing line comment
// Irrelevant leading line comment
foo: getText('Foo'), /* Relevant trailing block comment */ // Relevant trailing line comment
} // Irrelevant trailing line comment
)`, {
sameLineTrailing: ['Relevant trailing block comment', 'Relevant trailing line comment']
});
});

test('assignment shorthand', () => {
check(`(
// Irrelevant leading line comment
Expand All @@ -426,6 +504,19 @@ describe('JS: comments', () => {
sameLineTrailing: ['Relevant trailing line comment']
});
});

test('assignment shorthand - trailing comma', () => {
check(`(
// Irrelevant leading line comment
{
bar, // Irrelevant trailing line comment
// Irrelevant leading line comment
foo: getText('Foo'), // Relevant trailing line comment
} // Irrelevant trailing line comment
)`, {
sameLineTrailing: ['Relevant trailing line comment']
});
});
});
});

Expand Down

0 comments on commit a59fdef

Please sign in to comment.