-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Prevent type checking define:vars
scripts
#718
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 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 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 @@ | ||
--- | ||
'@astrojs/language-server': patch | ||
--- | ||
|
||
Fixes an issue where type checking errors were shown on define:vars scripts when "type=module" attribute was also present. |
This file contains 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 |
---|---|---|
|
@@ -12,43 +12,41 @@ export function extractScriptTags( | |
htmlDocument: HTMLDocument, | ||
ast: ParseResult['ast'] | ||
): VirtualFile[] { | ||
const embeddedJSFiles: VirtualFile[] = findIsolatedScripts( | ||
const embeddedJSFiles: VirtualFile[] = findModuleScripts( | ||
fileName, | ||
snapshot, | ||
htmlDocument.roots | ||
); | ||
|
||
const javascriptContexts = [ | ||
...findInlineScripts(htmlDocument, snapshot), | ||
...findClassicScripts(htmlDocument, snapshot), | ||
...findEventAttributes(ast), | ||
].sort((a, b) => a.startOffset - b.startOffset); | ||
|
||
if (javascriptContexts.length > 0) { | ||
// classic scripts share the same scope | ||
// merging them brings about redeclaration errors | ||
embeddedJSFiles.push(mergeJSContexts(fileName, javascriptContexts)); | ||
} | ||
|
||
return embeddedJSFiles; | ||
} | ||
|
||
function isIsolatedScriptTag(scriptTag: Node): boolean { | ||
// Using any kind of attributes on the script tag will disable hoisting | ||
if ( | ||
!scriptTag.attributes || | ||
(scriptTag.attributes && Object.entries(scriptTag.attributes).length === 0) || | ||
scriptTag.attributes['type']?.includes('module') | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
function getScriptType(scriptTag: Node): "classic" | "module" | "processed module" { | ||
// script tags without attributes are processed and converted into module scripts | ||
if (!scriptTag.attributes || Object.entries(scriptTag.attributes).length === 0) return "processed module"; | ||
// even when it is not processed by vite, scripts with type=module remain modules | ||
if (scriptTag.attributes["type"]?.includes("module") === true) return "module"; | ||
// whenever there are attributes, is:inline is implied and in the absence of type=module, the script is classic | ||
return "classic" | ||
} | ||
|
||
/** | ||
* Get all the isolated scripts in the HTML document | ||
* Isolated scripts are scripts that are hoisted by Astro and as such, are isolated from the rest of the code because of the implicit `type="module"` | ||
* All the isolated scripts are passed to the TypeScript language server as separate `.mts` files. | ||
*/ | ||
function findIsolatedScripts( | ||
function findModuleScripts( | ||
fileName: string, | ||
snapshot: ts.IScriptSnapshot, | ||
roots: Node[] | ||
|
@@ -64,11 +62,12 @@ function findIsolatedScripts( | |
node.tag === 'script' && | ||
node.startTagEnd !== undefined && | ||
node.endTagStart !== undefined && | ||
isIsolatedScriptTag(node) | ||
getScriptType(node) !== "classic" | ||
) { | ||
const scriptText = snapshot.getText(node.startTagEnd, node.endTagStart); | ||
const extension = getScriptType(node) === "processed module" ? "mts" : "mjs"; | ||
embeddedScripts.push({ | ||
fileName: fileName + `.${scriptIndex}.mts`, | ||
fileName: fileName + `.${scriptIndex}.${extension}`, | ||
Comment on lines
+68
to
+70
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. the fix is here, the rest is clerical |
||
kind: FileKind.TypeScriptHostFile, | ||
snapshot: { | ||
getText: (start, end) => scriptText.substring(start, end), | ||
|
@@ -113,7 +112,7 @@ interface JavaScriptContext { | |
* Inline scripts are scripts that are not hoisted by Astro and as such, are not isolated from the rest of the code. | ||
* All the inline scripts are concatenated into a single `.mjs` file and passed to the TypeScript language server. | ||
*/ | ||
function findInlineScripts( | ||
function findClassicScripts( | ||
htmlDocument: HTMLDocument, | ||
snapshot: ts.IScriptSnapshot | ||
): JavaScriptContext[] { | ||
|
@@ -128,7 +127,7 @@ function findInlineScripts( | |
node.startTagEnd !== undefined && | ||
node.endTagStart !== undefined && | ||
!isJSON(node.attributes?.type) && | ||
!isIsolatedScriptTag(node) | ||
getScriptType(node) === "classic" | ||
) { | ||
const scriptText = snapshot.getText(node.startTagEnd, node.endTagStart); | ||
inlineScripts.push({ | ||
|
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.
Please add the VS Code package and the Astro check ones to this changeset so they also get a release for 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.
Wouldn't their semver ranges include this release?