-
Notifications
You must be signed in to change notification settings - Fork 0
Route PEP 723 scripts to inline environments (PEP 723 PR 9/16) #7
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
Changes from all commits
2badf77
73a9426
d3dfbd8
b804ac5
3811479
021a198
ff04335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,15 @@ export interface InlineScriptMetadata { | |
| * newline (or end of string if there is no trailing newline). | ||
| */ | ||
| readonly range: { readonly start: number; readonly end: number }; | ||
| /** | ||
| * Character offsets of the same metadata block in source text after the | ||
| * parser's BOM handling. Unlike {@link range}, these preserve CRLF, so | ||
| * they can be compared with TextDocument change offsets. | ||
| * | ||
| * Optional to keep manually constructed metadata compatible; parser | ||
| * results always supply it. | ||
| */ | ||
| readonly sourceRange?: { readonly start: number; readonly end: number }; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -79,7 +88,8 @@ export function readInlineScriptMetadata(scriptText: string): InlineScriptMetada | |
| // "UTF-8 with BOM" on Windows have this; without stripping it the | ||
| // first line becomes "\uFEFF# /// script" and the regex fails to | ||
| // match. | ||
| let text = scriptText.charCodeAt(0) === 0xfeff ? scriptText.slice(1) : scriptText; | ||
| const sourceText = scriptText.charCodeAt(0) === 0xfeff ? scriptText.slice(1) : scriptText; | ||
| let text = sourceText; | ||
|
|
||
| // Normalize CRLF and lone CR to LF so the canonical regex (which | ||
| // was authored assuming `.` matches `\r`, true in Python's re but | ||
|
|
@@ -215,9 +225,27 @@ export function readInlineScriptMetadata(scriptText: string): InlineScriptMetada | |
| dependencies, | ||
| tool, | ||
| range: { start: matchStart, end }, | ||
| sourceRange: { | ||
| start: sourceOffsetForNormalizedOffset(sourceText, matchStart), | ||
| end: sourceOffsetForNormalizedOffset(sourceText, end), | ||
| }, | ||
| }; | ||
|
Owner
Author
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.
[verified] |
||
| } | ||
|
|
||
| function sourceOffsetForNormalizedOffset(sourceText: string, normalizedOffset: number): number { | ||
| let sourceOffset = 0; | ||
| let currentNormalizedOffset = 0; | ||
| while (currentNormalizedOffset < normalizedOffset && sourceOffset < sourceText.length) { | ||
| if (sourceText.charCodeAt(sourceOffset) === 0x0d) { | ||
| sourceOffset += sourceText.charCodeAt(sourceOffset + 1) === 0x0a ? 2 : 1; | ||
| } else { | ||
| sourceOffset += 1; | ||
| } | ||
| currentNormalizedOffset += 1; | ||
| } | ||
| return sourceOffset; | ||
| } | ||
|
|
||
| /** | ||
| * Read PEP 723 metadata from a file. Reads only the first | ||
| * `MAX_HEADER_BYTES` bytes of the file — PEP 723 blocks live at the | ||
|
|
||
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.
queuedis assigned once. Declare it asconst queued = operation.finally(...); the deferred callback can safely reference that binding.[verified]