Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions core/Parsers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FirstLineRes> =
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

Comment thread
dnut marked this conversation as resolved.
let ignoreAll ctx = Parsing_Internal.ignoreAll ctx

let godoc : ContentParser = fun _ctx ->
Expand Down
13 changes: 11 additions & 2 deletions core/Parsing.Documents.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions docs/specs/content-types/eslint.md
Original file line number Diff line number Diff line change
@@ -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 ¦
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading