Skip to content
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

Changed code that computes unescaped literal string type to convert C… #9688

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/pyright-internal/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5034,7 +5034,10 @@ export class Parser {
}
} else {
const stringToken = stringNode.d.strings[0].d.token;
const stringValue = StringTokenUtils.getUnescapedString(stringNode.d.strings[0].d.token);
const stringValue = StringTokenUtils.getUnescapedString(
stringNode.d.strings[0].d.token,
/* elideCrlf */ false
);
const unescapedString = stringValue.value;
const tokenOffset = stringToken.start;
const prefixLength = stringToken.prefixLength + stringToken.quoteMarkLength;
Expand Down
6 changes: 4 additions & 2 deletions packages/pyright-internal/src/parser/stringTokenUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function completeUnescapedString(incomplete: IncompleteUnescapedString, original
};
}

export function getUnescapedString(stringToken: StringToken | FStringMiddleToken): UnescapedString {
export function getUnescapedString(stringToken: StringToken | FStringMiddleToken, elideCrlf = true): UnescapedString {
const escapedString = stringToken.escapedValue;
const isRaw = (stringToken.flags & StringTokenFlags.Raw) !== 0;

Expand Down Expand Up @@ -305,7 +305,9 @@ export function getUnescapedString(stringToken: StringToken | FStringMiddleToken
} else if (curChar === Char.LineFeed || curChar === Char.CarriageReturn) {
// Skip over the escaped new line (either one or two characters).
if (curChar === Char.CarriageReturn && getEscapedCharacter(1) === Char.LineFeed) {
appendOutputChar(curChar);
if (!elideCrlf) {
appendOutputChar(curChar);
}
strOffset++;
curChar = getEscapedCharacter();
}
Expand Down
54 changes: 32 additions & 22 deletions packages/pyright-internal/src/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,6 @@ export function getAnalysisResults(
});
}

export function printDiagnostics(fileResults: FileAnalysisResult) {
if (fileResults.errors.length > 0) {
console.error(`Errors in ${fileResults.fileUri}:`);
for (const diag of fileResults.errors) {
console.error(` ${diag.message}`);
}
}

if (fileResults.warnings.length > 0) {
console.error(`Warnings in ${fileResults.fileUri}:`);
for (const diag of fileResults.warnings) {
console.error(` ${diag.message}`);
}
}
}

export function validateResults(
results: FileAnalysisResult[],
errorCount: number,
Expand All @@ -192,22 +176,48 @@ export function validateResults(
deprecated?: number
) {
assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].errors.length, errorCount);
assert.strictEqual(results[0].warnings.length, warningCount);

if (results[0].errors.length !== errorCount) {
logDiagnostics(results[0].errors);
assert.fail(`Expected ${errorCount} errors, got ${results[0].errors.length}`);
}

if (results[0].warnings.length !== warningCount) {
logDiagnostics(results[0].warnings);
assert.fail(`Expected ${warningCount} warnings, got ${results[0].warnings.length}`);
}

if (infoCount !== undefined) {
assert.strictEqual(results[0].infos.length, infoCount);
if (results[0].infos.length !== infoCount) {
logDiagnostics(results[0].infos);
assert.fail(`Expected ${infoCount} infos, got ${results[0].infos.length}`);
}
}

if (unusedCode !== undefined) {
assert.strictEqual(results[0].unusedCodes.length, unusedCode);
if (results[0].unusedCodes.length !== unusedCode) {
logDiagnostics(results[0].unusedCodes);
assert.fail(`Expected ${unusedCode} unused, got ${results[0].unusedCodes.length}`);
}
}

if (unreachableCode !== undefined) {
assert.strictEqual(results[0].unreachableCodes.length, unreachableCode);
if (results[0].unreachableCodes.length !== unreachableCode) {
logDiagnostics(results[0].unreachableCodes);
assert.fail(`Expected ${unreachableCode} unreachable, got ${results[0].unreachableCodes.length}`);
}
}

if (deprecated !== undefined) {
assert.strictEqual(results[0].deprecateds.length, deprecated);
if (results[0].deprecateds.length !== deprecated) {
logDiagnostics(results[0].deprecateds);
assert.fail(`Expected ${deprecated} deprecated, got ${results[0].deprecateds.length}`);
}
}
}

function logDiagnostics(diags: Diagnostic[]) {
for (const diag of diags) {
console.error(` [${diag.range.start.line + 1}:${diag.range.start.character + 1}] ${diag.message}`);
}
}
Loading