Skip to content

Commit

Permalink
Finish refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoern-jueliger-sap committed Jun 24, 2024
1 parent 6f58ad4 commit 8d35111
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 18 deletions.
80 changes: 70 additions & 10 deletions src/checks/#cc4a#proper_bool_expression.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class /cc4a/proper_bool_expression definition
true_then_false,
false_then_true,
end of enum ty_inverse_booleans structure inverse_booleans.
types:
begin of enum ty_initial_comparison structure initial_comparison,
none,
is_initial,
is_not_initial,
end of enum ty_initial_comparison structure initial_comparison.
types ty_full_names type sorted table of if_ci_atc_source_code_provider=>ty_full_name with unique key table_line.

data code_provider type ref to if_ci_atc_source_code_provider.
Expand Down Expand Up @@ -104,6 +110,11 @@ class /cc4a/proper_bool_expression definition
methods is_boolean_typed
importing token type if_ci_atc_source_code_provider=>ty_token
returning value(is_boolean_typed) type abap_bool.
methods is_initial_comparison
importing
tokens type if_ci_atc_source_code_provider=>ty_tokens
initial_idx type i
returning value(is_initial_comparison) type ty_initial_comparison.
methods synthesize_bool_comp_decl
importing declaration type ty_declaration
returning value(bool_declarations) type ty_declarations.
Expand Down Expand Up @@ -220,9 +231,10 @@ class /cc4a/proper_bool_expression implementation.
if sy-subrc = 0 and <endif>-keyword = 'ENDIF' and procedure-statements[ statement_idx + 2 ]-keyword = 'ELSE'.
insert lines of
analyze_pot_xsd_transform( procedure = procedure statement_idx = statement_idx ) into table findings.
else.
insert lines of
analyze_initial_bool_condition( procedure = procedure statement_idx = statement_idx ) into table findings.
endif.
insert lines of
analyze_initial_bool_condition( procedure = procedure statement_idx = statement_idx ) into table findings.
endif.
if <statement>-keyword = 'COMPUTE'.
if <statement>-tokens[ lines( <statement>-tokens ) - 1 ]-lexeme = '='.
Expand Down Expand Up @@ -478,10 +490,26 @@ class /cc4a/proper_bool_expression implementation.
fixes = assistant_factory->create_quickfixes( ).
data(if_statement) = procedure-statements[ statement_idx ].
delete if_statement-tokens index 1.
data(no_of_tokens) = lines( if_statement-tokens ).
if analyzer->is_token_keyword( token = if_statement-tokens[ no_of_tokens ] keyword = 'INITIAL' ).
data(is_initial_comparison) = is_initial_comparison(
tokens = if_statement-tokens
initial_idx = lines( if_statement-tokens ) ).
if is_initial_comparison <> initial_comparison-none.
if_statement-tokens[ no_of_tokens ]-lexeme = cond #(
when is_initial_comparison = initial_comparison-is_initial
then 'ABAP_FALSE'
else 'ABAP_TRUE' ).
if_statement-tokens[ no_of_tokens - 1 ]-lexeme = '='.
if is_initial_comparison = initial_comparison-is_not_initial.
delete if_statement-tokens index no_of_tokens - 2.
endif.
endif.
endif.
data(condition) = cond #(
when bool_sequence = inverse_booleans-true_then_false
then analyzer->flatten_tokens( if_statement-tokens )
else analyzer->negate_logical_expression( tokens = if_statement-tokens ) ).
else analyzer->negate_logical_expression( if_statement-tokens ) ).
data(xsd_statement) = |{ target } = xsdbool( { condition } ).|.
fixes->create_quickfix( quickfix_codes-if_else )->replace(
context = assistant_factory->create_quickfix_context( value #(
Expand All @@ -495,22 +523,54 @@ class /cc4a/proper_bool_expression implementation.
loop at <statement>-tokens assigning field-symbol(<token>).
data(token_idx) = sy-tabix.
if analyzer->is_token_keyword( token = <token> keyword = 'INITIAL' ).
assign <statement>-tokens[ token_idx - 1 ] to field-symbol(<previous_token>).
if ( analyzer->is_token_keyword( token = <previous_token> keyword = 'IS' )
and is_boolean_typed( <statement>-tokens[ token_idx - 2 ] ) )
or ( analyzer->is_token_keyword( token = <previous_token> keyword = 'NOT' )
and analyzer->is_token_keyword( token = <statement>-tokens[ token_idx - 2 ] keyword = 'IS' )
and is_boolean_typed( <statement>-tokens[ token_idx - 3 ] ) ).
data(is_initial_comparison) = is_initial_comparison(
tokens = <statement>-tokens
initial_idx = token_idx ).
if is_initial_comparison <> initial_comparison-none.
data(fixes) = assistant_factory->create_quickfixes( ).
fixes->create_quickfix( quickfix_codes-initial_boolean )->replace(
context = assistant_factory->create_quickfix_context( value #(
procedure_id = procedure-id
statements = value #( from = statement_idx to = statement_idx )
tokens = value #(
from = cond #(
when is_initial_comparison = initial_comparison-is_initial
then token_idx - 1
else token_idx - 2 )
to = token_idx ) ) )
code = cond #(
when is_initial_comparison = initial_comparison-is_initial
then value #( ( `= ABAP_FALSE` ) )
else value #( ( `= ABAP_TRUE` ) ) ) ).
insert value #(
code = finding_codes-initial
location = code_provider->get_statement_location( <statement> )
checksum = code_provider->get_statement_checksum( <statement> )
has_pseudo_comment = meta_data->has_valid_pseudo_comment(
statement = <statement>
finding_code = finding_codes-initial ) ) into table findings.
finding_code = finding_codes-initial )
details = assistant_factory->create_finding_details( )->attach_quickfixes( fixes ) ) into table findings.
endif.
endif.
endloop.
endmethod.

method is_initial_comparison.
assign tokens[ initial_idx - 1 ] to field-symbol(<previous_token>).
data(is_is_initial) = xsdbool(
( analyzer->is_token_keyword( token = <previous_token> keyword = 'IS' )
and is_boolean_typed( tokens[ initial_idx - 2 ] ) ) ).
data(is_is_not_initial) = xsdbool( (
analyzer->is_token_keyword( token = <previous_token> keyword = 'NOT' )
and analyzer->is_token_keyword( token = tokens[ initial_idx - 2 ] keyword = 'IS' )
and is_boolean_typed( tokens[ initial_idx - 3 ] ) ) ).
return cond #(
when is_is_initial = abap_true
then initial_comparison-is_initial
else cond #(
when is_is_not_initial = abap_true
then initial_comparison-is_not_initial
else initial_comparison-none ) ).
endmethod.

endclass.
4 changes: 2 additions & 2 deletions src/checks/#cc4a#proper_bool_expression.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class test implementation.
quickfix_code = /cc4a/proper_bool_expression=>quickfix_codes-if_else
location = if_then_else( value #( line = 48 column = 4 ) )
code = value #(
( `B = xsdbool( 1 <> 2 OR 'test' EQ SUBSTRING( LEN = TEST_METHOD( IPARAMETER = 3 ) VAL = STRING ) OR 5 LE 2 ).` )
( `B = xsdbool( NOT ( 1 = 2 AND 'test' NE SUBSTRING( LEN = TEST_METHOD( IPARAMETER = 3 ) VAL = STRING ) AND 5 GT 2 ) ).` )
( ` ` ) ( ` ` ) ( ` ` ) ( ` ` ) ) ) ) )
( location = if_then_else( value #( line = 54 column = 4 ) )
quickfixes = value #( (
Expand All @@ -97,7 +97,7 @@ class test implementation.
quickfix_code = /cc4a/proper_bool_expression=>quickfix_codes-if_else
location = if_then_else( value #( line = 66 column = 4 ) )
code = value #(
( `B = xsdbool( TABLE2[ 4 ]-TABLE[ 1 ]-BOOLEAN IS NOT INITIAL ).` ) ( ` ` ) ( ` ` ) ( ` ` ) ( ` ` ) ) ) ) )
( `B = xsdbool( TABLE2[ 4 ]-TABLE[ 1 ]-BOOLEAN = ABAP_TRUE ).` ) ( ` ` ) ( ` ` ) ( ` ` ) ( ` ` ) ) ) ) )
( location = if_then_else( value #( line = 72 column = 4 ) )
quickfixes = value #( (
quickfix_code = /cc4a/proper_bool_expression=>quickfix_codes-if_else
Expand Down
23 changes: 17 additions & 6 deletions src/core/#cc4a#abap_analyzer.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,23 @@ class /cc4a/abap_analyzer implementation.
else.
insert value #( lexeme = 'NOT' ) into new_tokens index tokens_end.
endif.
elseif is_token_keyword( token = new_tokens[ 2 ] keyword = 'IN' ).
insert value #( lexeme = 'NOT' ) into new_tokens index 2.
elseif is_token_keyword( token = new_tokens[ 2 ] keyword = 'NOT' ).
delete new_tokens index 2.
elseif /cc4a/if_abap_analyzer~token_is_comparison_operator( token = new_tokens[ 2 ] ).
new_tokens[ 2 ]-lexeme = /cc4a/if_abap_analyzer~negate_comparison_operator( new_tokens[ 2 ]-lexeme ).
elseif is_token_keyword( token = new_tokens[ tokens_end - 1 ] keyword = 'IN' ).
if new_tokens[ tokens_end - 2 ]-lexeme = 'NOT'.
delete new_tokens index tokens_end - 2.
else.
insert value #( lexeme = 'NOT' ) into new_tokens index tokens_end - 1.
endif.
elseif is_token_keyword( token = new_tokens[ tokens_end - 1 ] keyword = 'NOT' ).
delete new_tokens index tokens_end - 1.
elseif /cc4a/if_abap_analyzer~token_is_comparison_operator( token = new_tokens[ tokens_end - 1 ] ).
if new_tokens[ tokens_end ]-lexeme = 'ABAP_TRUE' or new_tokens[ tokens_end ]-lexeme = 'ABAP_FALSE'.
new_tokens[ tokens_end ]-lexeme = cond #(
when new_tokens[ tokens_end ]-lexeme = 'ABAP_TRUE'
then 'ABAP_FALSE'
else 'ABAP_TRUE' ).
else.
new_tokens[ tokens_end - 1 ]-lexeme = /cc4a/if_abap_analyzer~negate_comparison_operator( new_tokens[ tokens_end - 1 ]-lexeme ).
endif.
else.
insert value #( lexeme = 'NOT (' ) into new_tokens index 1.
insert value #( lexeme = ')' ) into table new_tokens.
Expand Down

0 comments on commit 8d35111

Please sign in to comment.