Skip to content

Commit

Permalink
Export DataType namespace
Browse files Browse the repository at this point in the history
Fixes #138
  • Loading branch information
frenic committed May 16, 2022
1 parent 684b078 commit 22f3c89
Show file tree
Hide file tree
Showing 28 changed files with 29 additions and 49 deletions.
3 changes: 0 additions & 3 deletions __tests__/__fixtures__/typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ propertyValueWithAutocompleteHack([1]); // Should fail

const differentMajorVersions: CSS.Properties = {} as OldCSS.Properties;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type DataTypeShouldNotBeExposed = CSS.DataType.Color;

css;
cssWithFallbackValues;
cssWithHyphenProperties;
Expand Down
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-3.5.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,3 @@ exports[`Typescript 3.5 detects errors 15`] = `
Type '[1]' is not assignable to type '[0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 4 more ... | \\"min-intrinsic\\"]'.
Type '1' is not assignable to type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 4 more ... | \\"min-intrinsic\\"'."
`;

exports[`Typescript 3.5 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-3.6.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,3 @@ exports[`Typescript 3.6 detects errors 15`] = `
Type '[1]' is not assignable to type '[0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 4 more ... | \\"min-intrinsic\\"]'.
Type '1' is not assignable to type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 4 more ... | \\"min-intrinsic\\"'."
`;

exports[`Typescript 3.6 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-3.7.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,3 @@ exports[`Typescript 3.7 detects errors 15`] = `
Type '[1]' is not assignable to type '[0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 4 more ... | \\"min-intrinsic\\"]'.
Type '1' is not assignable to type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 4 more ... | \\"min-intrinsic\\"'."
`;

exports[`Typescript 3.7 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-3.8.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 3.8 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 3.8 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 5 more ... | [...]'."`;

exports[`Typescript 3.8 detects errors 15`] = `"145:35 - Type '1' is not assignable to type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 4 more ... | \\"min-intrinsic\\"'."`;

exports[`Typescript 3.8 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-3.9.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 3.9 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 3.9 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 5 more ... | [...]'."`;

exports[`Typescript 3.9 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 3.9 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-4.0.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 4.0 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 4.0 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 5 more ... | [...]'."`;

exports[`Typescript 4.0 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 4.0 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-4.1.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 4.1 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 4.1 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type '0 | \\"auto\\" | \\"inherit\\" | (string & {}) | \\"-moz-initial\\" | \\"initial\\" | \\"revert\\" | \\"unset\\" | \\"-moz-fit-content\\" | \\"-moz-max-content\\" | \\"-moz-min-content\\" | \\"fit-content\\" | \\"max-content\\" | ... 5 more ... | [...]'."`;

exports[`Typescript 4.1 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 4.1 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-4.2.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 4.2 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 4.2 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type 'Width<0 | (string & {})> | [Width<0 | (string & {})>]'."`;

exports[`Typescript 4.2 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 4.2 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-4.3.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 4.3 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 4.3 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type 'Width<0 | (string & {})> | [Width<0 | (string & {})>]'."`;

exports[`Typescript 4.3 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 4.3 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-4.4.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 4.4 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 4.4 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type 'Width<0 | (string & {})> | [Width<0 | (string & {})>]'."`;

exports[`Typescript 4.4 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 4.4 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-4.5.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 4.5 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 4.5 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type 'Width<0 | (string & {})> | [Width<0 | (string & {})>]'."`;

exports[`Typescript 4.5 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 4.5 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 0 additions & 2 deletions __tests__/__snapshots__/dist.typescript-latest.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ exports[`Typescript 4.6 detects errors 13`] = `"134:25 - This condition will alw
exports[`Typescript 4.6 detects errors 14`] = `"144:34 - Argument of type '1' is not assignable to parameter of type 'Width<0 | (string & {})> | [Width<0 | (string & {})>]'."`;

exports[`Typescript 4.6 detects errors 15`] = `"145:35 - Type '1' is not assignable to type 'Width<0 | (string & {})>'."`;

exports[`Typescript 4.6 detects errors 16`] = `"150:38 - Namespace '\\"index\\"' has no exported member 'DataType'."`;
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-3.5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 3.5', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(3.5);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-3.6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 3.6', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(3.6);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-3.7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 3.7', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(3.7);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-3.8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 3.8', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(3.8);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-3.9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 3.9', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(3.9);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-4.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 4.0', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(4.0);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-4.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 4.1', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(4.1);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-4.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 4.2', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(4.2);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-4.3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 4.3', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(4.3);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-4.4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 4.4', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(4.4);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-4.5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 4.5', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(4.5);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dist.typescript-latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Typescript 4.6', () => {
});

expect(Number(ts.versionMajorMinor)).toBe(4.6);
expect(errors.length).toBe(16);
expect(errors.length).toBe(15);
for (const error of errors) {
expect(error).toMatchSnapshot();
}
Expand Down
8 changes: 7 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20313,7 +20313,13 @@ export namespace AtRule {
type Zoom = "auto" | (string & {}) | (number & {});
}

declare namespace DataType {
/**
* **Attention!** Data types receives its name from the spec. E.g. `<color>` becomes `DataType.Color` and
* `<content-distribution>` becomes `DataType.ContentDistribution`. It happens quite frequent that these data types
* are split into several data types or/and name changes as the spec develops. So there's a risk that a minor/patch
* update from `csstype` can break your typing if you're using the `DataType` namespace.
*/
export namespace DataType {
type AbsoluteSize = "large" | "medium" | "small" | "x-large" | "x-small" | "xx-large" | "xx-small" | "xxx-large";

type AnimateableFeature = "contents" | "scroll-position" | (string & {});
Expand Down
11 changes: 7 additions & 4 deletions src/declarator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export type SimpleType = Exclude<DeclarableType, IAlias>;

export interface INamespace {
name: string;
export: boolean;
comment?: string;
body: () => (Interface | IDeclaration)[];
}

Expand Down Expand Up @@ -274,12 +274,16 @@ export async function declarator(minTypesInDataTypes: number) {

const propertyNamespace: INamespace = {
name: 'Property',
export: true,
body: () => Array.from(propertyDeclarations.values()),
};
const dataTypeNamespace: INamespace = {
name: 'DataType',
export: false,
comment: `/**
* **Attention!** Data types receives its name from the spec. E.g. \`<color>\` becomes \`DataType.Color\` and
* \`<content-distribution>\` becomes \`DataType.ContentDistribution\`. It happens quite frequent that these data types
* are split into several data types or/and name changes as the spec develops. So there's a risk that a minor/patch
* update from \`csstype\` can break your typing if you're using the \`DataType\` namespace.
*/`,
body: () => Array.from(dataTypeDeclarations.values()),
};

Expand Down Expand Up @@ -374,7 +378,6 @@ export async function declarator(minTypesInDataTypes: number) {
const atRuleInterfaces: Interface[] = [];
const atRuleNamespace: INamespace = {
name: 'AtRule',
export: true,
body: () => [...atRuleInterfaces, ...Array.from(atRuleDeclarations.values())],
};

Expand Down
8 changes: 3 additions & 5 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ export default async function typescript() {
namespaceOutput += EOL;
}

if (namespace.export) {
namespaceOutput += 'export ';
} else {
namespaceOutput += 'declare ';
if (namespace.comment) {
namespaceOutput += namespace.comment + EOL;
}

namespaceOutput += `namespace ${namespace.name} {${EOL}`;
namespaceOutput += `export namespace ${namespace.name} {${EOL}`;

const body = namespace.body();

Expand Down

0 comments on commit 22f3c89

Please sign in to comment.