Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/code-analyzer-regex-engine/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function validateEngineName(engineName: string) {
export function createBaseRegexRules(now: Date): RegexRules {
return {
NoTrailingWhitespace: {
regex: (/(?<target>[ \t]+)((\r?\n)|$)/g).toString(),
regex: (/(?<target>((?<=\S.*)[ \t]+(?=\r?\n|$))|((?<=([ \t]*\r?\n)([ \t]*\r?\n))[ \t]*\r?\n))/g).toString(),
file_extensions: ['.cls', '.trigger'], // Currently restricted to apex files only... but we might want to extend where this rule applies in the future
description: getMessage('TrailingWhitespaceRuleDescription'),
violation_message: getMessage('TrailingWhitespaceRuleMessage'),
Expand Down
120 changes: 120 additions & 0 deletions packages/code-analyzer-regex-engine/test/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,127 @@ describe('Tests for runRules', () => {
endColumn: 21
}]
},
{
ruleName: "NoTrailingWhitespace",
message: getMessage('TrailingWhitespaceRuleMessage'),
primaryLocationIndex: 0,
codeLocations: [{
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesAtEof.cls"),
startLine: 7,
startColumn: 1,
endLine: 8,
endColumn: 1
}]
},
{
ruleName: "NoTrailingWhitespace",
message: getMessage('TrailingWhitespaceRuleMessage'),
primaryLocationIndex: 0,
codeLocations: [{
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "MultipleEmptyLines.cls"),
startLine: 6,
startColumn: 1,
endLine: 7,
endColumn: 1
}]
},
{
ruleName: "NoTrailingWhitespace",
message: getMessage('TrailingWhitespaceRuleMessage'),
primaryLocationIndex: 0,
codeLocations: [{
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls"),
startLine: 6,
startColumn: 1,
endLine: 7,
endColumn: 1
}]
},

];

expect(runResults.violations).toHaveLength(expectedViolations.length);
for (const expectedViolation of expectedViolations) {
expect(runResults.violations).toContainEqual(expectedViolation);
}
});

it("NoTrailingWhitespace rule should flag multiple consecutive empty lines", async () => {
const runOptions: RunOptions = createRunOptions(
new Workspace('id', [path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines")]));
const runResults: EngineRunResults = await engine.runRules(["NoTrailingWhitespace"], runOptions);

const expectedViolations: Violation[] = [
{
ruleName: "NoTrailingWhitespace",
message: getMessage('TrailingWhitespaceRuleMessage'),
primaryLocationIndex: 0,
codeLocations: [{
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "MultipleEmptyLines.cls"),
startLine: 6,
startColumn: 1,
endLine: 7,
endColumn: 1
}]
},
{
ruleName: "NoTrailingWhitespace",
message: getMessage('TrailingWhitespaceRuleMessage'),
primaryLocationIndex: 0,
codeLocations: [{
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesAtEof.cls"),
startLine: 7,
startColumn: 1,
endLine: 8,
endColumn: 1
}]
},
{
ruleName: "NoTrailingWhitespace",
message: getMessage('TrailingWhitespaceRuleMessage'),
primaryLocationIndex: 0,
codeLocations: [{
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls"),
startLine: 6,
startColumn: 1,
endLine: 7,
endColumn: 1
}]
}
];

expect(runResults.violations).toHaveLength(expectedViolations.length);
for (const expectedViolation of expectedViolations) {
expect(runResults.violations).toContainEqual(expectedViolation);
}
});

it("NoTrailingWhitespace rule should NOT flag single empty lines between code", async () => {
const runOptions: RunOptions = createRunOptions(
new Workspace('id', [path.resolve(__dirname, "test-data", "apexClassWhitespace", "5_singleEmptyLinesValid")]));
const runResults: EngineRunResults = await engine.runRules(["NoTrailingWhitespace"], runOptions);

expect(runResults.violations).toHaveLength(0);
});

it("NoTrailingWhitespace rule should flag multiple empty lines that contain only whitespace", async () => {
const runOptions: RunOptions = createRunOptions(
new Workspace('id', [path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls")]));
const runResults: EngineRunResults = await engine.runRules(["NoTrailingWhitespace"], runOptions);

const expectedViolations: Violation[] = [
{
ruleName: "NoTrailingWhitespace",
message: getMessage('TrailingWhitespaceRuleMessage'),
primaryLocationIndex: 0,
codeLocations: [{
file: path.resolve(__dirname, "test-data", "apexClassWhitespace", "4_multipleEmptyLines", "EmptyLinesWithWhitespace.cls"),
startLine: 6,
startColumn: 1,
endLine: 7,
endColumn: 1
}]
}
];

expect(runResults.violations).toHaveLength(expectedViolations.length);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class EmptyLinesAtEof {
public void method1() {
System.debug('test');
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class EmptyLinesWithWhitespace {
public void method1() {
System.debug('test1');
}


public void method2() {
System.debug('test2');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class MultipleEmptyLines {
public void method1() {
System.debug('test1');
}


public void method2() {
System.debug('test2');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class SingleEmptyLines {
public void method1() {
System.debug('test1');
}

public void method2() {
System.debug('test2');
}

public void method3() {
System.debug('test3');
}
}