Skip to content

Commit a1bb855

Browse files
committed
feat(oxlint-config): add no-enum, english-only-comments, suspense-fallback, time-datetime and no-classname-props rules
1 parent c8ce317 commit a1bb855

14 files changed

Lines changed: 387 additions & 1 deletion
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const LETTER_PATTERN = /\p{L}/gu
2+
// A letter of any script but Latin
3+
const NON_LATIN_LETTER_PATTERN = /(?=\p{L})\P{Script=Latin}/gu
4+
5+
// Most of its letters are of another script: a word quoted from another language, such as
6+
// "日本語", leaves an English comment English
7+
function isInAnotherLanguage(text) {
8+
const letters = text.match(LETTER_PATTERN)?.length ?? 0
9+
const nonLatinLetters = text.match(NON_LATIN_LETTER_PATTERN)?.length ?? 0
10+
11+
return nonLatinLetters * 2 > letters
12+
}
13+
14+
export default {
15+
meta: {
16+
type: 'suggestion',
17+
docs: {
18+
description: 'Require comments in English: most of a comment\'s letters are Latin.'
19+
},
20+
schema: []
21+
},
22+
create(context) {
23+
return {
24+
Program() {
25+
context.sourceCode.getAllComments().forEach((comment) => {
26+
if (isInAnotherLanguage(comment.value)) {
27+
context.report({
28+
loc: comment.loc,
29+
message: 'Write comments in English.'
30+
})
31+
}
32+
})
33+
}
34+
}
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
beforeAll
6+
} from 'vitest'
7+
import { lint } from './ruleTester.mock.js'
8+
9+
const rules = {
10+
'trigen/english-only-comments': 'error'
11+
}
12+
13+
describe('oxlint-config', () => {
14+
describe('plugin', () => {
15+
describe('english-only-comments', () => {
16+
let messages
17+
18+
beforeAll(async () => {
19+
messages = await lint(rules, {
20+
'cyrillic.ts': '// Сначала загрузка `fetchPlants`\nexport const value = 1\n',
21+
'quoted.ts': '// English, Română, 日本語, as the design lists them\nexport const value = 1\n',
22+
'japanese.ts': '/* 日本語の説明 */\nexport const value = 1\n',
23+
'english.ts': '// Loads first, naïvely: a café\'s menu → 3 items ✓\nexport const value = 1\n',
24+
'string.ts': "export const value = 'Привет'\n"
25+
})
26+
})
27+
28+
it('should report a comment in Cyrillic', () => {
29+
expect(messages['cyrillic.ts']).toEqual(['Write comments in English.'])
30+
})
31+
32+
it('should report a comment in another script', () => {
33+
expect(messages['japanese.ts']).toEqual(['Write comments in English.'])
34+
})
35+
36+
it('should allow an English comment quoting words of another script', () => {
37+
expect(messages['quoted.ts']).toEqual([])
38+
})
39+
40+
it('should allow Latin letters with diacritics and symbols', () => {
41+
expect(messages['english.ts']).toEqual([])
42+
})
43+
44+
it('should leave strings alone', () => {
45+
expect(messages['string.ts']).toEqual([])
46+
})
47+
})
48+
})
49+
})

‎packages/oxlint-config/src/plugin/index.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import englishOnlyCommentsRule from './english-only-comments.js'
12
import extensionsRule from './extensions.js'
23
import flatLayersRule from './flat-layers.js'
34
import funcStyleRule from './func-style.js'
@@ -6,14 +7,19 @@ import memberOrderingRule from './member-ordering.js'
67
import namedExportOrderRule from './named-export-order.js'
78
import namedImportOrderRule from './named-import-order.js'
89
import namingConventionRule from './naming-convention.js'
10+
import noClassnamePropsRule from './no-classname-props.js'
11+
import noEnumRule from './no-enum.js'
912
import noManualMemoRule from './no-manual-memo.js'
13+
import suspenseFallbackRule from './suspense-fallback.js'
14+
import timeDatetimeRule from './time-datetime.js'
1015
import typeImportStyleRule from './type-import-style.js'
1116

1217
export default {
1318
meta: {
1419
name: 'trigen'
1520
},
1621
rules: {
22+
'english-only-comments': englishOnlyCommentsRule,
1723
extensions: extensionsRule,
1824
'flat-layers': flatLayersRule,
1925
'func-style': funcStyleRule,
@@ -22,7 +28,11 @@ export default {
2228
'named-export-order': namedExportOrderRule,
2329
'named-import-order': namedImportOrderRule,
2430
'naming-convention': namingConventionRule,
31+
'no-classname-props': noClassnamePropsRule,
32+
'no-enum': noEnumRule,
2533
'no-manual-memo': noManualMemoRule,
34+
'suspense-fallback': suspenseFallbackRule,
35+
'time-datetime': timeDatetimeRule,
2636
'type-import-style': typeImportStyleRule
2737
}
2838
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// A class prop for a part, such as `titleClassName`: `className` itself is the root's
2+
const PART_CLASS_NAME_PATTERN = /^[a-z][a-zA-Z\d]*ClassName$/
3+
4+
export default {
5+
meta: {
6+
type: 'suggestion',
7+
docs: {
8+
description: 'Disallow a class prop for an inner part, such as `titleClassName`.'
9+
},
10+
schema: []
11+
},
12+
create(context) {
13+
return {
14+
JSXAttribute(node) {
15+
const name = node.name.type === 'JSXIdentifier' ? node.name.name : null
16+
17+
if (name && PART_CLASS_NAME_PATTERN.test(name)) {
18+
context.report({
19+
node: node.name,
20+
message: `Make the part a component of its own with its own \`className\` instead of \`${name}\`.`
21+
})
22+
}
23+
}
24+
}
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
beforeAll
6+
} from 'vitest'
7+
import { lint } from './ruleTester.mock.js'
8+
9+
const rules = {
10+
'trigen/no-classname-props': 'error'
11+
}
12+
13+
describe('oxlint-config', () => {
14+
describe('plugin', () => {
15+
describe('no-classname-props', () => {
16+
let messages
17+
18+
beforeAll(async () => {
19+
messages = await lint(rules, {
20+
'part.tsx': "export const view = <Card titleClassName='title'/>\n",
21+
'root.tsx': "export const view = <Card className='card'/>\n"
22+
})
23+
})
24+
25+
it('should report a class prop for a part', () => {
26+
expect(messages['part.tsx']).toEqual(['Make the part a component of its own with its own `className` instead of `titleClassName`.'])
27+
})
28+
29+
it('should allow the root\'s className', () => {
30+
expect(messages['root.tsx']).toEqual([])
31+
})
32+
})
33+
})
34+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default {
2+
meta: {
3+
type: 'problem',
4+
docs: {
5+
description: 'Disallow a TypeScript `enum`, which compiles to an object a bundler cannot drop.'
6+
},
7+
schema: []
8+
},
9+
create(context) {
10+
return {
11+
TSEnumDeclaration(node) {
12+
// A `const enum` is `oxc/no-const-enum`'s
13+
if (node.const) {
14+
return
15+
}
16+
17+
context.report({
18+
node: node.id,
19+
message: 'Do not use `enum`: it compiles to an object a bundler cannot drop. Use constants or a union of literals.'
20+
})
21+
}
22+
}
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
beforeAll
6+
} from 'vitest'
7+
import { lint } from './ruleTester.mock.js'
8+
9+
const rules = {
10+
'trigen/no-enum': 'error'
11+
}
12+
13+
describe('oxlint-config', () => {
14+
describe('plugin', () => {
15+
describe('no-enum', () => {
16+
let messages
17+
18+
beforeAll(async () => {
19+
messages = await lint(rules, {
20+
'enum.ts': 'export enum Size {\n Small,\n Large\n}\n',
21+
'const-enum.ts': 'export const enum Size {\n Small,\n Large\n}\n',
22+
'constants.ts': "export const SIZES = ['small', 'large'] as const\n"
23+
})
24+
})
25+
26+
it('should report an enum', () => {
27+
expect(messages['enum.ts']).toEqual(['Do not use `enum`: it compiles to an object a bundler cannot drop. Use constants or a union of literals.'])
28+
})
29+
30+
it('should leave a const enum to its own rule', () => {
31+
expect(messages['const-enum.ts']).toEqual([])
32+
})
33+
34+
it('should allow constants', () => {
35+
expect(messages['constants.ts']).toEqual([])
36+
})
37+
})
38+
})
39+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function isSuspense(name) {
2+
return (name.type === 'JSXIdentifier' && name.name === 'Suspense')
3+
|| (name.type === 'JSXMemberExpression' && name.property.name === 'Suspense')
4+
}
5+
6+
function isEmpty(value) {
7+
const expression = value?.type === 'JSXExpressionContainer' ? value.expression : null
8+
9+
return (expression?.type === 'Literal' && expression.value === null)
10+
|| (expression?.type === 'Identifier' && expression.name === 'undefined')
11+
}
12+
13+
export default {
14+
meta: {
15+
type: 'problem',
16+
docs: {
17+
description: 'Require `Suspense` to draw the shape of what is coming in its `fallback`.'
18+
},
19+
schema: []
20+
},
21+
create(context) {
22+
return {
23+
JSXOpeningElement(node) {
24+
if (!isSuspense(node.name) || node.attributes.some(attribute => attribute.type === 'JSXSpreadAttribute')) {
25+
return
26+
}
27+
28+
const fallback = node.attributes.find(attribute => attribute.name?.name === 'fallback')
29+
30+
if (!fallback || isEmpty(fallback.value)) {
31+
context.report({
32+
node: fallback ?? node,
33+
message: 'Give `Suspense` the skeleton of what is coming as its `fallback`: an empty one leaves a hole that fills in late.'
34+
})
35+
}
36+
}
37+
}
38+
}
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
beforeAll
6+
} from 'vitest'
7+
import { lint } from './ruleTester.mock.js'
8+
9+
const rules = {
10+
'trigen/suspense-fallback': 'error'
11+
}
12+
const message = 'Give `Suspense` the skeleton of what is coming as its `fallback`: an empty one leaves a hole that fills in late.'
13+
14+
describe('oxlint-config', () => {
15+
describe('plugin', () => {
16+
describe('suspense-fallback', () => {
17+
let messages
18+
19+
beforeAll(async () => {
20+
messages = await lint(rules, {
21+
'missing.tsx': "import { Suspense } from 'react'\n\nexport const view = <Suspense><div/></Suspense>\n",
22+
'null.tsx': "import { Suspense } from 'react'\n\nexport const view = <Suspense fallback={null}><div/></Suspense>\n",
23+
'member.tsx': "import React from 'react'\n\nexport const view = <React.Suspense fallback={undefined}><div/></React.Suspense>\n",
24+
'shape.tsx': "import { Suspense } from 'react'\n\nexport const view = <Suspense fallback={<span/>}><div/></Suspense>\n",
25+
'spread.tsx': "import { Suspense } from 'react'\n\nexport const view = (props: object) => <Suspense {...props}><div/></Suspense>\n"
26+
})
27+
})
28+
29+
it('should report a Suspense without a fallback', () => {
30+
expect(messages['missing.tsx']).toEqual([message])
31+
})
32+
33+
it('should report a null fallback', () => {
34+
expect(messages['null.tsx']).toEqual([message])
35+
})
36+
37+
it('should report an undefined fallback of React.Suspense', () => {
38+
expect(messages['member.tsx']).toEqual([message])
39+
})
40+
41+
it('should allow a fallback that draws a shape', () => {
42+
expect(messages['shape.tsx']).toEqual([])
43+
})
44+
45+
it('should leave a Suspense whose props come through a spread', () => {
46+
expect(messages['spread.tsx']).toEqual([])
47+
})
48+
})
49+
})
50+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default {
2+
meta: {
3+
type: 'problem',
4+
docs: {
5+
description: 'Require `dateTime` on a `<time>` element.'
6+
},
7+
schema: []
8+
},
9+
create(context) {
10+
return {
11+
JSXOpeningElement(node) {
12+
if (
13+
node.name.type !== 'JSXIdentifier'
14+
|| node.name.name !== 'time'
15+
|| node.attributes.some(attribute => attribute.type === 'JSXSpreadAttribute' || attribute.name?.name === 'dateTime')
16+
) {
17+
return
18+
}
19+
20+
context.report({
21+
node,
22+
message: 'Give `<time>` its `dateTime`, the value machines read.'
23+
})
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)