Skip to content

Commit 560028f

Browse files
committed
New: services.getTemplateBodyTokenStore
1 parent 89b16de commit 560028f

24 files changed

+383
-48
lines changed

lib/get-token-store.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @author Toru Nagashima <https://github.com/mysticatea>
3+
* @copyright 2017 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const TokenStore = require("eslint/lib/token-store")
13+
14+
//------------------------------------------------------------------------------
15+
// Helpers
16+
//------------------------------------------------------------------------------
17+
18+
const stores = new WeakMap()
19+
20+
/**
21+
* Get the token store of the template body.
22+
* @param {RuleContext} context The rule context to get.
23+
* @returns {TokenStore} The token store of template body.
24+
*/
25+
function getTokenStore(context) {
26+
const ast = context.getSourceCode().ast.templateBody
27+
if (stores.has(ast)) {
28+
return stores.get(ast)
29+
}
30+
const store = (ast != null)
31+
? new TokenStore(ast.tokens, ast.comments)
32+
: new TokenStore([], [])
33+
34+
stores.set(ast, store)
35+
return store
36+
}
37+
38+
//------------------------------------------------------------------------------
39+
// Exports
40+
//------------------------------------------------------------------------------
41+
42+
module.exports = getTokenStore

lib/parse-component.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const path = require("path")
1313
const html = require("parse5")
14+
const getTemplateBodyTokenStore = require("./get-token-store")
1415
const registerTemplateBodyVisitor = require("./register-template-body-visitor")
1516
const ScriptParser = require("./script-parser")
1617
const TokenGenerator = require("./token-generator")
@@ -173,7 +174,10 @@ function parse(code, options) {
173174

174175
return {
175176
ast,
176-
services: {registerTemplateBodyVisitor},
177+
services: {
178+
getTemplateBodyTokenStore,
179+
registerTemplateBodyVisitor,
180+
},
177181
}
178182
}
179183

test/fixtures/template-ast/attributes.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -1883,6 +1884,5 @@
18831884
}
18841885
],
18851886
"comments": []
1886-
},
1887-
"comments": []
1887+
}
18881888
}

test/fixtures/template-ast/comments.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -961,6 +962,5 @@
961962
"value": " comment 3 "
962963
}
963964
]
964-
},
965-
"comments": []
965+
}
966966
}

test/fixtures/template-ast/directive-argument-and-modifiers.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -847,6 +848,5 @@
847848
}
848849
],
849850
"comments": []
850-
},
851-
"comments": []
851+
}
852852
}

test/fixtures/template-ast/directive-arguments.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -1269,6 +1270,5 @@
12691270
}
12701271
],
12711272
"comments": []
1272-
},
1273-
"comments": []
1273+
}
12741274
}

test/fixtures/template-ast/directive-modifiers.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -1338,6 +1339,5 @@
13381339
}
13391340
],
13401341
"comments": []
1341-
},
1342-
"comments": []
1342+
}
13431343
}

test/fixtures/template-ast/directive-shorthands.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -846,6 +847,5 @@
846847
}
847848
],
848849
"comments": []
849-
},
850-
"comments": []
850+
}
851851
}

test/fixtures/template-ast/directives.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -2569,6 +2570,5 @@
25692570
}
25702571
],
25712572
"comments": []
2572-
},
2573-
"comments": []
2573+
}
25742574
}

test/fixtures/template-ast/elements.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -815,6 +816,5 @@
815816
}
816817
],
817818
"comments": []
818-
},
819-
"comments": []
819+
}
820820
}

test/fixtures/template-ast/empty-script-only.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"body": [],
1919
"sourceType": "script",
20+
"comments": [],
2021
"tokens": [
2122
{
2223
"type": "Punctuator",
@@ -55,6 +56,5 @@
5556
"value": "</script>"
5657
}
5758
],
58-
"templateBody": null,
59-
"comments": []
59+
"templateBody": null
6060
}

test/fixtures/template-ast/empty-template-only.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -219,6 +220,5 @@
219220
}
220221
],
221222
"comments": []
222-
},
223-
"comments": []
223+
}
224224
}

test/fixtures/template-ast/empty.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
22-
"templateBody": null,
23-
"comments": []
23+
"templateBody": null
2424
}

test/fixtures/template-ast/expression-container-only.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -337,6 +338,5 @@
337338
}
338339
],
339340
"comments": []
340-
},
341-
"comments": []
341+
}
342342
}

test/fixtures/template-ast/html-entities-in-static-places.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -691,6 +692,5 @@
691692
"value": " a && b "
692693
}
693694
]
694-
},
695-
"comments": []
695+
}
696696
}

test/fixtures/template-ast/html-entities-with-line-terminators.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -728,6 +729,5 @@
728729
}
729730
],
730731
"comments": []
731-
},
732-
"comments": []
732+
}
733733
}

test/fixtures/template-ast/html-entities.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -1041,6 +1042,5 @@
10411042
}
10421043
],
10431044
"comments": []
1044-
},
1045-
"comments": []
1045+
}
10461046
}

test/fixtures/template-ast/not-closing-elements.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -674,6 +675,5 @@
674675
}
675676
],
676677
"comments": []
677-
},
678-
"comments": []
678+
}
679679
}

test/fixtures/template-ast/text-and-expression-container.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -562,6 +563,5 @@
562563
}
563564
],
564565
"comments": []
565-
},
566-
"comments": []
566+
}
567567
}

test/fixtures/template-ast/text.ast.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"body": [],
2020
"sourceType": "script",
21+
"comments": [],
2122
"tokens": [],
2223
"templateBody": {
2324
"range": [
@@ -256,6 +257,5 @@
256257
}
257258
],
258259
"comments": []
259-
},
260-
"comments": []
260+
}
261261
}

0 commit comments

Comments
 (0)