Skip to content

Commit

Permalink
feat: support to access enum value through string like Enum['key']
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen committed Apr 17, 2020
1 parent 5e6dda3 commit db68147
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = {
]
},

create: function(context) {
create(context) {
// variables should be defined here
const {
parserServices,
Expand Down Expand Up @@ -256,6 +256,11 @@ module.exports = {
visited.add(node);
},

'MemberExpression > Literal'(node) {
// allow Enum['value']
visited.add(node);
},

'Literal:exit'(node) {
// visited and passed linting
if (visited.has(node)) return;
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ tsTester.run('no-literal-string', rule, {
{ code: `var a: 'abc' = 'abc'` },
{ code: `var a: 'abc' | 'name' | undefined= 'abc'` },
{ code: "type T = {name: 'b'} ; var a: T = {name: 'b'}" },
{ code: "enum T {howard=1} ; var a = T['howard']" },
{ code: "function Button({ t= 'name' }: {t: 'name'}){} " },
{ code: "type T ={t?:'name'|'abc'};function Button({t='name'}:T){}" }
],
Expand Down

0 comments on commit db68147

Please sign in to comment.