aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/preact/config/codemod-const.js
diff options
context:
space:
mode:
authortg(x) <*@tg-x.net>2016-10-05 00:02:10 +0200
committertg(x) <*@tg-x.net>2016-10-05 00:02:10 +0200
commitec62d29c90958aa8d41474ed2fe5a179d6fafed7 (patch)
tree367cc55bc6772cf194ed6c4778cd344d581d3d7c /thirdparty/preact/config/codemod-const.js
parentfda241d74d5c1c39203b64da676c684d4dc9d800 (diff)
parentd3ccf4103900b8d990b1970d135695b938d94eae (diff)
Merge branch 'master' of taler.net:/var/git/wallet-webex
Diffstat (limited to 'thirdparty/preact/config/codemod-const.js')
-rw-r--r--thirdparty/preact/config/codemod-const.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/thirdparty/preact/config/codemod-const.js b/thirdparty/preact/config/codemod-const.js
new file mode 100644
index 000000000..4bffd9add
--- /dev/null
+++ b/thirdparty/preact/config/codemod-const.js
@@ -0,0 +1,39 @@
+/* eslint no-console:0 */
+
+/** Find constants (identified by ALL_CAPS_DECLARATIONS), and inline them globally.
+ * This is safe because Preact *only* uses global constants.
+ */
+export default (file, api) => {
+ let j = api.jscodeshift,
+ code = j(file.source),
+ constants = {},
+ found = 0;
+
+ code.find(j.VariableDeclaration)
+ .filter( decl => {
+ for (let i=decl.value.declarations.length; i--; ) {
+ let node = decl.value.declarations[i],
+ name = node.id && node.id.name,
+ init = node.init;
+ if (name && init && name.match(/^[A-Z0-9_$]+$/g)) {
+ if (init.type==='Literal') {
+ console.log(`Inlining constant: ${name}=${init.raw}`);
+ found++;
+ constants[name] = init;
+ // remove declaration
+ decl.value.declarations.splice(i, 1);
+ // if it's the last, we'll remove the whole statement
+ return !decl.value.declarations.length;
+ }
+ }
+ }
+ return false;
+ })
+ .remove();
+
+ code.find(j.Identifier)
+ .filter( path => path.value.name && constants.hasOwnProperty(path.value.name) )
+ .replaceWith( path => (found++, constants[path.value.name]) );
+
+ return found ? code.toSource({ quote: 'single' }) : null;
+};