diff --git a/lib/array_pipe.js b/lib/array_compose.js similarity index 100% rename from lib/array_pipe.js rename to lib/array_compose.js diff --git a/rules/assignments.js b/rules/assignments.js index 26ec53b..41c42ce 100644 --- a/rules/assignments.js +++ b/rules/assignments.js @@ -1,7 +1,7 @@ const matchMultilineString = require('../lib/match_multiline_string.js') const ignoreStrings = require('../lib/ignore_strings.js') const arrayForEach = require('../lib/array_for_each.js') -const arrayPipe = require('../lib/array_pipe.js') +const arrayCompose = require('../lib/array_compose.js') module.exports = ({ lines, filename }) => { const indentation = [ @@ -92,7 +92,7 @@ module.exports = ({ lines, filename }) => { let assignmentBadSpacing = false - arrayPipe([ + arrayCompose([ { line: lines[index], transform: line => { @@ -169,7 +169,7 @@ module.exports = ({ lines, filename }) => { array: assignmentsReplacements, iteration: element => { const [assignment, replacement] = element - lines[index] = arrayPipe([ + lines[index] = arrayCompose([ { line: lines[index], transform: line => { diff --git a/rules/closures.js b/rules/closures.js index b0c3217..0744070 100644 --- a/rules/closures.js +++ b/rules/closures.js @@ -77,77 +77,88 @@ module.exports = ({ lines, filename }) => { if (!closureDeclaration.line) { const matchedClosureDeclaration = matchClosures({ indentationLevel, lines, index, filename }) - if (matchedClosureDeclaration) { - if (matchedClosureDeclaration.error) { + if (!matchedClosureDeclaration) { + return true + } + + if (matchedClosureDeclaration.error) { + return false + } + + const linesBefore = [ + lines[index - 1], + lines[index - 2] + ] + + const firstLine = linesBefore[0] === undefined + if (!firstLine) { + if ((!linesBefore[0].trimStart().startsWith('}') && linesBefore[0] !== '') || linesBefore[1] === '') { + console.log(`${filename} ${index + 1}`, '- There must be one blank line before each closure') return false } + } - const linesBefore = [ - lines[index - 1], - lines[index - 2] - ] + closureDeclaration = matchedClosureDeclaration - if (linesBefore[0] !== undefined) { - if ((!linesBefore[0].trimStart().startsWith('}') && linesBefore[0] !== '') || linesBefore[1] === '') { - console.log(`${filename} ${index + 1}`, '- There must be one blank line before each closure') - return false - } + return true + } + + if (lines[index] === '') { + const lastLine = lines.length - 1 === index + const topLevelClosure = indentationLevel === '' + + if (topLevelClosure) { + if (lastLine) { + lines[closureDeclaration.index] = `${closureDeclaration.line} {` + lines[index] = '}' + lines[index + 1] = '' } - closureDeclaration = matchedClosureDeclaration return true } - } else { - if (lines[index] === '') { - const lastLine = lines.length - 1 === index - const topLevelClosure = indentationLevel === '' - - if (topLevelClosure) { - if (lastLine) { - lines[closureDeclaration.index] = `${closureDeclaration.line} {` - lines[index] = '}' - lines[index + 1] = '' - } - } else { - const remainingExpressionsInTheScope = !lastLine && lines[index + 1].startsWith(`${indentationLevel} `) - if (remainingExpressionsInTheScope) { - return true - } - lines[closureDeclaration.index] = `${closureDeclaration.line} {` - lines[index] = `${indentationLevel}}` - closureDeclaration = {} - } - } else { - const topLevelDeclaration = !lines[index].startsWith(' ') - if (topLevelDeclaration) { - const linesBefore = [ - lines[index - 1], - lines[index - 2], - lines[index - 3] - ] - - const twoBlankLines = linesBefore[0] === '' && linesBefore[1] === '' && linesBefore[2] !== '' - - if (!twoBlankLines) { - console.log(`${filename} ${index + 1}`, '- There must be two blank lines before each top level closure') - return false - } + const remainingExpressionsInTheScope = !lastLine && lines[index + 1].startsWith(`${indentationLevel} `) + if (remainingExpressionsInTheScope) { + return true + } - lines[closureDeclaration.index] = `${closureDeclaration.line} {` - lines[index - 2] = '}' - closureDeclaration = {} + lines[closureDeclaration.index] = `${closureDeclaration.line} {` + lines[index] = `${indentationLevel}}` + closureDeclaration = {} + + return true + } - const matchedClosureDeclaration = matchClosures({ indentationLevel, lines, index, filename }) + const topLevelDeclaration = !lines[index].startsWith(' ') + if (topLevelDeclaration) { + const linesBefore = [ + lines[index - 1], + lines[index - 2], + lines[index - 3] + ] - if (matchedClosureDeclaration) { - if (matchedClosureDeclaration.error) { - return false - } - closureDeclaration = matchedClosureDeclaration - } - } + const twoBlankLines = linesBefore[0] === '' && linesBefore[1] === '' && linesBefore[2] !== '' + + if (!twoBlankLines) { + console.log(`${filename} ${index + 1}`, '- There must be two blank lines before each top level closure') + return false + } + + lines[closureDeclaration.index] = `${closureDeclaration.line} {` + lines[index - 2] = '}' + closureDeclaration = {} + + const matchedClosureDeclaration = matchClosures({ indentationLevel, lines, index, filename }) + + if (!matchedClosureDeclaration) { + return true } + + if (matchedClosureDeclaration.error) { + return false + } + + closureDeclaration = matchedClosureDeclaration } return true diff --git a/rules/comparison_operators.js b/rules/comparison_operators.js index 4942cd7..ada2416 100644 --- a/rules/comparison_operators.js +++ b/rules/comparison_operators.js @@ -1,6 +1,6 @@ const matchMultilineString = require('../lib/match_multiline_string.js') const ignoreStrings = require('../lib/ignore_strings.js') -const arrayPipe = require('../lib/array_pipe.js') +const arrayCompose = require('../lib/array_compose.js') const arrayForEach = require('../lib/array_for_each.js') module.exports = ({ lines, filename }) => { @@ -24,11 +24,11 @@ module.exports = ({ lines, filename }) => { 'is_greater_than_or_equal_to ': '>= ' } - arrayPipe([ + arrayCompose([ { array: Object.keys(operatorsReplacements), iteration: operator => { - lines[index] = arrayPipe([ + lines[index] = arrayCompose([ { line: lines[index], transform: line => line.replaceAll(operator, operatorsReplacements[operator]) diff --git a/rules/strings.js b/rules/strings.js index 7a79632..fdedf7a 100644 --- a/rules/strings.js +++ b/rules/strings.js @@ -1,5 +1,5 @@ const matchMultilineString = require('../lib/match_multiline_string.js') -const arrayPipe = require('../lib/array_pipe.js') +const arrayCompose = require('../lib/array_compose.js') const ignoreStrings = require('../lib/ignore_strings.js') module.exports = ({ lines, filename }) => { @@ -16,7 +16,7 @@ module.exports = ({ lines, filename }) => { let badStringAssignment = false - arrayPipe([ + arrayCompose([ { line: lines[index], transform: line => {