33 * @copyright 2017 Toru Nagashima. All rights reserved.
44 * See LICENSE file in root directory for full license.
55 */
6+ import assert from "assert"
67import EventEmitter from "events"
78import NodeEventGenerator from "eslint/lib/util/node-event-generator"
89import TokenStore from "eslint/lib/token-store"
@@ -21,7 +22,7 @@ const stores = new WeakMap<object, TokenStore>()
2122 * @returns The emitter for this context.
2223 */
2324function ensureEmitter ( context : any ) : EventEmitter {
24- const ast = context . getSourceCode ( )
25+ const ast = context . getSourceCode ( ) . ast
2526 let emitter = emitters . get ( ast )
2627
2728 if ( ! emitter ) {
@@ -44,31 +45,38 @@ function ensureEmitter(context: any): EventEmitter {
4445// Exports
4546//------------------------------------------------------------------------------
4647
47- export default {
48- registerTemplateBodyVisitor ( context : any , visitor : { [ key : string ] : Function } ) : void {
49- const emitter = ensureEmitter ( context )
48+ /**
49+ * Define the parser service
50+ * @param rootAST
51+ */
52+ export function define ( rootAST : ESLintProgram ) {
53+ return {
54+ registerTemplateBodyVisitor ( context : any , visitor : { [ key : string ] : Function } ) : void {
55+ assert ( context . getSourceCode ( ) . ast === rootAST )
56+ const emitter = ensureEmitter ( context )
5057
51- for ( const selector of Object . keys ( visitor ) ) {
52- emitter . on ( selector , visitor [ selector ] )
53- }
54- } ,
58+ for ( const selector of Object . keys ( visitor ) ) {
59+ emitter . on ( selector , visitor [ selector ] )
60+ }
61+ } ,
5562
56- /**
57- * Get the token store of the template body.
58- * @param context The rule context to get .
59- * @returns The token store of template body.
60- */
61- getTemplateBodyTokenStore ( context : any ) : TokenStore {
62- const ast = context . getSourceCode ( ) . ast . templateBody
63- let store = stores . get ( ast )
63+ /**
64+ * Get the token store of the template body.
65+ * @returns The token store of template body .
66+ */
67+ getTemplateBodyTokenStore ( ) : TokenStore {
68+ const ast = rootAST . templateBody
69+ const key = ast || stores
70+ let store = stores . get ( key )
6471
65- if ( ! store ) {
66- store = ( ast != null )
67- ? new TokenStore ( ast . tokens , ast . comments )
68- : new TokenStore ( [ ] , [ ] )
69- stores . set ( ast , store )
70- }
72+ if ( ! store ) {
73+ store = ( ast != null )
74+ ? new TokenStore ( ast . tokens , ast . comments )
75+ : new TokenStore ( [ ] , [ ] )
76+ stores . set ( key , store )
77+ }
7178
72- return store
73- } ,
79+ return store
80+ } ,
81+ }
7482}
0 commit comments