ether-code 0.5.3 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/compiler.js +86 -1
- package/cli/ether.js +1 -1
- package/package.json +1 -1
package/cli/compiler.js
CHANGED
|
@@ -68,6 +68,7 @@ class EtherCompiler {
|
|
|
68
68
|
|
|
69
69
|
this.generators = {}
|
|
70
70
|
this.parser = null
|
|
71
|
+
this.resolvedIncludes = new Set()
|
|
71
72
|
|
|
72
73
|
this.initGenerators()
|
|
73
74
|
this.initParser()
|
|
@@ -110,10 +111,14 @@ class EtherCompiler {
|
|
|
110
111
|
compileFile(filePath) {
|
|
111
112
|
const content = fs.readFileSync(filePath, 'utf-8')
|
|
112
113
|
const fileName = path.basename(filePath, '.eth')
|
|
114
|
+
const basePath = path.dirname(path.resolve(filePath))
|
|
113
115
|
|
|
114
116
|
const target = this.detectTarget(content, filePath)
|
|
115
117
|
const ast = this.parse(content, target)
|
|
116
|
-
|
|
118
|
+
|
|
119
|
+
const resolvedAst = this.resolveIncludes(ast, basePath, target)
|
|
120
|
+
|
|
121
|
+
const code = this.generate(resolvedAst, target)
|
|
117
122
|
|
|
118
123
|
const extension = this.getExtension(target)
|
|
119
124
|
const outputPath = fileName + extension
|
|
@@ -128,6 +133,86 @@ class EtherCompiler {
|
|
|
128
133
|
}
|
|
129
134
|
}
|
|
130
135
|
|
|
136
|
+
resolveIncludes(ast, basePath, targetLang) {
|
|
137
|
+
const self = this
|
|
138
|
+
|
|
139
|
+
const visit = (node) => {
|
|
140
|
+
if (!node || typeof node !== 'object') return node
|
|
141
|
+
|
|
142
|
+
if (node.type === 'Include' && node.path) {
|
|
143
|
+
let includePath = node.path
|
|
144
|
+
|
|
145
|
+
if (!includePath.endsWith('.eth') && !includePath.endsWith('.ether')) {
|
|
146
|
+
includePath = includePath.replace(/\.(html|php|js|css)$/i, '.eth')
|
|
147
|
+
if (!includePath.endsWith('.eth')) {
|
|
148
|
+
includePath += '.eth'
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const fullPath = path.resolve(basePath, includePath)
|
|
153
|
+
|
|
154
|
+
if (self.resolvedIncludes.has(fullPath)) {
|
|
155
|
+
console.warn(`Inclusion circulaire détectée: ${fullPath}`)
|
|
156
|
+
return null
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!fs.existsSync(fullPath)) {
|
|
160
|
+
console.warn(`Fichier inclus non trouvé: ${fullPath}`)
|
|
161
|
+
return {
|
|
162
|
+
type: 'Comment',
|
|
163
|
+
content: `Fichier non trouvé: ${node.path}`
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
self.resolvedIncludes.add(fullPath)
|
|
168
|
+
|
|
169
|
+
const includeSource = fs.readFileSync(fullPath, 'utf-8')
|
|
170
|
+
const includeDir = path.dirname(fullPath)
|
|
171
|
+
|
|
172
|
+
const normalizedTarget = self.normalizeTarget(targetLang)
|
|
173
|
+
const includeAst = self.parser.parse(includeSource, normalizedTarget)
|
|
174
|
+
|
|
175
|
+
const resolvedIncludeAst = self.resolveIncludes(includeAst, includeDir, targetLang)
|
|
176
|
+
|
|
177
|
+
self.resolvedIncludes.delete(fullPath)
|
|
178
|
+
|
|
179
|
+
if (resolvedIncludeAst.children && Array.isArray(resolvedIncludeAst.children)) {
|
|
180
|
+
return resolvedIncludeAst.children
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (resolvedIncludeAst.body && Array.isArray(resolvedIncludeAst.body)) {
|
|
184
|
+
return resolvedIncludeAst.body
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return resolvedIncludeAst
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (node.children && Array.isArray(node.children)) {
|
|
191
|
+
node.children = node.children.flatMap(child => {
|
|
192
|
+
const result = visit(child)
|
|
193
|
+
if (Array.isArray(result)) {
|
|
194
|
+
return result
|
|
195
|
+
}
|
|
196
|
+
return result ? [result] : []
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (node.body && Array.isArray(node.body)) {
|
|
201
|
+
node.body = node.body.flatMap(child => {
|
|
202
|
+
const result = visit(child)
|
|
203
|
+
if (Array.isArray(result)) {
|
|
204
|
+
return result
|
|
205
|
+
}
|
|
206
|
+
return result ? [result] : []
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return node
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return visit(ast)
|
|
214
|
+
}
|
|
215
|
+
|
|
131
216
|
parse(content, target) {
|
|
132
217
|
const normalizedTarget = this.normalizeTarget(target)
|
|
133
218
|
return this.parser.parse(content, normalizedTarget)
|
package/cli/ether.js
CHANGED