Skip to content

Commit 44baefc

Browse files
fix: script imports for tpescript (#46)
* 1.0.1 * 1.0.2 * 1.0.3 * 1.0.4 * 1.0.5 * fix: nothing * feat: make strip comments a es6 module and break everything * fix: sanity restored by Niraj * fix: test import and script * fix: tests * fix: remove redudant dependencies * fix: use npm ci in ci * chore: regenerate package-lock * Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * refactor: remove empty options Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
1 parent 4e5c3f1 commit 44baefc

File tree

13 files changed

+3678
-637
lines changed

13 files changed

+3678
-637
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
node-version: 12
1515

1616
# Install dependencies
17-
- run: npm install
17+
- run: npm ci
1818

1919
# Run tests
20-
- run: npm test
20+
- run: npm test

lib/__tests__/css-helper.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @jest-environment jsdom
33
*/
44
import { cssString } from "../__fixtures__/curriculum-helper-css";
5-
import { CSSHelp } from "../css-helper";
5+
import { CSSHelp } from "../index";
66

77
describe('css-help', () => {
88
const doc = document;

lib/class/node.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
interface CodeNodeProps {
3+
type: string;
4+
value: string;
5+
match: string;
6+
newline: string;
7+
nodes: CodeNode[];
8+
}
9+
10+
11+
export type CodeNodeType = Partial<CodeNodeProps> | null;
12+
13+
14+
export class CodeNode {
15+
type: string;
16+
value: any;
17+
match: string;
18+
newline: string;
19+
20+
constructor(node: CodeNodeType) {
21+
this.type = node.type;
22+
if (node.value) this.value = node.value;
23+
if (node.match) this.match = node.match;
24+
this.newline = node.newline || "";
25+
}
26+
get protected() {
27+
return Boolean(this.match) && this.match[1] === "!";
28+
}
29+
}
30+
31+
export class Block extends CodeNode {
32+
nodes: CodeNode[];
33+
34+
constructor(node: CodeNodeType) {
35+
super(node);
36+
this.nodes = node.nodes || [];
37+
}
38+
push(node: CodeNode) {
39+
this.nodes.push(node);
40+
}
41+
get protected() {
42+
return this.nodes.length > 0 && this.nodes[0].protected === true;
43+
}
44+
}
45+

lib/compile.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { CodeNodeType } from './class/node.js';
2+
import { options } from './option-types.js';
3+
4+
5+
export const compile = (cst, options :Partial<options>= {}) => {
6+
const keepProtected = options.safe === true || options.keepProtected === true;
7+
let firstSeen = false;
8+
9+
const walk = (node: CodeNodeType, child?: CodeNodeType) => {
10+
let output = '';
11+
let inner;
12+
let lines;
13+
14+
for (const child of node.nodes) {
15+
switch (child.type) {
16+
case 'block':
17+
if (options.first && firstSeen === true) {
18+
output += walk(child, node);
19+
break;
20+
}
21+
22+
if (options.preserveNewlines === true) {
23+
inner = walk(child, node);
24+
lines = inner.split('\n');
25+
output += '\n'.repeat(lines.length - 1);
26+
break;
27+
}
28+
29+
if (keepProtected === true && child.protected === true) {
30+
output += walk(child, node);
31+
break;
32+
}
33+
34+
firstSeen = true;
35+
break;
36+
case 'line':
37+
if (options.first && firstSeen === true) {
38+
output += child.value;
39+
break;
40+
}
41+
42+
if (keepProtected === true && child.protected === true) {
43+
output += child.value;
44+
}
45+
46+
firstSeen = true;
47+
break;
48+
case 'open':
49+
case 'close':
50+
case 'text':
51+
case 'newline':
52+
default: {
53+
output += child.value || '';
54+
break;
55+
}
56+
}
57+
}
58+
59+
return output;
60+
};
61+
62+
return walk(cst);
63+
};

lib/css-helper.ts

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)