15 Commits
Author SHA1 Message Date
Andrey Sitnik c05932eede Fix linter 2026-07-11 17:47:41 +00:00
Andrey Sitnik 3c60e5b691 Fix CI 2026-07-11 16:46:55 +00:00
Andrey Sitnik 62ad57f190 Prevent prototype hijacking in fromJSON 2026-07-11 16:44:46 +00:00
Sarath FrancisandGitHub be16af8528 Preserve node raws when rehydrating a JSON AST (#2100)
fromJSON() attached child nodes through the container constructor, which
runs them back through append()/normalize(). For root-level children that
re-applies insertion spacing normalization and overwrites each node's own
raws.before with the previous sibling's, so toJSON() -> fromJSON() was not
lossless: stylesheets with non-uniform blank lines between top-level rules
came back with their spacing flattened.

Rehydrate children separately and attach them directly, keeping the raws
exactly as serialized.
2026-06-28 12:56:11 +02:00
Andrey Sitnik 89b7f7ac78 Update dependencies and code style 2023-07-06 13:38:48 +02:00
Remco Haszing f1684ebfa6 Revert tests 2023-03-26 13:51:56 +02:00
Remco Haszing cae533107d Fix type definitions
Many files from postcss are exported. Each of this file is written using
the syntax:

```js
module.exports = Thing
```

This should be typed as:

```ts
export = Thing
```

If such a file needs to export additional types, a namespace should be
used.

However, it was typed as:

```ts
export default thing
```

This was solved at the time in #1459 (by me) by adding the following
code to each module:

```js
Thing.default = Thing
```

Whilst affective at the time, this was not the correct solution. The
introduction of the `"module": "node16"` option in TypeScript made this
issue more apparent. I recommend to remove this in a next semver major
release.

Usage has changed as follows:

**cts**

```jsonc
// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node16"
  }
}
```

```ts
// Old
import postcss = require('postcss')

postcss.default(/* … */)
```

```ts
// New
import postcss = require('postcss')

postcss(/* … */)
```

**cjs**

```jsonc
// tsconfig.json
{
  "compilerOptions": {
    "checkJs": true,
    "module": "commonjs",
    "moduleResolution": "node16"
  }
}
```

```ts
// Old
const postcss = require('postcss')

postcss.default(/* … */)
```

```ts
// New
const postcss = require('postcss')

postcss(/* … */)
```

**mts** / **mjs**

```jsonc
// tsconfig.json
{
  "compilerOptions": {
    "module": "node16"
  }
}
```

```ts
// Old
import postcss from 'postcss'

postcss.default(/* … */)
```

```ts
// New
import postcss from 'postcss'

postcss(/* … */)
```

**bundler**

```jsonc
// tsconfig.json
{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "module": "esnext",
    "moduleResolution": "node16"
  }
}
```

```ts
// Unchanged
import postcss from 'postcss'

postcss(/* … */)
```

**ES module interop** (Not recommended for library code!)

```jsonc
// tsconfig.json
{
  "compilerOptions": {
    "esModuleInterop": true,
    "module": "commonjs",
    "moduleResolution": "node16"
  }
}
```

```ts
// Unchanged
import postcss from 'postcss'

postcss(/* … */)
```

When compiling to CJS, it is still possible to write ESM-like syntax,
for example:

```jsonc
// Same as import postcss = require('postcss')
import * as postcss from 'postcss'
// Named imports from namespaces are allowed
import { root, Root } from 'postcss'
```

The following changes were made to `tsconfig.json`:

- `lib` was specified explicitly. By default, TypeScript includes the
  `dom` types. This is not desirable, it even causes some conflicts with
  types used by Postcss.
- `allowJs` was removed. When this is enabled, TypeScript treats `.d.ts`
  files matching `.js` files as output. This caused some unexpected
  issues where TypeScript simply ignored type errors.
- `skipLibCheck` was removed. Ideally all types just work together. This
  can be added again later if necessary.
- `esModuleInterop` was removed. Enabling this option causes TypeScript
  to allow code that is otherwise invalid. This may be useful for CLIs
  or website, but is dangerous to use for libraries.

Correcting these options also highlighted some issues in the tests.
These have been resolved. `nanodelay@1` has incorrect type definitions,
so it has been marked using `@ts-expect-error` comments.

In `test/errors.ts` were some type errors. These were marked as
`THROWS`, which signals `check-dts` a type error is expected.
This was changed to `@ts-expect-error`, which works too, but it also
works with other TypeScript tooling.

The `Postcss` type was missing some properties. As a side effect of the
corrected types, these are now available. This also affects the `Helper`
type.

Last, but not least, type definitions have been added for `postcss.mjs`.

Closes #1814
2023-02-16 16:17:58 +01:00
Andrey Kim cd9ec4f200 Move from Jest to uvu 2021-11-15 11:25:34 +03:00
Niklas Mischkulnig ad88ece3c5 Cleanup: Typescript 2021-01-07 12:33:44 +01:00
Niklas Mischkulnig 2837bf9435 Cleanup 2021-01-07 12:27:36 +01:00
Niklas Mischkulnig b3405e610c Fix JSON.stringify of an array of nodes 2021-01-07 12:04:45 +01:00
Niklas Mischkulnig 77e19c5179 Fixup test 2020-12-09 12:31:53 +01:00
Niklas Mischkulnig 472de313d2 Test stringifing fromJSON result 2020-12-09 12:31:53 +01:00
Niklas Mischkulnig 781e433bdb Add missing test, linting, typings 2020-12-05 11:07:00 +01:00
Niklas Mischkulnig ec69e352d9 fromJSON and toJSON 2020-12-05 00:15:24 +01:00