Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions crates/oxc_formatter/src/formatter/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,17 @@ impl<'a> Comments<'a> {
&[]
}

/// Checks if the node has a suppression comment (prettier-ignore).
/// Checks if the node has a suppression comment.
pub fn is_suppressed(&self, start: u32) -> bool {
self.comments_before(start).iter().any(|comment| self.is_suppression_comment(comment))
}

/// Checks if a comment is a suppression comment (`oxfmt-ignore`).
///
/// `prettier-ignore` is also supported for compatibility.
pub fn is_suppression_comment(&self, comment: &Comment) -> bool {
// TODO: Consider using `oxfmt-ignore` instead of `prettier-ignore`
self.source_text.text_for(&comment.content_span()).trim() == "prettier-ignore"
let text = self.source_text.text_for(&comment.content_span()).trim();
matches!(text, "oxfmt-ignore" | "prettier-ignore")
}

/// Checks if a comment is a type cast comment containing `@type` or `@satisfies`.
Expand Down
60 changes: 60 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file copies from https://github.com/prettier/prettier/blob/main/tests/format/js/ignore/ignore.js,
// and replace all `oxfmt-ignore` to `oxfmt-ignore` to verify that oxfmt correctly ignores those comments.
function a() {
// oxfmt-ignore
var fnString =
'"' + this.USE + ' ' + this.STRICT + '";\n' +
this.filterPrefix() +
'var fn=' + this.generateFunction('fn', 's,l,a,i') +
extra +
this.watchFns() +
'return fn;';

// oxfmt-ignore
const identity = Matrix.create(
1, 0, 0,
0, 1, 0,
0, 0, 0
);

// Let's make sure that this comment doesn't interfere

// oxfmt-ignore
const commentsWithPrettierIgnore = {
"ewww":
"gross-formatting",
};

function giveMeSome() {
a( a ); // oxfmt-ignore
// shouldn't I return something? :shrug:
}

// oxfmt-ignore
console.error(
'In order to use ' + prompt + ', you need to configure a ' +
'few environment variables to be able to commit to the ' +
'repository. Follow those steps to get you setup:\n' +
'\n' +
'Go to https://github.com/settings/tokens/new\n' +
' - Fill "Token description" with "' + prompt + ' for ' +
repoSlug + '"\n' +
' - Check "public_repo"\n' +
' - Press "Generate Token"\n' +
'\n' +
'In a different tab, go to https://travis-ci.org/' +
repoSlug + '/settings\n' +
' - Make sure "Build only if .travis.yml is present" is ON\n' +
' - Fill "Name" with "GITHUB_USER" and "Value" with the name of the ' +
'account you generated the token with. Press "Add"\n' +
'\n' +
'Once this is done, commit anything to the repository to restart ' +
'Travis and it should work :)'
);
}

const response = {
// oxfmt-ignore
'_text': 'Turn on the lights',
intent: 'lights',
};
195 changes: 195 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
// This file copies from https://github.com/prettier/prettier/blob/main/tests/format/js/ignore/ignore.js,
// and replace all `oxfmt-ignore` to `oxfmt-ignore` to verify that oxfmt correctly ignores those comments.
function a() {
// oxfmt-ignore
var fnString =
'"' + this.USE + ' ' + this.STRICT + '";\n' +
this.filterPrefix() +
'var fn=' + this.generateFunction('fn', 's,l,a,i') +
extra +
this.watchFns() +
'return fn;';

// oxfmt-ignore
const identity = Matrix.create(
1, 0, 0,
0, 1, 0,
0, 0, 0
);

// Let's make sure that this comment doesn't interfere

// oxfmt-ignore
const commentsWithPrettierIgnore = {
"ewww":
"gross-formatting",
};

function giveMeSome() {
a( a ); // oxfmt-ignore
// shouldn't I return something? :shrug:
}

// oxfmt-ignore
console.error(
'In order to use ' + prompt + ', you need to configure a ' +
'few environment variables to be able to commit to the ' +
'repository. Follow those steps to get you setup:\n' +
'\n' +
'Go to https://github.com/settings/tokens/new\n' +
' - Fill "Token description" with "' + prompt + ' for ' +
repoSlug + '"\n' +
' - Check "public_repo"\n' +
' - Press "Generate Token"\n' +
'\n' +
'In a different tab, go to https://travis-ci.org/' +
repoSlug + '/settings\n' +
' - Make sure "Build only if .travis.yml is present" is ON\n' +
' - Fill "Name" with "GITHUB_USER" and "Value" with the name of the ' +
'account you generated the token with. Press "Add"\n' +
'\n' +
'Once this is done, commit anything to the repository to restart ' +
'Travis and it should work :)'
);
}

const response = {
// oxfmt-ignore
'_text': 'Turn on the lights',
intent: 'lights',
};

==================== Output ====================
------------------
{ printWidth: 80 }
------------------
// This file copies from https://github.com/prettier/prettier/blob/main/tests/format/js/ignore/ignore.js,
// and replace all `oxfmt-ignore` to `oxfmt-ignore` to verify that oxfmt correctly ignores those comments.
function a() {
// oxfmt-ignore
var fnString =
'"' + this.USE + ' ' + this.STRICT + '";\n' +
this.filterPrefix() +
'var fn=' + this.generateFunction('fn', 's,l,a,i') +
extra +
this.watchFns() +
'return fn;';

// oxfmt-ignore
const identity = Matrix.create(
1, 0, 0,
0, 1, 0,
0, 0, 0
);

// Let's make sure that this comment doesn't interfere

// oxfmt-ignore
const commentsWithPrettierIgnore = {
"ewww":
"gross-formatting",
};

function giveMeSome() {
a(a); // oxfmt-ignore
// shouldn't I return something? :shrug:
}

// oxfmt-ignore
console.error(
'In order to use ' + prompt + ', you need to configure a ' +
'few environment variables to be able to commit to the ' +
'repository. Follow those steps to get you setup:\n' +
'\n' +
'Go to https://github.com/settings/tokens/new\n' +
' - Fill "Token description" with "' + prompt + ' for ' +
repoSlug + '"\n' +
' - Check "public_repo"\n' +
' - Press "Generate Token"\n' +
'\n' +
'In a different tab, go to https://travis-ci.org/' +
repoSlug + '/settings\n' +
' - Make sure "Build only if .travis.yml is present" is ON\n' +
' - Fill "Name" with "GITHUB_USER" and "Value" with the name of the ' +
'account you generated the token with. Press "Add"\n' +
'\n' +
'Once this is done, commit anything to the repository to restart ' +
'Travis and it should work :)'
);
}

const response = {
// oxfmt-ignore
'_text': 'Turn on the lights',
intent: "lights",
};

-------------------
{ printWidth: 100 }
-------------------
// This file copies from https://github.com/prettier/prettier/blob/main/tests/format/js/ignore/ignore.js,
// and replace all `oxfmt-ignore` to `oxfmt-ignore` to verify that oxfmt correctly ignores those comments.
function a() {
// oxfmt-ignore
var fnString =
'"' + this.USE + ' ' + this.STRICT + '";\n' +
this.filterPrefix() +
'var fn=' + this.generateFunction('fn', 's,l,a,i') +
extra +
this.watchFns() +
'return fn;';

// oxfmt-ignore
const identity = Matrix.create(
1, 0, 0,
0, 1, 0,
0, 0, 0
);

// Let's make sure that this comment doesn't interfere

// oxfmt-ignore
const commentsWithPrettierIgnore = {
"ewww":
"gross-formatting",
};

function giveMeSome() {
a(a); // oxfmt-ignore
// shouldn't I return something? :shrug:
}

// oxfmt-ignore
console.error(
'In order to use ' + prompt + ', you need to configure a ' +
'few environment variables to be able to commit to the ' +
'repository. Follow those steps to get you setup:\n' +
'\n' +
'Go to https://github.com/settings/tokens/new\n' +
' - Fill "Token description" with "' + prompt + ' for ' +
repoSlug + '"\n' +
' - Check "public_repo"\n' +
' - Press "Generate Token"\n' +
'\n' +
'In a different tab, go to https://travis-ci.org/' +
repoSlug + '/settings\n' +
' - Make sure "Build only if .travis.yml is present" is ON\n' +
' - Fill "Name" with "GITHUB_USER" and "Value" with the name of the ' +
'account you generated the token with. Press "Add"\n' +
'\n' +
'Once this is done, commit anything to the repository to restart ' +
'Travis and it should work :)'
);
}

const response = {
// oxfmt-ignore
'_text': 'Turn on the lights',
intent: "lights",
};

===================== End =====================
Loading