Coverage for sources / vibelinter / rules / violations.py: 89%

18 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-07 04:34 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2 

3# -*- coding: utf-8 -*- 

4 

5#============================================================================# 

6# # 

7# Licensed under the Apache License, Version 2.0 (the "License"); # 

8# you may not use this file except in compliance with the License. # 

9# You may obtain a copy of the License at # 

10# # 

11# http://www.apache.org/licenses/LICENSE-2.0 # 

12# # 

13# Unless required by applicable law or agreed to in writing, software # 

14# distributed under the License is distributed on an "AS IS" BASIS, # 

15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

16# See the License for the specific language governing permissions and # 

17# limitations under the License. # 

18# # 

19#============================================================================# 

20 

21 

22''' Core violation data structures for linting framework. ''' 

23 

24 

25from . import __ 

26 

27 

28class Violation( __.immut.DataclassObject ): 

29 ''' Represents a rule violation with precise location information. ''' 

30 

31 rule_id: __.typx.Annotated[ 

32 str, 

33 __.ddoc.Doc( 

34 'VBL code identifier for the rule that detected this violation.' 

35 ) ] 

36 filename: __.typx.Annotated[ 

37 str, __.ddoc.Doc( 'Path to source file containing violation.' ) ] 

38 line: __.typx.Annotated[ 

39 int, __.ddoc.Doc( 'One-indexed line number of violation.' ) ] 

40 column: __.typx.Annotated[ 

41 int, __.ddoc.Doc( 'One-indexed column position of violation.' ) ] 

42 message: __.typx.Annotated[ 

43 str, __.ddoc.Doc( 'Human-readable description of violation.' ) ] 

44 severity: __.typx.Annotated[ 

45 str, 

46 __.ddoc.Doc( "Severity level: 'error', 'warning', or 'info'." ) ] = ( 

47 'error' ) 

48 

49 def render_as_json( self ) -> dict[ str, __.typx.Any ]: 

50 ''' Renders violation as JSON-compatible dictionary. ''' 

51 return { 

52 'rule_id': self.rule_id, 

53 'filename': self.filename, 

54 'line': self.line, 

55 'column': self.column, 

56 'message': self.message, 

57 'severity': self.severity, 

58 } 

59 

60 def render_as_text( self ) -> str: 

61 ''' Renders violation as text line. ''' 

62 return ( 

63 f' {self.line}:{self.column} ' 

64 f'{self.rule_id} {self.message}' ) 

65 

66 

67class ViolationContext( __.immut.DataclassObject ): 

68 ''' Represents source code context surrounding a violation. 

69 

70 Provides enhanced error reporting with surrounding lines. 

71 ''' 

72 

73 violation: __.typx.Annotated[ 

74 Violation, __.ddoc.Doc( 'The violation this context describes.' ) ] 

75 context_lines: __.typx.Annotated[ 

76 tuple[ str, ... ], 

77 __.ddoc.Doc( 'Source lines surrounding violation.' ) ] 

78 context_start_line: __.typx.Annotated[ 

79 int, __.ddoc.Doc( 'One-indexed starting line of context display.' ) ] 

80 

81 

82# Type aliases for rule framework contracts 

83ViolationSequence: __.typx.TypeAlias = __.cabc.Sequence[ Violation ] 

84ViolationContextSequence: __.typx.TypeAlias = ( 

85 __.cabc.Sequence[ ViolationContext ] )