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

feat(code-formatter): Implement new code formatter #2268

Merged
merged 61 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
a17c0d4
added e2e test kit
barak007 Dec 22, 2021
7bcc5d0
initial one level of formatting (wip)
barak007 Dec 22, 2021
93f3125
start work on values
barak007 Dec 26, 2021
2b85632
* support nesting indent
barak007 Jan 2, 2022
25e6410
value foramt
barak007 Jan 6, 2022
f75cebb
atRules basic support
barak007 Jan 7, 2022
9e3ece3
add experimental flag to format-cli
barak007 Jan 7, 2022
6ccbdc7
handle rule separation
barak007 Jan 7, 2022
51720cb
refactor
barak007 Jan 7, 2022
f17a7cd
fix refactor
barak007 Jan 7, 2022
365eb7f
handle values with spaces around
barak007 Jan 7, 2022
f36b7d2
fix selector indent in nested scope
barak007 Jan 7, 2022
cc698de
preserve newline in selectors
barak007 Jan 7, 2022
8cdddce
refactor
barak007 Jan 7, 2022
1137a45
comments separation
barak007 Jan 7, 2022
535bf14
fix commnet after comment
barak007 Jan 7, 2022
8f9f953
handle decl values that start with newline
barak007 Jan 7, 2022
4a2b0fe
fix re-indent values with newlines
barak007 Jan 7, 2022
3422bbd
* preserve groups
barak007 Jan 9, 2022
1c30970
avoid running twice
barak007 Jan 9, 2022
01c05a1
Merge branch 'master' into barak/code-format-2
barak007 Jan 9, 2022
453020e
Merge branch 'master' into barak/code-format-2
barak007 Jan 17, 2022
b3d844f
fix merge
barak007 Jan 17, 2022
e79cd60
handle comments on raws of decls
barak007 Jan 28, 2022
a62d493
* fix decls and rules with comments between
barak007 Jan 29, 2022
9e68715
Merge branch 'master' into barak/code-format-2
barak007 Jan 29, 2022
0fa4842
cleanups
barak007 Jan 29, 2022
3d6dc1a
remove lock from core
barak007 Jan 29, 2022
e0448dc
support media with comments in raws
barak007 Jan 30, 2022
5c08a9f
check new postcss version
barak007 Feb 1, 2022
a80278c
back to safety
barak007 Feb 1, 2022
2515cc8
Merge branch 'master' into barak/code-format-2
barak007 Feb 7, 2022
a985aa3
fix grouped values
barak007 Feb 28, 2022
c44dd1d
Merge branch 'master' into barak/code-format-2
barak007 Feb 28, 2022
48d1c9b
fix tests
barak007 Feb 28, 2022
db14b24
remove newline between atrule and an atrule child
tomrav Mar 2, 2022
f9bfcaa
Merge branch 'master' of github.com:wix/stylable into barak/code-form…
tomrav Mar 28, 2022
163a5f4
Merge branch 'master' of github.com:wix/stylable into barak/code-form…
tomrav Mar 29, 2022
65a01ce
add cases for nested functions
tomrav Mar 30, 2022
7f460ab
Merge branch 'master' into barak/code-format-2
barak007 Jul 6, 2022
79b2ec0
Merge branch 'master' into barak/code-format-2
barak007 Oct 24, 2022
30a01cf
Merge remote-tracking branch 'origin' into barak/code-format-2
idoros Dec 26, 2022
b52ef8a
chore: update package-lock
idoros Dec 26, 2022
35376e2
Merge branch 'master' into barak/code-format-2
idoros Dec 26, 2022
98fea94
test: improve test and add issues to resolve
idoros Dec 28, 2022
6375c25
feat: decl value format
idoros Jan 1, 2023
1dd7e10
chore(deps): tokey/css-value-parser@0.1.2
idoros Jan 3, 2023
ff9e476
feat: format at-rule params
idoros Jan 3, 2023
2e2e570
fix: at-rule double space between name and params
idoros Jan 4, 2023
9b10118
chore: re-group experimental formatter tests
idoros Jan 5, 2023
a7c7ae4
feat: support `endWithNewLine` configuration
idoros Jan 5, 2023
0300210
feat: support `endOfLine` configuration
idoros Jan 5, 2023
3174b10
feat: support experimental formatter in lang service
idoros Jan 5, 2023
c4d125a
chore: lowercase "L" in "endWithNewline"
idoros Jan 5, 2023
6325af0
feat(cli): accept config for experimental formatter
idoros Jan 5, 2023
5682080
docs(cli): a note about experimental formatter
idoros Jan 5, 2023
06d830d
Merge remote-tracking branch 'origin/master' into barak/code-format-2
idoros Jan 5, 2023
6a125b1
chore: small review changes
idoros Jan 8, 2023
7c1d875
feat: respect decl args newline in every function scope
idoros Jan 8, 2023
a23cbfb
chore: fix spec file type
idoros Jan 8, 2023
ec28b81
Merge remote-tracking branch 'origin' into barak/code-format-2
idoros Jan 8, 2023
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
24 changes: 22 additions & 2 deletions package-lock.json

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

6 changes: 6 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ After installing `@stylable/cli`, the `stc-format` command will be available, ru
| `--help` | `h` | Show help | `boolean` | |
| `--version` | `v` | Show version number | `boolean` | |

### Experimental formatter

A new experimental formatter is available using the `--experimental` argument.

Currently not all configuration is accepted by the new formatter, the supported formatting options arguments are `--endWithNewline`, `--indentSize`, and a new `--wrapLineLength`.

### Formatting the source directory

```sh
Expand Down
69 changes: 43 additions & 26 deletions packages/cli/src/code-format.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/env node
import yargs from 'yargs';
import { nodeFs } from '@file-services/node';
import { getDocumentFormatting } from '@stylable/code-formatter';
import { getDocumentFormatting, formatCSS } from '@stylable/code-formatter';
import { createLogger } from './logger';
import { writeFileSync } from 'fs';

const { join } = nodeFs;

const argv = yargs
.usage('$0 [options]')
.option('experimental', {
type: 'boolean',
description: 'use experimental code formatter',
default: false,
})
.option('target', {
type: 'string',
description: 'file or directory to format',
Expand Down Expand Up @@ -83,6 +88,12 @@ const argv = yargs
alias: 'r',
default: [] as string[],
})
.option('wrapLineLength', {
type: 'number',
description: 'Length of line to be considered when wrapping',
alias: 'W',
default: 80,
})

.alias('h', 'help')
.alias('v', 'version')
Expand All @@ -104,6 +115,8 @@ const {
selectorSeparatorNewline,
target,
silent,
experimental,
wrapLineLength,
} = argv;

const log = createLogger(
Expand All @@ -120,18 +133,16 @@ for (const request of requires) {
require(request);
}

function readDirectoryDeep(dirPath: string, fileSuffixFilter = '.st.css') {
const files = nodeFs.readdirSync(dirPath, 'utf-8');
let res: string[] = [];
function readDirectoryDeep(dirPath: string, fileSuffixFilter = '.st.css', res = new Set<string>()) {
const items = nodeFs.readdirSync(dirPath, { withFileTypes: true });

for (const item of files) {
const currentlFilePath = join(dirPath, item);
const itemStat = nodeFs.statSync(join(currentlFilePath));
for (const item of items) {
const path = join(dirPath, item.name);

if (itemStat.isFile() && item.endsWith(fileSuffixFilter)) {
res.push(currentlFilePath);
} else if (itemStat.isDirectory()) {
res = res.concat(readDirectoryDeep(currentlFilePath));
if (item.isFile() && path.endsWith(fileSuffixFilter)) {
res.add(path);
} else if (item.isDirectory()) {
readDirectoryDeep(path, fileSuffixFilter, res);
}
}

Expand All @@ -141,20 +152,26 @@ function readDirectoryDeep(dirPath: string, fileSuffixFilter = '.st.css') {
function formatStylesheet(filePath: string) {
const fileContent = nodeFs.readFileSync(filePath, 'utf-8');

const newText = getDocumentFormatting(
fileContent,
{ start: 0, end: fileContent.length },
{
end_with_newline: endWithNewline,
indent_empty_lines: indentEmptyLines,
indent_size: indentSize,
indent_with_tabs: indentWithTabs,
max_preserve_newlines: maxPerserveNewlines,
newline_between_rules: newlineBetweenRules,
preserve_newlines: perserveNewlines,
selector_separator_newline: selectorSeparatorNewline,
}
);
const newText = experimental
? formatCSS(fileContent, {
indent: ' '.repeat(indentSize),
endWithNewline,
wrapLineLength,
})
: getDocumentFormatting(
fileContent,
{ start: 0, end: fileContent.length },
{
end_with_newline: endWithNewline,
indent_empty_lines: indentEmptyLines,
indent_size: indentSize,
indent_with_tabs: indentWithTabs,
max_preserve_newlines: maxPerserveNewlines,
newline_between_rules: newlineBetweenRules,
preserve_newlines: perserveNewlines,
selector_separator_newline: selectorSeparatorNewline,
}
);

if (newText.length) {
writeFileSync(filePath, newText);
Expand Down Expand Up @@ -182,7 +199,7 @@ if (formatPathStats.isFile()) {
} else if (formatPathStats.isDirectory()) {
const stylesheets = readDirectoryDeep(target);

if (stylesheets.length) {
if (stylesheets.size) {
for (const stylesheet of stylesheets) {
formatStylesheet(stylesheet);
}
Expand Down
57 changes: 57 additions & 0 deletions packages/cli/test/code-format-cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createTempDirectory,
ITempDirectory,
} from '@stylable/e2e-test-kit';
import { deindent } from '@stylable/core-test-kit';

describe('Stylable Code Format Cli', function () {
let tempDir: ITempDirectory;
Expand Down Expand Up @@ -135,6 +136,62 @@ describe('Stylable Code Format Cli', function () {
});
});

describe('experimental formatter', () => {
it('should format a directory with a single stylesheet (check indent diff)', () => {
const stylesheetPath = 'style.st.css';

populateDirectorySync(tempDir.path, {
[stylesheetPath]: `.a{prop:\ngreen,\nblue}`,
});

runFormatCliSync(['--target', tempDir.path, '--experimental']);

const dirContent = loadDirSync(tempDir.path);
expect(dirContent[stylesheetPath]).to.equal(
deindent(`
.a {
prop:
green,
blue;
}
`)
);
});
it('should accept configuration', () => {
const stylesheetPath = 'style.st.css';

populateDirectorySync(tempDir.path, {
[stylesheetPath]: `.a{prop:\ngreen,\nblue;prop2:123456789 123456789}`,
});

runFormatCliSync([
'--target',
tempDir.path,
'--experimental',
'--indentSize',
'2',
'--endWithNewline',
'true',
'--wrapLineLength',
'25',
]);

const dirContent = loadDirSync(tempDir.path);
expect(dirContent[stylesheetPath]).to.equal(
deindent(`
.a {
prop:
green,
blue;
prop2: 123456789
123456789;
}
`) + '\n'
);
//
});
});

describe('exceptions', () => {
it('should throw an exception when no stylable stylesheets are found to format', () => {
const filePath = 'file.txt';
Expand Down
4 changes: 3 additions & 1 deletion packages/code-formatter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
},
"dependencies": {
"@stylable/core": "^5.6.1",
"js-beautify": "^1.14.7"
"@tokey/css-value-parser": "^0.1.2",
"js-beautify": "^1.14.7",
"postcss": "^8.4.18"
},
"files": [
"dist",
Expand Down
Loading