-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new
ts/quote-props
rule (#275)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
19 changed files
with
225 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
description: 'Require quotes around object literal, type literal, interfaces and enums property names.' | ||
--- | ||
|
||
This rule extends the base [`quote-props`](/rules/js/quote-props) rule. | ||
It adds support for TypeScript's type literals, interfaces and enums. |
78 changes: 78 additions & 0 deletions
78
packages/eslint-plugin-ts/rules/quote-props/quote-props.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { RuleTester } from '@typescript-eslint/rule-tester' | ||
|
||
import rule from './quote-props' | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}) | ||
|
||
ruleTester.run('quote-props', rule, { | ||
valid: [ | ||
'type x = { "a": 1, b(): void, "c"(): void }', | ||
'interface x { "a": 1, b(): void, "c"(): void }', | ||
'enum x { "a" }', | ||
|
||
{ code: 'type x = { a: 1, "b-b": 1 }', options: ['as-needed'] }, | ||
{ code: 'interface x { a: 1, "b-b": 1 }', options: ['as-needed'] }, | ||
{ code: 'enum x { a = 1, "b-b" = 2 }', options: ['as-needed'] }, | ||
|
||
{ code: 'type x = { "a": 1, "b-b": 1 }', options: ['consistent-as-needed'] }, | ||
{ code: 'interface x { "a": 1, "b-b": 1 }', options: ['consistent-as-needed'] }, | ||
{ code: 'enum x { "a" = 1, "b-b" = 2 }', options: ['consistent-as-needed'] }, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'type x = { a: 1 }', | ||
output: 'type x = { "a": 1 }', | ||
errors: [{ messageId: 'unquotedPropertyFound' }], | ||
}, | ||
{ | ||
code: 'interface x { a: 1 }', | ||
output: 'interface x { "a": 1 }', | ||
errors: [{ messageId: 'unquotedPropertyFound' }], | ||
}, | ||
{ | ||
code: 'enum x { a = 1 }', | ||
output: 'enum x { "a" = 1 }', | ||
errors: [{ messageId: 'unquotedPropertyFound' }], | ||
}, | ||
|
||
{ | ||
code: 'type x = { "a": 1 }', | ||
output: 'type x = { a: 1 }', | ||
options: ['as-needed'], | ||
errors: [{ messageId: 'unnecessarilyQuotedProperty' }], | ||
}, | ||
{ | ||
code: 'interface x { "a": 1, "b-b": 1 }', | ||
output: 'interface x { a: 1, "b-b": 1 }', | ||
options: ['as-needed'], | ||
errors: [{ messageId: 'unnecessarilyQuotedProperty' }], | ||
}, | ||
{ | ||
code: 'enum x { "a" = 1, "b-b" = 2 }', | ||
output: 'enum x { a = 1, "b-b" = 2 }', | ||
options: ['as-needed'], | ||
errors: [{ messageId: 'unnecessarilyQuotedProperty' }], | ||
}, | ||
|
||
{ | ||
code: 'type x = { a: 1, "b-b": 1 }', | ||
output: 'type x = { "a": 1, "b-b": 1 }', | ||
options: ['consistent-as-needed'], | ||
errors: [{ messageId: 'inconsistentlyQuotedProperty' }], | ||
}, | ||
{ | ||
code: 'interface x { a: 1, "b-b": 1 }', | ||
output: 'interface x { "a": 1, "b-b": 1 }', | ||
options: ['consistent-as-needed'], | ||
errors: [{ messageId: 'inconsistentlyQuotedProperty' }], | ||
}, | ||
{ | ||
code: 'enum x { a = 1, "b-b" = 2 }', | ||
output: 'enum x { "a" = 1, "b-b" = 2 }', | ||
options: ['consistent-as-needed'], | ||
errors: [{ messageId: 'inconsistentlyQuotedProperty' }], | ||
}, | ||
], | ||
}) |
79 changes: 79 additions & 0 deletions
79
packages/eslint-plugin-ts/rules/quote-props/quote-props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils' | ||
|
||
import { createRule } from '../../utils' | ||
import { getESLintCoreRule } from '../../utils/getESLintCoreRule' | ||
import type { MessageIds, RuleOptions } from './types' | ||
|
||
const baseRule = getESLintCoreRule('quote-props') | ||
|
||
export default createRule<RuleOptions, MessageIds>({ | ||
name: 'quote-props', | ||
meta: { | ||
...baseRule.meta, | ||
docs: { | ||
description: 'Require quotes around object literal, type literal, interfaces and enums property names', | ||
extendsBaseRule: true, | ||
}, | ||
}, | ||
defaultOptions: ['always'], | ||
create(context) { | ||
const rules = baseRule.create(context) | ||
|
||
return { | ||
...rules, | ||
TSPropertySignature(node) { | ||
return rules.Property!({ | ||
...node, | ||
type: AST_NODE_TYPES.Property, | ||
shorthand: false, | ||
method: false, | ||
kind: 'init', | ||
value: null as any, | ||
}) | ||
}, | ||
TSMethodSignature(node) { | ||
return rules.Property!({ | ||
...node, | ||
type: AST_NODE_TYPES.Property, | ||
shorthand: false, | ||
method: true, | ||
kind: 'init', | ||
value: null as any, | ||
}) | ||
}, | ||
TSEnumMember(node) { | ||
return rules.Property!({ | ||
...node, | ||
type: AST_NODE_TYPES.Property, | ||
key: node.id as any, | ||
optional: false, | ||
shorthand: false, | ||
method: false, | ||
kind: 'init', | ||
value: null as any, | ||
}) | ||
}, | ||
TSTypeLiteral(node) { | ||
return rules.ObjectExpression!({ | ||
...node, | ||
type: AST_NODE_TYPES.ObjectExpression, | ||
properties: node.members as any, | ||
}) | ||
}, | ||
TSInterfaceBody(node) { | ||
return rules.ObjectExpression!({ | ||
...node, | ||
type: AST_NODE_TYPES.ObjectExpression, | ||
properties: node.body as any, | ||
}) | ||
}, | ||
TSEnumDeclaration(node) { | ||
return rules.ObjectExpression!({ | ||
...node, | ||
type: AST_NODE_TYPES.ObjectExpression, | ||
properties: node.members.map(member => ({ ...member, key: member.id })) as any, | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* GENERATED, DO NOT EDIT DIRECTLY */ | ||
|
||
export type Schema0 = | ||
| [] | ||
| ['always' | 'as-needed' | 'consistent' | 'consistent-as-needed'] | ||
| [] | ||
| ['always' | 'as-needed' | 'consistent' | 'consistent-as-needed'] | ||
| [ | ||
'always' | 'as-needed' | 'consistent' | 'consistent-as-needed', | ||
{ | ||
keywords?: boolean | ||
unnecessary?: boolean | ||
numbers?: boolean | ||
}, | ||
] | ||
|
||
export type RuleOptions = Schema0 | ||
export type MessageIds = 'requireQuotesDueToReservedWord' | 'inconsistentlyQuotedProperty' | 'unnecessarilyQuotedProperty' | 'unquotedReservedProperty' | 'unquotedNumericProperty' | 'unquotedPropertyFound' | 'redundantQuoting' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,7 +107,7 @@ let a, b, c, d, foo; | |
|
||
if (a || | ||
b || | ||
c || d | ||
c || d | ||
) { | ||
|
||
foo(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ export function HelloWorld({ | |
{ (silent) ? '.' : '!'} | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,4 @@ export function jsx2() { | |
</p> | ||
</a> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ let counter = ref<number | string>(0) | |
const incrementCounter = () => { | ||
counter.value++ | ||
} | ||
</script> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ let counter = ref(0) | |
const incrementCounter = () => { | ||
counter.value++ | ||
} | ||
</script> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ export function HelloWorld({ | |
{ (silent) ? "." : "!"} | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ let counter = ref<number | string>(0); | |
const incrementCounter = () => { | ||
counter.value++; | ||
}; | ||
</script> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ let counter = ref(0); | |
const incrementCounter = () => { | ||
counter.value++; | ||
}; | ||
</script> | ||
</script> |