-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some code improvements from a working branch: (#8822)
* Added some code improvements from a working branch: * Created a shortened "fileId" for each source file. This reduces memory used for tracking symbols and TypeVars that are scoped to files. * Removed some unused or redundant code. * Added assertNever calls to catch potential bugs. * Renamed a few symbols for consistency and readability. * Improved handling of literal type expressions that are lazily parsed. * Refactored literal type printing into a utility module. * Fixed style issue.
- Loading branch information
Showing
12 changed files
with
114 additions
and
66 deletions.
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
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
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
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
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
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
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
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
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
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
49 changes: 49 additions & 0 deletions
49
packages/pyright-internal/src/analyzer/typePrinterUtils.ts
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,49 @@ | ||
/* | ||
* typePrinterUtils.ts | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
* Author: Eric Traut | ||
* | ||
* Simple utility functions used by the type printer. | ||
*/ | ||
|
||
export function printStringLiteral(value: string, quotation = '"'): string { | ||
const singleTickRegEx = /'/g; | ||
const escapedDoubleQuoteRegEx = /\\"/g; | ||
|
||
// JSON.stringify will perform proper escaping for " case. | ||
// So, we only need to do our own escaping for ' case. | ||
let literalStr = JSON.stringify(value).toString(); | ||
if (quotation !== '"') { | ||
literalStr = `'${literalStr | ||
.substring(1, literalStr.length - 1) | ||
.replace(escapedDoubleQuoteRegEx, '"') | ||
.replace(singleTickRegEx, "\\'")}'`; // CodeQL [SM02383] Code ql is just wrong here. We don't need to replace backslashes. | ||
} | ||
|
||
return literalStr; | ||
} | ||
|
||
export function printBytesLiteral(value: string) { | ||
let bytesString = ''; | ||
|
||
// There's no good built-in conversion routine in javascript to convert | ||
// bytes strings. Determine on a character-by-character basis whether | ||
// it can be rendered into an ASCII character. If not, use an escape. | ||
for (let i = 0; i < value.length; i++) { | ||
const char = value.substring(i, i + 1); | ||
const charCode = char.charCodeAt(0); | ||
|
||
if (charCode >= 20 && charCode <= 126) { | ||
if (charCode === 34) { | ||
bytesString += '\\' + char; | ||
} else { | ||
bytesString += char; | ||
} | ||
} else { | ||
bytesString += `\\x${((charCode >> 4) & 0xf).toString(16)}${(charCode & 0xf).toString(16)}`; | ||
} | ||
} | ||
|
||
return `b"${bytesString}"`; | ||
} |
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