-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.js
70 lines (66 loc) · 2.45 KB
/
analyze.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const ts = require('typescript');
const { getLanguageService } = require('vscode-html-languageservice');
const { analyzeText } = require('web-component-analyzer');
const { getAttributeType, getNodeType } = require('./type.extension.js');
/**
* @param {string[]} paths
* @param {ts.Program} program
* @param {ts.TypeChecker} checker
* @returns
*/
function analyzeComponents(paths, program, checker) {
return Object.fromEntries(
analyzeText(
paths.map((x) => ({
fileName: x,
}))
)
.results.filter((x) => x.componentDefinitions?.length > 0)
.flatMap((x) => x.componentDefinitions)
.map((x) => [x.tagName, getNodeType(program, checker, Array.from(x.identifierNodes)[0])])
);
}
/**
* @param {import('vscode').TextDocument} document
*/
function analyzeHTMLDocument(document) {
var languageService = getLanguageService();
var htmlDocument = languageService.parseHTMLDocument(
Object.assign(Object.assign({}, document), { uri: document.uri.toString() })
);
var nodes = [...htmlDocument.roots];
for (var node of nodes) {
if (!node.children) continue;
nodes.push(...node.children);
}
return nodes.filter((x) => x.attributes && Object.values(x.attributes).length > 0);
}
/**
*
* @param {import('vscode-html-languageservice').Node[]} htmlNodes
* @param {ts.Node[]} tsNodes
* @param {ts.SourceFile} scriptFile
* @param {ts.TypeChecker} typeChecker
* @param { '.' | '@' } prefix
*/
function analyzeAttributeCollection(htmlNodes, tsNodes, scriptFile, typeChecker, prefix) {
/** @type {{parent: string, attributes: [string, import('./attribute-value').AttributeValue][]}[]} */
var attributeCollections = [];
for (var node of htmlNodes) {
var complexAttributes = Object.entries(node.attributes || {}).filter((x) => x[0]?.startsWith(prefix) && x[1]);
if (complexAttributes.length > 0)
attributeCollections.push({
parent: node.tag,
attributes: complexAttributes.map((x) => [
x[0],
{
type: getAttributeType(x[1], tsNodes, scriptFile, typeChecker),
valueText: x[1],
parentNode: node,
},
]),
});
}
return attributeCollections;
}
module.exports = { analyzeAttributeCollection, analyzeComponents, analyzeHTMLDocument };