Skip to content

Commit 14b6855

Browse files
authored
Fix: Add a migration step for importing Blockly correctly
Fixes #2349
2 parents 5e28520 + abadea5 commit 14b6855

7 files changed

Lines changed: 35 additions & 10 deletions

File tree

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = [
4040
'**/dist/',
4141
'**/build/',
4242
'examples/blockly-svelte/public/bundle.js',
43+
'plugins/migration/test/manual-test-data/',
4344
// specific examples that are sometimes copied into plugins
4445
'plugins/dev-create/templates/sample-app',
4546
'plugins/dev-create/templates/sample-app-ts',

plugins/migration/bin/fix-imports.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ const database = [
9999
newImport: `import * as libraryBlocks from 'blockly/blocks';`,
100100
newRequire: `const libraryBlocks = require('blockly/blocks');`,
101101
},
102+
{
103+
import: 'blockly',
104+
oldIdentifier: 'Blockly',
105+
newIdentifier: 'Blockly', // no-op
106+
newImport: `import * as Blockly from 'blockly';`,
107+
newRequire: `const Blockly = require('blockly');`, // no-op
108+
},
102109
];
103110

104111
/**
@@ -125,9 +132,18 @@ function migrateContents(contents) {
125132
*/
126133
function fixImport(contents, migrationData) {
127134
const identifier = getIdentifier(contents, migrationData);
135+
// Don't need to run if there are no references.
128136
if (!identifier) return contents;
129-
const newContents = replaceReferences(contents, migrationData, identifier);
130-
if (newContents !== contents) return addImport(newContents, migrationData);
137+
// If the identifier changed, update all references to it and the import
138+
if (migrationData.oldIdentifier !== migrationData.newIdentifier) {
139+
const newContents = replaceReferences(contents, migrationData, identifier);
140+
if (newContents !== contents) {
141+
return addImport(newContents, migrationData);
142+
}
143+
} else {
144+
// Just the import changed
145+
return addImport(contents, migrationData);
146+
}
131147
return contents;
132148
}
133149

plugins/migration/test/manual-test-data/fix-imports/import.txt renamed to plugins/migration/test/manual-test-data/fix-imports/import.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Manual test data for the renamings migration.
22
// Run using:
3-
// node ./bin/migrate fix-imports --from <version> ./test/manual-test-data/fix-imports/import.txt
3+
// node ./bin/migrate fix-imports --from <version> ./test/manual-test-data/fix-imports/import.js
44

55
import Blockly from 'blockly';
6-
import * as BlocklyDart from "blockly/dart";
6+
import * as BlocklyDart from 'blockly/dart';
77
import * as BlocklyLua from 'blockly/lua';
8-
import * as BlocklyPhp from "blockly/php";
8+
import * as BlocklyPhp from 'blockly/php';
99
import * as BlocklyPython from 'blockly/python';
1010

1111
Blockly.JavaScript.something;
@@ -31,3 +31,5 @@ Some.Other.identifer;
3131
Blockly.libraryBlocks.something;
3232
const something = Blockly.libraryBlocks.something;
3333
Some.Other.identifer;
34+
35+
Blockly.zelos;

plugins/migration/test/manual-test-data/fix-imports/mixed-1.txt renamed to plugins/migration/test/manual-test-data/fix-imports/mixed-1.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ Some.Other.identifer;
3131
Blockly.libraryBlocks.something;
3232
const something = Blockly.libraryBlocks.something;
3333
Some.Other.identifer;
34+
35+
Blockly.zelos;

plugins/migration/test/manual-test-data/fix-imports/mixed-2.txt renamed to plugins/migration/test/manual-test-data/fix-imports/mixed-2.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// node ./bin/migrate fix-imports --from <version> ./test/manual-test-data/fix-imports/mixed-1.txt
44

55
const Blockly = require('blockly');
6-
import * as BlocklyDart from "blockly/dart";
6+
import * as BlocklyDart from 'blockly/dart';
77
const BlocklyLua = require('blockly/lua');
8-
import * as BlocklyPhp from "blockly/php";
8+
import * as BlocklyPhp from 'blockly/php';
99
const BlocklyPython = require('blockly/python');
1010

1111
Blockly.JavaScript.something;
@@ -31,3 +31,5 @@ Some.Other.identifer;
3131
Blockly.libraryBlocks.something;
3232
const something = Blockly.libraryBlocks.something;
3333
Some.Other.identifer;
34+
35+
Blockly.zelos;

plugins/migration/test/manual-test-data/fix-imports/require.txt renamed to plugins/migration/test/manual-test-data/fix-imports/require.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// node ./bin/migrate fix-imports --from <version> ./test/manual-test-data/fix-imports/require.txt
44

55
const Blockly = require('blockly');
6-
const BlocklyDart = require("blockly/dart");
6+
const BlocklyDart = require('blockly/dart');
77
const BlocklyLua = require('blockly/lua');
8-
let BlocklyPhp = require("blockly/php");
8+
let BlocklyPhp = require('blockly/php');
99
var BlocklyPython = require('blockly/python');
1010

1111
Blockly.JavaScript.something;
@@ -31,3 +31,5 @@ Some.Other.identifer;
3131
Blockly.libraryBlocks.something;
3232
const something = Blockly.libraryBlocks.something;
3333
Some.Other.identifer;
34+
35+
Blockly.zelos;

plugins/migration/test/manual-test-data/rename.txt renamed to plugins/migration/test/manual-test-data/rename.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ class SubClass extends Blockly.moduleC {
2525
const thingA = /** @type {Blockly.moduleD} */ (new Blockly.moduleE());
2626
return thingA.someMethod(paramA, paramB);
2727
}
28-
};
28+
}

0 commit comments

Comments
 (0)