From f322252c362212a0095dfea00041d56a53f6e2ef Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 20 Jan 2015 00:17:22 +1100 Subject: [PATCH] add optional undeclared variable check transformer --- lib/6to5/transformation/transform.js | 1 + .../transformers/optional-undeclared-variable-check.js | 9 +++++++++ .../optional-undeclared-variable-check/declared/exec.js | 5 +++++ .../optional-undeclared-variable-check/options.json | 3 +++ .../undeclared/exec.js | 1 + .../undeclared/options.json | 3 +++ 6 files changed, 22 insertions(+) create mode 100644 lib/6to5/transformation/transformers/optional-undeclared-variable-check.js create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/declared/exec.js create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/options.json create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/undeclared/exec.js create mode 100644 test/fixtures/transformation/optional-undeclared-variable-check/undeclared/options.json 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" +}