ifc-language-server/node_modules/@stylistic/eslint-plugin/dist/rules/indent-binary-ops.js
Ryan Schultz 8afacf268a Implemented a working Language Server Protocol (LSP) for IFC files with:
- Hover provider showing entity information and type
- Go-to-definition (F12) for entity references
- Basic IFC file validation (ISO-10303-21 header check)
- Entity parsing with regex-based detection
- Proper CommonJS module system (avoiding ES module issues)

This replaces the broken baseline from ifc-developer-tools which had:
- Non-functional ES module configuration
- Circular dependency issues
- Parser crashes
- Non-working PositionVisitor

Built on Microsoft's LSP example template for a clean, maintainable foundation.

Next: Add hierarchical entity dependency tree in hover tooltip."
2025-12-07 10:20:07 -06:00

125 lines
4.4 KiB
JavaScript

'use strict';
var utils = require('../utils.js');
require('eslint-visitor-keys');
require('espree');
require('estraverse');
var indentBinaryOps = utils.createRule({
name: "indent-binary-ops",
package: "plus",
meta: {
type: "layout",
docs: {
description: "Indentation for binary operators"
},
fixable: "whitespace",
schema: [
{
oneOf: [
{
type: "integer",
minimum: 0
},
{
type: "string",
enum: ["tab"]
}
]
}
],
messages: {
wrongIndentation: "Expected indentation of {{expected}}"
}
},
defaultOptions: [2],
create: (context, options) => {
const { sourceCode } = context;
const indentStr = options[0] === "tab" ? " " : " ".repeat(options[0] ?? 2);
const indentCache = /* @__PURE__ */ new Map();
function getIndentOfLine(line) {
if (indentCache.has(line))
return indentCache.get(line);
return sourceCode.lines[line - 1].match(/^\s*/)?.[0] ?? "";
}
function firstTokenOfLine(line) {
return sourceCode.tokensAndComments.find((token) => token.loc.start.line === line);
}
function lastTokenOfLine(line) {
return [...sourceCode.tokensAndComments].reverse().find((token) => token.loc.end.line === line);
}
function handler(node, right) {
if (node.loc.start.line === node.loc.end.line)
return;
let tokenRight = sourceCode.getFirstToken(right);
let tokenOperator = sourceCode.getTokenBefore(tokenRight);
while (tokenOperator.value === "(") {
tokenRight = tokenOperator;
tokenOperator = sourceCode.getTokenBefore(tokenRight);
if (tokenOperator.range[0] <= right.parent.range[0])
return;
}
const tokenBeforeAll = sourceCode.getTokenBefore(node);
const tokenLeft = sourceCode.getTokenBefore(tokenOperator);
const isMultiline = tokenRight.loc.start.line !== tokenLeft.loc.start.line;
if (!isMultiline)
return;
const firstTokenOfLineLeft = firstTokenOfLine(tokenLeft.loc.start.line);
const lastTokenOfLineLeft = lastTokenOfLine(tokenLeft.loc.start.line);
const needAdditionIndent = firstTokenOfLineLeft?.type === "Keyword" && !["typeof", "instanceof", "this"].includes(firstTokenOfLineLeft.value) || firstTokenOfLineLeft?.type === "Identifier" && firstTokenOfLineLeft.value === "type" && node.parent?.type === "TSTypeAliasDeclaration" || [":", "[", "(", "<", "="].includes(lastTokenOfLineLeft?.value || "") || ["[", "(", "=>", ":"].includes(tokenBeforeAll?.value || "") && firstTokenOfLineLeft?.loc.start.line === tokenBeforeAll?.loc.start.line || ["||", "&&"].includes(lastTokenOfLineLeft?.value || "") && node.loc.start.line === tokenLeft.loc.start.line && node.loc.start.column !== getIndentOfLine(node.loc.start.line).length;
const indentTarget = getIndentOfLine(tokenLeft.loc.start.line) + (needAdditionIndent ? indentStr : "");
const indentRight = getIndentOfLine(tokenRight.loc.start.line);
if (indentTarget !== indentRight) {
const start = {
line: tokenRight.loc.start.line,
column: 0
};
const end = {
line: tokenRight.loc.start.line,
column: indentRight.length
};
context.report({
loc: {
start,
end
},
messageId: "wrongIndentation",
data: {
expected: `${indentTarget.length} ${options[0] === "tab" ? "tab" : "space"}${indentTarget.length === 1 ? "" : "s"}`
},
fix(fixer) {
return fixer.replaceTextRange(
[sourceCode.getIndexFromLoc(start), sourceCode.getIndexFromLoc(end)],
indentTarget
);
}
});
indentCache.set(tokenRight.loc.start.line, indentTarget);
}
}
return {
BinaryExpression(node) {
handler(node, node.right);
},
LogicalExpression(node) {
handler(node, node.right);
},
TSUnionType(node) {
if (node.types.length > 1) {
node.types.forEach((type) => {
handler(node, type);
});
}
},
TSIntersectionType(node) {
if (node.types.length > 1) {
node.types.forEach((type) => {
handler(node, type);
});
}
}
};
}
});
module.exports = indentBinaryOps;