Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for enum type #512

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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