-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of strict indentation
- Loading branch information
1 parent
69bf34e
commit ca3528e
Showing
9 changed files
with
197 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = ({ lines, filename }) => { | ||
lines.every((line, index) => { | ||
if (line.includes('#')) { | ||
if (!line.trimStart().startsWith('#')) { | ||
console.log(`${filename} ${index + 1}`, '- Comments should be on their own line') | ||
return false | ||
} | ||
|
||
if (line.includes('# ') || (line.includes('#') && !line.includes('# '))) { | ||
console.log(`${filename} ${index + 1}`, '- There must be one space between the octothrope and the comment.') | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = ({ lines, filename }) => { | ||
const noBlankLineEnd = lines.slice(-1)[0] !== '' | ||
if (noBlankLineEnd) { | ||
console.log(filename, '- The file must end with one blank line') | ||
} | ||
|
||
const twoBlankLinesEnd = lines.slice(-1)[0] === '' && lines.slice(-2)[0] === '' | ||
if (twoBlankLinesEnd) { | ||
console.log(filename, '- The file must end with one blank line') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = ({ lines, filename }) => { | ||
lines.every((line, index) => { | ||
if (line.endsWith(';')) { | ||
console.log(`${filename} ${index + 1}`, '- Lines should not end with a semicolon') | ||
return false | ||
} | ||
|
||
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};` | ||
} | ||
} | ||
} | ||
|
||
return true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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 { | ||
return { index, line } | ||
} | ||
} | ||
} | ||
|
||
const missingClosingParentheses = (line) => { | ||
const openingParenthesis = [...line.matchAll(/\(/g)].length | ||
const closingParenthesis = [...line.matchAll(/\)/g)].length | ||
|
||
return openingParenthesis - closingParenthesis | ||
} | ||
|
||
module.exports = ({ lines, filename }) => { | ||
const indentation = [ | ||
' ', | ||
' ', | ||
' ', | ||
' ', | ||
'' | ||
] | ||
|
||
indentation.every(indentationLevel => { | ||
lines.reduce((functionDeclaration, line, index) => { | ||
if (!functionDeclaration.line) { | ||
const matchedFunctionDeclaration = matchFunctionDeclaration({ indentationLevel, line, index, filename }) | ||
|
||
if (matchedFunctionDeclaration) { | ||
return matchedFunctionDeclaration | ||
} | ||
} else { | ||
if (line === '') { | ||
const lastLine = lines.length - 1 === index | ||
const topLevelFunction = indentationLevel === '' | ||
|
||
if (topLevelFunction) { | ||
if (lastLine) { | ||
lines[functionDeclaration.index] = `${functionDeclaration.line} {` | ||
lines[index] = '};\n' | ||
} | ||
} else { | ||
const remainingExpressionsInTheScope = !lastLine && lines[index + 1].startsWith(`${indentationLevel} `) | ||
if (remainingExpressionsInTheScope) { | ||
return functionDeclaration | ||
} | ||
|
||
lines[functionDeclaration.index] = `${functionDeclaration.line} {` | ||
if (missingClosingParentheses(lines[functionDeclaration.index])) { | ||
lines[index - 1] = `${lines[index - 1]}});` | ||
} else { | ||
lines[index - 1] = `${lines[index - 1]}};` | ||
} | ||
functionDeclaration = {} | ||
} | ||
} else { | ||
const topDeclaration = !line.startsWith(' ') | ||
if (topDeclaration) { | ||
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 between each top level function') | ||
return {} | ||
} | ||
lines[functionDeclaration.index] = `${functionDeclaration.line} {` | ||
lines[index - 2] = '};' | ||
functionDeclaration = {} | ||
|
||
const matchedFunctionDeclaration = matchFunctionDeclaration({ indentationLevel, line, index, filename }) | ||
|
||
if (matchedFunctionDeclaration) { | ||
return matchedFunctionDeclaration | ||
} | ||
} | ||
} | ||
} | ||
|
||
return functionDeclaration | ||
}, {}) | ||
|
||
return true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module.exports = ({ lines, filename }) => { | ||
const indentation = [ | ||
0, | ||
4, | ||
8, | ||
12, | ||
16 | ||
] | ||
|
||
lines.every((line, index) => { | ||
const lineIndentation = line.length - line.trimStart().length | ||
if (lineIndentation > 16) { | ||
console.log(`${filename} ${index + 1}`, '- Too much nesting, refactoring is needed.') | ||
return false | ||
} | ||
|
||
if (!indentation.includes(lineIndentation)) { | ||
console.log(`${filename} ${index + 1}`, '- Bad indentation') | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = ({ lines, filename }) => { | ||
lines.every((line, index) => { | ||
if (line.length > line.trimEnd().length) { | ||
console.log(`${filename} ${index + 1}`, '- Lines must not end with spaces') | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
} |