@lcap/nasl
Version:
NetEase Application Specific Language
95 lines • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.check = exports.isEnumIdentifier = void 0;
const asserts_1 = require("@lcap/nasl-concepts/asserts");
/**
* 枚举标识符
*
* @description 指向枚举本身的标志符
*/
function isEnumIdentifier(node) {
if (!(0, asserts_1.isStrictIdentifier)(node)) {
return false;
}
const typeNode = node.__TypeAnnotation ?? node.typeAnnotation;
// 枚举类型
if (!typeNode || typeNode.typeKind !== 'reference' || typeNode.typeNamespace !== 'app.enums') {
return false;
}
const { app } = node;
// 指向枚举本身
return node.namespace === 'app.enums' && app.enums.some((item) => item.name === node.name);
}
exports.isEnumIdentifier = isEnumIdentifier;
/**
* 必须在 MemberExpression 表达式中的标识符
*
* @description FIXME: 暂时只有枚举,实体和结构有点麻烦,后面再补
*/
function mustInMemberExpression(node) {
const diagnostics = [];
// 枚举类型
if (!isEnumIdentifier(node)) {
return [];
}
if (
// 不满足条件时判定为错误
!(
// 在 MemberExpression 表达式中,并且还是 object 属性
(node.parentKey === 'object' && (0, asserts_1.isStrictMemberExpression)(node.parentNode)) ||
// 在 SelectMembers 表达式中,并且还是 expression 属性
(node.parentKey === 'expression' && (0, asserts_1.isStrictSelectMembers)(node.parentNode)))) {
const errorDetail = {
node,
severity: 'error',
message: `${node.name} 是类型不是合法变量,不允许在这里使用`,
};
node.tsErrorDetail = errorDetail;
diagnostics.push(errorDetail);
}
return diagnostics;
}
/**
* 不允许被批量赋值直接连接
*/
function cannotBeLined(node) {
const diagnostics = [];
const { parentNode: selectMember, parentKey: key } = node;
if (!isEnumIdentifier(node) || !(0, asserts_1.isStrictSelectMembers)(selectMember) || key !== 'expression') {
return diagnostics;
}
const batchAssignment = selectMember.getAncestor('BatchAssignment');
const index = batchAssignment?.rights?.indexOf?.(selectMember) ?? -1;
// 没有找到右侧待赋值元素
if (index === -1) {
return diagnostics;
}
const isLined = batchAssignment.assignmentLines.some(({ rightIndex }) => {
// 指向了此项本身
return (rightIndex.length === 1 &&
rightIndex[0] === index);
});
// 确实被连线
if (isLined) {
const errorDetail = {
node,
severity: 'error',
message: `${node.name} 是类型,不允许连线`,
};
node.tsErrorDetail = errorDetail;
diagnostics.push(errorDetail);
}
return diagnostics;
}
/** 检查节点 */
function check(node) {
if (!(0, asserts_1.isStrictIdentifier)(node)) {
return [];
}
return [
...mustInMemberExpression(node),
...cannotBeLined(node),
];
}
exports.check = check;
//# sourceMappingURL=identifier.js.map