@@ -28,6 +28,18 @@ const assert = require("assert"),
28
28
rules = require ( "./rules" ) ,
29
29
timing = require ( "./timing" ) ;
30
30
31
+ //------------------------------------------------------------------------------
32
+ // Typedefs
33
+ //------------------------------------------------------------------------------
34
+
35
+ /**
36
+ * The result of a parsing operation from parseForESLint()
37
+ * @typedef {Object } CustomParseResult
38
+ * @property {ASTNode } ast The ESTree AST Program node.
39
+ * @property {Object } services An object containing additional services related
40
+ * to the parser.
41
+ */
42
+
31
43
//------------------------------------------------------------------------------
32
44
// Helpers
33
45
//------------------------------------------------------------------------------
@@ -598,7 +610,8 @@ module.exports = (function() {
598
610
* @param {string } text The text to parse.
599
611
* @param {Object } config The ESLint configuration object.
600
612
* @param {string } filePath The path to the file being parsed.
601
- * @returns {ASTNode } The AST if successful or null if not.
613
+ * @returns {ASTNode|CustomParseResult } The AST or parse result if successful,
614
+ * or null if not.
602
615
* @private
603
616
*/
604
617
function parse ( text , config , filePath ) {
@@ -642,7 +655,11 @@ module.exports = (function() {
642
655
* problem that ESLint identified just like any other.
643
656
*/
644
657
try {
645
- return parser . parse ( text , parserOptions ) ;
658
+ if ( typeof parser . parseForESLint === "function" ) {
659
+ return parser . parseForESLint ( text , parserOptions ) ;
660
+ } else {
661
+ return parser . parse ( text , parserOptions ) ;
662
+ }
646
663
} catch ( ex ) {
647
664
648
665
// If the message includes a leading line number, strip it:
@@ -738,6 +755,7 @@ module.exports = (function() {
738
755
api . verify = function ( textOrSourceCode , config , filenameOrOptions , saveState ) {
739
756
const text = ( typeof textOrSourceCode === "string" ) ? textOrSourceCode : null ;
740
757
let ast ,
758
+ parseResult ,
741
759
shebang ,
742
760
allowInlineConfig ;
743
761
@@ -778,7 +796,7 @@ module.exports = (function() {
778
796
return messages ;
779
797
}
780
798
781
- ast = parse (
799
+ parseResult = parse (
782
800
stripUnicodeBOM ( text ) . replace ( / ^ # ! ( [ ^ \r \n ] + ) / , function ( match , captured ) {
783
801
shebang = captured ;
784
802
return `//${ captured } ` ;
@@ -787,6 +805,14 @@ module.exports = (function() {
787
805
currentFilename
788
806
) ;
789
807
808
+ // if this result is from a parseForESLint() method, normalize
809
+ if ( parseResult && parseResult . ast ) {
810
+ ast = parseResult . ast ;
811
+ } else {
812
+ ast = parseResult ;
813
+ parseResult = null ;
814
+ }
815
+
790
816
if ( ast ) {
791
817
sourceCode = new SourceCode ( text , ast ) ;
792
818
}
@@ -832,7 +858,10 @@ module.exports = (function() {
832
858
try {
833
859
const ruleContext = new RuleContext (
834
860
key , api , severity , options ,
835
- config . settings , config . parserOptions , config . parser , ruleCreator . meta ) ;
861
+ config . settings , config . parserOptions , config . parser ,
862
+ ruleCreator . meta ,
863
+ ( parseResult && parseResult . services ? parseResult . services : { } )
864
+ ) ;
836
865
837
866
const rule = ruleCreator . create ? ruleCreator . create ( ruleContext ) :
838
867
ruleCreator ( ruleContext ) ;
0 commit comments