From 3e89534bb70bf9907f2c91b1d9de23db832fef25 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 28 Oct 2017 15:57:54 +0500 Subject: [PATCH] Auto-add `allowedStartRules` to test options for `changeAST` and `reportError` methods if it not defined --- lib/compiler/passes/report-undefined-rules.js | 14 +++++--------- test/spec/unit/compiler/passes/helpers.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/compiler/passes/report-undefined-rules.js b/lib/compiler/passes/report-undefined-rules.js index 798568e..e6a9b8e 100644 --- a/lib/compiler/passes/report-undefined-rules.js +++ b/lib/compiler/passes/report-undefined-rules.js @@ -24,19 +24,15 @@ function reportUndefinedRules( ast, options ) { check( ast ); - if ( options.allowedStartRules ) { + options.allowedStartRules.forEach( rule => { - options.allowedStartRules.forEach( rule => { + if ( ! asts.findRule( ast, rule ) ) { - if ( ! asts.findRule( ast, rule ) ) { + throw new GrammarError( `Start rule "${ rule }" is not defined.` ); - throw new GrammarError( `Start rule "${ rule }" is not defined.` ); - - } - - } ); + } - } + } ); } diff --git a/test/spec/unit/compiler/passes/helpers.js b/test/spec/unit/compiler/passes/helpers.js index 86f37e1..d897108 100644 --- a/test/spec/unit/compiler/passes/helpers.js +++ b/test/spec/unit/compiler/passes/helpers.js @@ -55,6 +55,14 @@ module.exports = function ( chai, utils ) { const ast = parser.parse( grammar ); + if ( ! options.allowedStartRules ) { + + options.allowedStartRules = ast.rules.length > 0 + ? [ ast.rules[ 0 ].name ] + : []; + + } + utils.flag( this, "object" )( ast, options ); this.assert( @@ -73,6 +81,14 @@ module.exports = function ( chai, utils ) { const ast = parser.parse( grammar ); + if ( ! options.allowedStartRules ) { + + options.allowedStartRules = ast.rules.length > 0 + ? [ ast.rules[ 0 ].name ] + : []; + + } + let passed, result; try {