Skip to content

Commit

Permalink
fixup! fixup! [#64] Implement copy/paste protection checks
Browse files Browse the repository at this point in the history
Review: style, typos, naming
  • Loading branch information
YuriRomanowski committed Dec 16, 2022
1 parent 337bec6 commit 9a6aedc
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Unreleased
* [#231](https://github.com/serokell/xrefcheck/pull/231)
+ Anchor analysis takes now into account the appropriate case-sensitivity depending on
the configured Markdown flavour.
* [240](https://github.com/serokell/xrefcheck/pull/240)
* [#240](https://github.com/serokell/xrefcheck/pull/240)
+ Now xrefcheck is able to detect possible copy-pastes relying on links and their names.

0.2.2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ There are several ways to fix this:
followed by an <!-- xrefcheck: no duplication check in link --> [copypasted intentionally](https://good.link.uri/).
```
* You can use a `<!-- xrefcheck: no duplication check in paragraph -->` annotation to disable copy-paste check in a paragraph.
* You can use a `<!-- xrefcheck: no duplication check in file -->` annotation to disable copy-paste check within an entire file.
* You can use a `<!-- xrefcheck: no duplication check in file -->` annotation at the top of the file to disable copy-paste check within an entire file.

## Further work [↑](#xrefcheck)

Expand Down
4 changes: 2 additions & 2 deletions src/Xrefcheck/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ data FileInfoDiff = FileInfoDiff
makeLenses ''FileInfoDiff

diffToFileInfo :: Bool -> FileInfoDiff -> FileInfo
diffToFileInfo ignoreCpcInFile (FileInfoDiff refs anchors) =
FileInfo (DList.toList refs) (DList.toList anchors) ignoreCpcInFile
diffToFileInfo cpcEnabledInFile (FileInfoDiff refs anchors) =
FileInfo (DList.toList refs) (DList.toList anchors) cpcEnabledInFile

instance Semigroup FileInfoDiff where
FileInfoDiff a b <> FileInfoDiff c d = FileInfoDiff (a <> c) (b <> d)
Expand Down
2 changes: 1 addition & 1 deletion src/Xrefcheck/Scan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ instance Buildable ScanErrorDescription where
ParagraphErrCpc txt -> [int||Expected a PARAGRAPH after \
"no duplication check in paragraph" annotation, but found #{txt}|]
UnrecognisedErr txt -> [int||Unrecognised option "#{txt}", perhaps you meant
<"ignore link"|"ignore paragraph"|"ignore all">
"ignore <link|paragraph|all>"
or "no duplication check in <link|paragraph|file>"?|]

specificFormatsSupport :: [([Extension], ScanAction)] -> FormatsSupport
Expand Down
49 changes: 25 additions & 24 deletions src/Xrefcheck/Scanners/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ processAnnotations fp = withIgnoreMode . cataNodeWithParentNodeInfo process
IMAGE {} -> handleLink ign ty traverseChildren
_ -> handleOther ign ty traverseChildren

handleLink ::
Maybe Ignore ->
NodeType ->
ScannerM NodeCPC ->
ScannerM NodeCPC
handleLink
:: Maybe Ignore
-> NodeType
-> ScannerM NodeCPC
-> ScannerM NodeCPC
handleLink ign ty traverseChildren = do
-- It's common for all ignore states
ssIgnore .= Nothing
Expand All @@ -230,10 +230,10 @@ processAnnotations fp = withIgnoreMode . cataNodeWithParentNodeInfo process
Just (Ignore (IMSLink _) _) -> do
pure defNode

handleParagraph ::
Maybe Ignore ->
ScannerM NodeCPC ->
ScannerM NodeCPC
handleParagraph
:: Maybe Ignore
-> ScannerM NodeCPC
-> ScannerM NodeCPC
handleParagraph ign traverseChildren = do
-- If a new paragraph was expected (this stands for True), now we
-- don't expect paragraphs any more.
Expand Down Expand Up @@ -263,11 +263,11 @@ processAnnotations fp = withIgnoreMode . cataNodeWithParentNodeInfo process

pure node

handleOther ::
Maybe Ignore ->
NodeType ->
ScannerM NodeCPC ->
ScannerM NodeCPC
handleOther
:: Maybe Ignore
-> NodeType
-> ScannerM NodeCPC
-> ScannerM NodeCPC
handleOther ign ty traverseChildren = do
-- If right now there was a copy/paste ignore annotation for paragraph,
-- emit an error and reset these states.
Expand Down Expand Up @@ -295,8 +295,9 @@ processAnnotations fp = withIgnoreMode . cataNodeWithParentNodeInfo process
Just (IMSLink _) -> ssIgnoreCopyPasteCheck .= Nothing
_ -> pass

reportExpectedParagraphAfterIgnoreCpcAnnotation ::
NodeType -> ScannerM ()
reportExpectedParagraphAfterIgnoreCpcAnnotation
:: NodeType
-> ScannerM ()
reportExpectedParagraphAfterIgnoreCpcAnnotation ty =
use ssIgnoreCopyPasteCheck >>= \case
Just (Ignore IMSParagraph modePos) ->
Expand All @@ -306,11 +307,11 @@ processAnnotations fp = withIgnoreMode . cataNodeWithParentNodeInfo process
ssIgnoreCopyPasteCheck .= Nothing
_ -> pass

wrapTraverseNodeWithLinkExpected ::
IgnoreLinkState ->
Maybe PosInfo ->
ScannerM NodeCPC ->
ScannerM NodeCPC
wrapTraverseNodeWithLinkExpected
:: IgnoreLinkState
-> Maybe PosInfo
-> ScannerM NodeCPC
-> ScannerM NodeCPC
wrapTraverseNodeWithLinkExpected ignoreLinkState modePos =
if ignoreLinkState /= ExpectingLinkInSubnodes
then id
Expand All @@ -325,9 +326,9 @@ processAnnotations fp = withIgnoreMode . cataNodeWithParentNodeInfo process
_ -> pass
return node'

wrapTraverseNodeWithLinkExpectedForCpc ::
ScannerM NodeCPC ->
ScannerM NodeCPC
wrapTraverseNodeWithLinkExpectedForCpc
:: ScannerM NodeCPC
-> ScannerM NodeCPC
wrapTraverseNodeWithLinkExpectedForCpc traverse' = do
ignoreCpc <- use ssIgnoreCopyPasteCheck
case ignoreCpc of
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/check-copy-paste/expected.gold
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
scan error at src:35:1-25:

Unrecognised option "no dh", perhaps you meant
<"ignore link"|"ignore paragraph"|"ignore all">
"ignore <link|paragraph|all>"
or "no duplication check in <link|paragraph|file>"?

➥ In file second-file.md
Expand Down
2 changes: 1 addition & 1 deletion tests/golden/check-scan-errors/expected.gold
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
scan error at src:21:1-50:

Unrecognised option "ignore unrecognised-annotation", perhaps you meant
<"ignore link"|"ignore paragraph"|"ignore all">
"ignore <link|paragraph|all>"
or "no duplication check in <link|paragraph|file>"?

➥ In file check-second-file.md
Expand Down

0 comments on commit 9a6aedc

Please sign in to comment.