1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00

Stop ignoring syntax errors and fix hints in Assignments.

This commit is contained in:
Patrick Thomson 2018-03-20 12:35:30 -04:00
parent b61938de73
commit bde8afb472
5 changed files with 41 additions and 41 deletions

View File

@ -6,8 +6,6 @@ ignore "Use mappend"
ignore "Redundant do"
-- TODO: investigate whether cost-center analysis is better with lambda-case than it was
ignore "Use lambda-case"
-- Hlint chokes and dies on certain where-clause annotations with type operators. TODO file a bug
ignore "Parse error"
error "generalize ++" = (++) ==> (<>)
-- AMP fallout

View File

@ -1,4 +1,4 @@
{-# LANGUAGE DataKinds, DeriveAnyClass, RankNTypes, TupleSections, TypeOperators #-}
{-# LANGUAGE DataKinds, RankNTypes, TypeOperators #-}
module Language.Go.Assignment
( assignment
, Syntax
@ -275,7 +275,7 @@ channelType = makeTerm' <$> symbol ChannelType <*> children (mkChannelType <$>
| otherwise = inj . Go.Type.BidirectionalChannel
fieldDeclaration :: Assignment
fieldDeclaration = mkFieldDeclarationWithTag <$> symbol FieldDeclaration <*> children ((,,) <$> (manyTermsTill expression (void (symbol TypeIdentifier)) <|> (manyTerm expression)) <*> optional expression <*> optional expression)
fieldDeclaration = mkFieldDeclarationWithTag <$> symbol FieldDeclaration <*> children ((,,) <$> (manyTermsTill expression (void (symbol TypeIdentifier)) <|> manyTerm expression) <*> optional expression <*> optional expression)
where
mkFieldDeclarationWithTag loc (fields, type', tag) | Just ty <- type', Just tag' <- tag = makeTerm loc (Go.Syntax.Field [ty, tag'] (makeTerm loc fields))
| Just ty <- type' = makeTerm loc (Go.Syntax.Field [ty] (makeTerm loc fields))
@ -409,7 +409,7 @@ indexExpression :: Assignment
indexExpression = makeTerm <$> symbol IndexExpression <*> children (Expression.Subscript <$> expression <*> manyTerm expression)
methodDeclaration :: Assignment
methodDeclaration = makeTerm <$> symbol MethodDeclaration <*> children (mkTypedMethodDeclaration <$> receiver <*> term fieldIdentifier <*> manyTerm parameters <*> ((makeTerm <$> location <*> (manyTermsTill expression (void (symbol Block)))) <|> emptyTerm) <*> (term block <|> emptyTerm))
methodDeclaration = makeTerm <$> symbol MethodDeclaration <*> children (mkTypedMethodDeclaration <$> receiver <*> term fieldIdentifier <*> manyTerm parameters <*> ((makeTerm <$> location <*> manyTermsTill expression (void (symbol Block))) <|> emptyTerm) <*> (term block <|> emptyTerm))
where
receiver = symbol ParameterList *> children ((symbol ParameterDeclaration *> children expressions) <|> expressions)
mkTypedMethodDeclaration receiver' name' parameters' type'' body' = Declaration.Method [type''] receiver' name' parameters' body'
@ -469,13 +469,13 @@ unaryExpression = makeTerm' <$> symbol UnaryExpression <*> ( notExpression
<|> unaryComplement
<|> unaryPlus )
where
notExpression = inj <$> (children (Expression.Not <$ symbol AnonBang <*> expression))
unaryAmpersand = inj <$> (children (Literal.Reference <$ symbol AnonAmpersand <*> expression))
unaryComplement = inj <$> (children (Expression.Complement <$ symbol AnonCaret <*> expression))
unaryMinus = inj <$> (children (Expression.Negate <$ symbol AnonMinus <*> expression))
unaryPlus = children (symbol AnonPlus *> (Term.termOut <$> expression))
unaryPointer = inj <$> (children (Literal.Pointer <$ symbol AnonStar <*> expression))
unaryReceive = inj <$> (children (Go.Syntax.ReceiveOperator <$ symbol AnonLAngleMinus <*> expression))
notExpression = inj <$> children (Expression.Not <$ symbol AnonBang <*> expression)
unaryAmpersand = inj <$> children (Literal.Reference <$ symbol AnonAmpersand <*> expression)
unaryComplement = inj <$> children (Expression.Complement <$ symbol AnonCaret <*> expression)
unaryMinus = inj <$> children (Expression.Negate <$ symbol AnonMinus <*> expression)
unaryPlus = children (symbol AnonPlus *> (Term.termOut <$> expression))
unaryPointer = inj <$> children (Literal.Pointer <$ symbol AnonStar <*> expression)
unaryReceive = inj <$> children (Go.Syntax.ReceiveOperator <$ symbol AnonLAngleMinus <*> expression)
varDeclaration :: Assignment
varDeclaration = (symbol ConstDeclaration <|> symbol VarDeclaration) *> children expressions
@ -489,7 +489,7 @@ variadicParameterDeclaration = makeTerm <$> symbol VariadicParameterDeclaration
varSpecification :: Assignment
varSpecification = makeTerm <$> (symbol ConstSpec <|> symbol VarSpec) <*> children (Statement.Assignment <$> pure [] <*> (annotatedLHS <|> identifiers) <*> expressions)
where
annotatedLHS = makeTerm <$> location <*> (Type.Annotation <$> (makeTerm <$> location <*> (manyTermsTill identifier (void (symbol TypeIdentifier)))) <*> expression)
annotatedLHS = makeTerm <$> location <*> (Type.Annotation <$> (makeTerm <$> location <*> manyTermsTill identifier (void (symbol TypeIdentifier))) <*> expression)
-- Statements
@ -513,7 +513,7 @@ assignment' = makeTerm' <$> symbol AssignmentStatement <*> children (infixTerm
assign :: Term -> Term -> Union Syntax Term
assign l r = inj (Statement.Assignment [] l r)
augmentedAssign :: f :< Syntax => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
augmentedAssign :: (f :< Syntax) => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
augmentedAssign c l r = assign l (makeTerm1 (c l r))
invert cons a b = Expression.Not (makeTerm1 (cons a b))
@ -610,3 +610,5 @@ manyTerm = many . term
-- | Match a term and contextualize any comments preceeding or proceeding the term.
term :: Assignment -> Assignment
term term' = contextualize comment term' <|> makeTerm1 <$> (Syntax.Context <$> some1 comment <*> emptyTerm)
{-# ANN module ("HLint: ignore Eta reduce" :: String) #-}

View File

@ -9,13 +9,8 @@ module Language.Python.Assignment
import Assigning.Assignment hiding (Assignment, Error)
import Data.Abstract.FreeVariables
import Data.Functor (void)
import Data.List.NonEmpty (some1)
import Data.Maybe (fromMaybe)
import Data.Record
import Data.Semigroup
import Data.Syntax (contextualize, emptyTerm, handleError, infixContext, makeTerm, makeTerm', makeTerm'', makeTerm1, parseError, postContextualize)
import Data.Union
import GHC.Stack
import Language.Python.Grammar as Grammar
import Language.Python.Syntax as Python.Syntax
@ -28,6 +23,7 @@ import qualified Data.Syntax.Literal as Literal
import qualified Data.Syntax.Statement as Statement
import qualified Data.Syntax.Type as Type
import qualified Data.Term as Term
import Prologue
-- | The type of Python syntax.
@ -250,7 +246,7 @@ functionDefinition
<|> makeFunctionDeclaration <$> (symbol Lambda' <|> symbol Lambda) <*> children ((,,,) <$ token AnonLambda <*> emptyTerm <*> (symbol LambdaParameters *> children (manyTerm expression) <|> pure []) <*> optional (symbol Type *> children (term expression)) <*> expressions)
where
makeFunctionDeclaration loc (functionName', functionParameters, ty, functionBody) = makeTerm loc $ Type.Annotation (makeTerm loc $ Declaration.Function [] functionName' functionParameters functionBody) (fromMaybe (makeTerm loc Syntax.Empty) ty)
makeAsyncFunctionDeclaration loc (async', functionName', functionParameters, ty, functionBody) = makeTerm loc $ Type.Annotation (makeTerm loc $ Type.Annotation (makeTerm loc $ Declaration.Function [] functionName' functionParameters functionBody) (maybe (makeTerm loc Syntax.Empty) id ty)) async'
makeAsyncFunctionDeclaration loc (async', functionName', functionParameters, ty, functionBody) = makeTerm loc $ Type.Annotation (makeTerm loc $ Type.Annotation (makeTerm loc $ Declaration.Function [] functionName' functionParameters functionBody) (fromMaybe (makeTerm loc Syntax.Empty) ty)) async'
async' :: Assignment
async' = makeTerm <$> symbol AnonAsync <*> (Syntax.Identifier <$> (name <$> source))
@ -335,7 +331,7 @@ assignment' = makeTerm <$> symbol Assignment <*> children (Statement.Assignmen
, assign Expression.BXOr <$ symbol AnonCaretEqual
])
where rvalue = expressionList <|> assignment' <|> yield
assign :: f :< Syntax => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
assign :: (f :< Syntax) => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
assign c l r = inj (Statement.Assignment [] l (makeTerm1 (c l r)))
yield :: Assignment
@ -386,7 +382,7 @@ comment = makeTerm <$> symbol Comment <*> (Comment.Comment <$> source)
import' :: Assignment
import' = makeTerm'' <$> symbol ImportStatement <*> children (manyTerm (aliasedImport <|> plainImport))
<|> makeTerm <$> symbol ImportFromStatement <*> children (Declaration.Import <$> (identifier <|> emptyTerm) <*> (wildcard <|> (some (aliasImportSymbol <|> importSymbol))) <*> emptyTerm)
<|> makeTerm <$> symbol ImportFromStatement <*> children (Declaration.Import <$> (identifier <|> emptyTerm) <*> (wildcard <|> some (aliasImportSymbol <|> importSymbol)) <*> emptyTerm)
where
-- `import a as b`
aliasedImport = makeImport <$> symbol AliasedImport <*> children ((,) <$> expression <*> (Just <$> expression))
@ -397,7 +393,7 @@ import' = makeTerm'' <$> symbol ImportStatement <*> children (manyTerm (aliase
-- `from a import foo as bar`
aliasImportSymbol = symbol AliasedImport *> children (makeNameAliasPair <$> rawIdentifier <*> (Just <$> rawIdentifier))
-- `from a import *`
wildcard = symbol WildcardImport *> source *> pure []
wildcard = symbol WildcardImport *> source $> []
rawIdentifier = (name <$> identifier') <|> (qualifiedName <$> dottedName')
dottedName' = symbol DottedName *> children (some identifier')
@ -512,3 +508,5 @@ infixTerm :: HasCallStack
-> [Assignment.Assignment [] Grammar (Term -> Term -> Union Syntax Term)]
-> Assignment.Assignment [] Grammar (Union Syntax Term)
infixTerm = infixContext comment
{-# ANN module ("HLint: ignore Eta reduce" :: String) #-}

View File

@ -121,7 +121,7 @@ expressionChoices =
, mk Yield Statement.Yield
, module'
, pair
, parenthesized_expressions
, parenthesizedExpressions
, parseError
, rescue
, scopeResolution
@ -140,8 +140,8 @@ expressionChoices =
expressions :: Assignment
expressions = makeTerm'' <$> location <*> many expression
parenthesized_expressions :: Assignment
parenthesized_expressions = makeTerm'' <$> symbol ParenthesizedStatements <*> children (many expression)
parenthesizedExpressions :: Assignment
parenthesizedExpressions = makeTerm'' <$> symbol ParenthesizedStatements <*> children (many expression)
identifier :: Assignment
identifier =
@ -241,7 +241,7 @@ lambda = makeTerm <$> symbol Lambda <*> children (
block :: Assignment
block = makeTerm <$> symbol DoBlock <*> children (Declaration.Function <$> pure [] <*> emptyTerm <*> params <*> expressions)
<|> makeTerm <$> symbol Block <*> children (Declaration.Function <$> pure [] <*> emptyTerm <*> params <*> expressions)
where params = (symbol BlockParameters) *> children (many parameter) <|> pure []
where params = symbol BlockParameters *> children (many parameter) <|> pure []
comment :: Assignment
comment = makeTerm <$> symbol Comment <*> (Comment.Comment <$> source)
@ -281,14 +281,14 @@ until' =
for :: Assignment
for = makeTerm <$> symbol For <*> children (Statement.ForEach <$> (makeTerm <$> location <*> manyTermsTill expression (symbol In)) <*> inClause <*> expressions)
where inClause = symbol In *> children (expression)
where inClause = symbol In *> children expression
case' :: Assignment
case' = makeTerm <$> symbol Case <*> children (Statement.Match <$> (symbol When *> emptyTerm <|> expression) <*> whens)
where
whens = makeTerm <$> location <*> many (when' <|> else' <|> expression)
when' = makeTerm <$> symbol When <*> children (Statement.Pattern <$> (makeTerm <$> location <*> some pattern) <*> whens)
pattern = postContextualize comment (symbol Pattern *> children ((symbol SplatArgument *> children expression) <|> expression))
when' = makeTerm <$> symbol When <*> children (Statement.Pattern <$> (makeTerm <$> location <*> some pattern') <*> whens)
pattern' = postContextualize comment (symbol Pattern *> children ((symbol SplatArgument *> children expression) <|> expression))
else' = postContextualize comment (symbol Else *> children expressions)
subscript :: Assignment
@ -303,11 +303,11 @@ methodCall = makeTerm' <$> symbol MethodCall <*> children (require <|> load <|>
regularCall = inj <$> (Expression.Call <$> pure [] <*> expression <*> args <*> (block <|> emptyTerm))
require = inj <$> (symbol Identifier *> do
s <- source
guard (elem s ["require", "require_relative"])
guard (s `elem` ["require", "require_relative"])
Ruby.Syntax.Require (s == "require_relative") <$> nameExpression)
load = inj <$> (symbol Identifier *> do
s <- source
guard (elem s ["load"])
guard (s == "load")
Ruby.Syntax.Load <$> loadArgs)
args = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children (many expression) <|> pure []
loadArgs = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children (some expression)
@ -316,7 +316,7 @@ methodCall = makeTerm' <$> symbol MethodCall <*> children (require <|> load <|>
call :: Assignment
call = makeTerm <$> symbol Call <*> children (Expression.MemberAccess <$> expression <*> (expression <|> args))
where
args = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children (expressions)
args = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children expressions
rescue :: Assignment
rescue = rescue'
@ -350,7 +350,7 @@ assignment' = makeTerm <$> symbol Assignment <*> children (Statement.As
, assign Expression.BXOr <$ symbol AnonCaretEqual
])
where
assign :: f :< Syntax => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
assign :: (f :< Syntax) => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
assign c l r = inj (Statement.Assignment [] l (makeTerm1 (c l r)))
lhs = makeTerm <$> symbol LeftAssignmentList <*> children (many expr) <|> expr
@ -423,3 +423,5 @@ infixTerm :: HasCallStack
-> [Assignment.Assignment [] Grammar (Term -> Term -> Union Syntax Term)]
-> Assignment.Assignment [] Grammar (Union Syntax Term)
infixTerm = infixContext comment
{-# ANN module ("HLint: ignore Eta reduce" :: String) #-}

View File

@ -245,7 +245,7 @@ augmentedAssignmentExpression = makeTerm' <$> symbol AugmentedAssignmentExpressi
, assign Expression.RShift <$ symbol AnonRAngleRAngleEqual
, assign Expression.UnsignedRShift <$ symbol AnonRAngleRAngleRAngleEqual
, assign Expression.BOr <$ symbol AnonPipeEqual ])
where assign :: f :< Syntax => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
where assign :: (f :< Syntax) => (Term -> Term -> f Term) -> Term -> Term -> Union Syntax Term
assign c l r = inj (Statement.Assignment [] l (makeTerm1 (c l r)))
@ -300,7 +300,7 @@ abstractMethodSignature = makeSignature <$> symbol Grammar.AbstractMethodSignatu
where makeSignature loc (modifier, propertyName, (typeParams, params, annotation)) = makeTerm loc (TypeScript.Syntax.AbstractMethodSignature [modifier, typeParams, annotation] propertyName params)
classHeritage' :: Assignment.Assignment [] Grammar [Term]
classHeritage' = symbol Grammar.ClassHeritage *> children (((++) `on` toList) <$> optional (term extendsClause) <*> optional (term implementsClause'))
classHeritage' = symbol Grammar.ClassHeritage *> children ((mappend `on` toList) <$> optional (term extendsClause) <*> optional (term implementsClause'))
extendsClause :: Assignment
extendsClause = makeTerm <$> symbol Grammar.ExtendsClause <*> children (TypeScript.Syntax.ExtendsClause <$> manyTerm (typeReference <|> expression))
@ -643,7 +643,7 @@ importStatement = makeImportTerm <$> symbol Grammar.ImportStatement <*> childr
<|> makeTerm' <$> symbol Grammar.ImportStatement <*> children (requireImport <|> sideEffectImport)
where
-- `import foo = require "./foo"`
requireImport = inj <$> (symbol Grammar.ImportRequireClause *> children (flip Declaration.QualifiedImport <$> (term identifier) <*> term fromClause <*> pure []))
requireImport = inj <$> (symbol Grammar.ImportRequireClause *> children (flip Declaration.QualifiedImport <$> term identifier <*> term fromClause <*> pure []))
-- `import "./foo"`
sideEffectImport = inj <$> (Declaration.SideEffectImport <$> term fromClause <*> emptyTerm)
-- `import { bar } from "./foo"`
@ -651,7 +651,7 @@ importStatement = makeImportTerm <$> symbol Grammar.ImportStatement <*> childr
-- `import defaultMember from "./foo"`
defaultImport = (,,,) <$> pure Prelude.False <*> pure Nothing <*> (pure <$> (makeNameAliasPair <$> rawIdentifier <*> pure Nothing)) <*> emptyTerm
-- `import * as name from "./foo"`
namespaceImport = symbol Grammar.NamespaceImport *> children ((,,,) <$> pure Prelude.True <*> (Just <$> (term identifier)) <*> pure [] <*> emptyTerm)
namespaceImport = symbol Grammar.NamespaceImport *> children ((,,,) <$> pure Prelude.True <*> (Just <$> term identifier) <*> pure [] <*> emptyTerm)
-- Combinations of the above.
importClause = symbol Grammar.ImportClause *>
@ -666,7 +666,7 @@ importStatement = makeImportTerm <$> symbol Grammar.ImportStatement <*> childr
makeImportTerm1 loc from (_, _, symbols, extra) = makeTerm loc (Declaration.Import from symbols extra)
makeImportTerm loc ([x], from) = makeImportTerm1 loc from x
makeImportTerm loc (xs, from) = makeTerm loc $ fmap (makeImportTerm1 loc from) xs
importSymbol = symbol Grammar.ImportSpecifier *> children (makeNameAliasPair <$> rawIdentifier <*> ((Just <$> rawIdentifier) <|> (pure Nothing)))
importSymbol = symbol Grammar.ImportSpecifier *> children (makeNameAliasPair <$> rawIdentifier <*> ((Just <$> rawIdentifier) <|> pure Nothing))
rawIdentifier = (symbol Identifier <|> symbol Identifier') *> (name <$> source)
makeNameAliasPair from (Just alias) = (from, alias)
makeNameAliasPair from Nothing = (from, from)
@ -717,13 +717,13 @@ ambientDeclaration :: Assignment
ambientDeclaration = makeTerm <$> symbol Grammar.AmbientDeclaration <*> children (TypeScript.Syntax.AmbientDeclaration <$> term (choice [declaration, statementBlock]))
exportStatement :: Assignment
exportStatement = makeTerm <$> symbol Grammar.ExportStatement <*> (children (flip Declaration.QualifiedExportFrom <$> exportClause <*> term fromClause))
exportStatement = makeTerm <$> symbol Grammar.ExportStatement <*> children (flip Declaration.QualifiedExportFrom <$> exportClause <*> term fromClause)
<|> makeTerm <$> symbol Grammar.ExportStatement <*> children (Declaration.QualifiedExport <$> exportClause)
<|> makeTerm <$> symbol Grammar.ExportStatement <*> children (Declaration.DefaultExport <$> contextualize decorator (term (declaration <|> expression <|> identifier <|> importAlias')))
where
exportClause = symbol Grammar.ExportClause *> children (many exportSymbol)
exportSymbol = symbol Grammar.ExportSpecifier *> children (makeNameAliasPair <$> rawIdentifier <*> (Just <$> rawIdentifier))
<|> symbol Grammar.ExportSpecifier *> children (makeNameAliasPair <$> rawIdentifier <*> (pure Nothing))
<|> symbol Grammar.ExportSpecifier *> children (makeNameAliasPair <$> rawIdentifier <*> pure Nothing)
makeNameAliasPair from (Just alias) = (from, alias)
makeNameAliasPair from Nothing = (from, from)
rawIdentifier = (symbol Identifier <|> symbol Identifier') *> (name <$> source)