-
-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixable rules #82
Comments
That would be really awesome! And it’s a pretty smart idea, to do it like that. Not entirely sure about al the methods though, why not just start/end position, and new text, so more like: function finalNewline(ast, file) {
var contents = file.toString();
var last = contents.length - 1;
var message;
if (last > -1 && contents.charAt(last) !== '\n') {
message = file.warn('Missing newline character at end of file');
message.fix = [last, last, '\n']; // start offset, end offset, replacement text
}
} From what I gather, ESLint/xo allows running with > remark -u lint --fix-promt Yields: - asdasdasd
+ asdasdasd\n
Missing newline character at end of file
Want to apply this fix? (yN) |
The ESLint fixer works on AST, not offsets. This one should do the same.
It tries to apply as many fixes as possible:
No. The goal should be to just fix everything without user interaction. I guess it could prompt for ambiguous cases. I would strongly recommend digging into more deeply how ESLint does it. Discussions in the issue tracker, etc. They've spent a lot of time on it. |
Not really, the
That works really well for some of the rules, like inserting some white-space or a few characters, but not so much for bigger changes (as ESLint mentions in there docs), as they often interfere with other changes. Plus it’d be cool for |
I do not use —fix because of the existence of prettier. |
is there a way to use the eslint ecosystem:
|
Maybe, can you clarify what you mean?
Again, maybe. |
Another approach for autofixing, using an attacher with linting context, and a transformer to update the document, similar to Stylelint's approach. |
@muescha Actually eslint-mdx is doing this. |
@JounQin i did not found any docs about it... can you point me to it? |
@muescha I've pointed the doc link. https://github.com/mdx-js/eslint-mdx#mdxremark
|
🤔 i did not expected it behind this sentence ... |
Would be amazing to have ability to determine that rule should make some modifications to module.exports.report = () => 'Avoid debugger statement';
module.exports.fix = (path, {options}) => {
path.remove();
};
module.exports.traverse = ({push}) => ({
'debugger'(path) {
push(path);
},
}); So when you pass
import {lintRule} from 'unified-lint-rule';
export const removeDependenciesStatusBadge = lintRule('remark-lint:remove-dependencies-status-badge', (tree, file) => {
const children = tree.children.filter(isDependencyStatus(file));
tree.children = children;
const [heading] = children;
if (heading.type !== 'heading')
return;
const headingChildren = heading.children.filter(isDependencyLink(file));
tree.children[0].children = headingChildren;
});
const isDependencyStatus = (file) => (child) => {
if (child.type !== 'definition')
return true;
if (child.label === 'DependencyStatusURL') {
file.message('Remove DependencyStatusURL', child);
return false;
}
if (child.label === 'DependencyStatusIMGURL') {
file.message('Remove DependencyStatusIMGURL', child);
return false;
}
return true;
};
const isDependencyLink = (file) => (child) => {
if (child.type !== 'linkReference')
return true;
if (child.children[0].label === 'DependencyStatusIMGURL') {
file.message('Remove reference to DependencyStatusIMGURL', child);
return false;
}
return true;
}; And import {lintRule} from 'unified-lint-rule';
export const removeTrailingWhitespacesFromHeading = lintRule('remark-lint:remove-trailing-whitespaces-from-heading', (tree, file, options) => {
console.log(options);
const [heading] = tree.children;
if (heading.type !== 'heading')
return;
const latest = heading.children[heading.children.length - 1];
if (latest.type === 'text' && / $/.test(latest.value)) {
latest.value = latest.value.slice(0, -1);
report(file, latest);
}
tree.children[0].children = heading.children;
});
function report(file, child) {
file.message('Avoid trailing whitespaces', child);
} I'm using Actually there is a possibility to pass @wooorm could you please suggest me a way to handle this case and not modify |
Here is what I came up with. Create one import {lintRule} from 'unified-lint-rule';
import removeDependenciesStatusBadge from './remove-dependencies-status-badge.mjs';
import removeTrailingWhitespacesFromHeading from './remove-trailing-whitespaces-from-heading.mjs';
const plugins = [
removeDependenciesStatusBadge,
removeTrailingWhitespacesFromHeading,
];
const maybeEmptyArray = (a) => isArray(a) ? a : [];
const {isArray} = Array;
export const run = lintRule('remark-lint:run', (tree, file, options) => {
for (const {fix, traverse, report, name} of plugins) {
const nodes = traverse(tree);
for (const node of maybeEmptyArray(nodes)) {
if (options.fix) {
fix(node);
continue;
}
const message = report(node);
file.message(`${name}: ${message}`, node);
}
}
});
export const rules = {
plugins,
}; And here is how my rules looks like:
const report = () => 'Remove dependencies status badge';
export default {
name: 'remove-dependencies-status-badge',
traverse,
fix,
report,
};
function fix(nodes, tree) {
const children = tree.children.filter(isDependencyStatus(nodes));
tree.children = children;
const [heading] = children;
if (heading.type !== 'heading')
return;
const headingChildren = heading.children.filter(isDependencyLink);
tree.children[0].children = headingChildren;
}
function traverse(tree) {
const nodes = [];
tree.children.filter(isDependencyStatus(nodes));
const [first] = nodes;
return [first];
}
const isDependencyStatus = (nodes) => (child) => {
if (child.type !== 'definition')
return true;
if (child.label === 'DependencyStatusURL') {
nodes.push(child);
return false;
}
if (child.label === 'DependencyStatusIMGURL') {
nodes.push(child);
return false;
}
return true;
};
const isDependencyLink = (child) => {
if (child.type !== 'linkReference')
return true;
if (child.children[0].label === 'DependencyStatusIMGURL')
return false;
return true;
}; And const report = () => 'Avoid trailing whitespaces';
const fix = (heading, tree) => {
const latest = heading.children[heading.children.length - 1];
if (latest.type === 'text' && / $/.test(latest.value))
latest.value = latest.value.slice(0, -1);
tree.children[0].children = heading.children;
};
const traverse = (tree) => {
const [heading] = tree.children;
if (heading.type !== 'heading')
return;
const latest = heading.children[heading.children.length - 1];
if (latest.type === 'text' && / $/.test(latest.value))
return [latest];
};
export default {
name: 'remove-trailing-whitespaces-from-heading',
fix,
traverse,
report,
}; And everything works good. But! This format is incompatible with format used by By the way similar format looks amazing for |
I'm aware I could use Remark to modify the Markdown, but I'm looking for a way to add autofixing to remark-lint rules, similar to ESLint.
See how ESLint solves it: http://eslint.org/docs/developer-guide/working-with-rules#applying-fixes
Could be a
--fix
flag toremark-lint
CLI.The text was updated successfully, but these errors were encountered: