Skip to content

Commit 5d1e5fd

Browse files
author
Rahul Mahato
committed
fix: Removed extra loop by mapping bottom-up instead of top-down
1 parent 5db2fac commit 5d1e5fd

File tree

1 file changed

+48
-71
lines changed

1 file changed

+48
-71
lines changed

src/platforms/vue-native/scripts/compiler.js

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,23 @@ export function compileVueToRn(resource, filename = 'sfc.vue') {
8787

8888
// parse script
8989
const script = parsedSFC.script
90+
9091
let generatedScriptCode = DEFAULT_OUTPUT.script
9192
if (script) {
92-
const indexofExport = script.content.indexOf('export')
93-
var indexofImport = script.content.indexOf('import')
94-
95-
if (indexofImport < 0) {
96-
indexofImport = indexofExport
97-
}
93+
const scriptContentStartLine = getFirstValidLine(script.content)
9894

9995
const scriptContent = script.content
100-
.slice(Math.min(indexofExport, indexofImport), script.content.length)
96+
.split(newLine)
97+
.slice(scriptContentStartLine)
98+
.join('\n')
10199
.trim()
102-
103100
generatedScriptCode = parseScript(scriptContent)
101+
104102
mappings = generateSourceMap(originalCodeString, filename)
105103
}
106104

107105
if (mappings) {
108106
// Start of the script content of the original code
109-
//
110-
var scriptTagLineNumber = getLineOfExport(parsedSFC.script.content)
111-
112107
var exportDefaultIndex = originalCodeString.indexOf('export default')
113108
var exportDefaultLineNumber = originalCodeString
114109
.substring(0, exportDefaultIndex)
@@ -118,53 +113,34 @@ export function compileVueToRn(resource, filename = 'sfc.vue') {
118113
// add vue options
119114
output += generatedScriptCode
120115
output += '\n\n'
121-
var lineAfterLastImport = getLineOfRVOptions(output)
122-
let originalCodeCursor = scriptTagLineNumber
116+
123117
var endLines = output.split(newLine).length - 1
118+
let firstLineOfScript = getFirstValidLine(parsedSFC.script.content) + 1
124119

125-
let generatedCodeCursor = lineAfterLastImport
126-
for (; generatedCodeCursor < endLines; generatedCodeCursor++) {
127-
//Skip export default line
120+
let generatedCodeCursor = endLines
121+
let scriptEndLine = parsedSFC.script.content.split(newLine).length
128122

129-
if (originalCodeCursor !== exportDefaultLineNumber) {
123+
// Mapping from Bottom to Up from the endlines of script and generated code to first valid line of parsed script content
124+
for (
125+
let scriptCodeCursor = scriptEndLine;
126+
scriptCodeCursor >= firstLineOfScript;
127+
scriptCodeCursor--
128+
) {
129+
//Skip export default line
130+
if (scriptCodeCursor !== exportDefaultLineNumber) {
130131
mappings.addMapping({
131132
source: mappings._hashedFilename,
132133
generated: {
133134
line: generatedCodeCursor,
134135
column: 0,
135136
},
136137
original: {
137-
line: originalCodeCursor,
138+
line: scriptCodeCursor,
138139
column: 0,
139140
},
140141
})
141142
}
142-
originalCodeCursor++
143-
}
144-
145-
// Mapping the contents in between export default and script opening tag
146-
147-
let lineOfRVOptionsInOutput = getLineOfRVOptions(output)
148-
let lineOfFirstImportInScript =
149-
getLineOfFirstImport(parsedSFC.script.content) + 1
150-
let lineOfExpDefaultInScript = scriptTagLineNumber
151-
152-
if (lineOfFirstImportInScript !== 0) {
153-
while (lineOfExpDefaultInScript >= lineOfFirstImportInScript) {
154-
mappings.addMapping({
155-
source: mappings._hashedFilename,
156-
generated: {
157-
line: lineOfRVOptionsInOutput,
158-
column: 0,
159-
},
160-
original: {
161-
line: lineOfExpDefaultInScript,
162-
column: 0,
163-
},
164-
})
165-
lineOfRVOptionsInOutput--
166-
lineOfExpDefaultInScript--
167-
}
143+
generatedCodeCursor--
168144
}
169145

170146
// add render funtion
@@ -296,44 +272,45 @@ function traverse(ast, nodes = []) {
296272
}
297273
}
298274

299-
function getLineOfExport(content) {
300-
var lineOfExport = 0
301-
content.split(newLine).some((line, index) => {
302-
if (line.includes('export')) {
303-
lineOfExport = index
275+
function getFirstLineOfText(content, text) {
276+
var firstLine = 0
277+
let splittedContent = content.split(newLine)
278+
if (!content.includes(text)) {
279+
return splittedContent.length
280+
}
281+
splittedContent.some((line, index) => {
282+
if (line.includes(text)) {
283+
firstLine = index
304284
return true
305285
} else {
306286
return false
307287
}
308288
})
309-
return lineOfExport
289+
return firstLine
310290
}
311291

312-
function getLineOfFirstImport(content) {
313-
var lineOfImport = 0
314-
if (!content.includes('import')) {
315-
return -1
292+
function getFirstNonLineOfText(content, text) {
293+
var firstNonCommentedLine = 0
294+
let splittedContent = content.split(newLine)
295+
296+
if (!content.includes(text)) {
297+
return 0
316298
}
317-
content.split(newLine).some((line, index) => {
318-
if (line.includes('import')) {
319-
lineOfImport = index
320-
return true
321-
} else {
299+
splittedContent.some((line, index) => {
300+
if (line.includes(text)) {
322301
return false
302+
} else {
303+
firstNonCommentedLine = index
304+
return true
323305
}
324306
})
325-
return lineOfImport
307+
return firstNonCommentedLine
326308
}
327309

328-
function getLineOfRVOptions(content) {
329-
var lineOfRVOptions = 0
330-
content.split(newLine).some((line, index) => {
331-
if (line.includes('const __react__vue__options')) {
332-
lineOfRVOptions = index
333-
return true
334-
} else {
335-
return false
336-
}
337-
})
338-
return lineOfRVOptions
310+
function getFirstValidLine(content) {
311+
let firstImport = getFirstLineOfText(content, 'import')
312+
let firstExport = getFirstLineOfText(content, 'export')
313+
let firstNonCommentedLine = getFirstNonLineOfText(content, '//')
314+
315+
return Math.min(firstImport, firstExport, firstNonCommentedLine)
339316
}

0 commit comments

Comments
 (0)