Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
Browse files Browse the repository at this point in the history
…onfig.ts
  • Loading branch information
aryaemami59 committed May 16, 2024
2 parents b532667 + 5c28d9a commit 9248177
Show file tree
Hide file tree
Showing 11 changed files with 2,412 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ The following companies, organizations, and individuals support ESLint's ongoing
<h3>Platinum Sponsors</h3>
<p><a href="https://automattic.com"><img src="https://images.opencollective.com/automattic/d0ef3e1/logo.png" alt="Automattic" height="undefined"></a></p><h3>Gold Sponsors</h3>
<p><a href="https://engineering.salesforce.com"><img src="https://images.opencollective.com/salesforce/ca8f997/logo.png" alt="Salesforce" height="96"></a> <a href="https://www.airbnb.com/"><img src="https://images.opencollective.com/airbnb/d327d66/logo.png" alt="Airbnb" height="96"></a></p><h3>Silver Sponsors</h3>
<p><a href="https://www.jetbrains.com/"><img src="https://images.opencollective.com/jetbrains/eb04ddc/logo.png" alt="JetBrains" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a> <a href="https://www.workleap.com"><img src="https://avatars.githubusercontent.com/u/53535748?u=d1e55d7661d724bf2281c1bfd33cb8f99fe2465f&v=4" alt="Workleap" height="64"></a></p><h3>Bronze Sponsors</h3>
<p><a href="https://www.jetbrains.com/"><img src="https://images.opencollective.com/jetbrains/fe76f99/logo.png" alt="JetBrains" height="64"></a> <a href="https://liftoff.io/"><img src="https://images.opencollective.com/liftoff/5c4fa84/logo.png" alt="Liftoff" height="64"></a> <a href="https://americanexpress.io"><img src="https://avatars.githubusercontent.com/u/3853301?v=4" alt="American Express" height="64"></a> <a href="https://www.workleap.com"><img src="https://avatars.githubusercontent.com/u/53535748?u=d1e55d7661d724bf2281c1bfd33cb8f99fe2465f&v=4" alt="Workleap" height="64"></a></p><h3>Bronze Sponsors</h3>
<p><a href="https://www.notion.so"><img src="https://images.opencollective.com/notion/bf3b117/logo.png" alt="notion" height="32"></a> <a href="https://www.crosswordsolver.org/anagram-solver/"><img src="https://images.opencollective.com/anagram-solver/2666271/logo.png" alt="Anagram Solver" height="32"></a> <a href="https://icons8.com/"><img src="https://images.opencollective.com/icons8/7fa1641/logo.png" alt="Icons8" height="32"></a> <a href="https://discord.com"><img src="https://images.opencollective.com/discordapp/f9645d9/logo.png" alt="Discord" height="32"></a> <a href="https://www.ignitionapp.com"><img src="https://avatars.githubusercontent.com/u/5753491?v=4" alt="Ignition" height="32"></a> <a href="https://nx.dev"><img src="https://avatars.githubusercontent.com/u/23692104?v=4" alt="Nx" height="32"></a> <a href="https://herocoders.com"><img src="https://avatars.githubusercontent.com/u/37549774?v=4" alt="HeroCoders" height="32"></a> <a href="https://usenextbase.com"><img src="https://avatars.githubusercontent.com/u/145838380?v=4" alt="Nextbase Starter Kit" height="32"></a></p>
<!--sponsorsend-->

Expand Down
33 changes: 33 additions & 0 deletions docs/src/rules/no-restricted-exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ By default, this rule doesn't disallow any names. Only the names you specify in
This rule has an object option:

* `"restrictedNamedExports"` is an array of strings, where each string is a name to be restricted.
* `"restrictedNamedExportsPattern"` is a string representing a regular expression pattern. Named exports matching this pattern will be restricted. This option does not apply to `default` named exports.
* `"restrictDefaultExports"` is an object option with boolean properties to restrict certain default export declarations. The option works only if the `restrictedNamedExports` option does not contain the `"default"` value. The following properties are allowed:
* `direct`: restricts `export default` declarations.
* `named`: restricts `export { foo as default };` declarations.
Expand Down Expand Up @@ -130,6 +131,38 @@ export default function foo() {}

:::

### restrictedNamedExportsPattern

Example of **incorrect** code for the `"restrictedNamedExportsPattern"` option:

::: incorrect

```js
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExportsPattern": "bar$"
}]*/

export const foobar = 1;
```

:::

Example of **correct** code for the `"restrictedNamedExportsPattern"` option:

::: correct

```js
/*eslint no-restricted-exports: ["error", {
"restrictedNamedExportsPattern": "bar$"
}]*/

export const abc = 1;
```

:::

Note that this option does not apply to `export default` or any `default` named exports. If you want to also restrict `default` exports, use the `restrictDefaultExports` option.

### restrictDefaultExports

This option allows you to restrict certain `default` declarations. The option works only if the `restrictedNamedExports` option does not contain the `"default"` value. This option accepts the following properties:
Expand Down
4 changes: 4 additions & 0 deletions docs/src/use/configure/migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export default [
];
```

::: tip
If you import a plugin and get an error such as "TypeError: context.getScope is not a function", then that means the plugin has not yet been updated to the ESLint v9.x rule API. While you should file an issue with the particular plugin, you can manually patch the plugin to work in ESLint v9.x using the [compatibility utilities](https://eslint.org/blog/2024/05/eslint-compatibility-utilities/).
:::

### Custom Parsers

In eslintrc files, importing a custom parser is similar to importing a plugin: you use a string to specify the name of the parser.
Expand Down
5 changes: 5 additions & 0 deletions docs/src/use/configure/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,8 @@ export default [
```

ESLint only lints named code blocks when they are JavaScript files or if they match a `files` entry in a config object. Be sure to add a config object with a matching `files` entry if you want to lint non-JavaScript named code blocks.

## Common Problems

* [Plugin rules using the ESLint < v9.0.0 API](../troubleshooting/v9-rule-api-changes)
* [Plugin configurations have not been upgraded to flat config](migration-guide#using-eslintrc-configs-in-flat-config)
6 changes: 6 additions & 0 deletions docs/src/use/troubleshooting/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ eleventyNavigation:

This page serves as a reference for common issues working with ESLint.

## Configuration

* [`TypeError: context.getScope is not a function`](./v9-rule-api-changes)

## Legacy (eslintrc) Configuration

* [`ESLint couldn't determine the plugin … uniquely`](./couldnt-determine-the-plugin-uniquely)
* [`ESLint couldn't find the config … to extend from`](./couldnt-find-the-config)
* [`ESLint couldn't find the plugin …`](./couldnt-find-the-plugin)
Expand Down
40 changes: 40 additions & 0 deletions docs/src/use/troubleshooting/v9-rule-api-changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: "TypeError: context.getScope is not a function"
eleventyNavigation:
key: v9 rule api changes
parent: troubleshooting
title: "TypeError: context.getScope is not a function"
---

## Symptoms

When using ESLint v9.0.0 or later with a plugin, you may see one of the following errors:

```plaintext
TypeError: context.getScope is not a function
TypeError: context.getAncestors is not a function
TypeError: context.markVariableAsUsed is not a function
TypeError: context.getDeclaredVariables is not a function
```

## Cause

ESLint v9.0.0 introduces [changes to the rules API](https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/) that plugin rules use, which included moving some methods from the `context` object to the `sourceCode` object. If you're seeing one of these errors, that means the plugin has not yet been updated to use the new rules API.

## Resolution

Common resolutions for this issue include:

* Upgrade the plugin to the latest version
* Use the [compatibility utilities](https://eslint.org/blog/2024/05/eslint-compatibility-utilities/) to patch the plugin in your config file

::: important
If you are already using the latest version of the plugin and you need to use the compatibility utilities to make the plugin work with ESLint v9.0.0 and later, make sure to open an issue on the plugin's repository to ask the maintainer to make the necessary API changes.
:::

## Resources

For more information, see:

* [Configure Plugins](../configure/plugins) for documentation on how to configure plugins
* [Create Plugins](../../extend/plugins#configs-in-plugins) for documentation on how to define plugins
15 changes: 13 additions & 2 deletions lib/rules/no-restricted-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ module.exports = {
type: "string"
},
uniqueItems: true
}
},
restrictedNamedExportsPattern: { type: "string" }
},
additionalProperties: false
},
Expand All @@ -52,6 +53,7 @@ module.exports = {
},
uniqueItems: true
},
restrictedNamedExportsPattern: { type: "string" },
restrictDefaultExports: {
type: "object",
properties: {
Expand Down Expand Up @@ -98,6 +100,7 @@ module.exports = {
create(context) {

const restrictedNames = new Set(context.options[0] && context.options[0].restrictedNamedExports);
const restrictedNamePattern = context.options[0] && context.options[0].restrictedNamedExportsPattern;
const restrictDefaultExports = context.options[0] && context.options[0].restrictDefaultExports;
const sourceCode = context.sourceCode;

Expand All @@ -109,7 +112,15 @@ module.exports = {
function checkExportedName(node) {
const name = astUtils.getModuleExportName(node);

if (restrictedNames.has(name)) {
let matchesRestrictedNamePattern = false;

if (restrictedNamePattern && name !== "default") {
const patternRegex = new RegExp(restrictedNamePattern, "u");

matchesRestrictedNamePattern = patternRegex.test(name);
}

if (matchesRestrictedNamePattern || restrictedNames.has(name)) {
context.report({
node,
messageId: "restrictedNamed",
Expand Down
41 changes: 21 additions & 20 deletions lib/rules/object-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,35 +284,22 @@ module.exports = {
const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken);
const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);

let shouldAddParensAroundParameters = false;
let tokenBeforeParams;

if (node.value.params.length === 0) {
tokenBeforeParams = sourceCode.getFirstToken(node.value, astUtils.isOpeningParenToken);
} else {
tokenBeforeParams = sourceCode.getTokenBefore(node.value.params[0]);
}

if (node.value.params.length === 1) {
const hasParen = astUtils.isOpeningParenToken(tokenBeforeParams);
const isTokenOutsideNode = tokenBeforeParams.range[0] < node.range[0];

shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode;
}
// First token should not be `async`
const firstValueToken = sourceCode.getFirstToken(node.value, {
skip: node.value.async ? 1 : 0
});

const sliceStart = shouldAddParensAroundParameters
? node.value.params[0].range[0]
: tokenBeforeParams.range[0];
const sliceStart = firstValueToken.range[0];
const sliceEnd = sourceCode.getTokenBefore(arrowToken).range[1];
const shouldAddParens = node.value.params.length === 1 && node.value.params[0].range[0] === sliceStart;

const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd);
const newParamText = shouldAddParensAroundParameters ? `(${oldParamText})` : oldParamText;
const newParamText = shouldAddParens ? `(${oldParamText})` : oldParamText;

return fixer.replaceTextRange(
fixRange,
methodPrefix + newParamText + fnBody
);

}

/**
Expand Down Expand Up @@ -497,6 +484,13 @@ module.exports = {
node,
messageId: "expectedPropertyShorthand",
fix(fixer) {

// x: /* */ x
// x: (/* */ x)
if (sourceCode.getCommentsInside(node).length > 0) {
return null;
}

return fixer.replaceText(node, node.value.name);
}
});
Expand All @@ -510,6 +504,13 @@ module.exports = {
node,
messageId: "expectedPropertyShorthand",
fix(fixer) {

// "x": /* */ x
// "x": (/* */ x)
if (sourceCode.getCommentsInside(node).length > 0) {
return null;
}

return fixer.replaceText(node, node.value.name);
}
});
Expand Down

0 comments on commit 9248177

Please sign in to comment.