From 7c2282d43e7ec00a31b3140a85bd545894c528bd Mon Sep 17 00:00:00 2001 From: Jelle Date: Sat, 21 May 2022 21:47:00 +0200 Subject: [PATCH 1/4] fix: Allow double underscore in variable names * rxVariableCheck now allows the use of underscore in the name * Added rxReservedVariableCheck that specifically tests for single underscore * Added checkReservedRenpyNames function that pushes a warning if a single underscore variable was found * Changed checkReservedPythonNames to result in a warning as the config suggests 'if (config.warnOnReservedVariableNames)' --- src/diagnostics.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/diagnostics.ts b/src/diagnostics.ts index 73c3279..9eee2bc 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -13,7 +13,8 @@ const rxReservedPythonCheck = /^\s*(default|define)\s+(ArithmeticError|Assertion // Obsolete Methods const rxObsoleteCheck = /[\s\(=]+(LiveCrop|LiveComposite|Tooltip|im\.Rotozoom|im\.ImageBase|im\.ramp|im\.Map|im\.Flip|im\.math|im\.expands_bounds|im\.threading|im\.zipfile|im\.Recolor|im\.Color|im\.io|im\.Alpha|im\.Data|im\.Image|im\.Twocolor|im\.MatrixColor|im\.free_memory|im\.Tile|im\.FactorScale|im\.Sepia|im\.Crop|im\.AlphaMask|im\.Blur|im\.tobytes|im\.matrix|im\.Grayscale|ui\.add|ui\.bar|ui\.imagebutton|ui\.input|ui\.key|ui\.label|ui\.null|ui\.text|ui\.textbutton|ui\.timer|ui\.vbar|ui\.hotspot|ui\.hotbar|ui\.spritemanager|ui\.button|ui\.frame|ui\.transform|ui\.window|ui\.drag|ui\.fixed|ui\.grid|ui\.hbox|ui\.side|ui\.vbox|ui\.imagemap|ui\.draggroup)[^a-zA-Z]/g; -const rxVariableCheck = /^\s*(default|define)\s+([^a-zA-Z\s][a-zA-Z0-9_]*)\s+=/g; +const rxVariableCheck = /^\s*(default|define)\s+([^a-zA-Z\s_][a-zA-Z0-9_]*)\s+=/g; +const rxReservedVariableCheck = /\s*(default|define)\s+(_[a-zA-Z0-9]*)\s+=/g; const rxPersistentDefines = /^\s*(default|define)\s+persistent\.([a-zA-Z]+[a-zA-Z0-9_]*)\s*=\s*(.*$)/g; const rxPersistentCheck = /\s+persistent\.(\w+)[^a-zA-Z]/g; const rxStoreCheck = /\s+store\.(\w+)[^a-zA-Z_]?/g; @@ -112,6 +113,7 @@ const rsComparisonCheck = /\s+(if|while)\s+(\w+)\s*(=)\s*(\w+)\s*/g; } if (config.warnOnReservedVariableNames) { + checkReservedRenpyNames(diagnostics, line, lineIndex); checkReservedPythonNames(diagnostics, line, lineIndex); } @@ -178,13 +180,24 @@ function checkComparisonVsAssignment(diagnostics: Diagnostic[], line: string, li } } +function checkReservedRenpyNames(diagnostics: Diagnostic[], line: string, lineIndex: number) { + // check for default/define variables that are Python reserved names + let matches; + while ((matches = rxReservedVariableCheck.exec(line)) !== null) { + const offset = matches.index + matches[0].indexOf(matches[2]); + const range = new Range(lineIndex, offset, lineIndex, offset + matches[2].length); + const diagnostic = new Diagnostic(range, `"${matches[2]}": Variables may not begin with a single underscore '_' as Ren'Py reserves such variables for its own purposes.`, DiagnosticSeverity.Warning); + diagnostics.push(diagnostic); + } +} + function checkReservedPythonNames(diagnostics: Diagnostic[], line: string, lineIndex: number) { // check for default/define variables that are Python reserved names let matches; while ((matches = rxReservedPythonCheck.exec(line)) !== null) { const offset = matches.index + matches[0].indexOf(matches[2]); const range = new Range(lineIndex, offset, lineIndex, offset + matches[2].length); - const diagnostic = new Diagnostic(range, `"${matches[2]}" is a Python reserved name, type, or function. Using it as a variable can lead to obscure problems or unpredictable behavior.`, DiagnosticSeverity.Error); + const diagnostic = new Diagnostic(range, `"${matches[2]}" is a Python reserved name, type, or function. Using it as a variable can lead to obscure problems or unpredictable behavior.`, DiagnosticSeverity.Warning); diagnostics.push(diagnostic); } } @@ -208,7 +221,7 @@ function checkInvalidVariableNames(diagnostics: Diagnostic[], line: string, line { const offset = matches.index + matches[0].indexOf(matches[2]); const range = new Range(lineIndex, offset, lineIndex, offset + matches[2].length); - const diagnostic = new Diagnostic(range, `"${matches[2]}": Variables must begin with a letter (and may contain numbers, letters, or underscores). Variables may not begin with '_' as Ren'Py reserves such variables for its own purposes.`, severity); + const diagnostic = new Diagnostic(range, `"${matches[2]}": Variables must begin with a letter (and may contain numbers, letters, or underscores).`, severity); diagnostics.push(diagnostic); } } From 9c1f46dedd71e2df594183f5639d04be13e9101d Mon Sep 17 00:00:00 2001 From: Jelle Date: Sat, 21 May 2022 21:57:49 +0200 Subject: [PATCH 2/4] fix: typo --- src/diagnostics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diagnostics.ts b/src/diagnostics.ts index 9eee2bc..351c26e 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -175,7 +175,7 @@ function checkComparisonVsAssignment(diagnostics: Diagnostic[], line: string, li while ((matches = rsComparisonCheck.exec(line)) !== null) { const offset = matches.index + matches[0].indexOf(matches[3]); const range = new Range(lineIndex, offset, lineIndex, offset + matches[3].length); - const diagnostic = new Diagnostic(range, `"=" is the equality operator. Use "==" for comparison.`, DiagnosticSeverity.Warning); + const diagnostic = new Diagnostic(range, `"=" is the assignment operator. Use "==" for comparison.`, DiagnosticSeverity.Warning); diagnostics.push(diagnostic); } } From 029d5c2502e502623ebdb3f60a99abae6eb14c3d Mon Sep 17 00:00:00 2001 From: Jelle Date: Mon, 23 May 2022 08:57:15 +0200 Subject: [PATCH 3/4] edit: Fixed requested review changes --- src/diagnostics.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/diagnostics.ts b/src/diagnostics.ts index 351c26e..115a3a5 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -181,7 +181,7 @@ function checkComparisonVsAssignment(diagnostics: Diagnostic[], line: string, li } function checkReservedRenpyNames(diagnostics: Diagnostic[], line: string, lineIndex: number) { - // check for default/define variables that are Python reserved names + // check for default/define variables that are Ren'Py reserved names let matches; while ((matches = rxReservedVariableCheck.exec(line)) !== null) { const offset = matches.index + matches[0].indexOf(matches[2]); @@ -197,7 +197,7 @@ function checkReservedPythonNames(diagnostics: Diagnostic[], line: string, lineI while ((matches = rxReservedPythonCheck.exec(line)) !== null) { const offset = matches.index + matches[0].indexOf(matches[2]); const range = new Range(lineIndex, offset, lineIndex, offset + matches[2].length); - const diagnostic = new Diagnostic(range, `"${matches[2]}" is a Python reserved name, type, or function. Using it as a variable can lead to obscure problems or unpredictable behavior.`, DiagnosticSeverity.Warning); + const diagnostic = new Diagnostic(range, `"${matches[2]}": is a Python reserved name, type, or function. Using it as a variable can lead to obscure problems or unpredictable behavior.`, DiagnosticSeverity.Warning); diagnostics.push(diagnostic); } } From f7289128e2e93287f84f09a488e042a014e21a9a Mon Sep 17 00:00:00 2001 From: Jelle Date: Mon, 23 May 2022 08:57:20 +0200 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e21da76..a3ac597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 2.0.10 (2022/05/21) + +### Fixes + +* Allow double underscore in variable names [#123](https://github.com/LuqueDaniel/vscode-language-renpy/pull/123) (fix issue [#122](https://github.com/LuqueDaniel/vscode-language-renpy/issues/122)) + +### Other changes + +* Reserved variable names now show as warnings instead of errors + ## 2.0.9 (2022/05/02) ### Fixes