mirror of
https://gitee.com/java110/MicroCommunityWeb.git
synced 2026-02-24 05:46:03 +08:00
31 lines
962 B
JavaScript
Executable File
31 lines
962 B
JavaScript
Executable File
var scope = require('lexical-scope')
|
|
|
|
module.exports = addWith
|
|
|
|
function addWith(obj, src, exclude) {
|
|
exclude = exclude || []
|
|
var objScope = scope(obj)
|
|
exclude = exclude.concat(objScope.globals.implicit).concat(objScope.globals.exported)
|
|
var s = scope('(function () {' + src + '}())')//allows the `return` keyword
|
|
var vars = s.globals.implicit
|
|
.filter(function (v) {
|
|
return !(v in global) && exclude.indexOf(v) === -1
|
|
})
|
|
|
|
if (vars.length === 0) return src
|
|
|
|
var declareLocal = ''
|
|
var local = 'locals'
|
|
if (/^[a-zA-Z0-9$_]+$/.test(obj)) {
|
|
local = obj
|
|
} else {
|
|
while (s.globals.implicit.indexOf(local) != -1 || s.globals.exported.indexOf(local) != -1 || exclude.indexOf(local) != -1 || obj.indexOf(local) != -1) {
|
|
local += '_'
|
|
}
|
|
declareLocal = local + ' = (' + obj + '),'
|
|
}
|
|
return 'var ' + declareLocal + vars
|
|
.map(function (v) {
|
|
return v + ' = ' + local + '.' + v
|
|
}).join(',') + ';' + src
|
|
} |