|
| 1 | +import type { LiquidTag, AssignMarkup } from './parser-compat'; |
| 2 | +import type { Context } from './context'; |
| 3 | +import { |
| 4 | + variableHasBareArrayAccess, |
| 5 | + hasSkippedCharacters, |
| 6 | + hasSkippedPrefixCharacters, |
| 7 | + hasRubyAcceptedEmptyFirstFilterArgument, |
| 8 | + hasRubyAcceptedEmptyAssignRhs, |
| 9 | + hasRubyAcceptedAssignLhsExtraIdentifier, |
| 10 | + hasRubyAcceptedAssignLhsUnclosedQuote, |
| 11 | + variableHasBareContainsValueExpression, |
| 12 | + rawMarkup, |
| 13 | +} from './utils'; |
| 14 | + |
| 15 | +export function checkAssignTag(node: LiquidTag, context: Context): void { |
| 16 | + if (typeof node.markup === 'string') { |
| 17 | + if (hasRubyAcceptedEmptyAssignRhs(node.markup)) return; |
| 18 | + if (hasRubyAcceptedEmptyFirstFilterArgument(node.markup)) return; |
| 19 | + if (hasRubyAcceptedAssignLhsExtraIdentifier(node.markup)) return; |
| 20 | + if (hasRubyAcceptedAssignLhsUnclosedQuote(node.markup)) return; |
| 21 | + |
| 22 | + context.report({ |
| 23 | + message: `Syntax error in 'assign' tag`, |
| 24 | + startIndex: node.position.start, |
| 25 | + endIndex: node.position.end, |
| 26 | + }); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const markup = node.markup as AssignMarkup; |
| 31 | + |
| 32 | + if (variableHasBareArrayAccess(markup.value)) { |
| 33 | + context.report({ |
| 34 | + message: 'Bare bracket access is not allowed', |
| 35 | + startIndex: node.position.start, |
| 36 | + endIndex: node.position.end, |
| 37 | + }); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (variableHasBareContainsValueExpression(markup.value)) { |
| 42 | + context.report({ |
| 43 | + message: `Syntax error in 'assign' tag`, |
| 44 | + startIndex: node.position.start, |
| 45 | + endIndex: node.position.end, |
| 46 | + }); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (hasSkippedPrefixCharacters(node.source, node.markupPosition.start, markup.position.start)) { |
| 51 | + context.report({ |
| 52 | + message: `Syntax error in 'assign' tag`, |
| 53 | + startIndex: node.position.start, |
| 54 | + endIndex: node.position.end, |
| 55 | + }); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const raw = rawMarkup(node); |
| 60 | + if (hasRubyAcceptedEmptyAssignRhs(raw)) return; |
| 61 | + if (hasRubyAcceptedEmptyFirstFilterArgument(raw)) return; |
| 62 | + if (hasRubyAcceptedAssignLhsExtraIdentifier(raw)) return; |
| 63 | + if (hasRubyAcceptedAssignLhsUnclosedQuote(raw)) return; |
| 64 | + |
| 65 | + const parsedRaw = node.source.slice(markup.position.start, node.markupPosition.end); |
| 66 | + if (hasSkippedCharacters(parsedRaw)) { |
| 67 | + context.report({ |
| 68 | + message: `Syntax error in 'assign' tag`, |
| 69 | + startIndex: node.position.start, |
| 70 | + endIndex: node.position.end, |
| 71 | + }); |
| 72 | + } |
| 73 | +} |
0 commit comments