diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index e0238cc5f0..a98b0fe820 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -98,6 +98,7 @@ _.each({ typeofSymbol: require("./transformers/optional-typeof-symbol"), coreAliasing: require("./transformers/optional-core-aliasing"), undefinedToVoid: require("./transformers/optional-undefined-to-void"), + undeclaredVariableCheck: require("./transformers/optional-undeclared-variable-check"), // spec specPropertyLiterals: require("./transformers/spec-property-literals"), diff --git a/lib/6to5/transformation/transformers/optional-undeclared-variable-check.js b/lib/6to5/transformation/transformers/optional-undeclared-variable-check.js new file mode 100644 index 0000000000..6640c0217f --- /dev/null +++ b/lib/6to5/transformation/transformers/optional-undeclared-variable-check.js @@ -0,0 +1,9 @@ +var t = require("../../types"); + +exports.optional = true; + +exports.Identifier = function (node, parent, scope, context, file) { + if (!scope.has(node.name, true)) { + throw file.errorWithNode(node, "Reference to undeclared variable"); + } +}; diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js b/test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js new file mode 100644 index 0000000000..e2721feb4a --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js @@ -0,0 +1,5 @@ +function foo() { + +} + +foo(); diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/options.json b/test/fixtures/transformation/optional-undeclared-variable-check/options.json new file mode 100644 index 0000000000..535d39109b --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/options.json @@ -0,0 +1,3 @@ +{ + "optional": ["undeclaredVariableCheck"] +} diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js new file mode 100644 index 0000000000..a280f9a5cc --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js @@ -0,0 +1 @@ +foo(); diff --git a/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json new file mode 100644 index 0000000000..a661ffc703 --- /dev/null +++ b/test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Reference to undeclared variable" +}