Skip to content

Commit

Permalink
Enforce some formatting (#723)
Browse files Browse the repository at this point in the history
* Specify pnpm @ 7

* Configure prettier

* Run format, add to CI

* Don't prettier json5 files

* Make prettier more agreeable with the eslint config (re: arrow-parens)

* Re-checkout and re-format files

* Update printWidth and trailingCommas to align with existing code

* Re-checkout and re-format code

* Don't format html

* Total diff is 4 lines smaller with the default trailingComma setting

* Lockfile

* Downgrade to prettier 2

* eslint

* Make prettier and eslint agree
  • Loading branch information
NullVoxPopuli authored Oct 25, 2024
1 parent b695601 commit dac333f
Show file tree
Hide file tree
Showing 23 changed files with 150 additions and 107 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ module.exports = {
requireConfigFile: false,
},
extends: 'simplabs',
overrides: [
{
files: ['**/*.js'],
rules: {
// conflicts with prettier
'space-before-function-paren': 0,
},
},
],
};
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:

- run: pnpm install --frozen-lockfile
- run: pnpm lint
- run: pnpm prettier --check .

test:
name: Node Tests
Expand Down
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.md
*.yml
*.yaml
*.html
*.json5
7 changes: 7 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
singleQuote: true,
arrowParens: 'avoid',
printWidth: 100,
};
2 changes: 1 addition & 1 deletion config/ember-try.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env node */
const getChannelURL = require('ember-source-channel-url');

module.exports = async function() {
module.exports = async function () {
return {
usePnpm: true,
scenarios: [
Expand Down
4 changes: 2 additions & 2 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

/* eslint-env node */

module.exports = function(/* environment, appConfig */) {
return { };
module.exports = function (/* environment, appConfig */) {
return {};
};
6 changes: 3 additions & 3 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function(defaults) {
module.exports = function (defaults) {
let app = new EmberAddon(defaults, {
// Add options here
'ember-hbs-minifier': {
skip: {
elements: ['pre', 'address'],
classes: ['description'],
components: ['foo-bar', 'no-minify']
}
components: ['foo-bar', 'no-minify'],
},
},

'ember-cli-babel': {
Expand Down
28 changes: 15 additions & 13 deletions hbs-minifier-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ function createGlimmerPlugin(config) {
}
});

node.value.parts = node.value.parts
.filter(part => part.type !== 'TextNode' || part.chars !== '');
node.value.parts = node.value.parts.filter(
part => part.type !== 'TextNode' || part.chars !== ''
);
}
}
},
Expand Down Expand Up @@ -110,9 +111,9 @@ function createGlimmerPlugin(config) {
if (skipStack[skipStack.length - 1] === node) {
skipStack.pop();
}
}
},
},
}
},
};
}

Expand All @@ -126,12 +127,14 @@ function isWhitespaceTextNode(node) {
}

function stripNoMinifyBlocks(nodes) {
return nodes.map(node => {
if (node.type === 'BlockStatement' && node.path.original === 'no-minify') {
return node.program.body;
}
return node;
}).reduce((a, b) => a.concat(b), []);
return nodes
.map(node => {
if (node.type === 'BlockStatement' && node.path.original === 'no-minify') {
return node.program.body;
}
return node;
})
.reduce((a, b) => a.concat(b), []);
}

function removeSurroundingWhitespaceNodes(nodes) {
Expand All @@ -149,7 +152,7 @@ function removeSurroundingWhitespaceNodes(nodes) {
function isClassIncluded(chars, classes) {
chars = (chars || '').trim().split(' ');

return chars.some((char) => {
return chars.some(char => {
return classes.indexOf(char) !== -1;
});
}
Expand Down Expand Up @@ -209,14 +212,13 @@ function canTrimWhiteSpaceBasedOnClassNames(value, configClassNames) {
} else if (type === 'ConcatStatement') {
let parts = value.parts;

return parts.every((part) => {
return parts.every(part => {
return canTrimWhiteSpaceBasedOnClassNames(part, configClassNames);
});
}
return true;
}


function shouldSkipBlockStatement(node, config) {
let components = config.skip.components;
if (components === 'all') {
Expand Down
72 changes: 41 additions & 31 deletions hbs-minifier-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
/* eslint-disable no-inner-declarations */

const DEP_PREFIX = '@glimmer/syntax';
const DEPS = Object.keys(require('./package.json').devDependencies)
.filter(it => it.startsWith(DEP_PREFIX));
const DEPS = Object.keys(require('./package.json').devDependencies).filter(it =>
it.startsWith(DEP_PREFIX)
);

// Remove the unnecessary `loc` properties from the AST snapshots and replace
// the `Object` prefix with the node `type`
Expand Down Expand Up @@ -35,94 +36,103 @@ for (let dep of DEPS) {
assert(`{{foo}} \n\n \n{{bar}}`);
});

it('strips leading and trailing whitespace from Program nodes', function() {
it('strips leading and trailing whitespace from Program nodes', function () {
assert(` {{foo}} `);
});

it('does not strip leading/trailing text from Program nodes', function() {
it('does not strip leading/trailing text from Program nodes', function () {
assert(`x {{foo}} y `);
});

it('strips leading and trailing whitespace from ElementNode nodes', function() {
it('strips leading and trailing whitespace from ElementNode nodes', function () {
assert(`<div> {{foo}} </div>`);
});

it('does not strip leading/trailing text from ElementNode nodes', function() {
it('does not strip leading/trailing text from ElementNode nodes', function () {
assert(`<div>x {{foo}} y </div>`);
});

it('does not strip inside of <pre> tags', function() {
it('does not strip inside of <pre> tags', function () {
assert(`<pre> {{foo}} </pre>`);
});

it('does not collapse whitespace inside of <pre> tags', function() {
it('does not collapse whitespace inside of <pre> tags', function () {
assert(`<pre> \n\n \n</pre>`);
});

it('does not strip inside of {{#no-minify}} tags', function() {
it('does not strip inside of {{#no-minify}} tags', function () {
assert(`{{#no-minify}} {{foo}} {{/no-minify}}`);
});

it('does not strip inside of {{#no-minify}} tags in other tags', function() {
it('does not strip inside of {{#no-minify}} tags in other tags', function () {
assert(`<div>{{#no-minify}} {{foo}} {{/no-minify}}</div>`);
});

it('does not collapse whitespace inside of {{#no-minify}} tags in other tags', function() {
it('does not collapse whitespace inside of {{#no-minify}} tags in other tags', function () {
assert(`<div>{{#no-minify}} \n\n \n{{/no-minify}}</div>`);
});

it('does not collapse multiple &nbsp; textNode into a single whitespace', function() {
it('does not collapse multiple &nbsp; textNode into a single whitespace', function () {
assert(`<span>1</span>&nbsp;&nbsp;<span>2</span>`);
});

it('does not collapse &nbsp; surrounding a text content into a single whitespace', function() {
it('does not collapse &nbsp; surrounding a text content into a single whitespace', function () {
assert(`<div>
<span> &nbsp;1&nbsp; </span>
<span> 2 </span>
</div>`);
});

it('does not minify `tagNames` specified in .hbs-minifyrc.js', function() {
it('does not minify `tagNames` specified in .hbs-minifyrc.js', function () {
let config = {
skip: { elements: ['address'] },
};

assert(`<address>
assert(
`<address>
Box 564,
<b>
Disneyland
</b>
<br>
<u> USA </u>
</address>`, config);
</address>`,
config
);
});

it('does not minify `classNames` specified in .hbs-minifyrc.js', function() {
it('does not minify `classNames` specified in .hbs-minifyrc.js', function () {
let config = {
skip: { classes: ['description'] },
};

assert(`<div class="description">
assert(
`<div class="description">
1
<span>
2
</span>
</div>`, config);
</div>`,
config
);
});

it('does not minify `components` specified in .hbs-minifyrc.js', function() {
it('does not minify `components` specified in .hbs-minifyrc.js', function () {
let config = {
skip: { components: ['foo-bar'] },
};

assert(`{{#foo-bar}}
assert(
`{{#foo-bar}}
<span>
yield content
</span>
{{/foo-bar}}`, config);
{{/foo-bar}}`,
config
);
});

it('minifies `tagNames` that are not specified in .hbs-minifyrc.js', function() {
it('minifies `tagNames` that are not specified in .hbs-minifyrc.js', function () {
assert(`<section>
Box 564,
<b>
Expand All @@ -133,7 +143,7 @@ for (let dep of DEPS) {
</section>`);
});

it('minifies `classNames` that are not specified in .hbs-minifyrc.js', function() {
it('minifies `classNames` that are not specified in .hbs-minifyrc.js', function () {
assert(`<div class="contact-details">
John Smith
<i>
Expand All @@ -142,23 +152,23 @@ for (let dep of DEPS) {
</div>`);
});

it('minifies `components` that are not specified in .hbs-minifyrc.js', function() {
it('minifies `components` that are not specified in .hbs-minifyrc.js', function () {
assert(`{{#my-component}}
<span>
yield content
</span>
{{/my-component}}`);
});

it('skips whitespace in regular attributes', function() {
it('skips whitespace in regular attributes', function () {
assert(`<div title=" foo " />`);
});

it('skips whitespace in concatenated attributes', function() {
it('skips whitespace in concatenated attributes', function () {
assert(`<div title=" foo {{if this.bar "BAR"}} " />`);
});

it('collapses whitespace in regular `class` attributes', function() {
it('collapses whitespace in regular `class` attributes', function () {
assert(`
<button
class="
Expand All @@ -170,7 +180,7 @@ for (let dep of DEPS) {
`);
});

it('collapses whitespace in concatenated `class` attributes', function() {
it('collapses whitespace in concatenated `class` attributes', function () {
assert(`
<button
class="
Expand Down Expand Up @@ -202,8 +212,8 @@ for (let dep of DEPS) {
};
return glimmer.preprocess(template, {
plugins: {
ast: [plugin]
}
ast: [plugin],
},
});
}
});
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ module.exports = {
return {
name: 'hbs-minifier-plugin',
plugin: HbsMinifierPlugin,
baseDir() { return __dirname; },
cacheKey() { return cacheKeyForConfig(config); }
baseDir() {
return __dirname;
},
cacheKey() {
return cacheKeyForConfig(config);
},
};
},
};
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"build": "ember build",
"changelog": "lerna-changelog",
"lint": "eslint . --cache",
"format": "prettier . --write",
"start": "ember server",
"test": "jest",
"test:ember": "ember test"
Expand Down Expand Up @@ -60,9 +61,11 @@
"jest": "28.1.3",
"lerna-changelog": "2.2.0",
"loader.js": "4.7.0",
"prettier": "^2.0.0",
"qunit": "2.19.4",
"webpack": "5.82.1"
},
"packageManager": "[email protected]+sha512.3ede4912357625239102ac3f5ed8df178937e0f050169a95de450c1584a5cab589b22a391ebde5ab2da8264a69accad56cec8126cc577d90d03a5be6cad22b80",
"engines": {
"node": "12.* || 14.* || >= 16.*"
},
Expand All @@ -81,6 +84,5 @@
},
"changelog": {
"repo": "Mainmatter/ember-hbs-minifier"
},
"packageManager": "[email protected]+sha512.3ede4912357625239102ac3f5ed8df178937e0f050169a95de450c1584a5cab589b22a391ebde5ab2da8264a69accad56cec8126cc577d90d03a5be6cad22b80"
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dac333f

Please sign in to comment.