-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
130 lines (103 loc) · 2.88 KB
/
parser.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const IMPORT_KEYWORD = 'import'
const FROM_KEYWORD = 'from'
const AS_KEYWORD = 'as'
const NAMED_IMPORTS_OPEN_TOKEN = '{'
const NAMED_IMPORTS_CLOSE_TOKEN = '}'
module.exports = function parseImport(line) {
const trimmedLine = line.trim()
const lineWithoutImport = removeImportKeyword(trimmedLine)
if (!hasFrom(lineWithoutImport)) {
const fromModule = removeQuotesAndSemicolon(lineWithoutImport)
return {
defaultImport: null,
namedImports: [],
starImport: null,
fromModule,
}
}
const [beforeFrom, afterFrom] = lineWithoutImport.split(FROM_KEYWORD)
const fromModule = removeQuotesAndSemicolon(afterFrom)
const defaultOrStarImportStr = getDefaultOrStarImportStr(beforeFrom)
const namedImportStr = getNamedImportsStr(beforeFrom)
const { defaultImport, starImport } = parseDefaultOrStarImport(defaultOrStarImportStr)
const namedImports = parseNamedImports(namedImportStr)
return {
defaultImport,
namedImports,
starImport,
fromModule,
}
}
function removeImportKeyword(line) {
return line.slice(IMPORT_KEYWORD.length)
}
function hasFrom(line) {
return line.indexOf(` ${FROM_KEYWORD} `) !== -1
}
function getDefaultOrStarImportStr(importVariables) {
if (containsNamedImports(importVariables)) {
const [beforeNamedImports] = importVariables.split(NAMED_IMPORTS_OPEN_TOKEN)
return beforeNamedImports
}
return importVariables
}
function getNamedImportsStr(importVariables) {
if (containsNamedImports(importVariables)) {
const [_, afterNamedImports] = importVariables.split(NAMED_IMPORTS_OPEN_TOKEN)
return afterNamedImports.replace(NAMED_IMPORTS_CLOSE_TOKEN, '')
}
return null
}
function containsNamedImports(str) {
return str.indexOf(NAMED_IMPORTS_OPEN_TOKEN) !== -1 &&
str.indexOf(NAMED_IMPORTS_CLOSE_TOKEN) !== -1
}
function parseDefaultOrStarImport(str) {
const noDefaultOrStarImport = {
defaultImport: null,
starImport: null,
}
const cleanStr = str.trim().replace(',', '')
if (cleanStr === '') {
return noDefaultOrStarImport
}
if (hasAs(cleanStr)) {
const { value } = parseAs(cleanStr)
return {
defaultImport: null,
starImport: value,
}
}
return {
defaultImport: cleanStr,
starImport: null,
}
}
function parseNamedImports(namedImportsStr) {
if (!namedImportsStr) return []
const namedImportsValues = namedImportsStr.split(',')
return namedImportsValues.map(mapNameAndValue)
}
function mapNameAndValue(str) {
const trimmedStr = str.trim()
if (hasAs(trimmedStr)) {
return parseAs(trimmedStr)
}
return {
name: trimmedStr,
value: trimmedStr,
}
}
function hasAs(str) {
return str.indexOf(` ${AS_KEYWORD} `) !== -1
}
function parseAs(str) {
const [ name, value ] = str.split(` ${AS_KEYWORD} `)
return {
name,
value,
}
}
function removeQuotesAndSemicolon(str) {
return str.trim().replace(/[\;\'\"]/g, '')
}