-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZK-4928: expose widget private functions and variables as public ones
- Loading branch information
1 parent
59bf368
commit 729fc6c
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// eslint-disable-next-line no-undef | ||
module.exports = function ({types: t}) { | ||
|
||
function createNestedMemberExpression(identifiers) { | ||
if (identifiers.length === 1) { | ||
return t.identifier(identifiers[0]); | ||
} else { | ||
const [head, ...tail] = identifiers; | ||
return t.memberExpression( | ||
createNestedMemberExpression(tail), | ||
t.identifier(head) | ||
); | ||
} | ||
} | ||
|
||
return { | ||
visitor: { | ||
Program: { | ||
exit(path) { | ||
let dir = this.file.opts.filename.replace(/-/g, '_').split('/'); | ||
const jsLoc = dir.findIndex(x => x === 'js'), | ||
file = dir[dir.length - 1], | ||
exports = {}; | ||
|
||
// pass if [not in js folder] or [not ts file] or [is global.d.ts] or [is index.ts] | ||
if (jsLoc === -1 || !file.endsWith('ts') || file === 'global.d.ts' || file === 'index.ts') return; | ||
|
||
// simplify whole dir | ||
dir = dir.slice(jsLoc + 1); | ||
dir[dir.length - 1] = file.replace('.ts', ''); | ||
|
||
// sort order for follow-up | ||
dir.unshift('window'); | ||
dir.push('_'); | ||
dir.reverse(); | ||
|
||
// visit all nodes | ||
path.node.body.forEach((node, index) => { | ||
if (t.isVariableDeclaration(node)) { | ||
node.declarations.forEach((declaration) => { | ||
if (t.isIdentifier(declaration.id)) { | ||
exports[declaration.id.name] = declaration.id.name; | ||
} | ||
}); | ||
} else if (t.isFunctionDeclaration(node) && t.isIdentifier(node.id)) { | ||
exports[node.id.name] = node.id.name; | ||
} | ||
}); | ||
|
||
// add check-exist if statements | ||
for (let i = dir.length - 2; i > 0; i--) { | ||
const nestedExpression = createNestedMemberExpression(dir.slice(i)); | ||
path.pushContainer('body', | ||
t.ifStatement( | ||
t.unaryExpression('!', nestedExpression), | ||
t.expressionStatement( | ||
t.assignmentExpression( | ||
'=', | ||
nestedExpression, | ||
t.objectExpression([]) | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
// export all global variables and functions | ||
path.pushContainer('body', | ||
// window.x.x.x._ = {...} | ||
t.expressionStatement( | ||
t.assignmentExpression( | ||
'=', | ||
createNestedMemberExpression(dir), | ||
t.objectExpression( | ||
Object.entries(exports).map(([k, v]) => { | ||
return t.objectProperty(t.identifier(k), t.identifier(v)); | ||
}) | ||
) | ||
) | ||
) | ||
); | ||
|
||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters