@@ -69,4 +69,63 @@ describe('parse(content, { settings, ecmaFeatures })', function () {
6969 expect ( parse . bind ( null , path , content , { settings : { 'import/parsers' : { [ parseStubParserPath ] : [ '.js' ] } } , parserPath : null , parserOptions } ) ) . not . to . throw ( Error ) ;
7070 expect ( parseSpy . callCount , 'custom parser to be called once' ) . to . equal ( 1 ) ;
7171 } ) ;
72+
73+ it ( 'throws on invalid languageOptions' , function ( ) {
74+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : null , languageOptions : null } ) ) . to . throw ( Error ) ;
75+ } ) ;
76+
77+ it ( 'throws on non-object languageOptions.parser' , function ( ) {
78+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : null , languageOptions : { parser : 'espree' } } ) ) . to . throw ( Error ) ;
79+ } ) ;
80+
81+ it ( 'throws on null languageOptions.parser' , function ( ) {
82+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : null , languageOptions : { parser : null } } ) ) . to . throw ( Error ) ;
83+ } ) ;
84+
85+ it ( 'throws on empty languageOptions.parser' , function ( ) {
86+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : null , languageOptions : { parser : { } } } ) ) . to . throw ( Error ) ;
87+ } ) ;
88+
89+ it ( 'throws on non-function languageOptions.parser.parse' , function ( ) {
90+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : null , languageOptions : { parser : { parse : 'espree' } } } ) ) . to . throw ( Error ) ;
91+ } ) ;
92+
93+ it ( 'throws on non-function languageOptions.parser.parse' , function ( ) {
94+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : null , languageOptions : { parser : { parseForESLint : 'espree' } } } ) ) . to . throw ( Error ) ;
95+ } ) ;
96+
97+ it ( 'requires only one of the parse methods' , function ( ) {
98+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : null , languageOptions : { parser : { parseForESLint : ( ) => ( { ast : { } } ) } } } ) ) . not . to . throw ( Error ) ;
99+ } ) ;
100+
101+ it ( 'uses parse from languageOptions.parser' , function ( ) {
102+ const parseSpy = sinon . spy ( ) ;
103+ expect ( parse . bind ( null , path , content , { settings : { } , languageOptions : { parser : { parse : parseSpy } } } ) ) . not . to . throw ( Error ) ;
104+ expect ( parseSpy . callCount , 'passed parser to be called once' ) . to . equal ( 1 ) ;
105+ } ) ;
106+
107+ it ( 'uses parseForESLint from languageOptions.parser' , function ( ) {
108+ const parseSpy = sinon . spy ( ( ) => ( { ast : { } } ) ) ;
109+ expect ( parse . bind ( null , path , content , { settings : { } , languageOptions : { parser : { parseForESLint : parseSpy } } } ) ) . not . to . throw ( Error ) ;
110+ expect ( parseSpy . callCount , 'passed parser to be called once' ) . to . equal ( 1 ) ;
111+ } ) ;
112+
113+ it ( 'prefers parsers specified in the settings over languageOptions.parser' , ( ) => {
114+ const parseSpy = sinon . spy ( ) ;
115+ parseStubParser . parse = parseSpy ;
116+ expect ( parse . bind ( null , path , content , { settings : { 'import/parsers' : { [ parseStubParserPath ] : [ '.js' ] } } , parserPath : null , languageOptions : { parser : { parse ( ) { } } } } ) ) . not . to . throw ( Error ) ;
117+ expect ( parseSpy . callCount , 'custom parser to be called once' ) . to . equal ( 1 ) ;
118+ } ) ;
119+
120+ it ( 'ignores parser options from language options set to null' , ( ) => {
121+ const parseSpy = sinon . spy ( ) ;
122+ parseStubParser . parse = parseSpy ;
123+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : 'espree' , languageOptions : { parserOptions : null } , parserOptions : { sourceType : 'module' , ecmaVersion : 2015 , ecmaFeatures : { jsx : true } } } ) ) . not . to . throw ( Error ) ;
124+ } ) ;
125+
126+ it ( 'prefers languageOptions.parserOptions over parserOptions' , ( ) => {
127+ const parseSpy = sinon . spy ( ) ;
128+ parseStubParser . parse = parseSpy ;
129+ expect ( parse . bind ( null , path , content , { settings : { } , parserPath : 'espree' , languageOptions : { parserOptions : { sourceType : 'module' , ecmaVersion : 2015 , ecmaFeatures : { jsx : true } } } , parserOptions : { sourceType : 'script' } } ) ) . not . to . throw ( Error ) ;
130+ } ) ;
72131} ) ;
0 commit comments