Skip to content
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

feat: add skipJSXText option to no-irregular-whitespace rule #17182

Merged
merged 6 commits into from Jun 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/src/rules/no-irregular-whitespace.md
Expand Up @@ -63,6 +63,7 @@ This rule has an object option for exceptions:
* `"skipComments": true` allows any whitespace characters in comments
* `"skipRegExps": true` allows any whitespace characters in regular expression literals
* `"skipTemplates": true` allows any whitespace characters in template literals
* `"skipJSXText": true` allows any whitespace characters in JSX text

### skipStrings

Expand Down Expand Up @@ -192,6 +193,23 @@ function thing() {

:::

### skipJSXText
azat-io marked this conversation as resolved.
Show resolved Hide resolved

Examples of additional **correct** code for this rule with the `{ "skipJSXText": true }` option:

::: correct

```js
/*eslint no-irregular-whitespace: ["error", { "skipJSXText": true }]*/
/*eslint-env es6*/

function Thing() {
return <div>text in <NBSP>JSX</div>;
}
```

:::

## When Not To Use It

If you decide that you wish to use whitespace other than tabs and spaces outside of strings in your application.
18 changes: 18 additions & 0 deletions lib/rules/no-irregular-whitespace.js
Expand Up @@ -55,6 +55,10 @@ module.exports = {
skipRegExps: {
type: "boolean",
default: false
},
skipJSXText: {
type: "boolean",
default: false
}
},
additionalProperties: false
Expand All @@ -77,6 +81,7 @@ module.exports = {
const skipStrings = options.skipStrings !== false;
const skipRegExps = !!options.skipRegExps;
const skipTemplates = !!options.skipTemplates;
const skipJSXText = !!options.skipJSXText;

const sourceCode = context.sourceCode;
const commentNodes = sourceCode.getAllComments();
Expand Down Expand Up @@ -144,6 +149,18 @@ module.exports = {
}
}

/**
* Checks JSX nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
* @param {ASTNode} node to check for matching errors.
* @returns {void}
* @private
*/
function removeInvalidNodeErrorsInJSXText(node) {
if (ALL_IRREGULARS.test(node.raw)) {
removeWhitespaceError(node);
}
}

/**
* Checks the program source for irregular whitespace
* @param {ASTNode} node The program node
Expand Down Expand Up @@ -239,6 +256,7 @@ module.exports = {

nodes.Literal = removeInvalidNodeErrorsInLiteral;
nodes.TemplateElement = skipTemplates ? removeInvalidNodeErrorsInTemplateLiteral : noop;
nodes.JSXText = skipJSXText ? removeInvalidNodeErrorsInJSXText : noop;
nodes["Program:exit"] = function() {
if (skipComments) {

Expand Down