eslint-plugin-hyoban 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2,10 +2,18 @@ Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
3
  var utils = require('@typescript-eslint/utils');
4
4
 
5
- var version = "0.5.0";
5
+ var version = "0.5.2";
6
6
 
7
7
  const createRule = utils.ESLintUtils.RuleCreator((name)=>`https://github.com/hyoban/eslint-plugin-hyoban/blob/main/src/${name}.ts`);
8
8
 
9
+ function getCorrectedSource(value) {
10
+ if (!value) return 'null';
11
+ if (value.type === 'JSONLiteral') return value.raw;
12
+ if (value.type === 'JSONArrayExpression') return `[${value.elements.map((element)=>getCorrectedSource(element)).join(', ')}]`;
13
+ if (value.type === 'JSONObjectExpression') {
14
+ return `{ ${value.properties.filter((property)=>property.key.type !== 'JSONIdentifier').map((property)=>`${property.key.raw}: ${getCorrectedSource(property.value)}`).join(', ')} }`;
15
+ }
16
+ }
9
17
  const rule$2 = createRule({
10
18
  name: 'jsonc-inline-spacing',
11
19
  meta: {
@@ -24,13 +32,12 @@ const rule$2 = createRule({
24
32
  return {
25
33
  JSONObjectExpression (node) {
26
34
  const { properties } = node;
27
- const shouldIgnore = properties.length === 0 || node.loc.start.line !== node.loc.end.line || !properties.every((property)=>property.key.type !== 'JSONIdentifier' && property.key.raw && property.value.type === 'JSONLiteral' && property.value.raw && property?.loc.start.line === node?.loc.start.line);
35
+ const shouldIgnore = properties.length === 0 || node.loc.start.line !== node.loc.end.line || !properties.every((property)=>property.key.type !== 'JSONIdentifier' && property.key.raw && getCorrectedSource(property.value) && property?.loc.start.line === node?.loc.start.line);
28
36
  if (shouldIgnore) return;
29
37
  const source = context.sourceCode.getText(node);
30
38
  // @ts-expect-error it's fine
31
- const keys = properties.map((property)=>property.key.raw);
32
- // @ts-expect-error it's fine
33
- const values = properties.map((property)=>property.value.raw);
39
+ const keys = properties.map((property)=>property.key.raw.trim());
40
+ const values = properties.map((property)=>getCorrectedSource(property.value)?.trim());
34
41
  if (keys.length !== values.length) return;
35
42
  const correctedSource = `{ ${keys.map((key, i)=>`${key}: ${values[i]}`).join(', ')} }`;
36
43
  const needFix = source !== correctedSource;
@@ -79,7 +86,8 @@ const rule$1 = createRule({
79
86
  },
80
87
  messages: {
81
88
  jsxAttributeSpacing: 'Expected space before and after JSX attribute',
82
- noExtraSpaceJsxExpression: 'No extra space in jsx expression'
89
+ noExtraSpaceJsxExpression: 'No extra space in jsx expression',
90
+ arrowFunctionStartNewLine: 'Arrow function with a single expression in JSX Container should start on a new line'
83
91
  },
84
92
  schema: []
85
93
  },
@@ -87,7 +95,19 @@ const rule$1 = createRule({
87
95
  create (context) {
88
96
  function check(node, isExit) {
89
97
  const { expression } = node;
90
- if (expressionTypesNoCheck.has(expression.type) || expression.type === utils.AST_NODE_TYPES.ArrowFunctionExpression && expression.body.type !== utils.AST_NODE_TYPES.BlockStatement) return;
98
+ if (expressionTypesNoCheck.has(expression.type)) return;
99
+ if (expression.type === utils.AST_NODE_TYPES.ArrowFunctionExpression && expression.body.type !== utils.AST_NODE_TYPES.BlockStatement) {
100
+ // enforce multiple lines arrow function do not start with same line as jsx expression container
101
+ if (expression.loc.start.line === node.loc.start.line) {
102
+ context.report({
103
+ node: expression,
104
+ messageId: 'noExtraSpaceJsxExpression',
105
+ loc: expression.loc,
106
+ fix: (fixer)=>fixer.insertTextBefore(expression, '\n')
107
+ });
108
+ }
109
+ return;
110
+ }
91
111
  const containerRange = node.range;
92
112
  const expressionRange = expression.range;
93
113
  const noSpace = isExit ? containerRange[1] - expressionRange[1] === 1 : expressionRange[0] - containerRange[0] === 1;
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
2
2
 
3
- type MessageIds = 'jsxAttributeSpacing' | 'noExtraSpaceJsxExpression';
3
+ type MessageIds = 'jsxAttributeSpacing' | 'noExtraSpaceJsxExpression' | 'arrowFunctionStartNewLine';
4
4
 
5
5
  declare const _default: {
6
6
  meta: {
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
2
2
 
3
- type MessageIds = 'jsxAttributeSpacing' | 'noExtraSpaceJsxExpression';
3
+ type MessageIds = 'jsxAttributeSpacing' | 'noExtraSpaceJsxExpression' | 'arrowFunctionStartNewLine';
4
4
 
5
5
  declare const _default: {
6
6
  meta: {
package/dist/index.js CHANGED
@@ -1,9 +1,17 @@
1
1
  import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils';
2
2
 
3
- var version = "0.5.0";
3
+ var version = "0.5.2";
4
4
 
5
5
  const createRule = ESLintUtils.RuleCreator((name)=>`https://github.com/hyoban/eslint-plugin-hyoban/blob/main/src/${name}.ts`);
6
6
 
7
+ function getCorrectedSource(value) {
8
+ if (!value) return 'null';
9
+ if (value.type === 'JSONLiteral') return value.raw;
10
+ if (value.type === 'JSONArrayExpression') return `[${value.elements.map((element)=>getCorrectedSource(element)).join(', ')}]`;
11
+ if (value.type === 'JSONObjectExpression') {
12
+ return `{ ${value.properties.filter((property)=>property.key.type !== 'JSONIdentifier').map((property)=>`${property.key.raw}: ${getCorrectedSource(property.value)}`).join(', ')} }`;
13
+ }
14
+ }
7
15
  const rule$2 = createRule({
8
16
  name: 'jsonc-inline-spacing',
9
17
  meta: {
@@ -22,13 +30,12 @@ const rule$2 = createRule({
22
30
  return {
23
31
  JSONObjectExpression (node) {
24
32
  const { properties } = node;
25
- const shouldIgnore = properties.length === 0 || node.loc.start.line !== node.loc.end.line || !properties.every((property)=>property.key.type !== 'JSONIdentifier' && property.key.raw && property.value.type === 'JSONLiteral' && property.value.raw && property?.loc.start.line === node?.loc.start.line);
33
+ const shouldIgnore = properties.length === 0 || node.loc.start.line !== node.loc.end.line || !properties.every((property)=>property.key.type !== 'JSONIdentifier' && property.key.raw && getCorrectedSource(property.value) && property?.loc.start.line === node?.loc.start.line);
26
34
  if (shouldIgnore) return;
27
35
  const source = context.sourceCode.getText(node);
28
36
  // @ts-expect-error it's fine
29
- const keys = properties.map((property)=>property.key.raw);
30
- // @ts-expect-error it's fine
31
- const values = properties.map((property)=>property.value.raw);
37
+ const keys = properties.map((property)=>property.key.raw.trim());
38
+ const values = properties.map((property)=>getCorrectedSource(property.value)?.trim());
32
39
  if (keys.length !== values.length) return;
33
40
  const correctedSource = `{ ${keys.map((key, i)=>`${key}: ${values[i]}`).join(', ')} }`;
34
41
  const needFix = source !== correctedSource;
@@ -77,7 +84,8 @@ const rule$1 = createRule({
77
84
  },
78
85
  messages: {
79
86
  jsxAttributeSpacing: 'Expected space before and after JSX attribute',
80
- noExtraSpaceJsxExpression: 'No extra space in jsx expression'
87
+ noExtraSpaceJsxExpression: 'No extra space in jsx expression',
88
+ arrowFunctionStartNewLine: 'Arrow function with a single expression in JSX Container should start on a new line'
81
89
  },
82
90
  schema: []
83
91
  },
@@ -85,7 +93,19 @@ const rule$1 = createRule({
85
93
  create (context) {
86
94
  function check(node, isExit) {
87
95
  const { expression } = node;
88
- if (expressionTypesNoCheck.has(expression.type) || expression.type === AST_NODE_TYPES.ArrowFunctionExpression && expression.body.type !== AST_NODE_TYPES.BlockStatement) return;
96
+ if (expressionTypesNoCheck.has(expression.type)) return;
97
+ if (expression.type === AST_NODE_TYPES.ArrowFunctionExpression && expression.body.type !== AST_NODE_TYPES.BlockStatement) {
98
+ // enforce multiple lines arrow function do not start with same line as jsx expression container
99
+ if (expression.loc.start.line === node.loc.start.line) {
100
+ context.report({
101
+ node: expression,
102
+ messageId: 'noExtraSpaceJsxExpression',
103
+ loc: expression.loc,
104
+ fix: (fixer)=>fixer.insertTextBefore(expression, '\n')
105
+ });
106
+ }
107
+ return;
108
+ }
89
109
  const containerRange = node.range;
90
110
  const expressionRange = expression.range;
91
111
  const noSpace = isExit ? containerRange[1] - expressionRange[1] === 1 : expressionRange[0] - containerRange[0] === 1;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "eslint-plugin-hyoban",
3
3
  "type": "module",
4
- "version": "0.5.0",
4
+ "version": "0.5.2",
5
5
  "description": "Hyoban extended ESLint rules.",
6
6
  "author": {
7
7
  "name": "hyoban",