Skip to content

Commit

Permalink
feat: add skipJSXText option to no-irregular-whitespace rule (#17182
Browse files Browse the repository at this point in the history
)

* feat: add `skipJSXText` option to `no-irregular-whitespace` rule

* fix: remove unnecessary jsx text value check

* docs: fix no-irregular-whitespace docs

* test: add invalid cases for jsx in no-irregular-whitespace rule

* docs: fix check jsx node func jsdoc

* style: fix trailing commas
  • Loading branch information
azat-io committed Jun 8, 2023
1 parent 788d836 commit 1b7faf0
Show file tree
Hide file tree
Showing 3 changed files with 394 additions and 0 deletions.
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

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

0 comments on commit 1b7faf0

Please sign in to comment.