1- import { Check , Context , SourceCodeType } from '../../..' ;
1+ import { LiquidTag } from '@shopify/liquid-html-parser' ;
2+ import { Problem , SourceCodeType } from '../../..' ;
23import { ensureValidAst , INVALID_SYNTAX_MESSAGE } from './utils' ;
34
45export function detectMultipleAssignValues (
5- context : Context < SourceCodeType . LiquidHtml > ,
6- ) : Check < SourceCodeType . LiquidHtml > {
6+ node : LiquidTag ,
7+ ) : Problem < SourceCodeType . LiquidHtml > | undefined {
78 // Using a regex to match the markup like we do in Shopify/liquid
89 // https://github.com/Shopify/liquid/blob/9bb7fbf123e6e2bd61e00189b1c83159f375d3f3/lib/liquid/tags/assign.rb#L21
910 //
@@ -14,84 +15,79 @@ export function detectMultipleAssignValues(
1415 // 4. The filter section (non-captured)
1516 const ASSIGN_MARKUP_REGEX = / ( [ ^ = ] + ) ( = \s * ) ( [ ^ | ] + ) (?: \s * \| \s * .* ) ? $ / m;
1617
17- return {
18- async LiquidTag ( node ) {
19- if ( node . name !== 'assign' ) {
20- return ;
21- }
18+ if ( node . name !== 'assign' ) {
19+ return ;
20+ }
2221
23- const markup = node . markup ;
22+ const markup = node . markup ;
2423
25- if ( typeof markup !== 'string' ) {
26- return ;
27- }
24+ if ( typeof markup !== 'string' ) {
25+ return ;
26+ }
2827
29- const match = markup . match ( ASSIGN_MARKUP_REGEX ) ;
30- if ( ! match ) {
31- return ;
32- }
28+ const match = markup . match ( ASSIGN_MARKUP_REGEX ) ;
29+ if ( ! match ) {
30+ return ;
31+ }
3332
34- // If we have a markup 'foo = "123" something | upcase: 123', we have the following groups
35- const [
36- // 'foo = "123" something | upcase: 123'
37- _fullMatch ,
38- // 'foo '
39- assignmentVariable ,
40- // '= '
41- assignmentOperator ,
42- // '"123" something'
43- assignmentValue ,
44- ] = match ;
33+ // If we have a markup 'foo = "123" something | upcase: 123', we have the following groups
34+ const [
35+ // 'foo = "123" something | upcase: 123'
36+ _fullMatch ,
37+ // 'foo '
38+ assignmentVariable ,
39+ // '= '
40+ assignmentOperator ,
41+ // '"123" something'
42+ assignmentValue ,
43+ ] = match ;
4544
46- // Only capture the first item in the value section
47- const firstValueMatch = assignmentValue . match ( / " [ ^ " ] * " | ' [ ^ ' ] * ' | \S + / ) ;
48- const firstAssignmentValue = firstValueMatch ? firstValueMatch [ 0 ] : null ;
45+ // Only capture the first item in the value section
46+ const firstValueMatch = assignmentValue . match ( / " [ ^ " ] * " | ' [ ^ ' ] * ' | \S + / ) ;
47+ const firstAssignmentValue = firstValueMatch ? firstValueMatch [ 0 ] : null ;
4948
50- if ( ! firstAssignmentValue ) {
51- return ;
52- }
49+ if ( ! firstAssignmentValue ) {
50+ return ;
51+ }
5352
54- const removalIndices = ( source : string ) => {
55- const offset = source . indexOf ( markup ) ;
53+ const removalIndices = ( source : string ) => {
54+ const offset = source . indexOf ( markup ) ;
5655
57- return {
58- startIndex :
59- offset +
60- assignmentVariable . length +
61- assignmentOperator . length +
62- firstAssignmentValue . length ,
63- endIndex :
64- offset +
65- assignmentVariable . length +
66- assignmentOperator . length +
67- assignmentValue . trimEnd ( ) . length ,
68- } ;
69- } ;
56+ return {
57+ startIndex :
58+ offset +
59+ assignmentVariable . length +
60+ assignmentOperator . length +
61+ firstAssignmentValue . length ,
62+ endIndex :
63+ offset +
64+ assignmentVariable . length +
65+ assignmentOperator . length +
66+ assignmentValue . trimEnd ( ) . length ,
67+ } ;
68+ } ;
7069
71- const tagSource = node . source . slice ( node . position . start , node . position . end ) ;
72- const { startIndex : tagSourceRemovalStartIndex , endIndex : tagSourceRemovalEndIndex } =
73- removalIndices ( tagSource ) ;
70+ const tagSource = node . source . slice ( node . position . start , node . position . end ) ;
71+ const { startIndex : tagSourceRemovalStartIndex , endIndex : tagSourceRemovalEndIndex } =
72+ removalIndices ( tagSource ) ;
7473
75- if (
76- ! ensureValidAst (
77- tagSource . slice ( 0 , tagSourceRemovalStartIndex ) +
78- tagSource . slice ( tagSourceRemovalEndIndex ) ,
79- )
80- ) {
81- // If the new AST is invalid, we don't want to auto-fix it
82- return ;
83- }
74+ if (
75+ ! ensureValidAst (
76+ tagSource . slice ( 0 , tagSourceRemovalStartIndex ) + tagSource . slice ( tagSourceRemovalEndIndex ) ,
77+ )
78+ ) {
79+ // If the new AST is invalid, we don't want to auto-fix it
80+ return ;
81+ }
8482
85- const { startIndex, endIndex } = removalIndices ( node . source ) ;
83+ const { startIndex, endIndex } = removalIndices ( node . source ) ;
8684
87- context . report ( {
88- message : INVALID_SYNTAX_MESSAGE ,
89- startIndex,
90- endIndex,
91- fix : ( corrector ) => {
92- corrector . replace ( startIndex , endIndex , '' ) ;
93- } ,
94- } ) ;
85+ return {
86+ message : INVALID_SYNTAX_MESSAGE ,
87+ startIndex,
88+ endIndex,
89+ fix : ( corrector ) => {
90+ corrector . replace ( startIndex , endIndex , '' ) ;
9591 } ,
9692 } ;
9793}
0 commit comments