From aec4beff0c2dae9ae99db8f5169d8460aa6db1dd Mon Sep 17 00:00:00 2001 From: Andrew Levine Date: Mon, 23 Jan 2017 16:56:39 -0600 Subject: [PATCH] Don't parse class properties without initializers when classProperties is disabled and Flow is enabled (#300) --- src/parser/statement.js | 7 ++++++- .../with-initializer-and-type-no-plugin/actual.js | 3 +++ .../with-initializer-and-type-no-plugin/options.json | 4 ++++ .../with-initializer-missing-plugin/actual.js | 3 +++ .../with-initializer-missing-plugin/options.json | 3 +++ .../without-initializer-missing-plugin/actual.js | 3 +++ .../without-initializer-missing-plugin/options.json | 3 +++ 7 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/actual.js create mode 100644 test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/options.json create mode 100644 test/fixtures/experimental/class-properties/with-initializer-missing-plugin/actual.js create mode 100644 test/fixtures/experimental/class-properties/with-initializer-missing-plugin/options.json create mode 100644 test/fixtures/experimental/class-properties/without-initializer-missing-plugin/actual.js create mode 100644 test/fixtures/experimental/class-properties/without-initializer-missing-plugin/options.json diff --git a/src/parser/statement.js b/src/parser/statement.js index 236c5dc019..f4e82f5993 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -751,8 +751,13 @@ pp.parseClassBody = function (node) { }; pp.parseClassProperty = function (node) { + const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled."; + if (!node.typeAnnotation && !this.hasPlugin("classProperties")) { + this.raise(node.start, noPluginMsg); + } + if (this.match(tt.eq)) { - if (!this.hasPlugin("classProperties")) this.unexpected(); + if (!this.hasPlugin("classProperties")) this.raise(this.state.start, noPluginMsg); this.next(); node.value = this.parseMaybeAssign(); } else { diff --git a/test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/actual.js b/test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/actual.js new file mode 100644 index 0000000000..f82f8d51d7 --- /dev/null +++ b/test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/actual.js @@ -0,0 +1,3 @@ +class Foo { + bar: string = 'bizz'; +} diff --git a/test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/options.json b/test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/options.json new file mode 100644 index 0000000000..e185feb80a --- /dev/null +++ b/test/fixtures/experimental/class-properties/with-initializer-and-type-no-plugin/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["flow"], + "throws": "You can only use Class Properties when the 'classProperties' plugin is enabled. (2:14)" +} diff --git a/test/fixtures/experimental/class-properties/with-initializer-missing-plugin/actual.js b/test/fixtures/experimental/class-properties/with-initializer-missing-plugin/actual.js new file mode 100644 index 0000000000..fa80e3af68 --- /dev/null +++ b/test/fixtures/experimental/class-properties/with-initializer-missing-plugin/actual.js @@ -0,0 +1,3 @@ +class Foo { + bar = 'bizz'; +} diff --git a/test/fixtures/experimental/class-properties/with-initializer-missing-plugin/options.json b/test/fixtures/experimental/class-properties/with-initializer-missing-plugin/options.json new file mode 100644 index 0000000000..af38bd1530 --- /dev/null +++ b/test/fixtures/experimental/class-properties/with-initializer-missing-plugin/options.json @@ -0,0 +1,3 @@ +{ + "throws": "You can only use Class Properties when the 'classProperties' plugin is enabled. (2:2)" +} diff --git a/test/fixtures/experimental/class-properties/without-initializer-missing-plugin/actual.js b/test/fixtures/experimental/class-properties/without-initializer-missing-plugin/actual.js new file mode 100644 index 0000000000..a36cdd975c --- /dev/null +++ b/test/fixtures/experimental/class-properties/without-initializer-missing-plugin/actual.js @@ -0,0 +1,3 @@ +class Foo { + bar; +} diff --git a/test/fixtures/experimental/class-properties/without-initializer-missing-plugin/options.json b/test/fixtures/experimental/class-properties/without-initializer-missing-plugin/options.json new file mode 100644 index 0000000000..af38bd1530 --- /dev/null +++ b/test/fixtures/experimental/class-properties/without-initializer-missing-plugin/options.json @@ -0,0 +1,3 @@ +{ + "throws": "You can only use Class Properties when the 'classProperties' plugin is enabled. (2:2)" +}