Skip to content

Commit

Permalink
missing test files #2
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Jul 1, 2023
1 parent b929df7 commit 3a5ff48
Show file tree
Hide file tree
Showing 8 changed files with 8,783 additions and 94 deletions.
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
"version": "0.0.1-alpha2",
"exports": {
".": "./dist/index.js",
"./umd": "./dist/index-umd.js"
},
"./umd": "./dist/index-umd.js"
},
"typings": "dist/index.d.ts",
"scripts": {
"build": "rollup -c",
"test": "mocha \"test/**/*.test.mjs\"",
"test2": "bun run test/parse.ts",
"watch": "rollup -c --watch --watch.onBundleEnd 'npm run test' &",
"syntax-update": "./update.sh"
"test": "mocha \"test/**/*.test.mjs\""
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
Expand All @@ -28,6 +25,6 @@
"tslib": "^2.5.0"
},
"dependencies": {
"@tbela99/workerize": "github:tbela99/workerize"
"source-map": "^0.7.4"
}
}
5 changes: 3 additions & 2 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface ErrorDescription {
export interface ParserOptions {

src?: string;
location?: boolean;
sourcemap?: boolean;
compress?: boolean;
processImport?: boolean;
removeEmpty?: boolean;
Expand All @@ -38,6 +38,7 @@ export interface ParserOptions {
export interface RenderOptions {

compress?: boolean;
sourcemap?: boolean;
preserveLicense?: boolean;
indent?: string;
newLine?: string;
Expand All @@ -60,7 +61,7 @@ export interface RenderResult {

export interface TransformResult extends ParseResult, RenderResult {

performance: {
stats: {
bytesIn: number;
bytesOut: number;
parse: string;
Expand Down
35 changes: 19 additions & 16 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {
const errors: ErrorDescription[] = [];
const options: ParserOptions = {
src: '',
location: false,
sourcemap: false,
compress: false,
processImport: false,
removeEmpty: true,
Expand Down Expand Up @@ -63,7 +63,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {

let context: AstRuleList = ast;

if (options.location) {
if (options.sourcemap) {

ast.loc = {
sta: {
Expand Down Expand Up @@ -286,7 +286,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {
src
};

if (options.location) {
if (options.sourcemap) {

(<AstNode>tokens[i]).loc = loc
}
Expand All @@ -301,6 +301,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {

const delim: Token = <Token>tokens.pop();

// @ts-ignore
while (['Whitespace', 'Bad-string', 'Bad-comment'].includes(tokens[tokens.length - 1]?.typ)) {

tokens.pop();
Expand All @@ -322,6 +323,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {
return null;
}

// @ts-ignore
while (['Whitespace'].includes(tokens[0]?.typ)) {

tokens.shift();
Expand Down Expand Up @@ -442,7 +444,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {
src
};

if (options.location) {
if (options.sourcemap) {

node.loc = loc
}
Expand Down Expand Up @@ -498,7 +500,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {
src
};

if (options.location) {
if (options.sourcemap) {

node.loc = loc
}
Expand Down Expand Up @@ -594,15 +596,16 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {
return null;
}

loc = <Location>{
sta: position,
src
};

if (options.location) {

node.loc = loc
}
// // location not needed for declaration
// loc = <Location>{
// sta: position,
// src
// };
//
// if (options.sourcemap) {
//
// node.loc = loc
// }

// @ts-ignore
context.chi.push(node);
Expand Down Expand Up @@ -705,7 +708,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {

if (ind >= total) {

pushToken({typ: hasNewLine ? 'Bad-string' : 'Unclosed-string', val: buffer});
pushToken(<Token>{typ: hasNewLine ? 'Bad-string' : 'Unclosed-string', val: buffer});
break;
}

Expand All @@ -727,7 +730,7 @@ export function parse(iterator: string, opt: ParserOptions = {}): ParseResult {
if (value == quote) {

buffer += value;
pushToken({typ: hasNewLine ? 'Bad-string' : 'String', val: buffer});
pushToken(<Token>{typ: hasNewLine ? 'Bad-string' : 'String', val: buffer});
next();
// i += value.length;
buffer = '';
Expand Down
23 changes: 12 additions & 11 deletions src/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Token
} from "../@types";
import {cmyk2hex, hsl2Hex, hwb2hex, NAMES_COLORS, rgb2Hex} from "./utils";
import {isLength} from "../parser/utils";
import {SourceMapGenerator} from "source-map";

const indents: string[] = [];

Expand All @@ -18,17 +18,18 @@ export function render(data: AstNode, opt: RenderOptions = {}): RenderResult {
const options = Object.assign(opt.compress ? {
indent: '',
newLine: '',
preserveLicense: false,
removeComments: true,
colorConvert: true
removeComments: true
} : {
indent: ' ',
newLine: '\n',
compress: false,
preserveLicense: false,
removeComments: false,
colorConvert: true
}, opt);

}, {src: '', sourcemap: false, colorConvert: true, preserveLicense: false}, opt);

let sourcemap = options.sourcemap ? new SourceMapGenerator() : null;
let line = 1;
let column = 0;

function reducer(acc: string, curr: Token, index: number, original: Token[]): string {

Expand Down Expand Up @@ -59,10 +60,10 @@ export function render(data: AstNode, opt: RenderOptions = {}): RenderResult {
return acc + renderToken(curr, options);
}

return {code: doRender(data, options, reducer)};
return {code: doRender(data, options, reducer, 0, <SourceMapGenerator>sourcemap, line, column)};
}

function doRender(data: AstNode, options: RenderOptions, reducer: Function, level: number = 0): string {
function doRender(data: AstNode, options: RenderOptions, reducer: Function, level: number = 0, sourcemap: SourceMapGenerator = <SourceMapGenerator>null, line: number = 1, column: number = 0): string {

if (indents.length < level + 1) {

Expand All @@ -87,7 +88,7 @@ function doRender(data: AstNode, options: RenderOptions, reducer: Function, leve

return (<AstRuleStyleSheet>data).chi.reduce((css: string, node) => {

const str: string = doRender(node, options, reducer);
const str: string = doRender(node, options, reducer, level, sourcemap, line, column);

if (str === '') {

Expand Down Expand Up @@ -127,7 +128,7 @@ function doRender(data: AstNode, options: RenderOptions, reducer: Function, leve
str = `@${(<AstAtRule>node).nam} ${(<AstAtRule>node).val};`;
} else {

str = doRender(node, options, reducer, level + 1);
str = doRender(node, options, reducer, level + 1, sourcemap, line, column);
}

if (css === '') {
Expand Down
2 changes: 1 addition & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function transform(css: string, options: TransformOptions = {}): Transfor
const endTime: number = performance.now();

return {
...parseResult, ...rendered, performance: {
...parseResult, ...rendered, stats: {
bytesIn: css.length,
bytesOut: rendered.code.length,
parse: `${(renderTime - startTime).toFixed(2)}ms`,
Expand Down
57 changes: 0 additions & 57 deletions src/worker.ts

This file was deleted.

Loading

0 comments on commit 3a5ff48

Please sign in to comment.