Created
February 5, 2016 08:36
-
-
Save evanrs/2ea5ff28a56bc3b83257 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
var _interopRequireDefault = require("babel-runtime/helpers/interop-require-default")["default"]; | |
exports.__esModule = true; | |
var _leven = require("leven"); | |
var _leven2 = _interopRequireDefault(_leven); | |
function log (name) { | |
console.log('"'+name+'",'); | |
} | |
exports["default"] = function (_ref) { | |
var messages = _ref.messages; | |
return { | |
visitor: { | |
ReferencedIdentifier: function ReferencedIdentifier(path) { | |
var node = path.node; | |
var scope = path.scope; | |
if ([ | |
"arguments", | |
"Array", | |
"Boolean", | |
'clearTimeout', | |
'clearInterval', | |
'Date', | |
'Error', | |
"Function", | |
'Infinity', | |
"JSON", | |
"Math", | |
"Object", | |
"parseInt", | |
"setInterval", | |
'setTimeout', | |
"RegExp", | |
"String", | |
"undefined", | |
'TypeError', | |
'isNaN', | |
'parseFloat', | |
// node | |
"require", | |
"module", | |
"global", | |
"module", | |
"process", | |
"exports", | |
// web | |
"FileReader", | |
"document", | |
"window", | |
"console", | |
"location", | |
"XMLHttpRequest", | |
"fetch", | |
'FormError', | |
'navigator', | |
'decodeURIComponent', | |
'localStorage', | |
'DataView', | |
// other | |
"self", | |
'setImmediate', | |
'regeneratorRuntime', | |
].indexOf(node.name) >= 0) return; | |
var binding = scope.getBinding(node.name); | |
if (binding && binding.kind === "type" && !path.parentPath.isFlow()) { | |
log(node.name); | |
throw path.buildCodeFrameError(messages.get("undeclaredVariableType", node.name), ReferenceError); | |
} | |
if (scope.hasBinding(node.name)) return; | |
// get the closest declaration to offer as a suggestion | |
// the variable name may have just been mistyped | |
var bindings = scope.getAllBindings(); | |
var closest = undefined; | |
var shortest = -1; | |
for (var _name in bindings) { | |
var distance = _leven2["default"](node.name, _name); | |
if (distance <= 0 || distance > 3) continue; | |
if (distance <= shortest) continue; | |
closest = _name; | |
shortest = distance; | |
} | |
var msg = undefined; | |
if (closest) { | |
msg = messages.get("undeclaredVariableSuggestion", node.name, closest); | |
} else { | |
msg = messages.get("undeclaredVariable", node.name); | |
} | |
// | |
log(node.name); | |
throw path.buildCodeFrameError(msg, ReferenceError); | |
} | |
} | |
}; | |
}; | |
module.exports = exports["default"]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment