forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.js
More file actions
153 lines (127 loc) · 3.93 KB
/
Copy pathast.js
File metadata and controls
153 lines (127 loc) · 3.93 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import { parse, parseExpression } from '@babel/parser'
import generate from '@babel/generator'
import traverse from '@babel/traverse'
import prettier from 'prettier'
import parserHtml from 'prettier/parser-html'
import parseCss from 'prettier/parser-postcss'
import parserBabel from 'prettier/parser-babel'
const METHOD_REGEXP = /function.*?\(/
export const insertName = (name, content) => content.replace(METHOD_REGEXP, `function ${name}(`)
export const removeName = (content) => content.replace(METHOD_REGEXP, 'function (')
export const string2Ast = (string = '') => parse(string, { sourceType: 'module', plugins: ['typescript'] })
export const ast2String = (ast) => generate(ast, { retainLines: true }).code
const formatScript = (string) => {
let newStr = string
const options = {
parser: 'babel',
plugins: [parserBabel],
printWidth: 120,
singleQuote: true,
semi: false,
trailingComma: 'none'
}
try {
// 低码中的编辑器大多只会输入js值,并不是一个完整的javascript表达式,无法格式化,因此需要特殊处理预格式化该种情形
newStr = prettier.format(`!${string}`, options).substring(1).replace(/\n$/, '')
} catch (error) {
newStr = prettier.format(newStr, options)
}
return newStr
}
const formatJson = (string) =>
prettier.format(string, {
parser: 'json',
plugins: [parserBabel],
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true
})
const formatHtml = (string) =>
prettier.format(string, {
parser: 'html',
plugins: [parserBabel, parserHtml]
})
const formatCss = (string) =>
prettier.format(string, {
parser: 'css',
plugins: [parseCss]
})
const formatterMap = {
json: formatJson,
typescript: formatScript,
javascript: formatScript,
html: formatHtml,
css: formatCss
}
export const formatString = (str, language) => {
const formatter = formatterMap[language] || formatJson
let result = str
try {
result = formatter(str)
} catch (error) {
const printer = console
printer.log(error)
}
return result
}
export { parse, parseExpression, traverse, generate }
export const includedExpression = (code, expression) => {
let flag = false
try {
traverse(parse(code), {
ExpressionStatement(path) {
if (path.toString().includes(expression)) {
flag = true
return
}
}
})
} catch (err) {
const printer = console
printer.log(err)
}
return flag
}
export const includedExpressionInSchema = (schemaObj, expression) => {
let hadFlag = false
const checkReferencedFromSchema = (_schemaObj) => {
Object.values(_schemaObj).forEach((schemaObjIner) => {
if (
['[object Array]', '[object Object]'].includes(Object.prototype.toString.call(schemaObjIner)) &&
Object.keys(schemaObjIner).length
) {
if (schemaObjIner.type && ['jsstring', 'JSExpression', 'JSFunction'].includes(schemaObjIner.type)) {
if (includedExpression(schemaObjIner.value, expression)) {
hadFlag = true
return
}
} else {
checkReferencedFromSchema(schemaObjIner)
}
}
})
}
checkReferencedFromSchema(schemaObj)
return hadFlag
}
export const findExpressionInAppSchema = (pageSchemas, expression) => {
const includedPage = []
pageSchemas.forEach((pageSchema) => {
if (includedExpressionInSchema(pageSchema, expression)) {
includedPage.push(pageSchema.fileName)
}
})
return includedPage
}