Skip to content

Commit

Permalink
Fix __defineGeneric not a function (#50)
Browse files Browse the repository at this point in the history
* simulate the error

* have many tests passing now

* update get comments syntax

* tests all passing

* cleanup
  • Loading branch information
Brianzchen authored Jan 27, 2024
1 parent 904a3cf commit 7e41249
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 134 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"ajv": "^8.6.3",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-transform-flow-enums": "^0.0.2",
"eslint": "^8.46.0",
"eslint": "^8.56.0",
"eslint-config-airbnb": "^19.0.2",
"eslint-config-bzc": "^1.0.5",
"eslint-plugin-fb-flow": "^0.0.4",
Expand All @@ -54,15 +54,15 @@
"flow-bin": "^0.167.1",
"flow-copy-source": "^2.0.9",
"glob": "^7.2.0",
"hermes-eslint": "^0.15.0",
"hermes-eslint": "^0.18.2",
"husky": "^7.0.4",
"jest": "^27.4.5",
"lint-staged": "^12.1.2",
"mocha": "^10.1.0",
"rimraf": "^3.0.2"
},
"peerDependencies": {
"eslint": "^8.1.0",
"eslint": "^8.56.0",
"hermes-eslint": ">=0.15.0"
},
"keywords": [
Expand Down
75 changes: 33 additions & 42 deletions src/rules/defineFlowType.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,78 @@ const schema = [];
const create = (context) => {
let globalScope;

// do nearly the same thing that eslint does for config globals
// https://github.com/eslint/eslint/blob/v2.0.0/lib/eslint.js#L118-L194
const makeDefined = (ident) => {
let ii;

// start from the right since we're going to remove items from the array
for (ii = globalScope.through.length - 1; ii >= 0; ii--) {
const ref = globalScope.through[ii];

if (ref.identifier.name === ident.name) {
// use "__defineGeneric" since we don't have a reference to "escope.Variable"

globalScope.__defineGeneric(
ident.name,
globalScope.set,
globalScope.variables,
);
const variable = globalScope.set.get(ident.name);

variable.writeable = false;

// "through" contains all references whose definition cannot be found
// so we need to update references and remove the ones that were added
globalScope.through.splice(ii, 1);
ref.resolved = variable;
variable.references.push(ref);
const makeDefined = (variableName) => {
// Add the variable to the global scope
globalScope.through = globalScope.through.filter((ref) => {
if (ref.identifier.name === variableName) {
globalScope.variables.push({
name: variableName,
identifiers: [ref.identifier],
references: [ref],
defs: [],
});
return false;
}
}
return true;
});
};

// NOTE: For future contributors, if you ever need to add support for a new identifier,
// use `Identifier(node) {}` to find out which identifiers should be handled.
return {
ClassImplements(node) {
makeDefined(node.id);
makeDefined(node.id.name);
},
DeclareInterface(node) {
makeDefined(node.id);
makeDefined(node.id.name);
},
DeclareTypeAlias(node) {
makeDefined(node.id);
makeDefined(node.id.name);
},
EnumDeclaration(node) {
makeDefined(node.id);
makeDefined(node.id.name);
},
EnumDefaultedMember(node) {
makeDefined(node.id);
makeDefined(node.id.name);
},
EnumNumberMember(node) {
makeDefined(node.id);
makeDefined(node.id.name);
},
EnumStringMember(node) {
makeDefined(node.id);
makeDefined(node.id.name);
},
GenericTypeAnnotation(node) {
if (node.id.type === 'Identifier') {
makeDefined(node.id);
makeDefined(node.id.name);
} else if (node.id.type === 'QualifiedTypeIdentifier') {
let qid;

qid = node.id;
do {
while (qid.qualification) {
qid = qid.qualification;
} while (qid.qualification);
}

makeDefined(qid);
makeDefined(qid.name);
}
},

// Can be removed once https://github.com/babel/babel-eslint/pull/696 is published
OpaqueType(node) {
if (node.id.type === 'Identifier') {
makeDefined(node.id);
makeDefined(node.id.name);
}
},
Program() {
globalScope = context.getScope();
Program(node) {
const newGetScope = context.sourceCode.getScope;
if (newGetScope) {
globalScope = context.sourceCode.getScope(node);
} else {
globalScope = context.getScope();
}
},
TypeParameterDeclaration(node) {
for (const param of node.params) {
makeDefined(param);
makeDefined(param.name);
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/isFlowFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import isFlowFileAnnotation from './isFlowFileAnnotation';
* is set to false, the function returns true if the flag has @noflow also.
*/
export default (context, strict = true) => {
const comments = context.getAllComments();
const comments = context.getSourceCode().getAllComments();

if (!comments.length) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/isNoFlowFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import isNoFlowFileAnnotation from './isNoFlowFileAnnotation';
* is set to false, the function returns true if the flag has @noflow also.
*/
export default (context, strict = true) => {
const comments = context.getAllComments();
const comments = context.getSourceCode().getAllComments();

if (!comments.length) {
return false;
Expand Down
6 changes: 0 additions & 6 deletions tests/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ for (const ruleName of reportingRules) {
'@babel/eslint-parser',
'hermes-eslint',
].forEach((parser) => {
const babelParserOnlyRules = ['define-flow-type', 'use-flow-type'];

if (babelParserOnlyRules.includes(ruleName)) {
return;
}

const ruleTester = new RuleTester({
parser: require.resolve(parser),
});
Expand Down
Loading

0 comments on commit 7e41249

Please sign in to comment.