Skip to content

Commit

Permalink
feat: 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 3d613a9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
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
15 changes: 15 additions & 0 deletions src/rules/defineFlowType.js
Original file line number Diff line number Diff line change
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 All @@ -43,6 +46,18 @@ const create = (context) => {
DeclareTypeAlias (node) {
makeDefined(node.id);
},
EnumDeclaration (node) {
makeDefined(node.id);
},
EnumDefaultedMember (node) {
makeDefined(node.id);
},
EnumNumberMember (node) {
makeDefined(node.id);
},
EnumStringMember (node) {
makeDefined(node.id);
},
GenericTypeAnnotation (node) {
if (node.id.type === 'Identifier') {
makeDefined(node.id);
Expand Down
38 changes: 36 additions & 2 deletions tests/rules/assertions/defineFlowType.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,34 @@ type Foo = $ReadOnly<{}>`,
},
},
},

// Enum types
{
code: 'enum Status { Active, Paused }',
errors: [
'\'Status\' is not defined.',
'\'Active\' is not defined.',
'\'Paused\' is not defined.',
],
},
{
// eslint-disable-next-line quotes
code: `enum Status { Active = 'active', Paused = 'paused' }`,
errors: [
'\'Status\' is not defined.',
'\'Active\' is not defined.',
'\'Paused\' is not defined.',
],
},
{
// eslint-disable-next-line quotes
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 +233,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 +253,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
Original file line number Diff line number Diff line change
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 3d613a9

Please sign in to comment.