From 38fc5159a3b8a6372d529dc7d676305dfda6bff3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 13 Oct 2014 03:25:48 +1100 Subject: [PATCH] add support for ClassDeclaration and FunctionDeclaration read-only checking - fixes #53 --- lib/6to5/transformers/constants.js | 4 +++- test/fixtures/syntax/constants/no-classes/actual.js | 5 +++++ test/fixtures/syntax/constants/no-classes/options.json | 3 +++ test/fixtures/syntax/constants/no-functions/actual.js | 5 +++++ test/fixtures/syntax/constants/no-functions/options.json | 3 +++ 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/syntax/constants/no-classes/actual.js create mode 100644 test/fixtures/syntax/constants/no-classes/options.json create mode 100644 test/fixtures/syntax/constants/no-functions/actual.js create mode 100644 test/fixtures/syntax/constants/no-functions/options.json diff --git a/lib/6to5/transformers/constants.js b/lib/6to5/transformers/constants.js index 6df3a4dbfe..6257b8a985 100644 --- a/lib/6to5/transformers/constants.js +++ b/lib/6to5/transformers/constants.js @@ -55,7 +55,9 @@ exports.ForStatement = function (node) { traverse(node, function (child) { if (child._ignoreConstant) return; - if (child.type === "VariableDeclarator") { + if (child.type === "VariableDeclarator" || + child.type === "FunctionDeclaration" || + child.type === "ClassDeclaration") { check(child, child.id.name); } else if (child.type === "AssignmentExpression") { check(child, child.left.name); diff --git a/test/fixtures/syntax/constants/no-classes/actual.js b/test/fixtures/syntax/constants/no-classes/actual.js new file mode 100644 index 0000000000..4d0266bd50 --- /dev/null +++ b/test/fixtures/syntax/constants/no-classes/actual.js @@ -0,0 +1,5 @@ +const MULTIPLIER = 5; + +class MULTIPLIER { + +} diff --git a/test/fixtures/syntax/constants/no-classes/options.json b/test/fixtures/syntax/constants/no-classes/options.json new file mode 100644 index 0000000000..9b96d41ac9 --- /dev/null +++ b/test/fixtures/syntax/constants/no-classes/options.json @@ -0,0 +1,3 @@ +{ + "throws": "MULTIPLIER is read-only" +} diff --git a/test/fixtures/syntax/constants/no-functions/actual.js b/test/fixtures/syntax/constants/no-functions/actual.js new file mode 100644 index 0000000000..8b3451172f --- /dev/null +++ b/test/fixtures/syntax/constants/no-functions/actual.js @@ -0,0 +1,5 @@ +const MULTIPLIER = 5; + +function MULTIPLIER() { + +} diff --git a/test/fixtures/syntax/constants/no-functions/options.json b/test/fixtures/syntax/constants/no-functions/options.json new file mode 100644 index 0000000000..9b96d41ac9 --- /dev/null +++ b/test/fixtures/syntax/constants/no-functions/options.json @@ -0,0 +1,3 @@ +{ + "throws": "MULTIPLIER is read-only" +}