-
Notifications
You must be signed in to change notification settings - Fork 8
/
DocumentParser.js
39 lines (34 loc) · 1.5 KB
/
DocumentParser.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
import { languages } from 'vscode';
import Parser from './Parser';
import JSONParser from './JSONParser';
import JSParser from './JSParser';
import YAMLParser from './YAMLParser';
import PkgParser from './PkgParser';
export default class DocumentParser extends Parser {
constructor(document) {
super(document);
function isMatch(language, ...patterns) {
return patterns.some(pattern => {
const selector = {
pattern,
scheme: 'file',
language
};
return (languages.match(selector, document) > 0);
});
}
if (new.target === DocumentParser) {
// Choose parser based on filename and language
if (isMatch('javascript', '**/.eslintrc.js', '**/.eslintrc.cjs') || isMatch('javascriptreact', '**/.eslintrc.js', '**/.eslintrc.cjs')) {
return new JSParser(document);
} else if (isMatch('json', '**/.eslintrc', '**/.eslintrc.json') || isMatch('jsonc', '**/.eslintrc', '**/.eslintrc.json')) {
return new JSONParser(document);
} else if (isMatch('yaml', '**/.eslintrc', '**/.eslintrc.yaml', '**/.eslintrc.yml')) {
return new YAMLParser(document);
} else if (isMatch('json', '**/package.json')) {
return new PkgParser(document);
}
// If code reaches here, a standard parser with no functionality will be returned
}
}
}