-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from protofire/add-prettier-support
Add prettier support
- Loading branch information
Showing
16 changed files
with
320 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = { | ||
getLocFromIndex(text, index) { | ||
let line = 1 | ||
let column = 0 | ||
let i = 0 | ||
while (i < index) { | ||
if (text[i] === '\n') { | ||
line++ | ||
column = 0 | ||
} else { | ||
column++ | ||
} | ||
i++ | ||
} | ||
|
||
return { line, column } | ||
} | ||
} |
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,6 @@ | ||
module.exports = Object.freeze({ | ||
RULES: { | ||
PRETTIER: 'prettier/prettier', | ||
QUOTES: 'quotes' | ||
} | ||
}) |
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
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,20 @@ | ||
const Prettier = require('./prettier') | ||
const QuotesChecker = require('./quotes') | ||
const { RULES } = require('../../constants') | ||
|
||
module.exports = function checkers(reporter, config, inputSrc, fileName) { | ||
const { rules, validateDisabledByDefault } = config | ||
|
||
const rulesToCheck = [ | ||
{ | ||
rule: RULES.QUOTES, | ||
instance: new QuotesChecker(reporter, config) | ||
}, | ||
{ | ||
rule: RULES.PRETTIER, | ||
instance: new Prettier(reporter, config, inputSrc, fileName) | ||
} | ||
] | ||
|
||
return validateDisabledByDefault(rules, rulesToCheck) | ||
} |
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,71 @@ | ||
const { showInvisibles, generateDifferences } = require('prettier-linter-helpers') | ||
const { getLocFromIndex } = require('../../common/utils') | ||
const BaseChecker = require('../base-checker') | ||
const { RULES } = require('../../constants') | ||
|
||
const { INSERT, DELETE, REPLACE } = generateDifferences | ||
|
||
class Prettier extends BaseChecker { | ||
constructor(reporter, config, inputSrc, fileName) { | ||
super(reporter) | ||
this.inputSrc = inputSrc | ||
this.fileName = fileName | ||
} | ||
|
||
enterSourceUnit(ctx) { | ||
try { | ||
// Check for optional dependencies with the try catch | ||
// Prettier is expensive to load, so only load it if needed. | ||
const prettier = require('prettier') | ||
|
||
const formatted = prettier.format(this.inputSrc, { | ||
filepath: this.fileName, | ||
plugins: ['prettier-plugin-solidity'] | ||
}) | ||
|
||
const differences = generateDifferences(this.inputSrc, formatted) | ||
|
||
differences.forEach(difference => { | ||
let loc = null | ||
switch (difference.operation) { | ||
case INSERT: | ||
loc = getLocFromIndex(this.inputSrc, difference.offset) | ||
this.errorAt( | ||
loc.line, | ||
loc.column, | ||
RULES.PRETTIER, | ||
`Insert ${showInvisibles(difference.insertText)}` | ||
) | ||
break | ||
case DELETE: | ||
loc = getLocFromIndex(this.inputSrc, difference.offset) | ||
this.errorAt( | ||
loc.line, | ||
loc.column, | ||
RULES.PRETTIER, | ||
`Delete ${showInvisibles(difference.deleteText)}` | ||
) | ||
break | ||
case REPLACE: | ||
loc = getLocFromIndex(this.inputSrc, difference.offset) | ||
this.errorAt( | ||
loc.line, | ||
loc.column, | ||
RULES.PRETTIER, | ||
`Replace ${showInvisibles(difference.deleteText)} with ${showInvisibles( | ||
difference.insertText | ||
)}` | ||
) | ||
break | ||
default: | ||
// A switch must have a default | ||
} | ||
}) | ||
} catch (e) { | ||
console.error(e) | ||
process.exit(1) | ||
} | ||
} | ||
} | ||
|
||
module.exports = Prettier |
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
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
Oops, something went wrong.