-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
win, compiler: fix and validate empty comment code
This commit fixes empty code scripts that create revert code with only comment lines. It also improves compiler validation so if a generated code consists solely of comments, it displays error message. TODO: Missing unit tests for compiler
- Loading branch information
1 parent
92ec138
commit 761e0b2
Showing
8 changed files
with
160 additions
and
28 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
33 changes: 33 additions & 0 deletions
33
src/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeCommentOnlyCode.ts
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,33 @@ | ||
import type { ScriptingLanguage } from '@/domain/ScriptingLanguage'; | ||
import { isCommentLine, type CommentLineChecker } from './Common/CommentLineChecker'; | ||
import { createSyntax, type SyntaxFactory } from './Syntax/SyntaxFactory'; | ||
import type { CodeLine, CodeValidationAnalyzer, InvalidCodeLine } from './CodeValidationAnalyzer'; | ||
|
||
export type CommentOnlyCodeAnalyzer = CodeValidationAnalyzer & { | ||
( | ||
...args: [ | ||
...Parameters<CodeValidationAnalyzer>, | ||
syntaxFactory?: SyntaxFactory, | ||
commentLineChecker?: CommentLineChecker, | ||
] | ||
): ReturnType<CodeValidationAnalyzer>; | ||
}; | ||
|
||
export const analyzeCommentOnlyCode: CommentOnlyCodeAnalyzer = ( | ||
lines: readonly CodeLine[], | ||
language: ScriptingLanguage, | ||
syntaxFactory: SyntaxFactory = createSyntax, | ||
commentLineChecker: CommentLineChecker = isCommentLine, | ||
) => { | ||
const syntax = syntaxFactory(language); | ||
if (!lines.every((line) => commentLineChecker(line.text, syntax))) { | ||
return []; | ||
} | ||
return lines | ||
.map((line): InvalidCodeLine => ({ | ||
lineNumber: line.lineNumber, | ||
error: (() => { | ||
return 'Script is consists of comments only'; | ||
})(), | ||
})); | ||
}; |
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
14 changes: 14 additions & 0 deletions
14
src/application/Parser/Executable/Script/Validation/Analyzers/Common/CommentLineChecker.ts
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,14 @@ | ||
import type { LanguageSyntax } from '../Syntax/LanguageSyntax'; | ||
|
||
export interface CommentLineChecker { | ||
( | ||
codeLine: string, | ||
syntax: LanguageSyntax, | ||
): boolean; | ||
} | ||
|
||
export const isCommentLine: CommentLineChecker = (codeLine, syntax) => { | ||
return syntax.commentDelimiters.some( | ||
(delimiter) => codeLine.startsWith(delimiter), | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ export enum CodeValidationRule { | |
NoEmptyLines, | ||
NoDuplicatedLines, | ||
NoTooLongLines, | ||
NoCommentOnlyLines, | ||
} |
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