Skip to content

Commit

Permalink
Add support for enum type
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcnunes committed Oct 29, 2021
1 parent 7989036 commit b49ff95
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -21,9 +21,11 @@
"@babel/register": "^7.15.3",
"ajv": "^8.6.3",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-transform-flow-enums": "^0.0.2",
"eslint": "^8.1.0",
"eslint-config-canonical": "^32.15.0",
"eslint-plugin-eslint-plugin": "^4.0.2",
"flow-enums-runtime": "^0.0.6",
"gitdown": "^3.1.4",
"glob": "^7.2.0",
"husky": "^7.0.4",
Expand Down
19 changes: 19 additions & 0 deletions src/rules/defineFlowType.js
Expand Up @@ -34,6 +34,9 @@ const create = (context) => {
};

return {
// 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.

ClassImplements (node) {
makeDefined(node.id);
},
Expand Down Expand Up @@ -72,6 +75,22 @@ const create = (context) => {
makeDefined(param);
}
},

EnumDeclaration(node) {
makeDefined(node.id);
},

EnumDefaultedMember(node) {
makeDefined(node.id);
},

EnumStringMember(node) {
makeDefined(node.id);
},

EnumNumberMember(node) {
makeDefined(node.id);
},
};
};

Expand Down
36 changes: 34 additions & 2 deletions tests/rules/assertions/defineFlowType.js
Expand Up @@ -150,6 +150,32 @@ type Foo = $ReadOnly<{}>`,
},
},
},

// Enum types
{
code: 'enum Status { Active, Paused }',
errors: [
'\'Status\' is not defined.',
'\'Active\' is not defined.',
'\'Paused\' is not defined.',
],
},
{
code: `enum Status { Active = 'active', Paused = 'paused' }`,
errors: [
'\'Status\' is not defined.',
'\'Active\' is not defined.',
'\'Paused\' is not defined.',
],
},
{
code: `enum Status { Active = 1, Paused = 2 }`,
errors: [
'\'Status\' is not defined.',
'\'Active\' is not defined.',
'\'Paused\' is not defined.',
],
},
];

const ALWAYS_INVALID = [
Expand Down Expand Up @@ -205,7 +231,10 @@ const ALWAYS_VALID = [
parser: require.resolve('@babel/eslint-parser'),
parserOptions: {
babelOptions: {
plugins: ['@babel/plugin-syntax-flow'],
plugins: [
'babel-plugin-transform-flow-enums',
'@babel/plugin-syntax-flow',
],
},
requireConfigFile: false,
},
Expand All @@ -222,7 +251,10 @@ const ALWAYS_VALID = [
parser: require.resolve('@babel/eslint-parser'),
parserOptions: {
babelOptions: {
plugins: ['@babel/plugin-syntax-flow'],
plugins: [
'babel-plugin-transform-flow-enums',
'@babel/plugin-syntax-flow',
],
},
requireConfigFile: false,
},
Expand Down
1 change: 1 addition & 0 deletions tests/rules/index.js
Expand Up @@ -12,6 +12,7 @@ const ruleTester = new RuleTester({
parserOptions: {
babelOptions: {
plugins: [
'babel-plugin-transform-flow-enums',
'@babel/plugin-transform-react-jsx',
'@babel/plugin-syntax-flow',
],
Expand Down

0 comments on commit b49ff95

Please sign in to comment.