Skip to content

Commit e4c34d7

Browse files
committed
improved indentations support
1 parent 4d649d1 commit e4c34d7

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

packages/stencil-library/eslint-plugin/src/rules/no-label-slot-in-checkbox.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ ruleTester.run("no-label-slot-in-checkbox", rule, {
2727
],
2828
invalid: [
2929
{
30-
code: "<dnn-checkbox onClick={e => console.log(e)}>Something</dnn-checkbox>",
30+
code: [
31+
"<dnn-checkbox onClick={e => console.log(e)}>",
32+
" Something",
33+
"</dnn-checkbox>",
34+
].join("\n"),
3135
languageOptions: jsxParserOptions,
3236
errors: [{ messageId: "noLabelSlotInCheckbox" }],
3337
output: [
3438
"<label>",
3539
" <dnn-checkbox onClick={e => console.log(e)} />",
3640
" Something",
37-
"</label>"
41+
"</label>",
3842
].join("\n"),
3943
},
4044
],

packages/stencil-library/eslint-plugin/src/rules/no-label-slot-in-checkbox.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ export const rule = createRule({
3333
node,
3434
messageId: "noLabelSlotInCheckbox",
3535
fix: fixer => {
36-
const sourceCode = context.getSourceCode();
36+
const sourceCode = context.sourceCode;
3737
const checkboxText = sourceCode.getText(node.openingElement);
3838
const selfClosing = checkboxText.replace(/>$/, " />");
3939
const innerContent = node.children.map(child => sourceCode.getText(child)).join("").trim();
4040

4141
// Detect leading indentation of the node
42-
const lines = sourceCode.getText(node).split("\n");
43-
const firstLine = lines[0];
44-
const indentMatch = firstLine.match(/^(\s*)/);
42+
const parent = node.parent;
43+
const parentLine = parent ? context.sourceCode.getLocFromIndex(parent.range[0]).line : node.loc.start.line;
44+
const baseLineText = context.sourceCode.lines[parentLine - 1];
45+
const indentMatch = baseLineText.match(/^(\s*)/);
4546
const indent = indentMatch?.[1] ?? "";
4647

4748
// Build replacement with proper indentation

0 commit comments

Comments
 (0)