From 63397d0aade7add238b27bfb5ff879f0a7fd5ed9 Mon Sep 17 00:00:00 2001 From: Raja Sekar Date: Thu, 16 Nov 2017 08:59:46 +0530 Subject: [PATCH] Better error message for super when not using an object method (#6754) --- packages/babylon/src/parser/expression.js | 5 ++++- .../class-methods/direct-super-in-object-method/actual.js | 7 +++++++ .../direct-super-in-object-method/options.json | 3 +++ .../test/fixtures/es2015/uncategorised/344/options.json | 4 ++-- .../invalid_super_not_inside_function/options.json | 4 ++-- 5 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/actual.js create mode 100644 packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/options.json diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index 6a4fcd0811..f4a39379c8 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -648,7 +648,10 @@ export default class ExpressionParser extends LValParser { !this.state.inClassProperty && !this.options.allowSuperOutsideMethod ) { - this.raise(this.state.start, "'super' outside of function or class"); + this.raise( + this.state.start, + "super is only allowed in object methods and classes", + ); } node = this.startNode(); diff --git a/packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/actual.js b/packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/actual.js new file mode 100644 index 0000000000..4f4affc38e --- /dev/null +++ b/packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/actual.js @@ -0,0 +1,7 @@ +Object.create({}, { + foo: { + get: function(){ + return super.foo; + } + } +}); diff --git a/packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/options.json b/packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/options.json new file mode 100644 index 0000000000..660e644cf6 --- /dev/null +++ b/packages/babylon/test/fixtures/es2015/class-methods/direct-super-in-object-method/options.json @@ -0,0 +1,3 @@ +{ + "throws": "super is only allowed in object methods and classes (4:13)" +} diff --git a/packages/babylon/test/fixtures/es2015/uncategorised/344/options.json b/packages/babylon/test/fixtures/es2015/uncategorised/344/options.json index ad647a87fc..ad3fdc7d73 100644 --- a/packages/babylon/test/fixtures/es2015/uncategorised/344/options.json +++ b/packages/babylon/test/fixtures/es2015/uncategorised/344/options.json @@ -1,3 +1,3 @@ { - "throws": "'super' outside of function or class (1:0)" -} \ No newline at end of file + "throws": "super is only allowed in object methods and classes (1:0)" +} diff --git a/packages/babylon/test/fixtures/esprima/es2015-super-property/invalid_super_not_inside_function/options.json b/packages/babylon/test/fixtures/esprima/es2015-super-property/invalid_super_not_inside_function/options.json index 1518a5e3fe..cf468e4854 100644 --- a/packages/babylon/test/fixtures/esprima/es2015-super-property/invalid_super_not_inside_function/options.json +++ b/packages/babylon/test/fixtures/esprima/es2015-super-property/invalid_super_not_inside_function/options.json @@ -1,3 +1,3 @@ { - "throws": "'super' outside of function or class (1:8)" -} \ No newline at end of file + "throws": "super is only allowed in object methods and classes (1:8)" +}