Skip to content

Commit

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

return lines.join('\n')
}
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.2",
"version": "2.0.3",
"main": "index.js",
"repository": {
"type": "git",
Expand Down
46 changes: 22 additions & 24 deletions rules/functions.js → rules/closures.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const matchFunctionDeclaration = ({ indentationLevel, line, index, filename }) => {
if (line.startsWith(indentationLevel) && line.endsWith(')') && line.includes('function')) {
if (!line.includes('function (')) {
console.log(`${filename} ${index + 1}`, '- There must be a space between the function and the parenthesis')
} else {
const matchClosures = ({ indentationLevel, line, index, filename }) => {
if (line.includes('function (') || line.includes('if (') || line.trim() === 'else') {
if (line.startsWith(indentationLevel) && line.endsWith(')')) {
return { index, line }
}
}
Expand All @@ -25,36 +23,36 @@ module.exports = ({ lines, filename }) => {
]

indentation.every(indentationLevel => {
lines.reduce((functionDeclaration, line, index) => {
if (!functionDeclaration.line) {
const matchedFunctionDeclaration = matchFunctionDeclaration({ indentationLevel, line, index, filename })
lines.reduce((closureDeclaration, line, index) => {
if (!closureDeclaration.line) {
const matchedClosureDeclaration = matchClosures({ indentationLevel, line, index, filename })

if (matchedFunctionDeclaration) {
return matchedFunctionDeclaration
if (matchedClosureDeclaration) {
return matchedClosureDeclaration
}
} else {
if (line === '') {
const lastLine = lines.length - 1 === index
const topLevelFunction = indentationLevel === ''
const topLevelClosure = indentationLevel === ''

if (topLevelFunction) {
if (topLevelClosure) {
if (lastLine) {
lines[functionDeclaration.index] = `${functionDeclaration.line} {`
lines[closureDeclaration.index] = `${closureDeclaration.line} {`
lines[index] = '};\n'
}
} else {
const remainingExpressionsInTheScope = !lastLine && lines[index + 1].startsWith(`${indentationLevel} `)
if (remainingExpressionsInTheScope) {
return functionDeclaration
return closureDeclaration
}

lines[functionDeclaration.index] = `${functionDeclaration.line} {`
if (missingClosingParentheses(lines[functionDeclaration.index])) {
lines[closureDeclaration.index] = `${closureDeclaration.line} {`
if (missingClosingParentheses(lines[closureDeclaration.index])) {
lines[index - 1] = `${lines[index - 1]}});`
} else {
lines[index - 1] = `${lines[index - 1]}};`
}
functionDeclaration = {}
closureDeclaration = {}
}
} else {
const topDeclaration = !line.startsWith(' ')
Expand All @@ -68,23 +66,23 @@ module.exports = ({ lines, filename }) => {
const twoBlankLines = linesBefore[0] === '' && linesBefore[1] === '' && linesBefore[2] !== ''

if (!twoBlankLines) {
console.log(`${filename} ${index + 1}`, '- There must be two blank lines between each top level function')
console.log(`${filename} ${index + 1}`, '- There must be two blank lines between each top level closure')
return {}
}
lines[functionDeclaration.index] = `${functionDeclaration.line} {`
lines[closureDeclaration.index] = `${closureDeclaration.line} {`
lines[index - 2] = '};'
functionDeclaration = {}
closureDeclaration = {}

const matchedFunctionDeclaration = matchFunctionDeclaration({ indentationLevel, line, index, filename })
const matchedClosureDeclaration = matchClosures({ indentationLevel, line, index, filename })

if (matchedFunctionDeclaration) {
return matchedFunctionDeclaration
if (matchedClosureDeclaration) {
return matchedClosureDeclaration
}
}
}
}

return functionDeclaration
return closureDeclaration
}, {})

return true
Expand Down
8 changes: 5 additions & 3 deletions rules/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module.exports = ({ lines, filename }) => {
}

if (line.trimStart() !== '' && !line.trimStart().startsWith('#')) {
if (!line.includes('function') && ['{', '(', '[', ','].every(lineEnd => !line.endsWith(lineEnd))) {
if ([']', ')'].every(nextLineStart => !lines[index + 1].trimStart().startsWith(nextLineStart))) {
lines[index] = `${line};`
if (!line.includes('function (') && !line.includes('if (')) {
if (['{', '(', '[', ','].every(lineEnd => !line.endsWith(lineEnd))) {
if ([']', ')'].every(nextLineStart => !lines[index + 1].trimStart().startsWith(nextLineStart))) {
lines[index] = `${line};`
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions rules/spaces.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = ({ lines, filename }) => {
lines.every((line, index) => {
if (line.trimStart().includes(' ')) {
console.log(`${filename} ${index + 1}`, '- There should not be two spaces in a row')
return false
}

if (line.length > line.trimEnd().length) {
console.log(`${filename} ${index + 1}`, '- Lines must not end with spaces')
return false
Expand Down

0 comments on commit e0287e8

Please sign in to comment.