Skip to content

Commit eb829e5

Browse files
committed
chore: some refactoriing not finished
1 parent d0cceb1 commit eb829e5

31 files changed

+11141
-2977
lines changed

.DS_Store

10 KB
Binary file not shown.

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
indent_size = 2
8+
indent_style = space
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
node_modules
1+
node_modules
2+
dist
3+
*.log
4+
.coverage
5+
.ignore
File renamed without changes.
File renamed without changes.

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package.json
2+
package-lock.json

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The MIT License (MIT)
22

33
Copyright (c) 2016 Waqar Ali
4+
Copyright (c) 2016 Krister Kari
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy
67
of this software and associated documentation files (the "Software"), to deal

babel-jest.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"use strict"; // eslint-disable-line strict
2+
3+
/**
4+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree. An additional grant
8+
* of patent rights can be found in the PATENTS file in the same directory.
9+
*
10+
*
11+
*/
12+
13+
const crypto = require("crypto");
14+
const fs = require("fs");
15+
const jestPreset = require("babel-preset-jest");
16+
const path = require("path");
17+
18+
const BABELRC_FILENAME = ".babelrc";
19+
const BABELRC_JS_FILENAME = ".babelrc.js";
20+
const BABEL_CONFIG_KEY = "babel";
21+
const PACKAGE_JSON = "package.json";
22+
const THIS_FILE = fs.readFileSync(__filename);
23+
24+
let babel;
25+
26+
const createTransformer = (options) => {
27+
const cache = Object.create(null);
28+
29+
const getBabelRC = (filename) => {
30+
const paths = [];
31+
let directory = filename;
32+
33+
while (directory !== (directory = path.dirname(directory))) {
34+
if (cache[directory]) {
35+
break;
36+
}
37+
38+
paths.push(directory);
39+
const configFilePath = path.join(directory, BABELRC_FILENAME);
40+
41+
if (fs.existsSync(configFilePath)) {
42+
cache[directory] = fs.readFileSync(configFilePath, "utf8");
43+
break;
44+
}
45+
46+
const configJsFilePath = path.join(directory, BABELRC_JS_FILENAME);
47+
48+
if (fs.existsSync(configJsFilePath)) {
49+
// $FlowFixMe
50+
cache[directory] = JSON.stringify(require(configJsFilePath));
51+
break;
52+
}
53+
54+
const packageJsonFilePath = path.join(directory, PACKAGE_JSON);
55+
56+
if (fs.existsSync(packageJsonFilePath)) {
57+
// $FlowFixMe
58+
const packageJsonFileContents = require(packageJsonFilePath);
59+
60+
if (packageJsonFileContents[BABEL_CONFIG_KEY]) {
61+
cache[directory] = JSON.stringify(
62+
packageJsonFileContents[BABEL_CONFIG_KEY]
63+
);
64+
65+
break;
66+
}
67+
}
68+
}
69+
paths.forEach((directoryPath) => (cache[directoryPath] = cache[directory]));
70+
71+
return cache[directory] || "";
72+
};
73+
74+
options = Object.assign({}, options, {
75+
plugins: (options && options.plugins) || [],
76+
presets: ((options && options.presets) || []).concat([jestPreset]),
77+
retainLines: true,
78+
});
79+
80+
delete options.cacheDirectory;
81+
delete options.filename;
82+
83+
return {
84+
canInstrument: true,
85+
getCacheKey(fileData, filename, configString, _ref) {
86+
const instrument = _ref.instrument;
87+
88+
return crypto
89+
.createHash("md5")
90+
.update(THIS_FILE)
91+
.update("\0", "utf8")
92+
.update(fileData)
93+
.update("\0", "utf8")
94+
.update(configString)
95+
.update("\0", "utf8")
96+
.update(getBabelRC(filename))
97+
.update("\0", "utf8")
98+
.update(instrument ? "instrument" : "")
99+
.digest("hex");
100+
},
101+
process(src, filename, config, transformOptions) {
102+
if (!babel) {
103+
babel = require("@babel/core");
104+
}
105+
106+
if (babel.util && !babel.util.canCompile(filename)) {
107+
return src;
108+
}
109+
110+
const theseOptions = Object.assign({ filename }, options);
111+
112+
if (transformOptions && transformOptions.instrument) {
113+
// theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
114+
// Copied from jest-runtime transform.js
115+
theseOptions.plugins = theseOptions.plugins.concat([
116+
[
117+
require("babel-plugin-istanbul").default,
118+
{
119+
// files outside `cwd` will not be instrumented
120+
cwd: config.rootDir,
121+
exclude: [],
122+
},
123+
],
124+
]);
125+
}
126+
127+
return babel.transform(src, theseOptions).code;
128+
},
129+
};
130+
};
131+
132+
module.exports = createTransformer();
133+
module.exports.createTransformer = createTransformer;

jest-setup.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
"use strict"; // eslint-disable-line strict
2+
3+
const _ = require("lodash");
4+
const stylelint = require("stylelint");
5+
6+
global.testRule = (rule, schema) => {
7+
expect.extend({
8+
toHaveMessage(testCase) {
9+
if (testCase.message === undefined) {
10+
return {
11+
message: () =>
12+
'Expected "reject" test case to have a "message" property',
13+
pass: false,
14+
};
15+
}
16+
17+
return {
18+
pass: true,
19+
};
20+
},
21+
});
22+
23+
// eslint-disable-next-line jest/valid-describe
24+
describe(`${schema.ruleName}`, () => {
25+
const stylelintConfig = {
26+
plugins: ["./src"],
27+
rules: {
28+
[schema.ruleName]: schema.config,
29+
},
30+
};
31+
32+
if (schema.accept && schema.accept.length) {
33+
describe("accept", () => {
34+
schema.accept.forEach((testCase) => {
35+
const spec = testCase.only ? it.only : it;
36+
37+
spec(testCase.description || "no description", () => {
38+
const options = {
39+
code: testCase.code,
40+
config: stylelintConfig,
41+
syntax: schema.syntax,
42+
};
43+
44+
return stylelint.lint(options).then((output) => {
45+
expect(output.results[0].warnings).toEqual([]);
46+
47+
if (!schema.fix) {
48+
return;
49+
}
50+
51+
// Check the fix
52+
return stylelint
53+
.lint(Object.assign({ fix: true }, options))
54+
.then((output2) => {
55+
const fixedCode = getOutputCss(output2);
56+
57+
expect(fixedCode).toBe(testCase.code);
58+
});
59+
});
60+
});
61+
});
62+
});
63+
}
64+
65+
if (schema.reject && schema.reject.length) {
66+
describe("reject", () => {
67+
schema.reject.forEach((testCase) => {
68+
const spec = testCase.only ? it.only : it;
69+
70+
spec(testCase.description || "no description", () => {
71+
const options = {
72+
code: testCase.code,
73+
config: stylelintConfig,
74+
syntax: schema.syntax,
75+
};
76+
77+
return stylelint.lint(options).then((output) => {
78+
const warnings = output.results[0].warnings;
79+
const warning = warnings[0];
80+
81+
expect(warnings.length).toBeGreaterThanOrEqual(1);
82+
// expect(testCase).toHaveMessage();
83+
84+
if (testCase.message !== undefined) {
85+
expect(_.get(warning, "text")).toBe(testCase.message);
86+
}
87+
88+
if (testCase.line !== undefined) {
89+
expect(_.get(warning, "line")).toBe(testCase.line);
90+
}
91+
92+
if (testCase.column !== undefined) {
93+
expect(_.get(warning, "column")).toBe(testCase.column);
94+
}
95+
96+
if (!schema.fix) {
97+
return;
98+
}
99+
100+
if (!testCase.fixed) {
101+
throw new Error(
102+
"If using { fix: true } in test schema, all reject cases must have { fixed: .. }"
103+
);
104+
}
105+
106+
// Check the fix
107+
return stylelint
108+
.lint(Object.assign({ fix: true }, options))
109+
.then((output2) => {
110+
const fixedCode = getOutputCss(output2);
111+
112+
expect(fixedCode).toBe(testCase.fixed);
113+
});
114+
});
115+
});
116+
});
117+
});
118+
}
119+
});
120+
};
121+
122+
function getOutputCss(output) {
123+
const result = output.results[0]._postcssResult;
124+
const css = result.root.toString(result.opts.syntax);
125+
126+
return css;
127+
}
128+
129+
global.testConfig = (input) => {
130+
let testFn;
131+
132+
if (input.only) {
133+
testFn = test.only;
134+
} else if (input.skip) {
135+
testFn = test.skip;
136+
} else {
137+
testFn = test;
138+
}
139+
140+
testFn(input.description, () => {
141+
const config = {
142+
plugins: ["./"],
143+
rules: {
144+
[input.ruleName]: input.config,
145+
},
146+
};
147+
148+
return stylelint
149+
.lint({
150+
code: "",
151+
config,
152+
})
153+
.then((data) => {
154+
const invalidOptionWarnings = data.results[0].invalidOptionWarnings;
155+
156+
if (input.valid) {
157+
expect(invalidOptionWarnings).toHaveLength(0);
158+
} else {
159+
expect(invalidOptionWarnings[0].text).toBe(input.message);
160+
}
161+
});
162+
});
163+
};

npm

Whitespace-only changes.

0 commit comments

Comments
 (0)