Test: add parser check (#8256)
This commit is contained in:
1 parent
7ad515111e
commit
ed3eeb82f8
20 files changed
+84
No files matched your search
@@ -322,6 +322,7 @@
|
||||
"standalones",
|
||||
"Stankiewicz",
|
||||
"starturl",
|
||||
"stylefmt",
|
||||
"styleguides",
|
||||
"stylelint",
|
||||
"stylelintrc",
|
||||
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -4,6 +4,7 @@ const fs = require("fs");
|
||||
const path = require("path");
|
||||
const raw = require("jest-snapshot-serializer-raw").wrap;
|
||||
const { isCI } = require("ci-info");
|
||||
const checkParsers = require("./utils/check-parsers");
|
||||
|
||||
const { TEST_STANDALONE } = process.env;
|
||||
const AST_COMPARE = isCI || process.env.AST_COMPARE;
|
||||
@@ -54,6 +55,9 @@ global.run_spec = (fixtures, parsers, options) => {
|
||||
fixtures = typeof fixtures === "string" ? { dirname: fixtures } : fixtures;
|
||||
const { dirname } = fixtures;
|
||||
|
||||
// Make sure tests are in correct location
|
||||
checkParsers(dirname, parsers);
|
||||
|
||||
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
|
||||
const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
|
||||
dirname,
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
"use strict";
|
||||
|
||||
const path = require("path");
|
||||
const { outdent } = require("outdent");
|
||||
|
||||
const TESTS_ROOT = path.join(__dirname, "../../tests");
|
||||
|
||||
const getCategory = (dirname) =>
|
||||
path.relative(TESTS_ROOT, dirname).split(path.sep).shift();
|
||||
|
||||
const categoryParsers = new Map(
|
||||
[
|
||||
["angular", ["angular", "__ng_interpolation"]],
|
||||
["css", ["css" /* ,"less", "scss" These may allow, but not using */]],
|
||||
["flow", ["babel", "flow", "babel-flow"]],
|
||||
["flow-repo", ["babel", "flow", "babel-flow"]],
|
||||
["graphql", ["graphql"]],
|
||||
["handlebars", ["glimmer"]],
|
||||
["html", ["html"]],
|
||||
["js", ["babel", "flow", "babel-flow", "typescript", "babel-ts"]],
|
||||
["json", ["json", "json5", "json-stringify"]],
|
||||
["jsx", ["babel", "flow", "babel-flow", "typescript", "babel-ts"]],
|
||||
["less", ["css", "less"]],
|
||||
["lwc", ["lwc"]],
|
||||
["markdown", ["markdown"]],
|
||||
["mdx", ["mdx"]],
|
||||
["scss", ["css", "scss"]],
|
||||
["stylefmt-repo", ["css"]],
|
||||
["typescript", ["typescript", "babel-ts"]],
|
||||
["vue", ["vue"]],
|
||||
["yaml", ["yaml"]],
|
||||
].map(([category, parsers]) => [category, new Set(parsers)])
|
||||
);
|
||||
|
||||
const getParserCategories = (parser) => {
|
||||
const categories = [];
|
||||
for (const [category, parsers] of categoryParsers) {
|
||||
if (parsers.has(parser)) {
|
||||
categories.push(category);
|
||||
}
|
||||
}
|
||||
|
||||
return categories;
|
||||
};
|
||||
|
||||
const checkParser = (dirname, parsers) => {
|
||||
const category = getCategory(dirname);
|
||||
const allowedParsers = categoryParsers.get(category);
|
||||
|
||||
if (!allowedParsers) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const parser of parsers) {
|
||||
if (!allowedParsers.has(parser)) {
|
||||
const suggestCategories = getParserCategories(parser);
|
||||
|
||||
const suggestion =
|
||||
suggestCategories.length === 0
|
||||
? ""
|
||||
: outdent`
|
||||
Suggest move your tests to:
|
||||
${suggestCategories
|
||||
.map((category) => `- ${path.join(TESTS_ROOT, category)}`)
|
||||
.join("\n")}
|
||||
|
||||
Or config to allow use this parser in "${__filename}".
|
||||
`;
|
||||
|
||||
throw new Error(
|
||||
`Parser "${parser}" should not used in "${dirname}".${
|
||||
suggestion ? `\n\n${suggestion}` : ""
|
||||
}`
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = checkParser;
|
||||
Reference in new issue
Block a user