Skip to content

Commit 02eaf59

Browse files
committed
New API
1 parent 5c96576 commit 02eaf59

File tree

14 files changed

+1964
-229
lines changed

14 files changed

+1964
-229
lines changed

.eslintrc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"extends": [
3+
"eslint:recommended",
4+
"plugin:@typescript-eslint/recommended",
5+
"prettier/@typescript-eslint",
6+
"plugin:prettier/recommended"
7+
],
8+
"plugins": ["@typescript-eslint", "prettier", "import"],
9+
"env": {
10+
"mocha": true
11+
},
12+
"rules": {
13+
"@typescript-eslint/explicit-function-return-type": "off",
14+
"@typescript-eslint/explicit-module-boundary-types": "off",
15+
"@typescript-eslint/no-use-before-define": [
16+
"error",
17+
{
18+
"functions": false,
19+
"classes": false
20+
}
21+
],
22+
"@typescript-eslint/consistent-type-definitions": ["error", "interface"],
23+
"@typescript-eslint/no-unused-vars": [
24+
"error",
25+
{
26+
"vars": "all",
27+
"args": "after-used",
28+
"ignoreRestSiblings": true,
29+
"argsIgnorePattern": "^_"
30+
}
31+
],
32+
"@typescript-eslint/no-explicit-any": "off",
33+
"@typescript-eslint/no-empty-interface": "error",
34+
"@typescript-eslint/no-non-null-assertion": "off",
35+
"@typescript-eslint/ban-types": "off",
36+
37+
"prettier/prettier": ["error", {"singleQuote": true}],
38+
"no-case-declarations": "off",
39+
"no-irregular-whitespace": "off",
40+
"no-control-regex": "off",
41+
"no-duplicate-imports": ["error", {"includeExports": true}],
42+
"arrow-body-style": ["error", "as-needed"],
43+
"no-restricted-globals": ["error", "name", "toString", "pending"],
44+
"import/order": ["error", {
45+
"groups": ["builtin", "external", "internal", "sibling"],
46+
"pathGroupsExcludedImportTypes": ["parent", "sibling", "index"],
47+
"alphabetize": {
48+
"order": "asc",
49+
"caseInsensitive": true
50+
}
51+
}]
52+
},
53+
"settings": {
54+
"import/resolver": {
55+
"node": {
56+
"extensions": [".ts"]
57+
}
58+
}
59+
},
60+
"parser": "@typescript-eslint/parser"
61+
}

.prettierrc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSpacing": false,
4+
"endOfLine": "lf",
5+
"jsxBracketSameLine": true,
6+
"jsxSingleQuote": true,
7+
"parser": "typescript",
8+
"printWidth": 120,
9+
"proseWrap": "preserve",
10+
"semi": true,
11+
"singleQuote": true,
12+
"tabWidth": 4,
13+
"trailingComma": "none",
14+
15+
"overrides": [
16+
{
17+
"files": ["*.json", ".eslintrc", ".prettierrc"],
18+
"options": {
19+
"parser": "json",
20+
"singleQuote": false,
21+
"trailingComma": "none",
22+
"useTabs": false,
23+
"tabWidth": 2
24+
}
25+
}
26+
]
27+
}

benchmark/benchmark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {CssSelectorParser} from '../src';
1+
import {CssSelectorParser} from '../src/old';
22

33
const parser = new CssSelectorParser();
44

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414
"dependencies": {},
1515
"devDependencies": {
1616
"@types/colors": "^1.2.1",
17+
"@typescript-eslint/eslint-plugin": "^4.11.1",
18+
"@typescript-eslint/parser": "^4.11.1",
1719
"colors": "*",
20+
"eslint": "^7.16.0",
21+
"eslint-config-prettier": "^7.1.0",
22+
"eslint-plugin-import": "^2.22.1",
23+
"eslint-plugin-prettier": "^3.3.0",
24+
"prettier": "^2.2.1",
1825
"ts-node": "^8.8.2",
19-
"typescript": "^3.8.3"
26+
"typescript": "^4.1.3"
2027
},
2128
"repository": {
2229
"type": "git",
@@ -29,6 +36,8 @@
2936
"scripts": {
3037
"build": "tsc",
3138
"test": "ts-node test/test.ts",
39+
"lint": "eslint src/**.ts",
40+
"lint:fix": "eslint --fix src/**.ts",
3241
"benchmark": "ts-node benchmark/benchmark.ts",
3342
"prepublishOnly": "yarn build"
3443
}

src/index.ts

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,2 @@
1-
import {PseudoSelectorType, parseCssSelector} from './parser-context';
2-
import {
3-
Selector, RuleAttr, Rule, Selectors, SelectorEntity, RulePseudo, RuleSet, AttrValueType
4-
} from './selector';
5-
import {renderEntity} from './render';
6-
7-
export {Selector, RuleAttr, Rule, Selectors, SelectorEntity, RulePseudo, RuleSet, AttrValueType, PseudoSelectorType};
8-
9-
export class CssSelectorParser {
10-
protected pseudos: {[pseudo: string]: PseudoSelectorType} = {};
11-
protected attrEqualityMods: {[mod: string]: true} = {};
12-
protected ruleNestingOperators: {[operator: string]: true} = {};
13-
protected substitutesEnabled = false;
14-
15-
public registerSelectorPseudos(...pseudos: string[]) {
16-
for (let pseudo of pseudos) {
17-
this.pseudos[pseudo] = 'selector';
18-
}
19-
return this;
20-
}
21-
22-
public unregisterSelectorPseudos(...pseudos: string[]) {
23-
for (let pseudo of pseudos) {
24-
delete this.pseudos[pseudo];
25-
}
26-
return this;
27-
}
28-
29-
public registerNumericPseudos(...pseudos: string[]) {
30-
for (let pseudo of pseudos) {
31-
this.pseudos[pseudo] = 'numeric';
32-
}
33-
return this;
34-
}
35-
36-
public unregisterNumericPseudos(...pseudos: string[]) {
37-
for (let pseudo of pseudos) {
38-
delete this.pseudos[pseudo];
39-
}
40-
return this;
41-
}
42-
43-
public registerNestingOperators(...operators: string[]) {
44-
for (let operator of operators) {
45-
this.ruleNestingOperators[operator] = true;
46-
}
47-
return this;
48-
}
49-
50-
public unregisterNestingOperators(...operators: string[]) {
51-
for (let operator of operators) {
52-
delete this.ruleNestingOperators[operator];
53-
}
54-
return this;
55-
}
56-
57-
public registerAttrEqualityMods(...mods: string[]) {
58-
for (let mod of mods) {
59-
this.attrEqualityMods[mod] = true;
60-
}
61-
return this;
62-
}
63-
64-
public unregisterAttrEqualityMods(...mods: string[]) {
65-
for (let mod of mods) {
66-
delete this.attrEqualityMods[mod];
67-
}
68-
return this;
69-
}
70-
71-
public enableSubstitutes() {
72-
this.substitutesEnabled = true;
73-
return this;
74-
}
75-
76-
public disableSubstitutes() {
77-
this.substitutesEnabled = false;
78-
return this;
79-
}
80-
81-
public parse(str: string) {
82-
return parseCssSelector(
83-
str,
84-
0,
85-
this.pseudos,
86-
this.attrEqualityMods,
87-
this.ruleNestingOperators,
88-
this.substitutesEnabled
89-
)!;
90-
}
91-
92-
public render(path: Selector) {
93-
return renderEntity(path).trim();
94-
}
95-
}
1+
export {parse} from './parse';
2+
export {render} from './render';

src/old/index.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {PseudoSelectorType, parseCssSelector} from './parser-context';
2+
import {renderEntity} from './render';
3+
import {Selector, RuleAttr, Rule, Selectors, SelectorEntity, RulePseudoClass, RuleSet, AttrValueType} from './selector';
4+
5+
export {Selector, RuleAttr, Rule, Selectors, SelectorEntity, RulePseudoClass, RuleSet, AttrValueType, PseudoSelectorType};
6+
7+
export class CssSelectorParser {
8+
protected pseudos: {[pseudo: string]: PseudoSelectorType} = {};
9+
protected attrEqualityMods: {[mod: string]: true} = {};
10+
protected ruleNestingOperators: {[operator: string]: true} = {};
11+
protected substitutesEnabled = false;
12+
13+
public registerSelectorPseudos(...pseudos: string[]) {
14+
for (const pseudo of pseudos) {
15+
this.pseudos[pseudo] = 'selector';
16+
}
17+
return this;
18+
}
19+
20+
public unregisterSelectorPseudos(...pseudos: string[]) {
21+
for (const pseudo of pseudos) {
22+
delete this.pseudos[pseudo];
23+
}
24+
return this;
25+
}
26+
27+
public registerNumericPseudos(...pseudos: string[]) {
28+
for (const pseudo of pseudos) {
29+
this.pseudos[pseudo] = 'numeric';
30+
}
31+
return this;
32+
}
33+
34+
public unregisterNumericPseudos(...pseudos: string[]) {
35+
for (const pseudo of pseudos) {
36+
delete this.pseudos[pseudo];
37+
}
38+
return this;
39+
}
40+
41+
public registerNestingOperators(...operators: string[]) {
42+
for (const operator of operators) {
43+
this.ruleNestingOperators[operator] = true;
44+
}
45+
return this;
46+
}
47+
48+
public unregisterNestingOperators(...operators: string[]) {
49+
for (const operator of operators) {
50+
delete this.ruleNestingOperators[operator];
51+
}
52+
return this;
53+
}
54+
55+
public registerAttrEqualityMods(...mods: string[]) {
56+
for (const mod of mods) {
57+
this.attrEqualityMods[mod] = true;
58+
}
59+
return this;
60+
}
61+
62+
public unregisterAttrEqualityMods(...mods: string[]) {
63+
for (const mod of mods) {
64+
delete this.attrEqualityMods[mod];
65+
}
66+
return this;
67+
}
68+
69+
public enableSubstitutes() {
70+
this.substitutesEnabled = true;
71+
return this;
72+
}
73+
74+
public disableSubstitutes() {
75+
this.substitutesEnabled = false;
76+
return this;
77+
}
78+
79+
public parse(str: string) {
80+
return parseCssSelector(
81+
str,
82+
0,
83+
this.pseudos,
84+
this.attrEqualityMods,
85+
this.ruleNestingOperators,
86+
this.substitutesEnabled
87+
)!;
88+
}
89+
90+
public render(path: Selector) {
91+
return renderEntity(path).trim();
92+
}
93+
}

0 commit comments

Comments
 (0)