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

Support elm-format 0.8 (and hence Elm 0.19) #4

Merged
merged 9 commits into from
Sep 16, 2018
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
25 changes: 19 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
## 0.4.0 (ongoing)

- Upgrade `elm-format` to `0.8` to support Elm `0.19` ([#4](https://github.com/gicentre/prettier-plugin-elm/issues/4))

This includes replacing `module Main exposing (..)` with `module Main exposing (something)` in tests, because this is what the new version of `elm-format` produces.

To support formatting of Elm code in markdown blocks, `prettier-plugin-elm` passes `--elm-version=0.19` to `elm-format`.
If you use Elm `0.18`, please install `prettier-plugin-elm@0.3`.

- Internal:
- Upgrade dev dependencies
- Add pre-push git hook via [`husky`](https://github.com/typicode/husky) to avoid dumb linting errors in CI.

## 0.3.0 (2018-05-23)

* Implement caching mechanism to speed-up formatting of Elm blocks in markdown files ([#3](https://github.com/gicentre/prettier-plugin-elm/issues/3))
- Implement caching mechanism to speed-up formatting of Elm blocks in markdown files ([#3](https://github.com/gicentre/prettier-plugin-elm/issues/3))

* Simplify `locStart()` and `locEnd()` ([#2](https://github.com/gicentre/prettier-plugin-elm/issues/2))
- Simplify `locStart()` and `locEnd()` ([#2](https://github.com/gicentre/prettier-plugin-elm/issues/2))

* Configure automatic testing on Windows using [AppVeyor](http://appveyor.com/).
- Configure automatic testing on Windows using [AppVeyor](http://appveyor.com/).

## 0.2.2 & 0.2.1 & 0.2.0 (2018-04-12)

* Switch to `elm-format@0.7.0-exp`
* Use correct instance of `elm-format` when globally installed with npm ([#1](https://github.com/gicentre/prettier-plugin-elm/issues/1))
* Fix ` ```markdown ` syntax highlighting on GitHub and NPM
- Switch to `elm-format@0.7.0-exp`
- Use correct instance of `elm-format` when globally installed with npm ([#1](https://github.com/gicentre/prettier-plugin-elm/issues/1))
- Fix ` ```markdown ` syntax highlighting on GitHub and NPM

## 0.1.0 (2018-03-16)

Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ main =
```
````

In order to successfully format Elm code in markdown blocks, `prettier-plugin-elm` assumes your Elm version is 0.19.
If you use Elm 0.18, please install `prettier-plugin-elm@0.3`.

## Getting started

Simply install `prettier` and `prettier-plugin-elm` as your project’s npm dependencies:
Expand Down Expand Up @@ -156,10 +159,10 @@ If you’re interested in contributing to the development of Prettier for Elm, y

To test it out on an Elm file:

* Clone this repository.
* Run `yarn`.
* Create a file called `prettier-test.elm` or `prettier-test.md`.
* Run `yarn prettier prettier-test.elm` or `yarn prettier prettier-test.md` to check the output.
- Clone this repository.
- Run `yarn`.
- Create a file called `prettier-test.elm` or `prettier-test.md`.
- Run `yarn prettier prettier-test.elm` or `yarn prettier prettier-test.md` to check the output.

## Credits

Expand Down
30 changes: 18 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@
"prepublishOnly": "yarn lint && yarn test"
},
"devDependencies": {
"eslint": "^4.19.1",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jest": "^21.15.0",
"eslint-plugin-prettier": "^2.6.0",
"jest": "^22.4.3",
"jest-runner-eslint": "^0.4.0",
"prettier": "^1.12.0",
"eslint": "^5.6.0",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jest": "^21.22.0",
"eslint-plugin-prettier": "^2.6.2",
"jest": "^23.6.0",
"jest-runner-eslint": "^0.6.0",
"prettier": "^1.14.2",
"prettier-check": "^2.0.0",
"rimraf": "^2.6.2",
"sleep-promise": "^6.0.0"
"sleep-promise": "^8.0.1"
},
"dependencies": {
"elm-format": "0.7.0-exp",
"execa": "^0.10.0",
"elm-format": "^0.8.1-rc2",
"execa": "^1.0.0",
"husky": "^1.0.0-rc.14",
"make-dir": "^1.3.0",
"object-hash": "^1.3.0",
"serialize-error": "^2.1.0",
Expand All @@ -51,5 +52,10 @@
"prettier": {},
"eslintIgnore": [
"coverage"
]
],
"husky": {
"hooks": {
"pre-push": "yarn lint"
}
}
}
37 changes: 21 additions & 16 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,47 @@

const pkg = require("../package.json");
const { getCachedValue } = require("./cache");
const { formatTextWithElmFormat } = require("./util");
const { formatTextWithElmFormat, getElmFormatVersion } = require("./util");

/*
* Simply passing text to elm-format is not enough because of two problems:
*
* 1. If the code chunk contains no symbol assignments, elm-format fails.
* 2. `module Main exposing (..)` is always added if no module has been defined,
* 1. If the code chunk only contains an import statement, no formatting takes place.
* Related to: https://github.com/avh4/elm-format/issues/551
* 2. `module Main exposing (.*)` is always added if no module has been defined,
* which is not wanted in markdown code blocks.
* Related to: https://github.com/avh4/elm-format/issues/65
*
* Both problems are related to https://github.com/avh4/elm-format/issues/65.
* Until this upstream issue is fixed, two custom patches are applied:
* Until the upstream issues are fixed, two custom patches are applied:
*
* 1. A temporary dummy statement is appended to text before sending it to elm-format.
* 2. If elm-format's result defines a module while the source does not,
* module definition is trimmed while working with a markdown code block.
* 1. A dummy comment is added to the source and is then removed from the result.
* 2. If current context is a markdown block and the source does not declare a module,
* module statement is removed from the result.
*
* Please submit an issue to https://github.com/gicentre/prettier-plugin-elm/issues
* if there are any problems caused by the patches.
*/

const dummyStatement = "\ndummyPrettierPluginElmSymbol = identity";
const dummyStatementRegExp = /\s*dummyPrettierPluginElmSymbol =\s+identity\n$/;
const autogeneratedModuleDefinitionRegExp = /[ \t]*module\s+Main\s+exposing\s+\(\s*\.\.\s*\)\s*/;
const dummyComment = "\n{- DUMMY COMMENT BY PRETTIER-PLUGIN-ELM -}";
const dummyCommentRemovalRegExp = /\n*\{- DUMMY COMMENT BY PRETTIER-PLUGIN-ELM -\}/;
const autogeneratedModuleDefinitionRegExp = /[ \t]*module\s+Main\s+exposing\s+\([^)\n]*\)\s*/;

function parse(text, parsers, opts) {
// patch 1 (step 1/2)
const textToSend = `${text}${dummyStatement}`;
// patch 1 step 1
const textToSend = `${text}${dummyComment}`;

// extract formatted text from elm-format
let formattedText = getCachedValue(formatTextWithElmFormat, [
textToSend,
pkg.version
pkg.version,
getElmFormatVersion()
]);

// path 1 (step 2/2)
formattedText = formattedText.replace(dummyStatementRegExp, "\n");
// patch 1 step 2
formattedText = formattedText.replace(dummyCommentRemovalRegExp, "");
if (formattedText === "\n") {
formattedText = "";
}

// patch 2
if (
Expand Down
15 changes: 14 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

const execa = require("execa");

let cachedElmFormatVersion;
function getElmFormatVersion() {
if (!cachedElmFormatVersion) {
// a cleaner way of getting elm-format version
// will be possible when this issue is closed:
// https://github.com/avh4/elm-format/issues/425
const help = execa.sync("elm-format", ["--help"]).stdout;
cachedElmFormatVersion = help.match(/elm-format ([^\n]+)/)[1];
}
return cachedElmFormatVersion;
}

function formatTextWithElmFormat(text) {
return execa.sync("elm-format", ["--stdin"], {
return execa.sync("elm-format", ["--stdin", "--elm-version=0.19"], {
input: text,
preferLocal: true,
localDir: __dirname,
Expand All @@ -12,5 +24,6 @@ function formatTextWithElmFormat(text) {
}

module.exports = {
getElmFormatVersion,
formatTextWithElmFormat
};
6 changes: 3 additions & 3 deletions test/caching.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ test(`correctly deals with cache`, () => {
plugins: [path.resolve(__dirname, "..")]
})
).toEqual(expectedFormattedText);
expect(spyForFormatTextWithElmFormat.mock.calls).toHaveLength(
expect(spyForFormatTextWithElmFormat.mock.calls.length).toBe(
numberOfFormatCallsInFirstRun
);

return sleep(cacheGCInterval * 2).then(() => {
// a call to formatTextWithElmFormat() that triggers garbage collection
prettier.format("", {
prettier.format("{- -}", {
parser: "elm",
plugins: [path.resolve(__dirname, "..")]
});
Expand All @@ -69,7 +69,7 @@ test(`correctly deals with cache`, () => {
plugins: [path.resolve(__dirname, "..")]
})
).toEqual(expectedFormattedText);
expect(spyForFormatTextWithElmFormat.mock.calls).toHaveLength(
expect(spyForFormatTextWithElmFormat.mock.calls.length).toBe(
numberOfFormatCallsInFirstRun + 1 /* for "" */ + 1 /* for GC-d block */
);
});
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/empty.prettified.elm

This file was deleted.

2 changes: 1 addition & 1 deletion test/fixtures/hello-world.prettified.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Main exposing (..)
module Main exposing (main)

import Html exposing (text)

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/leading-comment.prettified.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{- a comment in Elm code -}


module Main exposing (..)
module Main exposing (main)

import Html exposing (text)

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/let-in.prettified.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Source: https://github.com/avh4/elm-format/issues/350


module Main exposing (..)
module Main exposing (viewEntries)


viewEntries : String -> List Entry -> Html Msg
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/multiple-blocks.prettified.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
id: "litvis"
elm:
dependencies:
gicentre/elm-vega: latest
dependencies:
gicentre/elm-vega: latest
---

<!-- source (with minor amendments): https://github.com/gicentre/litvis/blob/master/documents/tutorials/geoTutorials/geoFormats.md -->
Expand Down Expand Up @@ -631,6 +631,7 @@ graticule gStep =
meridian lng =
if round lng % 90 == 0 then
List.map (\lat -> ( lng, lat )) (range -90 90 (min 10 gStep))

else
List.map (\lat -> ( lng, lat )) (range (gStep - 90) (90 - gStep) (min 5 gStep))

Expand Down
Loading