Skip to content

Commit

Permalink
Fix repeated variable names when using similar file names.
Browse files Browse the repository at this point in the history
This fixes a bug, where both `1chr1v1.png` and `#1chr1v1.png` used to produce the same variable name, causing compilation errors.
Partially related to #110, but not the same.
Merge pull request #111 from haroldo-ok/special-chars-in-filenames
  • Loading branch information
haroldo-ok committed May 24, 2023
2 parents 62d7e33 + 20c902d commit 90f014a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,16 @@ const addResource = (map, fileName, generator) => {
// No; add to the map

const suffix = fileName.trim().replace(/\W+/g, '_');
const variable = /^[^A-Za-z_]/.test(suffix) ? '_' + suffix : suffix;
const variableCandidate = /^[^A-Za-z_]/.test(suffix) ? '_' + suffix : suffix;

// Check if the variable name is duplicated
// TODO: Cache the existing variables to speed up the verification
let variable = variableCandidate;
let variableSuffix = 1;
const existingVariables = Object.values(map).map(o => o.variable);
while (existingVariables.includes(variable)) {
variable = variableCandidate + '_' + variableSuffix++;
}

map[fileName] = {
variable,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "choice4genesis",
"version": "0.14.1",
"version": "0.14.2",
"description": "A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis ",
"main": "index.js",
"targets": {
Expand Down

0 comments on commit 90f014a

Please sign in to comment.