-
Notifications
You must be signed in to change notification settings - Fork 56
Updated the undefined-object theme check to skip over inline-snippets #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
shoplando
wants to merge
2
commits into
main
Choose a base branch
from
update-undefined-object-for-inline-snippets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−7
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@shopify/theme-check-common': minor | ||
| --- | ||
|
|
||
| Updated the `Undefined-object` theme check to skip over the new `snippet` tags |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| LiquidTagDecrement, | ||
| LiquidTagFor, | ||
| LiquidTagIncrement, | ||
| LiquidTagSnippet, | ||
| LiquidTagTablerow, | ||
| LiquidVariableLookup, | ||
| NamedTags, | ||
|
|
@@ -16,7 +17,7 @@ import { | |
| import { LiquidCheckDefinition, Severity, SourceCodeType, ThemeDocset } from '../../types'; | ||
| import { isError, last } from '../../utils'; | ||
| import { hasLiquidDoc } from '../../liquid-doc/liquidDoc'; | ||
| import { isWithinRawTagThatDoesNotParseItsContents } from '../utils'; | ||
| import { isWithinRawTagThatDoesNotParseItsContents, findInlineSnippetAncestor } from '../utils'; | ||
|
|
||
| type Scope = { start?: number; end?: number }; | ||
|
|
||
|
|
@@ -38,13 +39,14 @@ export const UndefinedObject: LiquidCheckDefinition = { | |
| create(context) { | ||
| const relativePath = context.toRelativePath(context.file.uri); | ||
| const ast = context.file.ast; | ||
| const isSnippetFile = relativePath.startsWith('snippets/'); | ||
|
|
||
| if (isError(ast)) return {}; | ||
|
|
||
| /** | ||
| * Skip this check when a snippet does not have the presence of doc tags. | ||
| */ | ||
| if (relativePath.startsWith('snippets/') && !hasLiquidDoc(ast)) return {}; | ||
| if (isSnippetFile && !hasLiquidDoc(ast)) return {}; | ||
|
|
||
| /** | ||
| * Skip this check when definitions for global objects are unavailable. | ||
|
|
@@ -56,7 +58,9 @@ export const UndefinedObject: LiquidCheckDefinition = { | |
| const themeDocset = context.themeDocset; | ||
| const scopedVariables: Map<string, Scope[]> = new Map(); | ||
| const fileScopedVariables: Set<string> = new Set(); | ||
| const variables: LiquidVariableLookup[] = []; | ||
| const variables: Array<{ node: LiquidVariableLookup; inlineSnippet: LiquidTag | null }> = []; | ||
| const inlineSnippetsWithDocTags: Set<number> = new Set(); | ||
| let snippetFileHasDocTag = false; | ||
|
|
||
| function indexVariableScope(variableName: string | null, scope: Scope) { | ||
| if (!variableName) return; | ||
|
|
@@ -66,9 +70,27 @@ export const UndefinedObject: LiquidCheckDefinition = { | |
| } | ||
|
|
||
| return { | ||
| async LiquidDocParamNode(node: LiquidDocParamNode) { | ||
| async LiquidRawTag(node, ancestors) { | ||
| if (!isSnippetFile || node.name !== 'doc') return; | ||
|
|
||
| const parent = last(ancestors); | ||
| if (isLiquidTag(parent) && isLiquidTagSnippet(parent)) { | ||
| inlineSnippetsWithDocTags.add(parent.position.start); | ||
| } else { | ||
| snippetFileHasDocTag = true; | ||
| } | ||
| }, | ||
|
|
||
| async LiquidDocParamNode(node: LiquidDocParamNode, ancestors: LiquidHtmlNode[]) { | ||
| const paramName = node.paramName?.value; | ||
| if (paramName) { | ||
| if (!paramName) return; | ||
| const snippetAncestor = findInlineSnippetAncestor(ancestors); | ||
| if (snippetAncestor) { | ||
| indexVariableScope(paramName, { | ||
| start: snippetAncestor.blockStartPosition.end, | ||
| end: snippetAncestor.blockEndPosition?.start, | ||
| }); | ||
|
Comment on lines
+89
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL Cool didn't even know we had a variable scope like this |
||
| } else { | ||
| fileScopedVariables.add(paramName); | ||
| } | ||
| }, | ||
|
|
@@ -134,6 +156,10 @@ export const UndefinedObject: LiquidCheckDefinition = { | |
| end: node.blockEndPosition?.start, | ||
| }); | ||
| } | ||
|
|
||
| if (isLiquidTagSnippet(node) && node.markup.name) { | ||
| fileScopedVariables.add(node.markup.name); | ||
| } | ||
| }, | ||
|
|
||
| async VariableLookup(node, ancestors) { | ||
|
|
@@ -142,17 +168,33 @@ export const UndefinedObject: LiquidCheckDefinition = { | |
| const parent = last(ancestors); | ||
| if (isLiquidTag(parent) && isLiquidTagCapture(parent)) return; | ||
|
|
||
| variables.push(node); | ||
| if (isLiquidTag(parent) && isLiquidTagSnippet(parent)) return; | ||
|
|
||
| const inlineSnippet = findInlineSnippetAncestor(ancestors); | ||
| variables.push({ node, inlineSnippet }); | ||
| }, | ||
|
|
||
| async onCodePathEnd() { | ||
| const objects = await globalObjects(themeDocset, relativePath); | ||
|
|
||
| objects.forEach((obj) => fileScopedVariables.add(obj.name)); | ||
|
|
||
| variables.forEach((variable) => { | ||
| variables.forEach(({ node: variable, inlineSnippet }) => { | ||
| if (!variable.name) return; | ||
|
|
||
| // For snippet files: only check variables if they're in a scope with a doc tag. | ||
| if (isSnippetFile) { | ||
| if (inlineSnippet) { | ||
| if (!inlineSnippetsWithDocTags.has(inlineSnippet.position.start)) { | ||
| return; | ||
| } | ||
| } else { | ||
| if (!snippetFileHasDocTag) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const isVariableDefined = isDefined( | ||
| variable.name, | ||
| variable.position, | ||
|
|
@@ -268,6 +310,10 @@ function isLiquidTagCapture(node: LiquidTag): node is LiquidTagCapture { | |
| return node.name === NamedTags.capture; | ||
| } | ||
|
|
||
| function isLiquidTagSnippet(node: LiquidTag): node is LiquidTagSnippet { | ||
| return node.name === NamedTags.snippet; | ||
| } | ||
|
|
||
| function isLiquidTagAssign(node: LiquidTag): node is LiquidTagAssign { | ||
| return node.name === NamedTags.assign && typeof node.markup !== 'string'; | ||
| } | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So i think you have the right logic, but i think you need to modify the logic on line 48:
This might not make too much sense, but here is the logic:
doctagdoctag because technically, you can pass ANYTHING into snippets, and without adoctag we don't really know which one is Defined, and which one isn'tdoctag inside the inline snippets don't mean the top-level snippet file has one too...So we need a robust solution that works for the following situations:
I think we need to layout each situation before full implementation of this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so basically we would want:
And when the doc tag is inside the inline snippet, the check will be activated only on the inside of the inline snippet, and not outside of it.
I'll implement this right now.