Duplicate Identifiers within Tether.js Library
Duplicate Identifiers within Tether.js Library
Problem
The TypeScript static type analyser detected the following duplicate identifers within dist/tether.js:
node_modules/tether/dist/js/tether.js(52,10): error TS2300: Duplicate identifier 'getScrollParents'.
node_modules/tether/dist/js/tether.js(138,10): error TS2300: Duplicate identifier 'removeUtilElements'.
node_modules/tether/dist/js/tether.js(145,10): error TS2300: Duplicate identifier 'getBounds'.
node_modules/tether/dist/js/tether.js(178,10): error TS2300: Duplicate identifier 'getOffsetParent'.
node_modules/tether/dist/js/tether.js(182,10): error TS2300: Duplicate identifier 'getScrollBarSize'.
node_modules/tether/dist/js/tether.js(218,10): error TS2300: Duplicate identifier 'extend'.
node_modules/tether/dist/js/tether.js(238,10): error TS2300: Duplicate identifier 'removeClass'.
node_modules/tether/dist/js/tether.js(252,10): error TS2300: Duplicate identifier 'addClass'.
node_modules/tether/dist/js/tether.js(287,10): error TS2300: Duplicate identifier 'updateClasses'.
node_modules/tether/dist/js/tether.js(431,5): error TS2300: Duplicate identifier 'getScrollParents'.
node_modules/tether/dist/js/tether.js(432,5): error TS2300: Duplicate identifier 'getBounds'.
node_modules/tether/dist/js/tether.js(433,5): error TS2300: Duplicate identifier 'getOffsetParent'.
node_modules/tether/dist/js/tether.js(434,5): error TS2300: Duplicate identifier 'extend'.
node_modules/tether/dist/js/tether.js(435,5): error TS2300: Duplicate identifier 'addClass'.
node_modules/tether/dist/js/tether.js(436,5): error TS2300: Duplicate identifier 'removeClass'.
node_modules/tether/dist/js/tether.js(437,5): error TS2300: Duplicate identifier 'updateClasses'.
node_modules/tether/dist/js/tether.js(440,5): error TS2300: Duplicate identifier 'getScrollBarSize'.
node_modules/tether/dist/js/tether.js(441,5): error TS2300: Duplicate identifier 'removeUtilElements'.
node_modules/tether/dist/js/tether.js(1290,5): error TS2300: Duplicate identifier 'getBounds'.
node_modules/tether/dist/js/tether.js(1291,5): error TS2300: Duplicate identifier 'extend'.
node_modules/tether/dist/js/tether.js(1292,5): error TS2300: Duplicate identifier 'updateClasses'.
node_modules/tether/dist/js/tether.js(1676,5): error TS2300: Duplicate identifier 'getBounds'.
node_modules/tether/dist/js/tether.js(1677,5): error TS2300: Duplicate identifier 'updateClasses'.
Root Cause
The issue arises due to the Gulp build system doing a simple concat of the source files without reguard to file scope. Because src/utils.js and src/tether.js both define the same identifers within their top level file scope a duplicate identifer clash occurs when they are concatenated together.
Here's an illustration of what's happening in the source files:
src/utils.js
// define a function declaration in utils.js
function getScrollParents() {}
// assign function declaration to property on TetherBase.Utils in utils.js
TetherBase.Utils = {
getScrollParents
}
src/tether.js
// assign property from TetherBase.Utils to varible with same name in tether.js
var TetherBase$Utils = TetherBase.Utils
var getScrollParents = TetherBase$Utils.getScrollParents;
Here's an illustration of what's happening in the build output:
dist/tether.js
// tether.js output file from build system (simplified the UMD header for brevity)
(function() {
// utils.js
function getScrollParents() {}
TetherBase.Utils = {
getScrollParents
}
// tether.js
var TetherBase$Utils = TetherBase.Utils
var getScrollParents = TetherBase$Utils.getScrollParents;
}())
Notice in the dist/tether.js output file that the function declaration in src/utils.js and the variable declaration in src/tether.js both use the same identifer. This occurs for all the identifers listed in the TypeScript errors above.
Proposed Resolution
The following proposed resolutions should fix the issue:
- Skip the intermediary assignment in
src/tether.jsand use theTetherBase.Utilsmethods directly. - Switch over to using ES6 modules and use a module packer like
webpackto enforce module level scope isolation in the build output.
Sidenote: Consider switching the code base to
TypeScriptto catch errors like this in the future :) :rocket:
Related Issues
Issue #187
@thoughtentity hmm I have this problem with my project, do you have a workaround to ignore these errors until it is fixed ? I'd like to get rid of these error messages in my shell For now I'm ignoring error 2300, but I'd like something more specific to the lib (because this error message is useful on my own code)
What is worse, rollup in treeshake mode (default) removes the second declaration of each identifier, in this case the functions.
Here is what I did to both correct this issue:
Using Visual Studio 2015 or higher (would probably work in 2010 and up but may need a "renaming" extension tool)
My fix: - I clicked on the function name, selected function key "F2" which does a rename and updates those names that call it and added the prefix of "fn" to each name. (you can put what ever you want here to match your naming convention)
This turned the function into a stronger unique name to avoid the duplicate check. To confirm this fix, right click on the function name and select "Find all references" then see that count. Then do the above fix and check the "Find all references" count.
Hope this is a valid option and others find it useful.
This should be fixed now. Please try 2.0.0-beta.2