Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid extra iterator generation #17

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
indent_style = tab
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 80

[*.{yml,yaml,json}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[*.snap]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
8 changes: 6 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- name: Setup Git
if: matrix.os == 'windows-latest'
run: git config --global core.autocrlf input

- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: yarn
Expand Down
18 changes: 9 additions & 9 deletions compile-to-definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ const makeDefinitionsForSchema = async (absSchemaPath, schemasDir) => {
const filename = path.resolve(
root,
outputFolder,
`${path.join(directory, basename)}.d.ts`
`${path.join(directory, basename)}.d.ts`,
);
const schema = JSON.parse(fs.readFileSync(absSchemaPath, "utf-8"));
const keys = Object.keys(schema);
if (keys.length === 1 && keys[0] === "$ref") return;

const prettierConfig = await prettier.resolveConfig(
path.resolve(root, outputFolder, "result.d.ts")
path.resolve(root, outputFolder, "result.d.ts"),
);
const style = {
printWidth: prettierConfig.printWidth,
Expand All @@ -56,7 +56,7 @@ const makeDefinitionsForSchema = async (absSchemaPath, schemasDir) => {
(ts) => {
ts = ts.replace(
/\s+\*\s+\* This interface was referenced by `.+`'s JSON-Schema\s+\* via the `definition` ".+"\./g,
""
"",
);
let normalizedContent = "";
try {
Expand All @@ -70,14 +70,14 @@ const makeDefinitionsForSchema = async (absSchemaPath, schemasDir) => {
fs.mkdirSync(path.dirname(filename), { recursive: true });
fs.writeFileSync(filename, ts, "utf-8");
console.error(
`declarations/${relPath.replace(/\\/g, "/")}.d.ts updated`
`declarations/${relPath.replace(/\\/g, "/")}.d.ts updated`,
);
} else {
console.error(
`declarations/${relPath.replace(
/\\/g,
"/"
)}.d.ts need to be updated`
"/",
)}.d.ts need to be updated`,
);
process.exitCode = 1;
}
Expand All @@ -86,7 +86,7 @@ const makeDefinitionsForSchema = async (absSchemaPath, schemasDir) => {
(err) => {
console.error(err);
process.exitCode = 1;
}
},
);
};

Expand All @@ -113,7 +113,7 @@ const preprocessSchema = (schema, root = schema, path = []) => {
const result = resolvePath(root, property.$ref);
if (!result) {
throw new Error(
`Unable to resolve "$ref": "${property.$ref}" in ${path.join("/")}`
`Unable to resolve "$ref": "${property.$ref}" in ${path.join("/")}`,
);
}
schema.properties[key] = {
Expand Down Expand Up @@ -167,7 +167,7 @@ const preprocessSchema = (schema, root = schema, path = []) => {
implementedNames.push(/\/([^\/]+)$/.exec(impl)[1]);
}
const propEntries = Object.entries(schema.properties).filter(
([name]) => !implementedProps.has(name)
([name]) => !implementedProps.has(name),
);
if (propEntries.length > 0) {
const key =
Expand Down
4 changes: 2 additions & 2 deletions declarations.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as ts from "typescript";
declare module "typescript" {
export function getDeclarationModifierFlagsFromSymbol(
s: ts.Symbol
s: ts.Symbol,
): ts.ModifierFlags;
export function signatureHasRestParameter(signature: ts.Signature): boolean;
interface Symbol {
type?: ts.Type;
parent: Symbol | undefined
parent: Symbol | undefined;
}
interface Declaration {
expression?: ts.Expression;
Expand Down
23 changes: 13 additions & 10 deletions format-file-header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const execToArray = (content, regexp) => {
const schema = [
{
title: "license comment",
regexp: /\/\*\n\s*MIT License http:\/\/www\.opensource\.org\/licenses\/mit-license\.php\n\s*(?:(Authors? .+)\n)?\s*\*\/\n/g,
regexp:
/\/\*\n\s*MIT License http:\/\/www\.opensource\.org\/licenses\/mit-license\.php\n\s*(?:(Authors? .+)\n)?\s*\*\/\n/g,
updateMessage: "update the license comment",
update(content, author) {
return (
Expand Down Expand Up @@ -82,12 +83,13 @@ const schema = [
},
{
title: "imports",
regexp: /(const (\{\s+\w+(?::\s+\w+)?(,\s+\w+(?::\s+\w+)?)*\s+\}|\w+) = (\/\*\* @type \{TODO\} \*\/\s\()?require\("[^"]+"\)\)?(\.\w+)*;\n)+\n/g,
regexp:
/(const (\{\s+\w+(?::\s+\w+)?(,\s+\w+(?::\s+\w+)?)*\s+\}|\w+) = (\/\*\* @type \{TODO\} \*\/\s\()?require\("[^"]+"\)\)?(\.\w+)*;\n)+\n/g,
updateMessage: "sort imports alphabetically",
update(content) {
const items = execToArray(
content,
/const (?:\{\s+\w+(?::\s+\w+)?(?:,\s+\w+(?::\s+\w+)?)*\s+\}|\w+) = (?:\/\*\* @type \{TODO\} \*\/\s\()?require\("([^"]+)"\)\)?((?:\.\w+)*);\n/g
/const (?:\{\s+\w+(?::\s+\w+)?(?:,\s+\w+(?::\s+\w+)?)*\s+\}|\w+) = (?:\/\*\* @type \{TODO\} \*\/\s\()?require\("([^"]+)"\)\)?((?:\.\w+)*);\n/g,
);
items.sort(sortImport);
return items.map((item) => item.content).join("") + "\n";
Expand All @@ -97,12 +99,13 @@ const schema = [
},
{
title: "type imports",
regexp: /(\/\*\* (?:@template \w+ )*@typedef \{(?:typeof )?import\("[^"]+"\)(\.\w+)*(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)?\} \w+(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)? \*\/\n)+\n/g,
regexp:
/(\/\*\* (?:@template \w+ )*@typedef \{(?:typeof )?import\("[^"]+"\)(\.\w+)*(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)?\} \w+(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)? \*\/\n)+\n/g,
updateMessage: "sort type imports alphabetically",
update(content) {
const items = execToArray(
content,
/\/\*\* (?:@template \w+ )*@typedef \{(?:typeof )?import\("([^"]+)"\)((?:\.\w+)*(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)?)\} \w+(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)? \*\/\n/g
/\/\*\* (?:@template \w+ )*@typedef \{(?:typeof )?import\("([^"]+)"\)((?:\.\w+)*(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)?)\} \w+(?:<(?:(?:\w\.)*\w+, )*(?:\w\.)*\w+>)? \*\/\n/g,
);
items.sort(sortImport);
return items.map((item) => item.content).join("") + "\n";
Expand Down Expand Up @@ -139,7 +142,7 @@ for (const filePath of allFiles) {
}
if (match.index !== pos) {
console.log(
`${filePath}: Unexpected code at ${pos}-${match.index}, expected ${current.title}`
`${filePath}: Unexpected code at ${pos}-${match.index}, expected ${current.title}`,
);
process.exitCode = 1;
pos = match.index;
Expand Down Expand Up @@ -181,12 +184,12 @@ for (const filePath of allFiles) {
}

const matches = content.match(
/makeSerializable\(\s*[^,]+,\s*"webpack\/lib\/[^"]+"\s*(?:,[^)]+)?\)/g
/makeSerializable\(\s*[^,]+,\s*"webpack\/lib\/[^"]+"\s*(?:,[^)]+)?\)/g,
);
if (matches) {
for (const match of matches) {
const str = /makeSerializable\(\s*[^,]+,\s*"webpack\/lib\/([^"]+)"/.exec(
match
match,
)[1];
allSerializables.add(str);
}
Expand All @@ -195,13 +198,13 @@ for (const filePath of allFiles) {

// Check if internalSerializables.js includes all serializables in webpack
for (const internalSerializables of allFiles.filter((file) =>
file.includes("internalSerializables")
file.includes("internalSerializables"),
)) {
const content = fs.readFileSync(internalSerializables);
for (const serializable of allSerializables) {
if (!content.includes(`"../${serializable}"`)) {
console.log(
`${internalSerializables}: must include static require to ../${serializable}`
`${internalSerializables}: must include static require to ../${serializable}`,
);
process.exitCode = 1;
}
Expand Down
14 changes: 7 additions & 7 deletions format-schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const minSlashes = schemas
.map((p) => p.split(/[\\/]/).length)
.reduce((a, b) => Math.min(a, b), Infinity);
const baseSchemaPaths = schemas.filter(
(p) => p.split(/[\\/]/).length === minSlashes
(p) => p.split(/[\\/]/).length === minSlashes,
);
const baseDefinitions = new Map();
for (const baseSchemaPath of baseSchemaPaths) {
for (const [name, schema] of Object.entries(
require(baseSchemaPath).definitions
require(baseSchemaPath).definitions,
)) {
baseDefinitions.set(name, schema);
}
Expand Down Expand Up @@ -131,15 +131,15 @@ const processJson = processSchema.bind(null, {
...json,
tsType: tsType.replace(
/\.\.\//g,
context.importPrefix + "../"
context.importPrefix + "../",
),
};
}
return json;
},
},
baseDef,
{}
{},
);
json.definitions[key] = baseDef;
}
Expand All @@ -151,21 +151,21 @@ const processJson = processSchema.bind(null, {
for (const impl of [].concat(json.implements)) {
if (!impl.startsWith(prefix)) {
console.warn(
`"implements": "${impl}" -> should start with "${prefix}"`
`"implements": "${impl}" -> should start with "${prefix}"`,
);
continue;
}
const name = impl.slice(prefix.length);
const referencedSchema = context.definitions[name];
if (!referencedSchema) {
console.warn(
`"implements": "${impl}" -> referenced schema not found`
`"implements": "${impl}" -> referenced schema not found`,
);
continue;
}
if (typeof referencedSchema.properties !== "object") {
console.warn(
`"implements": "${impl}" -> referenced schema has no properties`
`"implements": "${impl}" -> referenced schema has no properties`,
);
continue;
}
Expand Down
Loading