Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var StylePropObjectRule = rule.Rule{ Name: "react/style-prop-object", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { // Parse the `allow` option: list of component names to skip var allowedComponents map[string]bool optsMap := utils.GetOptionsMap(options) if optsMap != nil { if allowList, ok := optsMap["allow"]; ok { if arr, ok := allowList.([]interface{}); ok { allowedComponents = make(map[string]bool) for _, item := range arr { if name, ok := item.(string); ok { allowedComponents[name] = true } } } } } report := func(node *ast.Node) { ctx.ReportNode(node, rule.RuleMessage{ Id: "stylePropNotObject", Description: "Style prop value must be an object", }) } checkIdentifier := func(expr *ast.Node, reportNode *ast.Node) { decl := utils.GetDeclaration(ctx.TypeChecker, expr) if decl == nil { return } if decl.Kind != ast.KindVariableDeclaration { return } init := decl.AsVariableDeclaration().Initializer if init == nil { return } if isNonObjectExpression(init) { report(reportNode) } } return rule.RuleListeners{ ast.KindJsxAttribute: func(node *ast.Node) { attr := node.AsJsxAttribute() name := attr.Name() if name == nil || name.Kind != ast.KindIdentifier { return } if name.AsIdentifier().Text != "style" { return } if len(allowedComponents) > 0 { componentName := getParentComponentName(ctx.SourceFile, node) if allowedComponents[componentName] { return } } initializer := attr.Initializer if initializer == nil { return } if initializer.Kind == ast.KindStringLiteral { report(node) return } if initializer.Kind == ast.KindJsxExpression { expr := initializer.AsJsxExpression().Expression if expr == nil { return } if isNonObjectExpression(expr) { report(node) return } if expr.Kind == ast.KindIdentifier { checkIdentifier(expr, node) } } }, ast.KindCallExpression: func(node *ast.Node) { call := node.AsCallExpression() if !reactutil.IsCreateElementCall(call.Expression, reactutil.GetReactPragma(ctx.Settings)) { return } args := call.Arguments if args == nil || len(args.Nodes) < 2 { return } firstArg := args.Nodes[0] if firstArg.Kind == ast.KindIdentifier { if allowedComponents[firstArg.AsIdentifier().Text] { return } } secondArg := args.Nodes[1] if secondArg.Kind != ast.KindObjectLiteralExpression { return } obj := secondArg.AsObjectLiteralExpression() if obj.Properties == nil { return } for _, prop := range obj.Properties.Nodes { switch prop.Kind { case ast.KindPropertyAssignment: pa := prop.AsPropertyAssignment() nameNode := pa.Name() if nameNode == nil || nameNode.Kind == ast.KindComputedPropertyName { continue } if nameNode.Kind != ast.KindIdentifier || nameNode.AsIdentifier().Text != "style" { continue } value := pa.Initializer if value == nil { continue } if value.Kind == ast.KindIdentifier { checkIdentifier(value, value) } else if isNonObjectExpression(value) { report(value) } return case ast.KindShorthandPropertyAssignment: spa := prop.AsShorthandPropertyAssignment() nameNode := spa.Name() if nameNode == nil || nameNode.Kind != ast.KindIdentifier || nameNode.AsIdentifier().Text != "style" { continue } valueSymbol := ctx.TypeChecker.GetShorthandAssignmentValueSymbol(prop) if valueSymbol != nil && len(valueSymbol.Declarations) > 0 { decl := valueSymbol.Declarations[0] if decl.Kind == ast.KindVariableDeclaration { init := decl.AsVariableDeclaration().Initializer if init != nil && isNonObjectExpression(init) { report(nameNode) } } } return default: continue } } }, } }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.