Skip to content

Commit 7e41249

Browse files
authored
Fix __defineGeneric not a function (#50)
* simulate the error * have many tests passing now * update get comments syntax * tests all passing * cleanup
1 parent 904a3cf commit 7e41249

File tree

6 files changed

+143
-134
lines changed

6 files changed

+143
-134
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"ajv": "^8.6.3",
4141
"babel-plugin-add-module-exports": "^1.0.4",
4242
"babel-plugin-transform-flow-enums": "^0.0.2",
43-
"eslint": "^8.46.0",
43+
"eslint": "^8.56.0",
4444
"eslint-config-airbnb": "^19.0.2",
4545
"eslint-config-bzc": "^1.0.5",
4646
"eslint-plugin-fb-flow": "^0.0.4",
@@ -54,15 +54,15 @@
5454
"flow-bin": "^0.167.1",
5555
"flow-copy-source": "^2.0.9",
5656
"glob": "^7.2.0",
57-
"hermes-eslint": "^0.15.0",
57+
"hermes-eslint": "^0.18.2",
5858
"husky": "^7.0.4",
5959
"jest": "^27.4.5",
6060
"lint-staged": "^12.1.2",
6161
"mocha": "^10.1.0",
6262
"rimraf": "^3.0.2"
6363
},
6464
"peerDependencies": {
65-
"eslint": "^8.1.0",
65+
"eslint": "^8.56.0",
6666
"hermes-eslint": ">=0.15.0"
6767
},
6868
"keywords": [

src/rules/defineFlowType.js

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,78 @@ const schema = [];
33
const create = (context) => {
44
let globalScope;
55

6-
// do nearly the same thing that eslint does for config globals
7-
// https://github.com/eslint/eslint/blob/v2.0.0/lib/eslint.js#L118-L194
8-
const makeDefined = (ident) => {
9-
let ii;
10-
11-
// start from the right since we're going to remove items from the array
12-
for (ii = globalScope.through.length - 1; ii >= 0; ii--) {
13-
const ref = globalScope.through[ii];
14-
15-
if (ref.identifier.name === ident.name) {
16-
// use "__defineGeneric" since we don't have a reference to "escope.Variable"
17-
18-
globalScope.__defineGeneric(
19-
ident.name,
20-
globalScope.set,
21-
globalScope.variables,
22-
);
23-
const variable = globalScope.set.get(ident.name);
24-
25-
variable.writeable = false;
26-
27-
// "through" contains all references whose definition cannot be found
28-
// so we need to update references and remove the ones that were added
29-
globalScope.through.splice(ii, 1);
30-
ref.resolved = variable;
31-
variable.references.push(ref);
6+
const makeDefined = (variableName) => {
7+
// Add the variable to the global scope
8+
globalScope.through = globalScope.through.filter((ref) => {
9+
if (ref.identifier.name === variableName) {
10+
globalScope.variables.push({
11+
name: variableName,
12+
identifiers: [ref.identifier],
13+
references: [ref],
14+
defs: [],
15+
});
16+
return false;
3217
}
33-
}
18+
return true;
19+
});
3420
};
3521

3622
// NOTE: For future contributors, if you ever need to add support for a new identifier,
3723
// use `Identifier(node) {}` to find out which identifiers should be handled.
3824
return {
3925
ClassImplements(node) {
40-
makeDefined(node.id);
26+
makeDefined(node.id.name);
4127
},
4228
DeclareInterface(node) {
43-
makeDefined(node.id);
29+
makeDefined(node.id.name);
4430
},
4531
DeclareTypeAlias(node) {
46-
makeDefined(node.id);
32+
makeDefined(node.id.name);
4733
},
4834
EnumDeclaration(node) {
49-
makeDefined(node.id);
35+
makeDefined(node.id.name);
5036
},
5137
EnumDefaultedMember(node) {
52-
makeDefined(node.id);
38+
makeDefined(node.id.name);
5339
},
5440
EnumNumberMember(node) {
55-
makeDefined(node.id);
41+
makeDefined(node.id.name);
5642
},
5743
EnumStringMember(node) {
58-
makeDefined(node.id);
44+
makeDefined(node.id.name);
5945
},
6046
GenericTypeAnnotation(node) {
6147
if (node.id.type === 'Identifier') {
62-
makeDefined(node.id);
48+
makeDefined(node.id.name);
6349
} else if (node.id.type === 'QualifiedTypeIdentifier') {
6450
let qid;
6551

6652
qid = node.id;
67-
do {
53+
while (qid.qualification) {
6854
qid = qid.qualification;
69-
} while (qid.qualification);
55+
}
7056

71-
makeDefined(qid);
57+
makeDefined(qid.name);
7258
}
7359
},
7460

7561
// Can be removed once https://github.com/babel/babel-eslint/pull/696 is published
7662
OpaqueType(node) {
7763
if (node.id.type === 'Identifier') {
78-
makeDefined(node.id);
64+
makeDefined(node.id.name);
7965
}
8066
},
81-
Program() {
82-
globalScope = context.getScope();
67+
Program(node) {
68+
const newGetScope = context.sourceCode.getScope;
69+
if (newGetScope) {
70+
globalScope = context.sourceCode.getScope(node);
71+
} else {
72+
globalScope = context.getScope();
73+
}
8374
},
8475
TypeParameterDeclaration(node) {
8576
for (const param of node.params) {
86-
makeDefined(param);
77+
makeDefined(param.name);
8778
}
8879
},
8980
};

src/utilities/isFlowFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import isFlowFileAnnotation from './isFlowFileAnnotation';
99
* is set to false, the function returns true if the flag has @noflow also.
1010
*/
1111
export default (context, strict = true) => {
12-
const comments = context.getAllComments();
12+
const comments = context.getSourceCode().getAllComments();
1313

1414
if (!comments.length) {
1515
return false;

src/utilities/isNoFlowFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import isNoFlowFileAnnotation from './isNoFlowFileAnnotation';
99
* is set to false, the function returns true if the flag has @noflow also.
1010
*/
1111
export default (context, strict = true) => {
12-
const comments = context.getAllComments();
12+
const comments = context.getSourceCode().getAllComments();
1313

1414
if (!comments.length) {
1515
return false;

tests/rules/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ for (const ruleName of reportingRules) {
102102
'@babel/eslint-parser',
103103
'hermes-eslint',
104104
].forEach((parser) => {
105-
const babelParserOnlyRules = ['define-flow-type', 'use-flow-type'];
106-
107-
if (babelParserOnlyRules.includes(ruleName)) {
108-
return;
109-
}
110-
111105
const ruleTester = new RuleTester({
112106
parser: require.resolve(parser),
113107
});

0 commit comments

Comments
 (0)