-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathjs-parse-engine.ts
More file actions
34 lines (27 loc) · 1.17 KB
/
js-parse-engine.ts
File metadata and controls
34 lines (27 loc) · 1.17 KB
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
import * as css from 'css';
import * as vscode from 'vscode';
import CssClassDefinition from '../../common/css-class-definition';
import ParseEngine from '../common/parse-engine';
import SimpleTextDocument from '../common/simple-textdocument';
import CssClassExtractor from '../common/css-class-extractor';
class JsParseEngine implements ParseEngine {
public languageId: string = 'js';
public extension: string = 'js';
public async parse(textDocument: SimpleTextDocument): Promise<CssClassDefinition[]> {
let code: string = textDocument.getText();
const matchTemplateString: Array<string> = code.match(/([^`]).([^`]+)/gmi);
let cssToParse: string = '';
for (let i = 0; i < matchTemplateString.length; i++) {
if (matchTemplateString[i].endsWith('css')) {
cssToParse += matchTemplateString[i + 1] + ' ';
}
if (matchTemplateString[i].endsWith('<style jsx>{')) {
cssToParse += matchTemplateString[i + 1] + ' ';
}
}
cssToParse = cssToParse.replace(/(\$\{[\s]*.*?[\s]*\})/gmi, '0');
let codeAst: css.Stylesheet = css.parse(cssToParse);
return CssClassExtractor.extract(codeAst);
}
}
export default JsParseEngine;