Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lovedder1995 committed Dec 23, 2023
1 parent e0287e8 commit 7a2c1b9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const indentation = ({ file, filename }) => {
require('php-bundler/rules/indentation.js')({ lines, filename })
require('./rules/comments.js')({ lines, filename })
require('./rules/end_of_file.js')({ lines, filename })
require('./rules/expressions.js')({ lines, filename })
require('./rules/closures.js')({ lines, filename })
require('./rules/expressions.js')({ lines, filename })
require('./rules/arrays.js')({ lines, filename })

return lines.join('\n')
}
Expand All @@ -25,7 +26,13 @@ const resoveModule = ({ file, moduleFilename }) => {
if (!moduleFilename) return file
moduleFilename = moduleFilename[0]

let moduleFile = readFileSync(moduleFilename, 'utf-8')
let moduleFile

try {
moduleFile = readFileSync(`node_modules/${moduleFilename}/index.php`, 'utf-8')
} catch (error) {
moduleFile = readFileSync(moduleFilename, 'utf-8')
}

if (moduleFile.startsWith('<?php')) {
moduleFile = moduleFile.replace('<?php\n', '')
Expand All @@ -35,6 +42,11 @@ const resoveModule = ({ file, moduleFilename }) => {

moduleFile = closure(moduleFile)

moduleFile = moduleFile.split('\n').map(line => {
if (line === '') return line
return ` ${line}`
}).join('\n')

const bundle = file.replace(aModuleExpression, moduleFile)

const anotherModuleFilename = bundle.match(aModuleFilename)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "php-bundler",
"version": "2.0.3",
"version": "2.0.4",
"main": "index.js",
"repository": {
"type": "git",
Expand Down
40 changes: 40 additions & 0 deletions rules/arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = ({ lines, filename }) => {
const indentation = [
' ',
' ',
' ',
' ',
''
]

indentation.every(indentationLevel => {
lines.reduce((arrayDeclaration, line, index) => {
const currentIndentationLevel = line.startsWith(indentationLevel) && !line.startsWith(`${indentationLevel} `)

if (!arrayDeclaration.line && currentIndentationLevel && (line.endsWith('[') || line.endsWith('('))) {
return { line, index }
}

if (currentIndentationLevel && arrayDeclaration.line) {
return {}
}

const arrayItemsScope = line.startsWith(`${indentationLevel} `) && !line.startsWith(`${indentationLevel} `)

if (arrayItemsScope && arrayDeclaration.line) {
if (['{', '(', '['].every(lineEnd => !line.endsWith(lineEnd))) {
if ([']', ')'].every(nextLineStart => !lines[index + 1].trimStart().startsWith(nextLineStart))) {
if (line.endsWith(';')) {
lines[index] = line.slice(0, -1)
}
lines[index] = `${lines[index]},`
}
}
}

return arrayDeclaration
}, {})

return true
})
}
17 changes: 3 additions & 14 deletions rules/closures.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ const matchClosures = ({ indentationLevel, line, index, filename }) => {
}
}

const missingClosingParentheses = (line) => {
const openingParenthesis = [...line.matchAll(/\(/g)].length
const closingParenthesis = [...line.matchAll(/\)/g)].length

return openingParenthesis - closingParenthesis
}

module.exports = ({ lines, filename }) => {
const indentation = [
' ',
Expand All @@ -38,7 +31,7 @@ module.exports = ({ lines, filename }) => {
if (topLevelClosure) {
if (lastLine) {
lines[closureDeclaration.index] = `${closureDeclaration.line} {`
lines[index] = '};\n'
lines[index] = '}\n'
}
} else {
const remainingExpressionsInTheScope = !lastLine && lines[index + 1].startsWith(`${indentationLevel} `)
Expand All @@ -47,11 +40,7 @@ module.exports = ({ lines, filename }) => {
}

lines[closureDeclaration.index] = `${closureDeclaration.line} {`
if (missingClosingParentheses(lines[closureDeclaration.index])) {
lines[index - 1] = `${lines[index - 1]}});`
} else {
lines[index - 1] = `${lines[index - 1]}};`
}
lines[index] = `${indentationLevel}}`
closureDeclaration = {}
}
} else {
Expand All @@ -70,7 +59,7 @@ module.exports = ({ lines, filename }) => {
return {}
}
lines[closureDeclaration.index] = `${closureDeclaration.line} {`
lines[index - 2] = '};'
lines[index - 2] = '}'
closureDeclaration = {}

const matchedClosureDeclaration = matchClosures({ indentationLevel, line, index, filename })
Expand Down
2 changes: 1 addition & 1 deletion rules/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = ({ lines, filename }) => {

if (line.trimStart() !== '' && !line.trimStart().startsWith('#')) {
if (!line.includes('function (') && !line.includes('if (')) {
if (['{', '(', '[', ','].every(lineEnd => !line.endsWith(lineEnd))) {
if (['{', '(', '['].every(lineEnd => !line.endsWith(lineEnd))) {
if ([']', ')'].every(nextLineStart => !lines[index + 1].trimStart().startsWith(nextLineStart))) {
lines[index] = `${line};`
}
Expand Down

0 comments on commit 7a2c1b9

Please sign in to comment.