Skip to content

Commit

Permalink
Use prettier instead of clang-format.
Browse files Browse the repository at this point in the history
  • Loading branch information
rictic committed Feb 4, 2024
1 parent 8c2e1df commit 779502b
Show file tree
Hide file tree
Showing 21 changed files with 852 additions and 509 deletions.
12 changes: 0 additions & 12 deletions .clang-format

This file was deleted.

44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ fast and flexible CSS parsing and transformation is a critical feature.

### Goals

- Feasibility of being used in conjunction with Polymer or Polymer
Designer.
- Parse CSS loosely and flexibly. This parser is not spec-compliant, however
it will parse all spec-compliant CSS.
- Parse CSS quickly and efficiently. This parser is a suitable tool to aide in
the design and implementation of runtime transformations.
- Graceful error recovery. Malformed CSS will be parsed by this
parser as closely as possible to the way a browser would parse it.
- Feasibility of being used in conjunction with Polymer or Polymer
Designer.
- Parse CSS loosely and flexibly. This parser is not spec-compliant, however
it will parse all spec-compliant CSS.
- Parse CSS quickly and efficiently. This parser is a suitable tool to aide in
the design and implementation of runtime transformations.
- Graceful error recovery. Malformed CSS will be parsed by this
parser as closely as possible to the way a browser would parse it.

### Installing

Expand Down Expand Up @@ -50,7 +50,6 @@ const ast = parser.parse(css);
```js
/* Step 1: Inherit from NodeFactory */
class CustomNodeFactory extends shadyCss.NodeFactory {

/*
* Step 2: Implement a custom node factory method. Here we override the
* default factory for Expression nodes
Expand Down Expand Up @@ -89,13 +88,12 @@ from parsed CSS.
```js
/* Step 1: Inherit from Stringifier. */
class CustomStringifier extends shadyCss.Stringifier {

/**
* Step 2: Implement a stringification method named after the type of the node
* you are interested in stringifying. In this case, we are implementing
* stringification for the Darken Expression nodes we implemented parsing for
* above.
*/
* Step 2: Implement a stringification method named after the type of the node
* you are interested in stringifying. In this case, we are implementing
* stringification for the Darken Expression nodes we implemented parsing for
* above.
*/
darkenExpression(darkenExpression) {
// For the sake of brevity, please assume that the darken function returns
// a darker version of the color parameter:
Expand All @@ -117,6 +115,7 @@ const css = stringifier.stringify(ast);
--nog: blue;
}
```

```js
{
"type": 1, /* stylesheet */
Expand Down Expand Up @@ -151,6 +150,7 @@ ruleset {
};
}
```

```js
{
"type": 1, /* stylesheet */
Expand Down Expand Up @@ -185,9 +185,10 @@ ruleset {

```css
.title {
@apply(--my-toolbar-title-theme);
@apply (--my-toolbar-title-theme);
}
```

```js
{
"type": 1, /* stylesheet */
Expand Down Expand Up @@ -222,6 +223,7 @@ ruleset {
};
}
```

```js
{
"type": 1, /* stylesheet */
Expand Down Expand Up @@ -258,9 +260,11 @@ ruleset {
/* before */
body {
margin: 0;
padding: 0px
padding: 0px;
}
```

<!-- prettier-ignore -->
```css
/* after */
body{margin:0;padding:0px;}
Expand All @@ -278,6 +282,8 @@ body{margin:0;padding:0px;}

@charset 'foo';
```

<!-- prettier-ignore -->
```css
/* after */
@import url('foo.css');@font-face{font-family:foo;}@charset 'foo';
Expand All @@ -296,9 +302,11 @@ body{margin:0;padding:0px;}

#target {
gak: var(--qux);
@apply(--foo);
@apply (--foo);
}
```

<!-- prettier-ignore -->
```css
/* after */
:root{--qux:vim;--foo:{bar:baz;};}#target{gak:var(--qux);@apply (--foo);}
Expand Down
39 changes: 16 additions & 23 deletions package-lock.json

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

34 changes: 29 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"@types/node": "^8.0.13",
"chai": "^4.1.0",
"chai-subset": "^1.5.0",
"clang-format": "^1.0.53",
"mocha": "^3.0.0",
"prettier": "^3.2.5",
"source-map-support": "^0.4.15",
"tslint": "^5.5.0",
"typescript": "^5.3.3",
Expand Down Expand Up @@ -52,7 +52,8 @@
"test": {
"dependencies": [
"test:actual",
"lint"
"lint",
"format:check"
]
},
"test:actual": {
Expand All @@ -71,10 +72,33 @@
},
"format": {
"files": [
"src/**/*.ts",
"src/**/*.js"
"src/",
"*.json",
"*.md"
],
"command": "find src | grep '\\.js$\\|\\.ts$' | xargs clang-format --style=file -i"
"command": "prettier -w src *.md *.json"
},
"format:check": {
"files": [
"src/",
"*.json",
"*.md"
],
"command": "prettier --list-different src *.md *.json"
}
},
"prettier": {
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "preserve",
"bracketSpacing": false,
"trailingComma": "all",
"arrowParens": "always",
"bracketSameLine": true,
"singleAttributePerLine": false,
"htmlWhitespaceSensitivity": "strict"
}
}
15 changes: 14 additions & 1 deletion src/shady-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@
*/

export {iterateOverAst} from './shady-css/ast-iterator';
export {AtRule, Comment, Declaration, Discarded, Expression, Node, nodeType, Range, Rule, Rulelist, Ruleset, Stylesheet} from './shady-css/common';
export {
AtRule,
Comment,
Declaration,
Discarded,
Expression,
Node,
nodeType,
Range,
Rule,
Rulelist,
Ruleset,
Stylesheet,
} from './shady-css/common';
export {NodeFactory} from './shady-css/node-factory';
export {NodeVisitor} from './shady-css/node-visitor';
export {Parser} from './shady-css/parser';
Expand Down
2 changes: 1 addition & 1 deletion src/shady-css/ast-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function* getChildren(node: Node) {
case nodeType.expression:
case nodeType.comment:
case nodeType.discarded:
return; // no child nodes
return; // no child nodes
default:
const never: never = node;
console.error(`Got a node of unknown type: `, never);
Expand Down
38 changes: 22 additions & 16 deletions src/shady-css/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const matcher = {
whitespaceGreedy: /(\s+)/g,
commentGreedy: /(\*\/)/g,
boundary: /[\(\)\{\}'"@;:\s]/,
stringBoundary: /['"]/
stringBoundary: /['"]/,
};

/**
Expand All @@ -31,13 +31,19 @@ export enum nodeType {
expression = 'expression',
declaration = 'declaration',
rulelist = 'rulelist',
discarded = 'discarded'
discarded = 'discarded',
}


export type Node =
Stylesheet|AtRule|Comment|Rulelist|Ruleset|Expression|Declaration|Discarded;
export type Rule = Ruleset|Declaration|AtRule|Discarded|Comment;
| Stylesheet
| AtRule
| Comment
| Rulelist
| Ruleset
| Expression
| Declaration
| Discarded;
export type Rule = Ruleset | Declaration | AtRule | Discarded | Comment;

/** A Stylesheet node. */
export interface Stylesheet {
Expand All @@ -58,9 +64,9 @@ export interface AtRule {
nameRange: Range;
/** The "parameters" of the At Rule (e.g., `utf8`) */
parameters: string;
parametersRange: Range|undefined;
parametersRange: Range | undefined;
/** The Rulelist node (if any) of the At Rule. */
rulelist: Rulelist|undefined;
rulelist: Rulelist | undefined;

range: Range;
}
Expand Down Expand Up @@ -123,7 +129,7 @@ export interface Declaration {
* Either an Expression node, or a Rulelist node, that
* corresponds to the value of the Declaration.
*/
value: Expression|Rulelist|undefined;
value: Expression | Rulelist | undefined;

range: Range;
}
Expand Down Expand Up @@ -160,14 +166,14 @@ export interface Range {
}

export interface NodeTypeMap {
'stylesheet': Stylesheet;
'atRule': AtRule;
'comment': Comment;
'rulelist': Rulelist;
'ruleset': Ruleset;
'declaration': Declaration;
'expression': Expression;
'discarded': Discarded;
stylesheet: Stylesheet;
atRule: AtRule;
comment: Comment;
rulelist: Rulelist;
ruleset: Ruleset;
declaration: Declaration;
expression: Expression;
discarded: Discarded;
}

export {matcher};
5 changes: 4 additions & 1 deletion src/shady-css/get-nodes-at-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {Node} from './common';
* child of its predecessor.
*/
export function getNodesAtLocation(
initialNode: Node, location: number, path: Node[] = []): Node[] {
initialNode: Node,
location: number,
path: Node[] = [],
): Node[] {
let current = initialNode;
outer: while (true) {
for (const child of getChildren(current)) {
Expand Down
Loading

0 comments on commit 779502b

Please sign in to comment.