Skip to content

Commit a8c5a43

Browse files
daniel-stoian-lgpstanca-pop-lgphong6316
authored
WRR-1005: Updated Eslint to version 9 and applied breaking changes on enact custom rules (#41)
* updated eslint to v9 * updated custom rules to work with eslint9 * removed unnecessary changes * removed unnecessary changes * removed unnecessary changes * disabling tests in travis until full flat config support * Update CHANGELOG.md * comment travis.yml * migrated to flatConfig * fixed lint errors * removed unused import * Update CHANGELOG.md Co-authored-by: Stanca <63341832+stanca-pop-lgp@users.noreply.github.com> * rollback of uneccessary change * rollback unnecessary change * rollback unnecessary change * rollback unnecessary change * <Summary Line> :Release Notes: :Detailed Notes: :Testing Performed: :QA Notes: :Issues Addressed: [GF-9999] Summary * rollback unnecessary change * fixed changelog * fixed changelog * minor fix * removed consoleLog * code review fix * fix * fix * fixed typos * Update CHANGELOG.md * updated eslint minor version --------- Co-authored-by: Stanca <63341832+stanca-pop-lgp@users.noreply.github.com> Co-authored-by: taeyoung.hong <taeyoung.hong@lge.com> Co-authored-by: Stanca <stanca.pop@lgepartner.com> Co-authored-by: taeyoung.hong <35059065+hong6316@users.noreply.github.com>
1 parent 8d6c84e commit a8c5a43

11 files changed

+2853
-816
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The following is a curated list of changes in the Enact eslint plugin:
44

5+
## [unreleased]
6+
7+
* Updated Enact ESLint config to v9 including eslint related modules.
8+
59
## [2.0.0-alpha.1] - (December 6, 2024)
610

711
* Fixed a deprecation warning regarding nodejs.

index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@ module.exports = {
2525
rules: exportedRules,
2626
configs: {
2727
recommended: {
28-
parserOptions: {
29-
ecmaFeatures: {
30-
jsx: true
28+
languageOptions: {
29+
parserOptions: {
30+
ecmaFeatures: {
31+
jsx: true
32+
}
3133
}
3234
},
3335
rules: {
3436
'enact/prop-types': 2,
3537
'enact/kind-name': 2,
3638
'enact/display-name': 2,
3739
'enact/no-module-exports-import': 1
38-
},
40+
}
3941
},
4042
all: {
41-
parserOptions: {
42-
ecmaFeatures: {
43-
jsx: true
43+
languageOptions: {
44+
parserOptions: {
45+
ecmaFeatures: {
46+
jsx: true
47+
}
4448
}
4549
},
4650
rules: allRules

lib/rules/display-name.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ module.exports = {
9090
}
9191

9292
/**
93-
* Checks if the component have a name set by the transpiler
93+
* Checks if the component has a name set by the transpiler
9494
* @param {ASTNode} node The AST node being checked.
95-
* @returns {Boolean} True if component have a name, false if not.
95+
* @returns {Boolean} True if component has a name, false if not.
9696
*/
9797
function hasTranspilerName(node) {
9898
var namedObjectAssignment = (

lib/rules/prop-types.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = {
9090
*/
9191
function isPropTypesUsage(node) {
9292
var isClassUsage = (
93-
(utils.getParentES6Component() || utils.getParentES5Component()) &&
93+
(utils.getParentES6Component(node) || utils.getParentES5Component(node)) &&
9494
node.object.type === 'ThisExpression' && node.property.name === 'props'
9595
);
9696
var isStatelessFunctionUsage = node.object.name === 'props';
@@ -104,7 +104,7 @@ module.exports = {
104104
*/
105105
function isAnnotatedClassPropsDeclaration(node) {
106106
if (node && (node.type === 'ClassProperty' || node.type === 'PropertyDefinition')) {
107-
var tokens = context.getFirstTokens(node, 2);
107+
let tokens = sourceCode.getFirstTokens ? sourceCode.getFirstTokens(node, 2) : context.getFirstTokens(node, 2);
108108
if (
109109
node.typeAnnotation && (
110110
tokens[0].value === 'props' ||
@@ -124,7 +124,7 @@ module.exports = {
124124
*/
125125
function isAnnotatedFunctionPropsDeclaration(node) {
126126
if (node && node.params && node.params.length) {
127-
var tokens = context.getFirstTokens(node.params[0], 2);
127+
let tokens = sourceCode.getFirstTokens ? sourceCode.getFirstTokens(node.params[0], 2) : context.getFirstTokens(node.params[0], 2);
128128
var isAnnotated = node.params[0].typeAnnotation;
129129
var isDestructuredProps = node.params[0].type === 'ObjectPattern';
130130
var isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
@@ -170,7 +170,7 @@ module.exports = {
170170
// Special case for class properties
171171
// (babel-eslint does not expose property name so we have to rely on tokens)
172172
if (node && (node.type === 'ClassProperty' || node.type === 'PropertyDefinition')) {
173-
var tokens = context.getFirstTokens(node, 2);
173+
let tokens = sourceCode.getFirstTokens ? sourceCode.getFirstTokens(node, 2) : context.getFirstTokens(node, 2);
174174
if (
175175
tokens[0].value === 'propTypes' ||
176176
(tokens[1] && tokens[1].value === 'propTypes')
@@ -317,7 +317,7 @@ module.exports = {
317317
*/
318318
function getKeyValue(node) {
319319
if (node.type === 'ObjectTypeProperty') {
320-
var tokens = context.getFirstTokens(node, 1);
320+
let tokens = sourceCode.getFirstTokens ? sourceCode.getFirstTokens(node, 1) : context.getFirstTokens(node, 1);
321321
return tokens[0].value;
322322
}
323323
var key = node.key || node.argument || node.property;
@@ -511,8 +511,8 @@ module.exports = {
511511
* Check if we are in a class constructor
512512
* @return {boolean} true if we are in a class constructor, false if not
513513
*/
514-
function inConstructor() {
515-
var scope = context.getScope();
514+
function inConstructor(node) {
515+
let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
516516
while (scope) {
517517
if (scope.block && scope.block.parent && scope.block.parent.kind === 'constructor') {
518518
return true;
@@ -529,8 +529,8 @@ module.exports = {
529529
*/
530530
function getPropertyName(node) {
531531
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));
532-
var isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
533-
var isNotInConstructor = !inConstructor();
532+
var isInClassComponent = utils.getParentES6Component(node) || utils.getParentES5Component(node);
533+
var isNotInConstructor = !inConstructor(node);
534534

535535
if (isInClassComponent && utils.isKindComponent(isInClassComponent)) {
536536
isInClassComponent = false;
@@ -576,7 +576,7 @@ module.exports = {
576576
* @param {ASTNode} node The AST node being marked.
577577
*/
578578
function markComputedPropTypes(node) {
579-
var component = components.get(utils.getParentComponent());
579+
var component = components.get(utils.getParentComponent(node));
580580
var computed = component && component.computedProps || [];
581581
for(var i=0; i<node.properties.length; i++) {
582582
if(node.properties[i] && node.properties[i].key && node.properties[i].key.name) {
@@ -593,7 +593,7 @@ module.exports = {
593593
* @param {ASTNode} node The AST node being marked.
594594
*/
595595
function markHandlersPropTypes(node) {
596-
var component = components.get(utils.getParentComponent());
596+
var component = components.get(utils.getParentComponent(node));
597597
var handlers = component && component.handlersProps || [];
598598
for(var i=0; i<node.properties.length; i++) {
599599
if(node.properties[i] && node.properties[i].key && node.properties[i].key.name) {
@@ -693,7 +693,7 @@ module.exports = {
693693
// let {firstname} = props
694694
var directDestructuring =
695695
node.init.name === 'props' &&
696-
(utils.getParentStatelessComponent() || inConstructor())
696+
(utils.getParentStatelessComponent(node) || inConstructor(node))
697697
;
698698

699699
if (thisDestructuring) {
@@ -711,7 +711,7 @@ module.exports = {
711711
throw new Error(node.type + ' ASTNodes are not handled by markPropTypesAsUsed');
712712
}
713713

714-
var component = components.get(utils.getParentComponent());
714+
var component = components.get(utils.getParentComponent(node));
715715
var usedPropTypes = component && component.usedPropTypes || [];
716716

717717
switch (type) {
@@ -726,7 +726,7 @@ module.exports = {
726726
usedPropTypes.push({
727727
name: name,
728728
allNames: allNames,
729-
node: !isDirectProp && !inConstructor() ? node.parent.property : node.property
729+
node: !isDirectProp && !inConstructor(node) ? node.parent.property : node.property
730730
});
731731
break;
732732
case 'destructuring':
@@ -820,7 +820,7 @@ module.exports = {
820820
}
821821
break;
822822
case 'Identifier':
823-
var variablesInScope = variable.variablesInScope(context);
823+
var variablesInScope = variable.variablesInScope(sourceCode, context, componentNode)
824824
for (var i = 0, j = variablesInScope.length; i < j; i++) {
825825
if (variablesInScope[i].name !== propTypes.name) {
826826
continue;
@@ -1000,7 +1000,7 @@ module.exports = {
10001000
var directDestructuring =
10011001
destructuring &&
10021002
node.init.name === 'props' &&
1003-
(utils.getParentStatelessComponent() || inConstructor())
1003+
(utils.getParentStatelessComponent(node) || inConstructor(node))
10041004
;
10051005

10061006
if (!thisDestructuring && !directDestructuring) {

lib/util/Components.js

+36-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
'use strict';
77

8-
var util = require('util');
98
var doctrine = require('doctrine');
109
var variableUtil = require('./variable');
1110
var pragmaUtil = require('./pragma');
@@ -140,7 +139,7 @@ function componentRule(rule, context) {
140139
var hoc = pragmaUtil.getHocFromContext(context);
141140
var createClass = pragmaUtil.getCreateClassFromContext(context);
142141
var pragma = pragmaUtil.getFromContext(context);
143-
var sourceCode = context.getSourceCode();
142+
const sourceCode = context.sourceCode || context.getSourceCode();
144143
var components = new Components();
145144

146145
// Utilities for component detection
@@ -340,11 +339,11 @@ function componentRule(rule, context) {
340339
*
341340
* @returns {ASTNode} component node, null if we are not in a component
342341
*/
343-
getParentComponent: function() {
342+
getParentComponent: function(node) {
344343
return (
345-
utils.getParentES6Component() ||
346-
utils.getParentES5Component() ||
347-
utils.getParentStatelessComponent()
344+
utils.getParentES6Component(node) ||
345+
utils.getParentES5Component(node) ||
346+
utils.getParentStatelessComponent(node)
348347
);
349348
},
350349

@@ -353,12 +352,12 @@ function componentRule(rule, context) {
353352
*
354353
* @returns {ASTNode|Null} component node, null if we are not in a component
355354
*/
356-
getParentES5Component: function() {
357-
var scope = context.getScope();
355+
getParentES5Component: function(node) {
356+
let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
358357
while (scope) {
359-
var node = scope.block && scope.block.parent && scope.block.parent.parent;
360-
if (node && (utils.isES5Component(node) || utils.isKindComponent(node))) {
361-
return node;
358+
var parentNode = scope.block && scope.block.parent && scope.block.parent.parent;
359+
if (parentNode && (utils.isES5Component(parentNode) || utils.isKindComponent(parentNode))) {
360+
return parentNode;
362361
}
363362
scope = scope.upper;
364363
}
@@ -370,42 +369,42 @@ function componentRule(rule, context) {
370369
*
371370
* @returns {ASTNode|Null} component node, null if we are not in a component
372371
*/
373-
getParentES6Component: function() {
374-
var scope = context.getScope();
372+
getParentES6Component: function(node) {
373+
let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
375374
while (scope && scope.type !== 'class') {
376375
scope = scope.upper;
377376
}
378-
var node = scope && scope.block;
379-
if (!node || !utils.isES6Component(node)) {
377+
let parentNode = scope && scope.block;
378+
if (!parentNode || !utils.isES6Component(parentNode)) {
380379
return null;
381380
}
382-
return node;
381+
return parentNode;
383382
},
384383

385384
/**
386385
* Get the parent stateless component node from the current scope
387386
*
388387
* @returns {ASTNode|Null} component node, null if we are not in a component
389388
*/
390-
getParentStatelessComponent: function() {
391-
var scope = context.getScope();
389+
getParentStatelessComponent: function(node) {
390+
let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
392391
while (scope) {
393-
var node = scope.block;
394-
var isClass = node.type === 'ClassExpression';
395-
var isFunction = /Function/.test(node.type); // Functions
396-
var isMethod = node.parent && node.parent.type === 'MethodDefinition'; // Classes methods
397-
var isArgument = node.parent && node.parent.type === 'CallExpression'; // Arguments (callback, etc.)
392+
var parentNode = scope.block;
393+
var isClass = parentNode.type === 'ClassExpression';
394+
var isFunction = /Function/.test(parentNode.type); // Functions
395+
var isMethod = parentNode.parent && parentNode.parent.type === 'MethodDefinition'; // Classes methods
396+
var isArgument = parentNode.parent && parentNode.parent.type === 'CallExpression'; // Arguments (callback, etc.)
398397
// Stop moving up if we reach a class or an argument (like a callback)
399398
if (isClass || isArgument) {
400399
return null;
401400
}
402401
// Return the node if it is a function that is not a class method
403402
if (isFunction && !isMethod) {
404403
// if stateless function is an Enact kind computed property, return the kind
405-
if(utils.isComputedKindProp(node) || utils.isHandlersKindProp(node)) {
406-
return(node.parent.parent.parent.parent);
404+
if(utils.isComputedKindProp(parentNode) || utils.isHandlersKindProp(parentNode)) {
405+
return(parentNode.parent.parent.parent.parent);
407406
} else {
408-
return node;
407+
return parentNode;
409408
}
410409
}
411410
scope = scope.upper;
@@ -426,6 +425,7 @@ function componentRule(rule, context) {
426425
var l;
427426
var componentName;
428427
var componentNode;
428+
let nodeParam = node;
429429
// Get the component path
430430
var componentPath = [];
431431
while (node) {
@@ -446,7 +446,7 @@ function componentRule(rule, context) {
446446
return null;
447447
}
448448
var variableInScope;
449-
var variables = variableUtil.variablesInScope(context);
449+
var variables = variableUtil.variablesInScope(sourceCode, context, nodeParam);
450450
for (i = 0, j = variables.length; i < j; i++) {
451451
if (variables[i].name === variableName) {
452452
variableInScope = variables[i];
@@ -534,7 +534,7 @@ function componentRule(rule, context) {
534534
},
535535

536536
'ClassProperty, PropertyDefinition': function(node) {
537-
node = utils.getParentComponent();
537+
node = utils.getParentComponent(node);
538538
if (!node) {
539539
return;
540540
}
@@ -549,7 +549,7 @@ function componentRule(rule, context) {
549549
},
550550

551551
FunctionExpression: function(node) {
552-
var component = utils.getParentComponent();
552+
var component = utils.getParentComponent(node);
553553
if (
554554
!component ||
555555
(component.parent && component.parent.type === 'JSXExpressionContainer')
@@ -562,15 +562,15 @@ function componentRule(rule, context) {
562562
},
563563

564564
FunctionDeclaration: function(node) {
565-
node = utils.getParentComponent();
565+
node = utils.getParentComponent(node);
566566
if (!node) {
567567
return;
568568
}
569569
components.add(node, 1);
570570
},
571571

572572
ArrowFunctionExpression: function(node) {
573-
var component = utils.getParentComponent();
573+
var component = utils.getParentComponent(node);
574574
if (
575575
!component ||
576576
(component.parent && component.parent.type === 'JSXExpressionContainer')
@@ -587,7 +587,7 @@ function componentRule(rule, context) {
587587
},
588588

589589
ThisExpression: function(node) {
590-
var component = utils.getParentComponent();
590+
var component = utils.getParentComponent(node);
591591
if (!component || !/Function/.test(component.type) || !node.parent.property) {
592592
return;
593593
}
@@ -603,9 +603,11 @@ function componentRule(rule, context) {
603603
if (!utils.isReturningJSX(node)) {
604604
return;
605605
}
606-
node = utils.getParentComponent();
606+
let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
607+
608+
node = utils.getParentComponent(node);
609+
607610
if (!node) {
608-
var scope = context.getScope();
609611
components.add(scope.block, 1);
610612
return;
611613
}

lib/util/variable.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ function findVariable(variables, name) {
2828
*
2929
* Contain a patch for babel-eslint to avoid https://github.com/babel/babel-eslint/issues/21
3030
*
31+
* @param {Object} sourceCode The current rule sourceCode.
3132
* @param {Object} context The current rule context.
32-
* @returns {Array} The list of variables in the given scope.
33+
* @param {ASTNode} node The AST node being checked (must be a MemberExpression).
34+
* @returns {Boolean} True if the variable was found, false if not.
3335
*/
34-
function variablesInScope(context) {
35-
var scope = context.getScope();
36+
function variablesInScope(sourceCode, context, node) {
37+
let scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope();
3638
var variables = scope.variables;
3739

3840
while (scope.type !== 'global') {

0 commit comments

Comments
 (0)