-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: new rules noNestedMacros & hasMacroParentheses #22
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { Severity } from '../types/Severity' | ||
import { hasMacroParentheses } from './hasMacroParentheses' | ||
|
||
describe('hasMacroParentheses', () => { | ||
it('should return an empty array when macro defined correctly', () => { | ||
const content = ` | ||
%macro somemacro(); | ||
%put &sysmacroname; | ||
%mend somemacro;` | ||
|
||
expect(hasMacroParentheses.test(content)).toEqual([]) | ||
}) | ||
|
||
it('should return an array with a single diagnostics when macro defined without parentheses', () => { | ||
const content = ` | ||
%macro somemacro; | ||
%put &sysmacroname; | ||
%mend somemacro;` | ||
|
||
expect(hasMacroParentheses.test(content)).toEqual([ | ||
{ | ||
message: 'Macro definition missing parentheses', | ||
lineNumber: 2, | ||
startColumnNumber: 10, | ||
endColumnNumber: 18, | ||
severity: Severity.Warning | ||
} | ||
]) | ||
}) | ||
|
||
it('should return an array with a single diagnostics when macro defined without name', () => { | ||
const content = ` | ||
%macro (); | ||
%put &sysmacroname; | ||
%mend;` | ||
|
||
expect(hasMacroParentheses.test(content)).toEqual([ | ||
{ | ||
message: 'Macro definition missing name', | ||
lineNumber: 2, | ||
startColumnNumber: 3, | ||
endColumnNumber: 12, | ||
severity: Severity.Warning | ||
} | ||
]) | ||
}) | ||
|
||
it('should return an array with a single diagnostics when macro defined without name and parentheses', () => { | ||
const content = ` | ||
%macro ; | ||
%put &sysmacroname; | ||
%mend;` | ||
|
||
expect(hasMacroParentheses.test(content)).toEqual([ | ||
{ | ||
message: 'Macro definition missing name', | ||
lineNumber: 2, | ||
startColumnNumber: 3, | ||
endColumnNumber: 10, | ||
severity: Severity.Warning | ||
} | ||
]) | ||
}) | ||
|
||
it('should return an empty array when the file is undefined', () => { | ||
const content = undefined | ||
|
||
expect(hasMacroParentheses.test((content as unknown) as string)).toEqual([]) | ||
}) | ||
|
||
describe('with extra spaces and comments', () => { | ||
it('should return an empty array when %mend has correct macro name', () => { | ||
const content = ` | ||
/* 1st comment */ | ||
%macro somemacro(); | ||
%put &sysmacroname; | ||
/* 2nd | ||
comment */ | ||
/* 3rd comment */ %mend somemacro ;` | ||
|
||
expect(hasMacroParentheses.test(content)).toEqual([]) | ||
}) | ||
|
||
it('should return an array with a single diagnostic when macro defined without parentheses having code in comments', () => { | ||
const content = `/** | ||
@file examplemacro.sas | ||
@brief an example of a macro to be used in a service | ||
@details This macro is great. Yadda yadda yadda. Usage: | ||
* code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces; code formatting applies when indented by 4 spaces; | ||
some code | ||
%macro examplemacro123(); | ||
%examplemacro() | ||
<h4> SAS Macros </h4> | ||
@li doesnothing.sas | ||
@author Allan Bowe | ||
**/ | ||
%macro examplemacro; | ||
proc sql; | ||
create table areas | ||
as select area | ||
from sashelp.springs; | ||
%doesnothing(); | ||
%mend;` | ||
|
||
expect(hasMacroParentheses.test(content)).toEqual([ | ||
{ | ||
message: 'Macro definition missing parentheses', | ||
lineNumber: 19, | ||
startColumnNumber: 12, | ||
endColumnNumber: 23, | ||
severity: Severity.Warning | ||
} | ||
]) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Diagnostic } from '../types/Diagnostic' | ||
import { FileLintRule } from '../types/LintRule' | ||
import { LintRuleType } from '../types/LintRuleType' | ||
import { Severity } from '../types/Severity' | ||
import { trimComments } from '../utils/trimComments' | ||
import { getLineNumber } from '../utils/getLineNumber' | ||
import { getColNumber } from '../utils/getColNumber' | ||
|
||
const name = 'hasMacroParentheses' | ||
const description = 'Macros are always defined with parentheses' | ||
const message = 'Macro definition missing parentheses' | ||
const test = (value: string) => { | ||
const diagnostics: Diagnostic[] = [] | ||
|
||
const statements: string[] = value ? value.split(';') : [] | ||
|
||
let trimmedStatement = '', | ||
commentStarted = false | ||
statements.forEach((statement, index) => { | ||
;({ statement: trimmedStatement, commentStarted } = trimComments( | ||
statement, | ||
commentStarted | ||
)) | ||
|
||
if (trimmedStatement.startsWith('%macro')) { | ||
const macroNameDefinition = trimmedStatement | ||
.slice(7, trimmedStatement.length) | ||
.trim() | ||
|
||
const macroNameDefinitionParts = macroNameDefinition.split('(') | ||
const macroName = macroNameDefinitionParts[0] | ||
|
||
if (!macroName) | ||
diagnostics.push({ | ||
message: 'Macro definition missing name', | ||
lineNumber: getLineNumber(statements, index + 1), | ||
startColumnNumber: getColNumber(statement, '%macro'), | ||
endColumnNumber: statement.length, | ||
severity: Severity.Warning | ||
}) | ||
else if (macroNameDefinitionParts.length === 1) | ||
diagnostics.push({ | ||
message, | ||
lineNumber: getLineNumber(statements, index + 1), | ||
startColumnNumber: getColNumber(statement, macroNameDefinition), | ||
endColumnNumber: | ||
getColNumber(statement, macroNameDefinition) + | ||
macroNameDefinition.length - | ||
1, | ||
severity: Severity.Warning | ||
}) | ||
else if (macroName !== macroName.trim()) | ||
diagnostics.push({ | ||
message: 'Macro definition cannot have space', | ||
lineNumber: getLineNumber(statements, index + 1), | ||
startColumnNumber: getColNumber(statement, macroNameDefinition), | ||
endColumnNumber: | ||
getColNumber(statement, macroNameDefinition) + | ||
macroNameDefinition.length - | ||
1, | ||
severity: Severity.Warning | ||
}) | ||
} | ||
}) | ||
return diagnostics | ||
} | ||
|
||
/** | ||
* Lint rule that checks for the presence of macro name in %mend statement. | ||
*/ | ||
export const hasMacroParentheses: FileLintRule = { | ||
type: LintRuleType.File, | ||
name, | ||
description, | ||
message, | ||
test | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Severity } from '../types/Severity' | ||
import { noNestedMacros } from './noNestedMacros' | ||
|
||
describe('noNestedMacros', () => { | ||
it('should return an empty array when no nested macro', () => { | ||
const content = ` | ||
%macro somemacro(); | ||
%put &sysmacroname; | ||
%mend somemacro;` | ||
|
||
expect(noNestedMacros.test(content)).toEqual([]) | ||
}) | ||
|
||
it('should return an array with a single diagnostics when nested macro defined', () => { | ||
const content = ` | ||
%macro outer(); | ||
/* any amount of arbitrary code */ | ||
%macro inner(); | ||
%put inner; | ||
%mend; | ||
%inner() | ||
%put outer; | ||
%mend; | ||
%outer()` | ||
|
||
expect(noNestedMacros.test(content)).toEqual([ | ||
{ | ||
message: "Macro definition present inside another macro 'outer'", | ||
lineNumber: 4, | ||
startColumnNumber: 7, | ||
endColumnNumber: 20, | ||
severity: Severity.Warning | ||
} | ||
]) | ||
}) | ||
|
||
it('should return an array with a single diagnostics when nested macro defined 2 levels', () => { | ||
const content = ` | ||
%macro outer(); | ||
/* any amount of arbitrary code */ | ||
%macro inner(); | ||
%put inner; | ||
%macro inner2(); | ||
%put inner2; | ||
%mend; | ||
%mend; | ||
%inner() | ||
%put outer; | ||
%mend; | ||
%outer()` | ||
|
||
expect(noNestedMacros.test(content)).toEqual([ | ||
{ | ||
message: "Macro definition present inside another macro 'outer'", | ||
lineNumber: 4, | ||
startColumnNumber: 7, | ||
endColumnNumber: 20, | ||
severity: Severity.Warning | ||
}, | ||
{ | ||
message: "Macro definition present inside another macro 'inner'", | ||
lineNumber: 7, | ||
startColumnNumber: 17, | ||
endColumnNumber: 31, | ||
severity: Severity.Warning | ||
} | ||
]) | ||
}) | ||
|
||
it('should return an empty array when the file is undefined', () => { | ||
const content = undefined | ||
|
||
expect(noNestedMacros.test((content as unknown) as string)).toEqual([]) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the correct description for this rule.