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
);
function reportInsert(context, offset, text) {
const pos = context.getSourceCode().getLocFromIndex(offset);
const range = [offset, offset];
context.report({
message: 'Insert `{{ code }}`',
data: { code: showInvisibles(text) },
loc: { start: pos, end: pos },
fix(fixer) {
return fixer.insertTextAfterRange(range, text);
}
});
}
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(
difference.deleteText
)}\``;
break;
case REPLACE:
message = `Replace \`${showInvisibles(
difference.deleteText
)}\` with \`${difference.insertText}\``;
break;
}
this.log({ message, line, column, source });
});
}
function reportDelete(context, offset, text) {
const start = context.getSourceCode().getLocFromIndex(offset);
const end = context.getSourceCode().getLocFromIndex(offset + text.length);
const range = [offset, offset + text.length];
context.report({
message: 'Delete `{{ code }}`',
data: { code: showInvisibles(text) },
loc: { start, end },
fix(fixer) {
return fixer.removeRange(range);
}
});
}
insert: (code) => `Insert "${showInvisibles(code)}"`,
delete: (code) => `Delete "${showInvisibles(code)}"`,
function reportReplace(context, offset, deleteText, insertText) {
const start = context.getSourceCode().getLocFromIndex(offset);
const end = context
.getSourceCode()
.getLocFromIndex(offset + deleteText.length);
const range = [offset, offset + deleteText.length];
context.report({
message: 'Replace `{{ deleteCode }}` with `{{ insertCode }}`',
data: {
deleteCode: showInvisibles(deleteText),
insertCode: showInvisibles(insertText)
},
loc: { start, end },
fix(fixer) {
return fixer.replaceTextRange(range, insertText);
}
});
}
function reportDelete(context, offset, text) {
const start = context.getSourceCode().getLocFromIndex(offset)
const end = context.getSourceCode().getLocFromIndex(offset + text.length)
const range = [offset, offset + text.length]
context.report({
message: 'Delete `{{ code }}`',
data: { code: showInvisibles(text) },
loc: { start, end },
fix(fixer) {
return fixer.removeRange(range)
},
})
}