Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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, `Insert ${showInvisibles(difference.insertText)}`)
break
case DELETE:
loc = getLocFromIndex(this.inputSrc, difference.offset)
this.errorAt(loc.line, loc.column, `Delete ${showInvisibles(difference.deleteText)}`)
break
case REPLACE:
loc = getLocFromIndex(this.inputSrc, difference.offset)
this.errorAt(
loc.line,
message,
node: root,
index: getIndexFromLoc(source, err.loc.start),
});
return;
}
// Everything is the same. Nothing to do here;
if (source === prettierSource) {
return;
}
// Otherwise let's generate some differences
const differences = generateDifferences(source, prettierSource);
const report = (message, index) => {
return stylelint.utils.report({
ruleName,
result,
message,
node: root,
index,
});
};
if (context.fix) {
// Fixes must be processed in reverse order, as an early delete shall
// change the modification offsets for anything after it
const rawData = differences.reverse().reduce((rawData, difference) => {
let insertText = '';
// ESLint shows those pieces of information elsewhere already so
// remove them from the message
if (err.codeFrame) {
message = message.replace(`\n${err.codeFrame}`, '')
}
if (err.loc) {
message = message.replace(/ \(\d+:\d+\)$/, '')
}
context.report({ message, loc: err.loc })
return
}
if (source !== prettierSource) {
const differences = generateDifferences(source, prettierSource)
differences.forEach(difference => {
switch (difference.operation) {
case INSERT:
reportInsert(
context,
difference.offset + offset,
difference.insertText
)
break
case DELETE:
reportDelete(
context,
difference.offset + offset,
difference.deleteText
)
// ESLint shows those pieces of information elsewhere already so
// remove them from the message
if (err.codeFrame) {
message = message.replace(`\n${err.codeFrame}`, '');
}
if (err.loc) {
message = message.replace(/ \(\d+:\d+\)$/, '');
}
context.report({ message, loc: err.loc });
return;
}
if (source !== prettierSource) {
const differences = generateDifferences(source, prettierSource);
differences.forEach(difference => {
switch (difference.operation) {
case INSERT:
reportInsert(
context,
difference.offset,
difference.insertText
);
break;
case DELETE:
reportDelete(
context,
difference.offset,
difference.deleteText
);
if (err.loc) {
message = message.replace(/ \(\d+:\d+\)$/, "");
}
this.log({
message,
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source
});
return;
}
if (source !== prettierSource) {
const differences = generateDifferences(source, prettierSource);
differences.forEach(difference => {
let message = "";
let { line, column } = getLocFromIndex(
difference.offset,
this.source
);
switch (difference.operation) {
case INSERT:
message = `Insert \`${showInvisibles(
difference.insertText
)}\``;
break;
case DELETE:
message = `Delete \`${showInvisibles(