eslint-plugin-toml 1.2.1 → 1.3.0

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.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/lib/index.mjs +226 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -167,7 +167,9 @@ The rules with the following star :star: are included in the configs.
167
167
  | [toml/array-bracket-spacing](https://ota-meshi.github.io/eslint-plugin-toml/rules/array-bracket-spacing.html) | enforce consistent spacing inside array brackets | :wrench: | | :star: |
168
168
  | [toml/array-element-newline](https://ota-meshi.github.io/eslint-plugin-toml/rules/array-element-newline.html) | enforce line breaks between array elements | :wrench: | | :star: |
169
169
  | [toml/comma-style](https://ota-meshi.github.io/eslint-plugin-toml/rules/comma-style.html) | enforce consistent comma style in array | :wrench: | | :star: |
170
+ | [toml/inline-table-curly-newline](https://ota-meshi.github.io/eslint-plugin-toml/rules/inline-table-curly-newline.html) | enforce linebreaks after opening and before closing braces | :wrench: | | :star: |
170
171
  | [toml/inline-table-curly-spacing](https://ota-meshi.github.io/eslint-plugin-toml/rules/inline-table-curly-spacing.html) | enforce consistent spacing inside braces | :wrench: | | :star: |
172
+ | [toml/inline-table-key-value-newline](https://ota-meshi.github.io/eslint-plugin-toml/rules/inline-table-key-value-newline.html) | enforce placing inline table key-value pairs on separate lines | :wrench: | | :star: |
171
173
  | [toml/key-spacing](https://ota-meshi.github.io/eslint-plugin-toml/rules/key-spacing.html) | enforce consistent spacing between keys and values in key/value pairs | :wrench: | | :star: |
172
174
  | [toml/spaced-comment](https://ota-meshi.github.io/eslint-plugin-toml/rules/spaced-comment.html) | enforce consistent spacing after the `#` in a comment | :wrench: | | :star: |
173
175
  | [toml/table-bracket-spacing](https://ota-meshi.github.io/eslint-plugin-toml/rules/table-bracket-spacing.html) | enforce consistent spacing inside table brackets | :wrench: | | :star: |
package/lib/index.mjs CHANGED
@@ -1173,6 +1173,153 @@ var indent_default = createRule("indent", {
1173
1173
  }
1174
1174
  });
1175
1175
 
1176
+ //#endregion
1177
+ //#region src/rules/inline-table-curly-newline.ts
1178
+ var inline_table_curly_newline_default = createRule("inline-table-curly-newline", {
1179
+ meta: {
1180
+ docs: {
1181
+ description: "enforce linebreaks after opening and before closing braces",
1182
+ categories: ["standard"],
1183
+ extensionRule: "object-curly-newline"
1184
+ },
1185
+ type: "layout",
1186
+ fixable: "whitespace",
1187
+ schema: [{ oneOf: [{
1188
+ type: "string",
1189
+ enum: ["always", "never"]
1190
+ }, {
1191
+ type: "object",
1192
+ properties: {
1193
+ multiline: { type: "boolean" },
1194
+ minProperties: {
1195
+ type: "integer",
1196
+ minimum: 0
1197
+ },
1198
+ consistent: { type: "boolean" }
1199
+ },
1200
+ additionalProperties: false,
1201
+ minProperties: 1
1202
+ }] }],
1203
+ messages: {
1204
+ unexpectedLinebreakBeforeClosingBrace: "Unexpected line break before this closing brace.",
1205
+ unexpectedLinebreakAfterOpeningBrace: "Unexpected line break after this opening brace.",
1206
+ expectedLinebreakBeforeClosingBrace: "Expected a line break before this closing brace.",
1207
+ expectedLinebreakAfterOpeningBrace: "Expected a line break after this opening brace."
1208
+ }
1209
+ },
1210
+ create(context) {
1211
+ const sourceCode = context.sourceCode;
1212
+ if (!sourceCode.parserServices?.isTOML) return {};
1213
+ if (context.languageOptions.parserOptions?.tomlVersion) {
1214
+ const tomlVersion = context.languageOptions.parserOptions.tomlVersion.includes(".") && context.languageOptions.parserOptions.tomlVersion.split(".");
1215
+ if (tomlVersion && tomlVersion[0] === "1" && tomlVersion[1] === "0") return {};
1216
+ }
1217
+ /**
1218
+ * Normalizes a given option.
1219
+ * @param value An option value to parse.
1220
+ * @returns Normalized option object.
1221
+ */
1222
+ function normalizeOptions(value) {
1223
+ let multiline = false;
1224
+ let minProperties = Number.POSITIVE_INFINITY;
1225
+ let consistent = false;
1226
+ if (value) if (value === "always") minProperties = 0;
1227
+ else if (value === "never") minProperties = Number.POSITIVE_INFINITY;
1228
+ else {
1229
+ multiline = Boolean(value.multiline);
1230
+ minProperties = value.minProperties || Number.POSITIVE_INFINITY;
1231
+ consistent = Boolean(value.consistent);
1232
+ }
1233
+ else consistent = true;
1234
+ return {
1235
+ multiline,
1236
+ minProperties,
1237
+ consistent
1238
+ };
1239
+ }
1240
+ const options = normalizeOptions(context.options[0]);
1241
+ /**
1242
+ * Determines if ObjectExpression, ObjectPattern, ImportDeclaration, ExportNamedDeclaration, TSTypeLiteral or TSInterfaceBody
1243
+ * node needs to be checked for missing line breaks
1244
+ * @param node Node under inspection
1245
+ * @param options option specific to node type
1246
+ * @param first First object property
1247
+ * @param last Last object property
1248
+ * @returns `true` if node needs to be checked for missing line breaks
1249
+ */
1250
+ function areLineBreaksRequired(node, options, first, last) {
1251
+ const objectProperties = node.body;
1252
+ return objectProperties.length >= options.minProperties || options.multiline && objectProperties.length > 0 && !isTokenOnSameLine(last, first);
1253
+ }
1254
+ /**
1255
+ * Reports a given node if it violated this rule.
1256
+ * @param node A node to check. This is an ObjectExpression, ObjectPattern, ImportDeclaration, ExportNamedDeclaration, TSTypeLiteral or TSInterfaceBody node.
1257
+ */
1258
+ function check(node) {
1259
+ const openBrace = sourceCode.getFirstToken(node, (token) => token.value === "{");
1260
+ const closeBrace = sourceCode.getLastToken(node, (token) => token.value === "}");
1261
+ const firstTokenOrComment = sourceCode.getTokenAfter(openBrace, { includeComments: true });
1262
+ const lastTokenOrComment = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
1263
+ const needsLineBreaks = areLineBreaksRequired(node, options, firstTokenOrComment, lastTokenOrComment);
1264
+ const hasCommentsFirstToken = isCommentToken(firstTokenOrComment);
1265
+ const hasCommentsLastToken = isCommentToken(lastTokenOrComment);
1266
+ /**
1267
+ * Use tokens or comments to check multiline or not.
1268
+ * But use only tokens to check whether line breaks are needed.
1269
+ * This allows:
1270
+ * obj = { # eslint-disable-line foo
1271
+ * a: 1
1272
+ * }
1273
+ */
1274
+ const firstToken = sourceCode.getTokenAfter(openBrace);
1275
+ const lastToken = sourceCode.getTokenBefore(closeBrace);
1276
+ if (needsLineBreaks) {
1277
+ if (isTokenOnSameLine(openBrace, firstToken)) context.report({
1278
+ messageId: "expectedLinebreakAfterOpeningBrace",
1279
+ node,
1280
+ loc: openBrace.loc,
1281
+ fix(fixer) {
1282
+ if (hasCommentsFirstToken) return null;
1283
+ return fixer.insertTextAfter(openBrace, "\n");
1284
+ }
1285
+ });
1286
+ if (isTokenOnSameLine(lastToken, closeBrace)) context.report({
1287
+ messageId: "expectedLinebreakBeforeClosingBrace",
1288
+ node,
1289
+ loc: closeBrace.loc,
1290
+ fix(fixer) {
1291
+ if (hasCommentsLastToken) return null;
1292
+ return fixer.insertTextBefore(closeBrace, "\n");
1293
+ }
1294
+ });
1295
+ } else {
1296
+ const consistent = options.consistent;
1297
+ const hasLineBreakBetweenOpenBraceAndFirst = !isTokenOnSameLine(openBrace, firstToken);
1298
+ const hasLineBreakBetweenCloseBraceAndLast = !isTokenOnSameLine(lastToken, closeBrace);
1299
+ if (!consistent && hasLineBreakBetweenOpenBraceAndFirst || consistent && hasLineBreakBetweenOpenBraceAndFirst && !hasLineBreakBetweenCloseBraceAndLast) context.report({
1300
+ messageId: "unexpectedLinebreakAfterOpeningBrace",
1301
+ node,
1302
+ loc: openBrace.loc,
1303
+ fix(fixer) {
1304
+ if (hasCommentsFirstToken) return null;
1305
+ return fixer.removeRange([openBrace.range[1], firstToken.range[0]]);
1306
+ }
1307
+ });
1308
+ if (!consistent && hasLineBreakBetweenCloseBraceAndLast || consistent && !hasLineBreakBetweenOpenBraceAndFirst && hasLineBreakBetweenCloseBraceAndLast) context.report({
1309
+ messageId: "unexpectedLinebreakBeforeClosingBrace",
1310
+ node,
1311
+ loc: closeBrace.loc,
1312
+ fix(fixer) {
1313
+ if (hasCommentsLastToken) return null;
1314
+ return fixer.removeRange([lastToken.range[1], closeBrace.range[0]]);
1315
+ }
1316
+ });
1317
+ }
1318
+ }
1319
+ return { TOMLInlineTable: check };
1320
+ }
1321
+ });
1322
+
1176
1323
  //#endregion
1177
1324
  //#region src/rules/inline-table-curly-spacing.ts
1178
1325
  /**
@@ -1183,7 +1330,7 @@ var indent_default = createRule("indent", {
1183
1330
  * @param sourceCode The source code object, used to get nodes by range index.
1184
1331
  * @returns An object containing the spacing option, the emptyObjects option, and two functions to determine if there should be spaces after the opening curly brace and before the closing curly brace.
1185
1332
  */
1186
- function parseOptions(options, sourceCode) {
1333
+ function parseOptions$1(options, sourceCode) {
1187
1334
  const spaced = options[0] ?? "always";
1188
1335
  /**
1189
1336
  * Determines whether an option is set, relative to the spacing option.
@@ -1267,7 +1414,7 @@ var inline_table_curly_spacing_default = createRule("inline-table-curly-spacing"
1267
1414
  create(context) {
1268
1415
  const sourceCode = context.sourceCode;
1269
1416
  if (!sourceCode.parserServices?.isTOML) return {};
1270
- const options = parseOptions(context.options, sourceCode);
1417
+ const options = parseOptions$1(context.options, sourceCode);
1271
1418
  /**
1272
1419
  * Reports that there shouldn't be a space after the first token
1273
1420
  * @param node The node to report in the event of an error.
@@ -1442,6 +1589,78 @@ var inline_table_curly_spacing_default = createRule("inline-table-curly-spacing"
1442
1589
  }
1443
1590
  });
1444
1591
 
1592
+ //#endregion
1593
+ //#region src/rules/inline-table-key-value-newline.ts
1594
+ /**
1595
+ * Parses the options for the rule and returns a normalized options object.
1596
+ * @param options The raw options provided to the rule.
1597
+ * @returns A normalized options object with default values applied.
1598
+ */
1599
+ function parseOptions(options) {
1600
+ return { allowAllPropertiesOnSameLine: options?.allowAllPropertiesOnSameLine ?? true };
1601
+ }
1602
+ var inline_table_key_value_newline_default = createRule("inline-table-key-value-newline", {
1603
+ meta: {
1604
+ docs: {
1605
+ description: "enforce placing inline table key-value pairs on separate lines",
1606
+ categories: ["standard"],
1607
+ extensionRule: "object-property-newline"
1608
+ },
1609
+ fixable: "whitespace",
1610
+ hasSuggestions: false,
1611
+ schema: [{
1612
+ type: "object",
1613
+ properties: { allowAllPropertiesOnSameLine: { type: "boolean" } },
1614
+ additionalProperties: false
1615
+ }],
1616
+ messages: {
1617
+ propertiesOnNewlineAll: "Inline table key-value pairs must go on a new line if they aren't all on the same line.",
1618
+ propertiesOnNewline: "Inline table key-value pairs must go on a new line."
1619
+ },
1620
+ type: "layout"
1621
+ },
1622
+ create(context) {
1623
+ const sourceCode = context.sourceCode;
1624
+ if (!sourceCode.parserServices?.isTOML) return {};
1625
+ if (context.languageOptions.parserOptions?.tomlVersion) {
1626
+ const tomlVersion = context.languageOptions.parserOptions.tomlVersion.includes(".") && context.languageOptions.parserOptions.tomlVersion.split(".");
1627
+ if (tomlVersion && tomlVersion[0] === "1" && tomlVersion[1] === "0") return {};
1628
+ }
1629
+ const allowSameLine = parseOptions(context.options[0]).allowAllPropertiesOnSameLine;
1630
+ const messageId = allowSameLine ? "propertiesOnNewlineAll" : "propertiesOnNewline";
1631
+ /**
1632
+ * Checks whether the given tokens are on the same line.
1633
+ * @param token1 The first token to compare.
1634
+ * @param token2 The second token to compare.
1635
+ * @returns True if the tokens are on the same line, false otherwise.
1636
+ */
1637
+ function check(node, children) {
1638
+ if (allowSameLine) {
1639
+ if (children.length > 1) {
1640
+ if (isTokenOnSameLine(sourceCode.getFirstToken(children[0]), sourceCode.getLastToken(children[children.length - 1]))) return;
1641
+ }
1642
+ }
1643
+ for (let i = 1; i < children.length; i++) {
1644
+ const lastTokenOfPreviousProperty = sourceCode.getLastToken(children[i - 1]);
1645
+ const firstTokenOfCurrentProperty = sourceCode.getFirstToken(children[i]);
1646
+ if (isTokenOnSameLine(lastTokenOfPreviousProperty, firstTokenOfCurrentProperty)) context.report({
1647
+ node,
1648
+ loc: firstTokenOfCurrentProperty.loc,
1649
+ messageId,
1650
+ fix(fixer) {
1651
+ const rangeAfterComma = [sourceCode.getTokenBefore(firstTokenOfCurrentProperty).range[1], firstTokenOfCurrentProperty.range[0]];
1652
+ if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) return null;
1653
+ return fixer.replaceTextRange(rangeAfterComma, "\n");
1654
+ }
1655
+ });
1656
+ }
1657
+ }
1658
+ return { TOMLInlineTable(node) {
1659
+ check(node, node.body);
1660
+ } };
1661
+ }
1662
+ });
1663
+
1445
1664
  //#endregion
1446
1665
  //#region src/rules/key-spacing.ts
1447
1666
  /**
@@ -3094,7 +3313,9 @@ const rules$1 = [
3094
3313
  array_element_newline_default,
3095
3314
  comma_style_default,
3096
3315
  indent_default,
3316
+ inline_table_curly_newline_default,
3097
3317
  inline_table_curly_spacing_default,
3318
+ inline_table_key_value_newline_default,
3098
3319
  key_spacing_default,
3099
3320
  keys_order_default,
3100
3321
  no_mixed_type_in_array_default,
@@ -3143,7 +3364,9 @@ var standard_default = [...base_default, { rules: {
3143
3364
  "toml/array-element-newline": "error",
3144
3365
  "toml/comma-style": "error",
3145
3366
  "toml/indent": "error",
3367
+ "toml/inline-table-curly-newline": "error",
3146
3368
  "toml/inline-table-curly-spacing": "error",
3369
+ "toml/inline-table-key-value-newline": "error",
3147
3370
  "toml/key-spacing": "error",
3148
3371
  "toml/keys-order": "error",
3149
3372
  "toml/no-space-dots": "error",
@@ -3162,7 +3385,7 @@ var standard_default = [...base_default, { rules: {
3162
3385
  //#endregion
3163
3386
  //#region package.json
3164
3387
  var name$1 = "eslint-plugin-toml";
3165
- var version$1 = "1.2.1";
3388
+ var version$1 = "1.3.0";
3166
3389
 
3167
3390
  //#endregion
3168
3391
  //#region src/meta.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-toml",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "This ESLint plugin provides linting rules for TOML.",
5
5
  "type": "module",
6
6
  "exports": {