eslint-plugin-toml 1.1.2 → 1.2.1
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/lib/index.d.mts +1 -0
- package/lib/index.mjs +135 -41
- package/package.json +1 -1
package/lib/index.d.mts
CHANGED
|
@@ -262,6 +262,7 @@ declare class TOMLSourceCode extends TextSourceCodeBase<{
|
|
|
262
262
|
getCommentsInside(nodeOrToken: TOMLSyntaxElement): AST.Comment[];
|
|
263
263
|
getCommentsBefore(nodeOrToken: TOMLSyntaxElement): AST.Comment[];
|
|
264
264
|
getCommentsAfter(nodeOrToken: TOMLSyntaxElement): AST.Comment[];
|
|
265
|
+
commentsExistBetween(first: TOMLSyntaxElement, second: TOMLSyntaxElement): boolean;
|
|
265
266
|
isSpaceBetween(first: AST.Token | AST.Comment, second: AST.Token | AST.Comment): boolean;
|
|
266
267
|
/**
|
|
267
268
|
* Compatibility for ESLint's SourceCode API
|
package/lib/index.mjs
CHANGED
|
@@ -1175,6 +1175,58 @@ var indent_default = createRule("indent", {
|
|
|
1175
1175
|
|
|
1176
1176
|
//#endregion
|
|
1177
1177
|
//#region src/rules/inline-table-curly-spacing.ts
|
|
1178
|
+
/**
|
|
1179
|
+
* Parses the options for this rule and returns an object containing the spacing option,
|
|
1180
|
+
* the emptyObjects option, and two functions to determine if there should be spaces
|
|
1181
|
+
* after the opening curly brace and before the closing curly brace, based on the options and the surrounding tokens.
|
|
1182
|
+
* @param options The options passed to the rule.
|
|
1183
|
+
* @param sourceCode The source code object, used to get nodes by range index.
|
|
1184
|
+
* @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
|
+
*/
|
|
1186
|
+
function parseOptions(options, sourceCode) {
|
|
1187
|
+
const spaced = options[0] ?? "always";
|
|
1188
|
+
/**
|
|
1189
|
+
* Determines whether an option is set, relative to the spacing option.
|
|
1190
|
+
* If spaced is "always", then check whether option is set to false.
|
|
1191
|
+
* If spaced is "never", then check whether option is set to true.
|
|
1192
|
+
* @param option The option to exclude.
|
|
1193
|
+
* @returns Whether or not the property is excluded.
|
|
1194
|
+
*/
|
|
1195
|
+
function isOptionSet(option) {
|
|
1196
|
+
return options[1] ? options[1][option] === (spaced === "never") : false;
|
|
1197
|
+
}
|
|
1198
|
+
const arraysInObjectsException = isOptionSet("arraysInObjects");
|
|
1199
|
+
const objectsInObjectsException = isOptionSet("objectsInObjects");
|
|
1200
|
+
const emptyObjects = options[1]?.emptyObjects ?? "ignore";
|
|
1201
|
+
/**
|
|
1202
|
+
* Determines if there should be a space after the opening curly brace,
|
|
1203
|
+
* based on the spacing option and the second token.
|
|
1204
|
+
* @param spaced The spacing option ("always" or "never").
|
|
1205
|
+
* @param second The second token after the opening curly brace.
|
|
1206
|
+
* @returns Whether or not there should be a space after the opening curly brace.
|
|
1207
|
+
*/
|
|
1208
|
+
function isOpeningCurlyBraceMustBeSpaced(spaced, _second) {
|
|
1209
|
+
return spaced === "always";
|
|
1210
|
+
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Determines if there should be a space before the closing curly brace,
|
|
1213
|
+
* based on the spacing option and the penultimate token.
|
|
1214
|
+
* @param spaced The spacing option ("always" or "never").
|
|
1215
|
+
* @param penultimate The penultimate token before the closing curly brace.
|
|
1216
|
+
* @returns Whether or not there should be a space before the closing curly brace.
|
|
1217
|
+
*/
|
|
1218
|
+
function isClosingCurlyBraceMustBeSpaced(spaced, penultimate) {
|
|
1219
|
+
const targetPenultimateType = arraysInObjectsException && isClosingBracketToken(penultimate) ? "TOMLArray" : objectsInObjectsException && isClosingBraceToken(penultimate) ? "TOMLInlineTable" : null;
|
|
1220
|
+
const node = sourceCode.getNodeByRangeIndex(penultimate.range[0]);
|
|
1221
|
+
return targetPenultimateType && node?.type === targetPenultimateType ? spaced === "never" : spaced === "always";
|
|
1222
|
+
}
|
|
1223
|
+
return {
|
|
1224
|
+
spaced,
|
|
1225
|
+
emptyObjects,
|
|
1226
|
+
isOpeningCurlyBraceMustBeSpaced,
|
|
1227
|
+
isClosingCurlyBraceMustBeSpaced
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1178
1230
|
var inline_table_curly_spacing_default = createRule("inline-table-curly-spacing", {
|
|
1179
1231
|
meta: {
|
|
1180
1232
|
docs: {
|
|
@@ -1191,7 +1243,15 @@ var inline_table_curly_spacing_default = createRule("inline-table-curly-spacing"
|
|
|
1191
1243
|
type: "object",
|
|
1192
1244
|
properties: {
|
|
1193
1245
|
arraysInObjects: { type: "boolean" },
|
|
1194
|
-
objectsInObjects: { type: "boolean" }
|
|
1246
|
+
objectsInObjects: { type: "boolean" },
|
|
1247
|
+
emptyObjects: {
|
|
1248
|
+
type: "string",
|
|
1249
|
+
enum: [
|
|
1250
|
+
"ignore",
|
|
1251
|
+
"always",
|
|
1252
|
+
"never"
|
|
1253
|
+
]
|
|
1254
|
+
}
|
|
1195
1255
|
},
|
|
1196
1256
|
additionalProperties: false
|
|
1197
1257
|
}],
|
|
@@ -1199,35 +1259,15 @@ var inline_table_curly_spacing_default = createRule("inline-table-curly-spacing"
|
|
|
1199
1259
|
requireSpaceBefore: "A space is required before '{{token}}'.",
|
|
1200
1260
|
requireSpaceAfter: "A space is required after '{{token}}'.",
|
|
1201
1261
|
unexpectedSpaceBefore: "There should be no space before '{{token}}'.",
|
|
1202
|
-
unexpectedSpaceAfter: "There should be no space after '{{token}}'."
|
|
1262
|
+
unexpectedSpaceAfter: "There should be no space after '{{token}}'.",
|
|
1263
|
+
requiredSpaceInEmptyObject: "A space is required in empty inline table.",
|
|
1264
|
+
unexpectedSpaceInEmptyObject: "There should be no space in empty inline table."
|
|
1203
1265
|
}
|
|
1204
1266
|
},
|
|
1205
1267
|
create(context) {
|
|
1206
1268
|
const sourceCode = context.sourceCode;
|
|
1207
1269
|
if (!sourceCode.parserServices?.isTOML) return {};
|
|
1208
|
-
const
|
|
1209
|
-
/**
|
|
1210
|
-
* Determines whether an option is set, relative to the spacing option.
|
|
1211
|
-
* If spaced is "always", then check whether option is set to false.
|
|
1212
|
-
* If spaced is "never", then check whether option is set to true.
|
|
1213
|
-
* @param option The option to exclude.
|
|
1214
|
-
* @returns Whether or not the property is excluded.
|
|
1215
|
-
*/
|
|
1216
|
-
function isOptionSet(option) {
|
|
1217
|
-
return context.options[1] ? context.options[1][option] === !spaced : false;
|
|
1218
|
-
}
|
|
1219
|
-
const options = {
|
|
1220
|
-
spaced,
|
|
1221
|
-
arraysInObjectsException: isOptionSet("arraysInObjects"),
|
|
1222
|
-
objectsInObjectsException: isOptionSet("objectsInObjects"),
|
|
1223
|
-
isOpeningCurlyBraceMustBeSpaced(_second) {
|
|
1224
|
-
return options.spaced;
|
|
1225
|
-
},
|
|
1226
|
-
isClosingCurlyBraceMustBeSpaced(penultimate) {
|
|
1227
|
-
const targetPenultimateType = options.arraysInObjectsException && isClosingBracketToken(penultimate) ? "TOMLArray" : options.objectsInObjectsException && isClosingBraceToken(penultimate) ? "TOMLInlineTable" : null;
|
|
1228
|
-
return targetPenultimateType && sourceCode.getNodeByRangeIndex(penultimate.range[0])?.type === targetPenultimateType ? !options.spaced : options.spaced;
|
|
1229
|
-
}
|
|
1230
|
-
};
|
|
1270
|
+
const options = parseOptions(context.options, sourceCode);
|
|
1231
1271
|
/**
|
|
1232
1272
|
* Reports that there shouldn't be a space after the first token
|
|
1233
1273
|
* @param node The node to report in the event of an error.
|
|
@@ -1308,18 +1348,64 @@ var inline_table_curly_spacing_default = createRule("inline-table-curly-spacing"
|
|
|
1308
1348
|
* @param penultimate The penultimate token to check (should be last before closing brace)
|
|
1309
1349
|
* @param last The last token to check (should be closing brace)
|
|
1310
1350
|
*/
|
|
1311
|
-
function validateBraceSpacing(node,
|
|
1312
|
-
if (isTokenOnSameLine(
|
|
1313
|
-
const firstSpaced = sourceCode.isSpaceBetween(
|
|
1314
|
-
if (options.isOpeningCurlyBraceMustBeSpaced(second)) {
|
|
1315
|
-
if (!firstSpaced) reportRequiredBeginningSpace(node,
|
|
1316
|
-
} else if (firstSpaced && second.type !== "Block") reportNoBeginningSpace(node,
|
|
1351
|
+
function validateBraceSpacing(node, spaced, openingToken, second, penultimate, closingToken) {
|
|
1352
|
+
if (isTokenOnSameLine(openingToken, second)) {
|
|
1353
|
+
const firstSpaced = sourceCode.isSpaceBetween(openingToken, second);
|
|
1354
|
+
if (options.isOpeningCurlyBraceMustBeSpaced(spaced, second)) {
|
|
1355
|
+
if (!firstSpaced) reportRequiredBeginningSpace(node, openingToken);
|
|
1356
|
+
} else if (firstSpaced && second.type !== "Block") reportNoBeginningSpace(node, openingToken);
|
|
1357
|
+
}
|
|
1358
|
+
if (isTokenOnSameLine(penultimate, closingToken)) {
|
|
1359
|
+
const lastSpaced = sourceCode.isSpaceBetween(penultimate, closingToken);
|
|
1360
|
+
if (options.isClosingCurlyBraceMustBeSpaced(spaced, penultimate)) {
|
|
1361
|
+
if (!lastSpaced) reportRequiredEndingSpace(node, closingToken);
|
|
1362
|
+
} else if (lastSpaced) reportNoEndingSpace(node, closingToken);
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
* Checks spacing in empty objects. Depending on the options, reports
|
|
1367
|
+
* if there is an unexpected space or if there is no space when there should be.
|
|
1368
|
+
* @param node The node to check.
|
|
1369
|
+
*/
|
|
1370
|
+
function checkSpaceInEmptyObject(node) {
|
|
1371
|
+
if (options.emptyObjects === "ignore") return;
|
|
1372
|
+
const openingToken = sourceCode.getFirstToken(node);
|
|
1373
|
+
const closingToken = sourceCode.getLastToken(node);
|
|
1374
|
+
const second = sourceCode.getTokenAfter(openingToken, { includeComments: true });
|
|
1375
|
+
if (second !== closingToken && isCommentToken(second)) {
|
|
1376
|
+
const penultimate = sourceCode.getTokenBefore(closingToken, { includeComments: true });
|
|
1377
|
+
validateBraceSpacing(node, options.emptyObjects, openingToken, second, penultimate, closingToken);
|
|
1378
|
+
return;
|
|
1317
1379
|
}
|
|
1318
|
-
if (isTokenOnSameLine(
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1380
|
+
if (!isTokenOnSameLine(openingToken, closingToken)) return;
|
|
1381
|
+
const sourceBetween = sourceCode.text.slice(openingToken.range[1], closingToken.range[0]);
|
|
1382
|
+
if (sourceBetween.trim() !== "") return;
|
|
1383
|
+
if (options.emptyObjects === "always") {
|
|
1384
|
+
if (sourceBetween) return;
|
|
1385
|
+
context.report({
|
|
1386
|
+
node,
|
|
1387
|
+
loc: {
|
|
1388
|
+
start: openingToken.loc.end,
|
|
1389
|
+
end: closingToken.loc.start
|
|
1390
|
+
},
|
|
1391
|
+
messageId: "requiredSpaceInEmptyObject",
|
|
1392
|
+
fix(fixer) {
|
|
1393
|
+
return fixer.replaceTextRange([openingToken.range[1], closingToken.range[0]], " ");
|
|
1394
|
+
}
|
|
1395
|
+
});
|
|
1396
|
+
} else if (options.emptyObjects === "never") {
|
|
1397
|
+
if (!sourceBetween) return;
|
|
1398
|
+
context.report({
|
|
1399
|
+
node,
|
|
1400
|
+
loc: {
|
|
1401
|
+
start: openingToken.loc.end,
|
|
1402
|
+
end: closingToken.loc.start
|
|
1403
|
+
},
|
|
1404
|
+
messageId: "unexpectedSpaceInEmptyObject",
|
|
1405
|
+
fix(fixer) {
|
|
1406
|
+
return fixer.removeRange([openingToken.range[1], closingToken.range[0]]);
|
|
1407
|
+
}
|
|
1408
|
+
});
|
|
1323
1409
|
}
|
|
1324
1410
|
}
|
|
1325
1411
|
/**
|
|
@@ -1342,10 +1428,15 @@ var inline_table_curly_spacing_default = createRule("inline-table-curly-spacing"
|
|
|
1342
1428
|
* @param node An ObjectExpression or ObjectPattern node to check.
|
|
1343
1429
|
*/
|
|
1344
1430
|
function checkForObject(node) {
|
|
1345
|
-
if (node.body.length === 0)
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1431
|
+
if (node.body.length === 0) {
|
|
1432
|
+
checkSpaceInEmptyObject(node);
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
const openingToken = sourceCode.getFirstToken(node);
|
|
1436
|
+
const closingToken = getClosingBraceOfObject(node);
|
|
1437
|
+
const second = sourceCode.getTokenAfter(openingToken, { includeComments: true });
|
|
1438
|
+
const penultimate = sourceCode.getTokenBefore(closingToken, { includeComments: true });
|
|
1439
|
+
validateBraceSpacing(node, options.spaced, openingToken, second, penultimate, closingToken);
|
|
1349
1440
|
}
|
|
1350
1441
|
return { TOMLInlineTable: checkForObject };
|
|
1351
1442
|
}
|
|
@@ -3071,7 +3162,7 @@ var standard_default = [...base_default, { rules: {
|
|
|
3071
3162
|
//#endregion
|
|
3072
3163
|
//#region package.json
|
|
3073
3164
|
var name$1 = "eslint-plugin-toml";
|
|
3074
|
-
var version$1 = "1.1
|
|
3165
|
+
var version$1 = "1.2.1";
|
|
3075
3166
|
|
|
3076
3167
|
//#endregion
|
|
3077
3168
|
//#region src/meta.ts
|
|
@@ -3305,6 +3396,9 @@ var TOMLSourceCode = class extends TextSourceCodeBase {
|
|
|
3305
3396
|
getCommentsAfter(nodeOrToken) {
|
|
3306
3397
|
return this.tokenStore.getCommentsAfter(nodeOrToken);
|
|
3307
3398
|
}
|
|
3399
|
+
commentsExistBetween(first, second) {
|
|
3400
|
+
return this.tokenStore.commentsExistBetween(first, second);
|
|
3401
|
+
}
|
|
3308
3402
|
isSpaceBetween(first, second) {
|
|
3309
3403
|
const [left, right] = first.range[1] <= second.range[0] ? [first, second] : [second, first];
|
|
3310
3404
|
return this.tokenStore.isSpaceBetween(left, right);
|