Skip to content

Commit e6d57ea

Browse files
michaelfaithdddlratlas-dst-botgithub-actions[bot]
authored
feat(eslint-plugin): add support for eslint v9 (#1760)
* feat(eslint-plugin): add support for eslint v9 This change adds a set of compatibility functions to bridge the gap between the legacy context api and the new context api, allowing projects using this plugin to upgrade their version of eslint to v9. * docs(eslint-plugin): changeset * build(eslint-plugin): add engines to explicitly declare supported node versions This change updates the versions of nodes we're running the test workflow again to v16, v18, v20, and v22 (the new LTS) * docs(eslint-plugin): add changeset * Fix shorthand-property-sorting crashing when styles not initialised (#1765) * Fix shorthand-property-sorting crashing when styles not initialised * Add changeset * Added changeset reminder * Version Packages (#1766) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: Grant Wong <[email protected]> Co-authored-by: atlas-dst-bot <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 7254b42 commit e6d57ea

File tree

23 files changed

+471
-131
lines changed

23 files changed

+471
-131
lines changed

.changeset/five-dolphins-taste.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compiled/eslint-plugin': minor
3+
---
4+
5+
add engines to formalize supported node versions - ^16.0.0 || >= 18.0.0

.changeset/five-pigs-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compiled/eslint-plugin': minor
3+
---
4+
5+
support eslint v9

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
ecmaFeatures: {
1111
jsx: true,
1212
},
13-
ecmaVersion: 2018,
13+
ecmaVersion: 2020,
1414
sourceType: 'module',
1515
},
1616
plugins: ['react-hooks'],
@@ -48,6 +48,8 @@ module.exports = {
4848
},
4949
],
5050
'import/no-unresolved': 'off',
51+
// TypeScript takes care of this for us (https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/namespace.md)
52+
'import/namespace': 'off',
5153
'import/order': [
5254
'error',
5355
{

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
node-version: [14.x, 16.x, 18.x]
16+
node-version: [16.x, 18.x, 20.x, 22.x]
1717

1818
steps:
1919
- uses: actions/checkout@v3

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@
8484
"@types/react": "^17.0.69",
8585
"@types/react-dom": "^17.0.22",
8686
"@types/svgo": "^2.6.4",
87-
"@typescript-eslint/eslint-plugin": "^5.59.8",
88-
"@typescript-eslint/parser": "^5.59.8",
87+
"@typescript-eslint/eslint-plugin": "^6.21.0",
88+
"@typescript-eslint/parser": "^6.21.0",
89+
"@typescript-eslint/utils": "^6.21.0",
8990
"babel-loader": "^9.1.2",
9091
"eslint": "^8.44.0",
9192
"eslint-plugin-import": "^2.27.5",

packages/eslint-plugin/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@
2828
"@types/estraverse": "^5.1.7",
2929
"@types/estree": "^1.0.3",
3030
"@types/estree-jsx": "^1.0.2",
31-
"@typescript-eslint/parser": "^5.59.8",
31+
"@typescript-eslint/parser": "^6.21.0",
32+
"@typescript-eslint/utils": "^6.21.0",
3233
"eslint": "^8.41.0",
3334
"outdent": "^0.8.0",
34-
"typescript": "^4.9.5"
35+
"typescript": "~4.9.5"
36+
},
37+
"engines": {
38+
"node": "^16.0.0 || >=18.0.0"
3539
},
3640
"publishConfig": {
3741
"registry": "https://registry.npmjs.org/"

packages/eslint-plugin/src/rules/jsx-pragma/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
usesCompiledAPI,
99
} from '../../utils/ast';
1010
import { addImportToDeclaration, removeImportFromDeclaration } from '../../utils/ast-to-string';
11+
import { getDeclaredVariables, getSourceCode } from '../../utils/context-compat';
1112
import {
1213
findJsxImportSourcePragma,
1314
findJsxPragma,
@@ -75,7 +76,7 @@ function createFixer(context: Rule.RuleContext, source: SourceCode, options: Opt
7576
const reactImport = findReactDeclarationWithDefaultImport(source);
7677
if (reactImport) {
7778
const [declaration, defaultImport] = reactImport;
78-
const [defaultImportVariable] = context.getDeclaredVariables(defaultImport);
79+
const [defaultImportVariable] = getDeclaredVariables(context, defaultImport);
7980

8081
if (defaultImportVariable && defaultImportVariable.references.length === 0) {
8182
if (declaration.specifiers.length === 1) {
@@ -166,7 +167,7 @@ export const jsxPragmaRule: Rule.RuleModule = {
166167
importSources: [...DEFAULT_IMPORT_SOURCES, ...(optionsRaw.importSources ?? [])],
167168
};
168169

169-
const source = context.getSourceCode();
170+
const source = getSourceCode(context);
170171
const comments = source.getAllComments();
171172

172173
const compiledImports = findLibraryImportDeclarations(context, options.importSources);

packages/eslint-plugin/src/rules/local-cx-xcss/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Rule } from 'eslint';
22

33
import { isCxFunction } from '../../utils';
4+
import { getScope } from '../../utils/context-compat';
45

56
function getParentJSXAttribute(node: Rule.Node) {
67
let parent: Rule.Node | null = node.parent;
@@ -34,7 +35,7 @@ export const localCXXCSSRule: Rule.RuleModule = {
3435
'CallExpression[callee.name="cx"]': (node: Rule.Node) => {
3536
if (
3637
node.type === 'CallExpression' &&
37-
isCxFunction(node.callee as Rule.Node, context.getScope().references)
38+
isCxFunction(node.callee as Rule.Node, getScope(context, node).references)
3839
) {
3940
const parentJSXAttribute = getParentJSXAttribute(node);
4041
const propName = parentJSXAttribute?.name.name.toString();

packages/eslint-plugin/src/rules/no-css-prop-without-css-function/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
buildImportDeclaration,
1212
getImportedName,
1313
} from '../../utils/ast-to-string';
14+
import { getScope, getSourceCode } from '../../utils/context-compat';
1415

1516
type Q<T> = T extends TSESLint.Scope.Definition
1617
? T['type'] extends 'Variable'
@@ -39,7 +40,7 @@ class NoCssPropWithoutCssFunctionRunner {
3940

4041
constructor(private baseNode: TSESTree.JSXExpressionContainer, private context: Context) {
4142
this.jsxElement = traverseUpToJSXOpeningElement(this.baseNode);
42-
this.references = context.getScope().references;
43+
this.references = getScope(context, baseNode).references;
4344

4445
this.ignoreIfImported = [];
4546
this.excludeReactComponents = false;
@@ -125,7 +126,7 @@ class NoCssPropWithoutCssFunctionRunner {
125126
private fixWrapper(node: CSSValue, context: Context) {
126127
function* fix(fixer: TSESLint.RuleFixer) {
127128
const compiledImports = findTSLibraryImportDeclarations(context);
128-
const source = context.getSourceCode();
129+
const source = getSourceCode(context);
129130

130131
// The string that `css` from `@compiled/css` is imported as
131132
const cssImportName = getImportedName(compiledImports, 'css');
@@ -220,7 +221,6 @@ export const noCssPropWithoutCssFunctionRule: TSESLint.RuleModule<string> = {
220221
meta: {
221222
docs: {
222223
url: 'https://github.com/atlassian-labs/compiled/tree/master/packages/eslint-plugin/src/rules/no-css-prop-without-css-function',
223-
recommended: 'error',
224224
description:
225225
'Disallows `css` prop usages where it is either not wrapped in the `css` import from `@compiled/react` or where `@compiled` cannot determine whether the `css` import is included at build time.',
226226
},

packages/eslint-plugin/src/rules/no-emotion-css/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Rule } from 'eslint';
33
import type { ImportSpecifier, ImportDeclaration } from 'estree';
44

55
import { buildImportDeclaration, buildNamedImport } from '../../utils/ast-to-string';
6+
import { getSourceCode } from '../../utils/context-compat';
67

78
const ALLOWED_EMOTION_IMPORTS = ['css', 'keyframes', 'ClassNames', 'jsx'];
89

@@ -18,8 +19,7 @@ const isEmotionImport = (node: ImportDeclaration) =>
1819
* @returns {Rule.Node} The `@compiled/react` node or undefined
1920
*/
2021
const getCompiledNode = (context: Rule.RuleContext) => {
21-
return context
22-
.getSourceCode()
22+
return getSourceCode(context)
2323
.ast.body.filter((node): node is ImportDeclaration => node.type === 'ImportDeclaration')
2424
.find((node) => node.source.value === COMPILED_IMPORT);
2525
};
@@ -39,8 +39,7 @@ export const noEmotionCssRule: Rule.RuleModule = {
3939
create(context) {
4040
return {
4141
Program() {
42-
const pragma = context
43-
.getSourceCode()
42+
const pragma = getSourceCode(context)
4443
.getAllComments()
4544
.find((n) => n.value.includes('@jsxImportSource @emotion/react'));
4645

0 commit comments

Comments
 (0)