generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ui5-lint-disable directives
- Loading branch information
1 parent
e316ef0
commit 4063044
Showing
10 changed files
with
158 additions
and
32 deletions.
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
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,98 @@ | ||
import ts from "typescript"; | ||
import {LintMetadata} from "../LinterContext.js"; | ||
|
||
export function findDirectives(sourceFile: ts.SourceFile, metadata: LintMetadata) { | ||
metadata.directives = new Set<Directive>(); | ||
|
||
const possibleDirectives = collectPossibleDirectives(sourceFile); | ||
if (possibleDirectives.size === 0) { | ||
return; | ||
} | ||
const sourceText = sourceFile.getFullText(); | ||
|
||
traverseAndFindDirectives(sourceFile, sourceText, possibleDirectives, metadata.directives); | ||
} | ||
|
||
function traverseAndFindDirectives( | ||
node: ts.Node, sourceText: string, possibleDirectives: Set<Directive>, confirmedDirectives: Set<Directive> | ||
) { | ||
findDirectivesAroundNode(node, sourceText, possibleDirectives, confirmedDirectives); | ||
node.getChildren().forEach((child) => { | ||
traverseAndFindDirectives(child, sourceText, possibleDirectives, confirmedDirectives); | ||
}); | ||
} | ||
|
||
export function findDirectivesAroundNode( | ||
node: ts.Node, sourceText: string, possibleDirectives: Set<Directive>, confirmedDirectives: Set<Directive> | ||
) { | ||
/* | ||
// This is a comment | ||
// ui5-lint-disable | ||
myCallExpression() | ||
// ui5-lint-enable | ||
// This is a comment | ||
*/ | ||
for (const directive of possibleDirectives) { | ||
if (directive.pos >= node.getFullStart() && directive.pos + directive.length < node.getStart()) { | ||
const leadingComments = ts.getLeadingCommentRanges(sourceText, node.getFullStart()); | ||
if (leadingComments?.length) { | ||
leadingComments.some((comment) => { | ||
if (comment.pos === directive.pos) { | ||
possibleDirectives.delete(directive); | ||
confirmedDirectives.add(directive); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
break; | ||
} | ||
} else if (directive.pos > node.getEnd() && directive.pos + directive.length <= node.getEnd()) { | ||
const trailingComments = ts.getTrailingCommentRanges(sourceText, node.getEnd()); | ||
if (trailingComments?.length) { | ||
trailingComments.some((comment) => { | ||
if (comment.pos === directive.pos) { | ||
possibleDirectives.delete(directive); | ||
confirmedDirectives.add(directive); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* Match things like: | ||
ui5-lint-disable-next-line no-deprecated-api no-global | ||
ui5-lint-disable | ||
no-deprecated-api | ||
no-global | ||
ui5-lint-enable-next-line | ||
*/ | ||
const disableCommentRegex = /\/[/*]\s*ui5lint-(enable|disable)((?:-next)?-line)?((?:\s+[\w-]+,)*(?:\s+[\w-]+))?\s*(?:\*\/|$)/mg; | ||
|
||
export interface Directive { | ||
action: string; | ||
isLine: boolean; | ||
isNextLine: boolean; | ||
ruleNames: string[]; | ||
pos: number; | ||
length: number; | ||
} | ||
|
||
export function collectPossibleDirectives(sourceFile: ts.SourceFile) { | ||
const text = sourceFile.getFullText(); | ||
let match; | ||
const comments = new Set<Directive>(); | ||
while ((match = disableCommentRegex.exec(text)) !== null) { | ||
const [, action, nextLineOrLine, rules] = match; | ||
const pos = match.index; | ||
const length = match[0].length; | ||
const ruleNames = rules?.trim().split(",") ?? []; | ||
const isLine = nextLineOrLine === "-line"; | ||
const isNextLine = nextLineOrLine === "-next-line"; | ||
comments.add({action, isLine, isNextLine, ruleNames, pos, length}); | ||
} | ||
return comments; | ||
} |
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
Binary file not shown.