diff --git a/core/Parsers.fs b/core/Parsers.fs index 79e6d25..5dabf59 100644 --- a/core/Parsers.fs +++ b/core/Parsers.fs @@ -35,6 +35,34 @@ let dartdoc : ContentParser -> ContentParser = let dartdoc_markdown ctx = dartdoc markdown_noHeader ctx +/// ESLint configuration comments parser that prevents wrapping of disable/enable lines +/// https://github.com/dnut/Rewrap/issues/33 +let eslintConfigComments : ContentParser -> ContentParser = + fun content ctx -> + + let tryMatchDirective : Line -> Option = + tryMatch' (regex @"^\s*(eslint-disable-next-line|eslint-disable-line|eslint-disable|eslint-enable)\b") <>>> fun (_, line) -> + finished_ line noWrapBlock + + let rec wrapFLR : FirstLineRes -> FirstLineRes = function + | Pending r -> Pending ^| wrapResultParser nlpWrapper r + | Finished r -> Finished ^| wrapResultParser (fun p -> Some (flpWrapper p)) r + + and flpWrapper maybeInnerParser : FirstLineParser = + (tryMatchDirective |? (maybeInnerParser |? content ctx)) >> wrapFLR + + and nlpWrapper innerParser : NextLineParser = + fun line -> + tryMatchDirective line + |> map (FinishedOnPrev << Some) + |> Option.defaultWith ^| fun _ -> + match innerParser line with + | ThisLine r -> ThisLine (wrapFLR r) + | FinishedOnPrev maybeR -> + FinishedOnPrev (wrapFLR <<|> (maybeR <|> (fun _ -> Some (content ctx line)))) + + (tryMatchDirective |? content ctx) >> wrapFLR + let ignoreAll ctx = Parsing_Internal.ignoreAll ctx let godoc : ContentParser = fun _ctx -> diff --git a/core/Parsing.Documents.fs b/core/Parsing.Documents.fs index 6380590..c7e0efc 100644 --- a/core/Parsing.Documents.fs +++ b/core/Parsing.Documents.fs @@ -46,6 +46,15 @@ let private configFile = sc [line "#"] let java : DocumentProcessor = sc [ jsDocBlock; cBlock; line' "//[/!]" jsdoc_markdown; line "//" ] +let private cBlockWithEslint = + block' ("*", "") (@"/\*", @"\*/") (eslintConfigComments markdown) + +let private jsDocBlockWithEslint = + block' ("*", " * ") javadocMarkers (eslintConfigComments jsdoc_markdown) + +let javascript : DocumentProcessor = + sc [ jsDocBlockWithEslint; cBlockWithEslint; line' "//[/!]" (eslintConfigComments jsdoc_markdown); line' "//" (eslintConfigComments markdown) ] + // Takes 4 args to create a Language: // 1. display name (used only in VS) // 2. string of aliases (language IDs used by the client. Not needed if they only differ @@ -129,7 +138,7 @@ let mutable languages = [ lang "INI" "" ".ini" <| sc [line "[#;]"] lang "J" "" ".ijs" <| sc [line @"NB\."] lang "Java" "" ".java" java - lang "JavaScript" "javascriptreact|js" ".js|.jsx" java + lang "JavaScript" "javascriptreact|js" ".js|.jsx" javascript lang "Julia" "" ".jl" <| sc [block ("#=", "=#"); line "#"; block (@".*?""""""", "\"\"\"")] lang "JSON" "json5|jsonc" ".json|.json5|.jsonc" java lang "LaTeX" "tex" ".bbx|.cbx|.cls|.sty|.tex" @@ -182,7 +191,7 @@ let mutable languages = [ lang "Tcl" "" ".tcl" <| configFile lang "Textile" "" ".textile" <| docOf markdown lang "TOML" "" ".toml" <| configFile - lang "TypeScript" "typescriptreact" ".ts|.tsx" java + lang "TypeScript" "typescriptreact" ".ts|.tsx" javascript lang "Verilog/SystemVerilog" "systemverilog|verilog" ".sv|.svh|.v|.vh|.vl" java lang "XAML" "" ".xaml" html diff --git a/docs/specs/content-types/eslint.md b/docs/specs/content-types/eslint.md new file mode 100644 index 0000000..2479c8a --- /dev/null +++ b/docs/specs/content-types/eslint.md @@ -0,0 +1,44 @@ +# ESLint Directives + +JavaScript and TypeScript ESLint directives must remain on a single line to preserve their +effect, so Rewrap does not wrap them. + + +## Line Comments + +> language: javascript + + // eslint-disable-next-line @typescript-eslint/no-base-to-string -- ModuleSource returns -> // eslint-disable-next-line @typescript-eslint/no-base-to-string -- ModuleSource returns + nextLoad(url).source ¦ nextLoad(url).source ¦ + +> language: typescript + + // eslint-disable-line @typescript-eslint/no-base-to-string -- ModuleSource returns -> // eslint-disable-line @typescript-eslint/no-base-to-string -- ModuleSource returns + nextLoad(url).source ¦ nextLoad(url).source ¦ + + // eslint-disable no-console, @typescript-eslint/no-base-to-string -- temporary exception -> // eslint-disable no-console, @typescript-eslint/no-base-to-string -- temporary exception + nextLoad(url).source ¦ nextLoad(url).source ¦ + + // eslint-enable no-console, @typescript-eslint/no-base-to-string -- restore lint checks -> // eslint-enable no-console, @typescript-eslint/no-base-to-string -- restore lint checks + nextLoad(url).source ¦ nextLoad(url).source ¦ + + // Some comment text ¦ -> // Some comment text ¦ + // eslint-disable-next-line @typescript-eslint/no-base-to-string -- more comment text // eslint-disable-next-line @typescript-eslint/no-base-to-string -- more comment text + nextLoad(url).source ¦ nextLoad(url).source ¦ + + +## Block Comments + +> language: javascript + + /* eslint-disable no-console, @typescript-eslint/no-base-to-string -- temporary exception */ -> /* eslint-disable no-console, @typescript-eslint/no-base-to-string -- temporary exception */ + nextLoad(url).source ¦ nextLoad(url).source ¦ + + /* eslint-enable no-console, @typescript-eslint/no-base-to-string -- restore lint checks */ -> /* eslint-enable no-console, @typescript-eslint/no-base-to-string -- restore lint checks */ + nextLoad(url).source ¦ nextLoad(url).source ¦ + + /* ¦ -> /* ¦ + * Some comment text ¦ * Some comment text ¦ + * eslint-disable no-console -- more comment text * eslint-disable no-console -- more comment text + */ ¦ */ ¦ + nextLoad(url).source ¦ nextLoad(url).source ¦ diff --git a/mkdocs.yml b/mkdocs.yml index 280971e..e938830 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -68,6 +68,7 @@ nav: - specs/content-types/dart.md - specs/content-types/doxygen.md - specs/content-types/elixir.md + - specs/content-types/eslint.md - specs/content-types/go.md - specs/content-types/graphql.md - specs/content-types/html.md