Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#64] Add annotations for the copypaste check #246

Open
wants to merge 3 commits into
base: YuriRomanowski/#64-Implement-copy-paste-protection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! fixup! [#64] Implement copy/paste protection checks
Review: style, typos, naming
  • Loading branch information
YuriRomanowski committed Dec 16, 2022
commit 9a6aedca0c91a77648edb4b2ba638a2fa744c9a5
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>"?|]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two kind of annotation suggestions look a bit different. Maybe would be better something like

"ignore <link|paragraph|all>"
or "no duplication check in <link|paragraph|file>"

or

<"ignore link"|"ignore paragraph"|"ignore all">
or <"no duplication check in link"|"no duplication check in paragraph"|"no duplication check in 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