Skip to content

Commit

Permalink
feat: support union type
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen committed May 15, 2022
1 parent 91084b6 commit a885683
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
5 changes: 0 additions & 5 deletions lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ function isValidTypeScriptAnnotation(esTreeNodeToTSNodeMap, typeChecker, node) {
const tsNode = esTreeNodeToTSNodeMap.get(node);
const typeObj = typeChecker.getTypeAtLocation(tsNode.parent);

// var a: 'abc' = 'abc'
if (typeObj.isStringLiteral()) {
return true;
}

// var a: 'abc' | 'name' = 'abc'
if (typeObj.isUnion()) {
const found = typeObj.types.some(item => {
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/fixtures/valid-typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare module 'country-emoji' {}
var a: Element['nodeName'];
var a3: Omit<T, 'af'>;

function Button({ t = 'name' }: { t: string }) {}
// var aa: 'abc' = 'abc';
var a4: 'abc' | 'name' | undefined = 'abc';

// type T = { name: 'b' };
// var a5: T = { name: 'b' };

enum T2 {
howard = 1,
'a b' = 2,
}
var a6 = T2['howard'];

function Button2({ t = 'name' }: { t: 'name' }) {}

type Ta = { t?: 'name' | 'abc' };
function Button3({ t = 'name' }: Ta) {}
25 changes: 14 additions & 11 deletions tests/lib/rules/no-literal-string/typescript.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const RuleTester = require('eslint').RuleTester;
const path = require('path');
const rule = require('../../../../lib/rules/no-literal-string');
const testFile = require('../../helpers/testFile');

const tsTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
Expand All @@ -18,23 +19,26 @@ tsTester.run('no-literal-string: typescript', rule, {
filename: 'a.jsx',
},
{
code: `declare module 'country-emoji' {}`,
...testFile('valid-typescript.ts'),
options: [{ mode: 'all' }],
},
{
code: '<div className="hello"></div>',
options: [{ mode: 'all' }],
filename: 'a.tsx',
},
{ code: "var a: Element['nodeName']" },
{ code: "var a: Omit<T, 'af'>" },
{ code: "function Button({ t= 'name' }: {t: string}){} " },
{ 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, 'a b'=2} ; var a = T['howard']" },
{ code: "function Button({ t= 'name' }: {t: 'name'}){} " },
{ code: "type T ={t?:'name'|'abc'};function Button({t='name'}:T){}" },
],
invalid: [
{
code: 'let a:string; a="hello"',
options: [{ mode: 'all' }],
errors: 1,
},
{
code: 'const a="foobar"',
options: [{ mode: 'all' }],
errors: 1,
},
{
code: '<>foo123</>',
filename: 'a.tsx',
Expand All @@ -58,4 +62,3 @@ tsTester.run('no-literal-string: typescript', rule, {
},
],
});
// ────────────────────────────────────────────────────────────────────────────────

0 comments on commit a885683

Please sign in to comment.