Skip to content
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: add new rule 'noGremlins' #189

Merged
merged 4 commits into from
Dec 27, 2022
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Configuration is via a `.sasjslint` file with the following structure (these are
"lowerCaseFileNames": true,
"maxLineLength": 80,
"noNestedMacros": true,
"noGremlins": true,
"noSpacesInFileNames": true,
"noTabs": true,
"noTrailingSpaces": true,
Expand Down Expand Up @@ -125,6 +126,15 @@ We strongly recommend a line length limit, and set the bar at 80. To turn this f
- Default: 80
- Severity: WARNING

### noGremlins

Capture zero-width whitespace and other non-standard characters. The logic is borrowed from the [VSCode Gremlins Extension](https://github.com/nhoizey/vscode-gremlins) - if you are looking for more advanced gremlin zapping capabilities, we highly recommend to use their extension instead.

The list of characters can be found in this file: [https://github.com/sasjs/lint/blob/main/src/rules/line/noGremlins.ts](https://github.com/sasjs/lint/blob/main/src/rules/line/noGremlins.ts)

- Default: true
- Severity: WARNING

### noNestedMacros

Where macros are defined inside other macros, they are recompiled every time the outer macro is invoked. Hence, it is widely considered inefficient, and bad practice, to nest macro definitions.
Expand Down
17 changes: 17 additions & 0 deletions sasjslint-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"indentationMultiple": 2,
"lowerCaseFileNames": true,
"maxLineLength": 80,
"noGremlins": true,
"noNestedMacros": true,
"noSpacesInFileNames": true,
"noTabs": true,
Expand All @@ -29,6 +30,7 @@
"noSpacesInFileNames": true,
"lowerCaseFileNames": true,
"maxLineLength": 80,
"noGremlins": true,
"noTabs": true,
"indentationMultiple": 4,
"hasMacroNameInMend": true,
Expand Down Expand Up @@ -64,6 +66,14 @@
"default": "/**{lineEnding} @file{lineEnding} @brief <Your brief here>{lineEnding} <h4> SAS Macros </h4>{lineEnding}**/",
"examples": []
},
"noGremlins": {
"$id": "#/properties/noGremlins",
"type": "array",
"title": "noGremlins",
"description": "Captures problematic characters such as zero-width whitespace and others that look valid but usually are not (such as the en dash)",
"default": [true],
"examples": [true, false]
},
"hasMacroNameInMend": {
"$id": "#/properties/hasMacroNameInMend",
"type": "boolean",
Expand Down Expand Up @@ -193,6 +203,13 @@
"enum": ["error", "warn"],
"default": "warn"
},
"noGremlins": {
"$id": "#/properties/severityLevel/noGremlins",
"title": "noGremlins",
"type": "string",
"enum": ["error", "warn"],
"default": "warn"
},
"hasMacroNameInMend": {
"$id": "#/properties/severityLevel/hasMacroNameInMend",
"title": "hasMacroNameInMend",
Expand Down
1 change: 1 addition & 0 deletions src/rules/line/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { noGremlins } from './noGremlins'
export { indentationMultiple } from './indentationMultiple'
export { maxLineLength } from './maxLineLength'
export { noEncodedPasswords } from './noEncodedPasswords'
Expand Down
15 changes: 15 additions & 0 deletions src/rules/line/noGremlins.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Severity } from '../../types/Severity'
import { noGremlins } from './noGremlins'

describe('noTabs', () => {
it('should return an empty array when the line does not have any gremlin', () => {
const line = "%put 'hello';"
expect(noGremlins.test(line, 1)).toEqual([])
})

it('should return a diagnostic array when the line contains gremlins', () => {
const line = "– ‘ %put 'hello';"
const diagnostics = noGremlins.test(line, 1)
expect(diagnostics.length).toEqual(2)
})
})
128 changes: 128 additions & 0 deletions src/rules/line/noGremlins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { Diagnostic, LintConfig } from '../../types'
import { LineLintRule } from '../../types/LintRule'
import { LintRuleType } from '../../types/LintRuleType'
import { Severity } from '../../types/Severity'

const name = 'noGremlins'
const description = 'Disallow characters specified in gremlins array'
const message = 'Line contains a gremlin'

const test = (value: string, lineNumber: number, config?: LintConfig) => {
const severity = config?.severityLevel[name] || Severity.Warning

const diagnostics: Diagnostic[] = []

const gremlins: any = {}

for (const [hexCode, config] of Object.entries(gremlinCharacters)) {
gremlins[charFromHex(hexCode)] = Object.assign({}, config, {
hexCode
})
}

const regexpWithAllChars = new RegExp(
Object.keys(gremlins)
.map((char) => `${char}+`)
.join('|'),
'g'
)

let match
while ((match = regexpWithAllChars.exec(value))) {
const matchedCharacter = match[0][0]
const gremlin = gremlins[matchedCharacter]

diagnostics.push({
message: `${message}: ${gremlin.description}, hexCode(${gremlin.hexCode})`,
lineNumber,
startColumnNumber: match.index + 1,
endColumnNumber: match.index + 1 + match[0].length,
severity
})
}

return diagnostics
}

/**
* Lint rule that checks if a given line of text contains any gremlins.
*/
export const noGremlins: LineLintRule = {
type: LintRuleType.Line,
name,
description,
message,
test
}

const charFromHex = (hexCode: string) => String.fromCodePoint(parseInt(hexCode))

const gremlinCharacters = {
'0x2013': {
description: 'en dash'
},
'0x2018': {
description: 'left single quotation mark'
},
'0x2019': {
description: 'right single quotation mark'
},
'0x2029': {
zeroWidth: true,
description: 'paragraph separator'
},
'0x2066': {
zeroWidth: true,
description: 'Left to right'
},
'0x2069': {
zeroWidth: true,
description: 'Pop directional'
},
'0x0003': {
description: 'end of text'
},
'0x000b': {
description: 'line tabulation'
},
'0x00a0': {
description: 'non breaking space'
},
'0x00ad': {
description: 'soft hyphen'
},
'0x200b': {
zeroWidth: true,
description: 'zero width space'
},
'0x200c': {
zeroWidth: true,
description: 'zero width non-joiner'
},
'0x200e': {
zeroWidth: true,
description: 'left-to-right mark'
},
'0x201c': {
description: 'left double quotation mark'
},
'0x201d': {
description: 'right double quotation mark'
},
'0x202c': {
zeroWidth: true,
description: 'pop directional formatting'
},
'0x202d': {
zeroWidth: true,
description: 'left-to-right override'
},
'0x202e': {
zeroWidth: true,
description: 'right-to-left override'
},
'0xfffc': {
zeroWidth: true,
description: 'object replacement character'
}
}
7 changes: 6 additions & 1 deletion src/types/LintConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
maxLineLength,
noEncodedPasswords,
noTabs,
noTrailingSpaces
noTrailingSpaces,
noGremlins
} from '../rules/line'
import { lowerCaseFileNames, noSpacesInFileNames } from '../rules/path'
import { LineEndings } from './LineEndings'
Expand Down Expand Up @@ -119,6 +120,10 @@ export class LintConfig {
this.fileLintRules.push(strictMacroDefinition)
}

if (json?.noGremlins) {
this.lineLintRules.push(noGremlins)
}

if (json?.severityLevel) {
for (const [rule, severity] of Object.entries(json.severityLevel)) {
if (severity === 'warn') this.severityLevel[rule] = Severity.Warning
Expand Down