-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8c6674d
Showing
81 changed files
with
9,194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"env": { | ||
"node": true, | ||
"es6": true, | ||
"jest": true | ||
}, | ||
"extends": ["eslint:recommended", "prettier"], | ||
"plugins": ["prettier"], | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly", | ||
"run_spec": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"prettier/prettier": ["error"], | ||
"no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": false }] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# To get started with Dependabot version updates, you'll need to specify which | ||
# package ecosystems to update and where the package manifests are located. | ||
# Please see the documentation for all configuration options: | ||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: 'npm' | ||
directory: '/' | ||
schedule: | ||
interval: 'weekly' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node: ['10', '12', '14'] | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
|
||
- name: Get yarn cache directory path | ||
id: yarn-cache-dir-path | ||
run: echo "::set-output name=dir::$(yarn cache dir)" | ||
|
||
- uses: actions/cache@v1 | ||
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) | ||
with: | ||
path: ${{ steps.yarn-cache-dir-path.outputs.dir }} | ||
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-yarn- | ||
- run: yarn install --frozen-lockfile | ||
- run: yarn lint | ||
- run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
node_modules/ | ||
.vscode/ | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"singleQuote": true, | ||
"printWidth": 120 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Changelog | ||
|
||
All notable changes to the prettier-html-templates will be documented in this file. | ||
|
||
## v.0.1.0 - 7 February 2021 | ||
|
||
Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2021 Adam Zapaśnik | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Prettier HTML Templates | ||
|
||
This package is used to build prettier plugins to format html files that include template languages' markup, like jinja, erb and others. | ||
|
||
## Existing plugins built on top of this package | ||
|
||
### Elixir | ||
|
||
[prettier-plugin-eex](https://github.com/adamzapasnik/prettier-plugin-eex) | ||
|
||
### Ruby | ||
|
||
[prettier-plugin-erb](https://github.com/adamzapasnik/prettier-plugin-erb) | ||
|
||
## How to build a plugin? | ||
|
||
This section is currently missing. Please use one of the plugins above to figure it out. Feel free to ask questions via Issues. | ||
|
||
## Known issues | ||
|
||
- class attributes are not formatted | ||
- style attributes are not formatted | ||
- prettier ignore comments don't work | ||
- missing documentation | ||
- more tests (need 100% coverage, atm 99%) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
const ENABLE_CODE_COVERAGE = !!process.env.ENABLE_CODE_COVERAGE; | ||
|
||
module.exports = { | ||
setupFiles: ['<rootDir>/tests_config/run_spec.js'], | ||
snapshotSerializers: ['jest-snapshot-serializer-raw'], | ||
testRegex: 'jsfmt\\.spec\\.js$|tests/.*\\.js$', | ||
collectCoverage: ENABLE_CODE_COVERAGE, | ||
testEnvironment: 'node', | ||
transform: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
const { breakParent, concat, group, line } = require('prettier').doc.builders; | ||
const { isInElement, decodeInAttributes } = require('./decoder/attributes_decoder'); | ||
const { isInTableOrHead, decodeInTableOrHead } = require('./decoder/table_and_head_decoder'); | ||
const { | ||
isSelfClosingInText, | ||
decodeSelfClosingInText, | ||
isSelfClosingAfterOpenTag, | ||
} = require('./decoder/html_body_decoder'); | ||
|
||
const decodeExpressions = (expressionMap) => { | ||
const opts = { removeWhitespace: false }; | ||
const scriptTagExpressions = []; | ||
|
||
return (doc) => { | ||
if (!doc.parts || (!expressionMap.size && !opts.removeWhitespace)) return doc; | ||
|
||
const parts = [...doc.parts]; | ||
const decodedParts = []; | ||
|
||
// it also deals with head it seems | ||
// is in nonTextElement! | ||
if (isInTableOrHead(parts)) { | ||
// deals with non conditional expressions in table/head elements | ||
const partlyDecodedDoc = { ...doc, parts: decodeInTableOrHead(parts, expressionMap) }; | ||
// deals with the rest of encoded | ||
return decodeExpressions(expressionMap)(partlyDecodedDoc); | ||
} | ||
|
||
if (isSelfClosingInText(parts)) { | ||
const { removeWhitespace, decodedParts: newDecodedParts } = decodeSelfClosingInText(parts, expressionMap); | ||
opts.removeWhitespace = removeWhitespace; | ||
decodedParts.push(...newDecodedParts); | ||
} else if (isInElement(parts)) { | ||
decodedParts.push(...decodeInAttributes(parts, expressionMap)); | ||
} else { | ||
for (const part of parts) { | ||
if (part === '</script>') { | ||
for (const match of scriptTagExpressions) { | ||
expressionMap.delete(match); | ||
} | ||
|
||
decodedParts.push(part); | ||
continue; | ||
} | ||
// ORIGINAL: e <em><% e %></em>. | ||
// WITH: e <em><% e %></em>. | ||
// WITHOUT: e <em><% e %> </em>. | ||
if (part === ' ' && opts.removeWhitespace) { | ||
opts.removeWhitespace = false; | ||
|
||
continue; | ||
} | ||
|
||
// ORIGINAL: <span><% e %></span> | ||
// WITH: <span><% e %></span> | ||
// WITHOUT: <span><% e %> </span> | ||
if (part.type === 'line' && !part.soft && opts.removeWhitespace) { | ||
opts.removeWhitespace = false; | ||
|
||
continue; | ||
} | ||
|
||
// <script src="<%= static_url(@conn, "/js/app.js") %>"></script> | ||
if (/eex\d+eex/.test(part.contents)) { | ||
const decodedContents = part.contents.replace(/eex\d+eex/g, (match) => { | ||
const expression = expressionMap.get(match); | ||
expressionMap.delete(match); | ||
|
||
return expression.print; | ||
}); | ||
|
||
decodedParts.push({ ...part, contents: decodedContents }); | ||
continue; | ||
} | ||
|
||
// Deals with expressions between script tags | ||
if (/eexs\d+eexs/.test(part)) { | ||
const decodedPart = part.replace(/eexs\d+eexs/, (match) => { | ||
const expression = expressionMap.get(match); | ||
// Match can't be deleted immediately from expressionMap because it could be reused | ||
// That's why we remove them after closing script tag | ||
// script.html.test console.log(...) | ||
scriptTagExpressions.push(match); | ||
|
||
return expression.print; | ||
}); | ||
|
||
decodedParts.push(decodedPart); | ||
continue; | ||
} | ||
|
||
if (isSelfClosingAfterOpenTag(part)) { | ||
let placeholder = part.contents.contents.parts[0].contents.contents.trim(); | ||
|
||
if (placeholder.startsWith('/>')) { | ||
placeholder = placeholder.substring(2); | ||
decodedParts.push('/>'); | ||
} | ||
|
||
const expression = expressionMap.get(placeholder); | ||
expressionMap.delete(placeholder); | ||
|
||
// !expression.afterWhitespace | ||
// ORIGINAL: <span><% e %></span> | ||
// WITH: <span><% e %></span> | ||
// WITHOUT: <span> <% e %></span> | ||
|
||
// !decodedParts[decodedParts.length - 1].soft | ||
// ORIGINAL: <div><% e %></div> | ||
// WITH: <div>\n<% e %>\n</div> | ||
// WITHOUT: <div><% e %>\n</div> | ||
|
||
// !(decodedParts.length && decodedParts[decodedParts.length - 1].soft) | ||
// Without first check it breaks double_expression.html.test | ||
if (!expression.afterWhitespace && !(decodedParts.length && decodedParts[decodedParts.length - 1].soft)) { | ||
decodedParts.pop(); | ||
} | ||
|
||
decodedParts.push(expression.print); | ||
|
||
if (!expression.beforeWhitespace && expression.beforeInlineEndTag) { | ||
// ORIGINAL: <span><% e %></span> | ||
// WITH: <span><% e %></span> | ||
// WITHOUT: <span><% e %> </span> | ||
// expression.beforeInlineEndTag: | ||
// ORIGINAL: <div><% e %></div> | ||
// WITH: <div><% e %></div> | ||
// WITHOUT: (nothing) | ||
opts.removeWhitespace = true; | ||
} else if (expression.beforeWhitespace) { | ||
// ORIGINAL: <span><% e %> a</span> | ||
// WITH: <span><% e %> a</span> | ||
// WITHOUT: <span><% e %>a</span> | ||
if (part.contents.contents.parts[2] && part.contents.contents.parts[2].type === 'line') { | ||
decodedParts.pop(); | ||
decodedParts.push(group(concat([expression.print, line]))); | ||
} | ||
} | ||
|
||
continue; | ||
} | ||
|
||
const possibleTag = part.contents || part; | ||
|
||
const expression = /<\/?eext\d+>/.test(possibleTag) && expressionMap.get(possibleTag.trim()); | ||
|
||
if (expression) { | ||
expressionMap.delete(possibleTag.trim()); | ||
|
||
if (expression.print !== '') { | ||
if (expression.isMidExpression) { | ||
decodedParts.push(concat([expression.print, breakParent])); | ||
} else { | ||
decodedParts.push(expression.print); | ||
|
||
if (expression.type === 'start' || expression.type === 'middle_nested') { | ||
decodedParts.push(breakParent); | ||
} | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if (expression.isMidExpression) { | ||
opts.removeWhitespace = true; | ||
} | ||
|
||
// cond end | ||
// removes empty line | ||
// TODO: show an example | ||
if (expression.type === 'end') { | ||
const lastPart = decodedParts.pop(); | ||
lastPart.contents.parts.pop(); | ||
decodedParts.push(lastPart); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
decodedParts.push(part); | ||
} | ||
} | ||
|
||
return Object.assign({}, doc, { parts: decodedParts }); | ||
}; | ||
}; | ||
|
||
module.exports = decodeExpressions; |
Oops, something went wrong.