diff --git a/crates/oxc_formatter/src/formatter/comments.rs b/crates/oxc_formatter/src/formatter/comments.rs index 0b9a815cf9acc..4e0c3658272f6 100644 --- a/crates/oxc_formatter/src/formatter/comments.rs +++ b/crates/oxc_formatter/src/formatter/comments.rs @@ -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`. diff --git a/crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js b/crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js new file mode 100644 index 0000000000000..5b4fa465c3d2c --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js @@ -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', +}; diff --git a/crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js.snap b/crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js.snap new file mode 100644 index 0000000000000..188b34188788b --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/ignore/oxfmt.js.snap @@ -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 =====================