Skip to content

Commit

Permalink
feat: Add ES6 module export (#680)
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
spocke committed Dec 23, 2021
1 parent 6d7cfde commit b3e3d5c
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 94 deletions.
14 changes: 10 additions & 4 deletions package.json
Expand Up @@ -7,10 +7,13 @@
"url": "https://github.com/sponsors/fb55"
},
"repository": {
"type": "git",
"url": "https://github.com/fb55/css-what"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "lib/commonjs/index.js",
"module": "lib/es/index.js",
"types": "lib/es/index.d.ts",
"sideEffects": false,
"files": [
"lib/**/*"
],
Expand All @@ -24,7 +27,7 @@
"format:es": "npm run lint:es -- --fix",
"format:prettier": "npm run prettier -- --write",
"prettier": "prettier '**/*.{ts,md,json,yml}'",
"build": "tsc",
"build": "tsc && tsc -p tsconfig.es.json",
"prepare": "npm run build"
},
"devDependencies": {
Expand All @@ -45,7 +48,10 @@
},
"license": "BSD-2-Clause",
"jest": {
"preset": "ts-jest"
"preset": "ts-jest",
"roots": [
"src"
]
},
"prettier": {
"tabWidth": 4
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
@@ -1,3 +1,3 @@
export * from "./parse";
export { default as parse } from "./parse";
export { default as stringify } from "./stringify";
export * from "./types";
export { isTraversal, parse } from "./parse";
export { stringify } from "./stringify";
94 changes: 10 additions & 84 deletions src/parse.ts
@@ -1,83 +1,12 @@
export interface Options {
/**
* When false, tag names will not be lowercased.
* @default true
*/
lowerCaseAttributeNames?: boolean;
/**
* When false, attribute names will not be lowercased.
* @default true
*/
lowerCaseTags?: boolean;
/**
* When `true`, `xmlMode` implies both `lowerCaseTags` and `lowerCaseAttributeNames` are set to `false`.
* Also, `ignoreCase` on attributes will not be inferred based on HTML rules anymore.
* @default false
*/
xmlMode?: boolean;
}

export type Selector =
| PseudoSelector
| PseudoElement
| AttributeSelector
| TagSelector
| UniversalSelector
| Traversal;

export interface AttributeSelector {
type: "attribute";
name: string;
action: AttributeAction;
value: string;
ignoreCase: boolean | null;
namespace: string | null;
}

type DataType = Selector[][] | null | string;

export interface PseudoSelector {
type: "pseudo";
name: string;
data: DataType;
}

export interface PseudoElement {
type: "pseudo-element";
name: string;
}

export interface TagSelector {
type: "tag";
name: string;
namespace: string | null;
}

export interface UniversalSelector {
type: "universal";
namespace: string | null;
}

export interface Traversal {
type: TraversalType;
}

export type AttributeAction =
| "any"
| "element"
| "end"
| "equals"
| "exists"
| "hyphen"
| "not"
| "start";

export type TraversalType =
| "adjacent"
| "child"
| "descendant"
| "parent"
| "sibling";
import type {
Options,
Selector,
AttributeSelector,
Traversal,
AttributeAction,
TraversalType,
DataType,
} from "./types";

const reName = /^[^\\#]?(?:\\(?:[\da-f]{1,6}\s?|.)|[\w\-\u00b0-\uFFFF])+/;
const reEscape = /\\([\da-f]{1,6}\s?|(\s)|.)/gi;
Expand Down Expand Up @@ -220,10 +149,7 @@ function isWhitespace(c: string) {
* The first dimension represents selectors separated by commas (eg. `sub1, sub2`),
* the second contains the relevant tokens for that selector.
*/
export default function parse(
selector: string,
options?: Options
): Selector[][] {
export function parse(selector: string, options?: Options): Selector[][] {
const subselects: Selector[][] = [];

const endIndex = parseSelector(subselects, `${selector}`, options, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/stringify.ts
@@ -1,4 +1,4 @@
import { Selector } from "./parse";
import type { Selector } from "./types";

const actionTypes: Record<string, string> = {
equals: "",
Expand Down Expand Up @@ -29,7 +29,7 @@ const charsToEscape = new Set([
*
* @param selector Selector to stringify.
*/
export default function stringify(selector: Selector[][]): string {
export function stringify(selector: Selector[][]): string {
return selector.map(stringifySubselector).join(", ");
}

Expand Down
80 changes: 80 additions & 0 deletions src/types.ts
@@ -0,0 +1,80 @@
export interface Options {
/**
* When false, tag names will not be lowercased.
* @default true
*/
lowerCaseAttributeNames?: boolean;
/**
* When false, attribute names will not be lowercased.
* @default true
*/
lowerCaseTags?: boolean;
/**
* When `true`, `xmlMode` implies both `lowerCaseTags` and `lowerCaseAttributeNames` are set to `false`.
* Also, `ignoreCase` on attributes will not be inferred based on HTML rules anymore.
* @default false
*/
xmlMode?: boolean;
}

export type Selector =
| PseudoSelector
| PseudoElement
| AttributeSelector
| TagSelector
| UniversalSelector
| Traversal;

export interface AttributeSelector {
type: "attribute";
name: string;
action: AttributeAction;
value: string;
ignoreCase: boolean | null;
namespace: string | null;
}

export type DataType = Selector[][] | null | string;

export interface PseudoSelector {
type: "pseudo";
name: string;
data: DataType;
}

export interface PseudoElement {
type: "pseudo-element";
name: string;
}

export interface TagSelector {
type: "tag";
name: string;
namespace: string | null;
}

export interface UniversalSelector {
type: "universal";
namespace: string | null;
}

export interface Traversal {
type: TraversalType;
}

export type AttributeAction =
| "any"
| "element"
| "end"
| "equals"
| "exists"
| "hyphen"
| "not"
| "start";

export type TraversalType =
| "adjacent"
| "child"
| "descendant"
| "parent"
| "sibling";
9 changes: 9 additions & 0 deletions tsconfig.es.json
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES2019",
"module": "es2015",
"outDir": "lib/es",
"moduleResolution": "node"
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -7,7 +7,7 @@
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
// "sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "lib" /* Redirect output structure to the directory. */,
"outDir": "lib/commonjs" /* Redirect output structure to the directory. */,
// "importHelpers": true, /* Import emit helpers from 'tslib'. */

/* Strict Type-Checking Options */
Expand Down

0 comments on commit b3e3d5c

Please sign in to comment.