-
Notifications
You must be signed in to change notification settings - Fork 0
/
tspatterns.js
38 lines (33 loc) · 1.22 KB
/
tspatterns.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
"use strict";
const ts = require("typescript");
const getLine =
(sourceFile, node) => sourceFile
.getLineAndCharacterOfPosition(node.getStart(sourceFile))
.line + 1; // + 1 because it is 0-based by default.;
exports.parse = function parse(filename, data) {
const sourceFile = ts.createSourceFile(filename, data);
const { statements } = sourceFile;
for (let statementsIx = 0; statementsIx < statements.length; ++statementsIx) {
const statement = statements[statementsIx];
if (statement.kind !== ts.SyntaxKind.VariableStatement ||
statement.declarationList.kind !==
ts.SyntaxKind.VariableDeclarationList) {
continue; // eslint-disable-line no-continue
}
const decls = statement.declarationList.declarations;
for (let declsIx = 0; declsIx < decls.length; ++declsIx) {
const node = decls[declsIx];
const { initializer } = node;
if (node.kind === ts.SyntaxKind.VariableDeclaration &&
node.name.text === "version" &&
initializer.kind === ts.SyntaxKind.StringLiteral) {
// Bingo!
return {
version: initializer.text,
line: getLine(sourceFile, initializer),
};
}
}
}
return undefined;
};