Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Apr 10, 2021
1 parent ba583f5 commit 024e479
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 1,306 deletions.
29 changes: 22 additions & 7 deletions .eslintrc.js
@@ -1,22 +1,37 @@
"use strict";

const baseRules = require("eslint-config-lydell");

module.exports = {
root: true,
plugins: ["import", "jest", "prettier"],
extends: ["eslint:recommended"],
env: {
es6: true,
node: true,
},
rules: Object.assign({}, baseRules({ import: true }), {
"prettier/prettier": "error",
}),
rules: {
"arrow-body-style": "error",
curly: "error",
"dot-notation": "error",
"no-shadow": "error",
"no-var": "error",
"prefer-const": "error",
"object-shorthand": "error",
"one-var": ["error", "never"],
"prefer-arrow-callback": "error",
"prefer-destructuring": ["error", { array: false, object: true }],
"prefer-rest-params": "error",
"prefer-spread": "error",
"prefer-template": "error",
eqeqeq: ["error", "always", { null: "ignore" }],
strict: "error",
},
overrides: [
{
files: ["*.test.js"],
extends: ["plugin:jest/recommended", "plugin:jest/style"],
env: { jest: true },
rules: baseRules({ builtin: false, jest: true }),
rules: {
"jest/valid-title": "off",
},
},
],
};
6 changes: 2 additions & 4 deletions .github/workflows/check.yml
Expand Up @@ -27,16 +27,14 @@ jobs:
uses: actions/cache@v2
with:
path: node_modules
key:
node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{
hashFiles('package-lock.json') }}
key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}

- name: npm ci
if: steps.cache-node_modules.outputs.cache-hit != 'true'
run: npm ci

- name: ESLint
run: npx --no-install eslint .
run: npx --no-install eslint . --report-unused-disable-directives

- name: Prettier
run: npx --no-install prettier --check .
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Expand Up @@ -27,9 +27,7 @@ jobs:
uses: actions/cache@v2
with:
path: node_modules
key:
node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{
hashFiles('package-lock.json') }}
key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}

- name: npm ci
if: steps.cache-node_modules.outputs.cache-hit != 'true'
Expand Down
3 changes: 3 additions & 0 deletions .prettierrc.json
@@ -0,0 +1,3 @@
{
"proseWrap": "never"
}
30 changes: 9 additions & 21 deletions CHANGELOG.md
@@ -1,37 +1,28 @@
### Version 2.0.0 (2018-12-02)

- Fixed: Test files provided to the CLI are now resolved relative to CWD as
expected.
- Fixed: Test files provided to the CLI are now resolved relative to CWD as expected.
- Changed: Dropped support for Node.js 4.
- Improved: Updated dependencies.
- Improved: Added some tests.

### Version 1.6.2 (2018-04-23)

- Fixed: The dependency range for shift-codegen allowed newer versions, but
eslump relies on a non-public part of it which can change at any time – which
it did (see [#5]). The shift-codegen version is now updated and the changes
for the non-public stuff has been made. The version has also been locked down
to avoid this problem in the future. Thanks to Toru Nagashima (@mysticatea)!
- Fixed: The dependency range for shift-codegen allowed newer versions, but eslump relies on a non-public part of it which can change at any time – which it did (see [#5]). The shift-codegen version is now updated and the changes for the non-public stuff has been made. The version has also been locked down to avoid this problem in the future. Thanks to Toru Nagashima (@mysticatea)!

### Version 1.6.1 (2018-03-12)

- Fixed: The "Babel code frame" now shows up for parse errors again. (Regression
since 1.6.0.)
- Fixed: The "Babel code frame" now shows up for parse errors again. (Regression since 1.6.0.)
- Fixed: `random.js` is now created as expected again. (Regression since 1.6.0.)
- Fixed: The examples were upgraded and fixed, and a `cherow` example was added.
(Not part of the npm package.)
- Fixed: The examples were upgraded and fixed, and a `cherow` example was added. (Not part of the npm package.)

### Version 1.6.0 (2017-06-26)

- Added: Node.js 4 support. Thanks to Teddy Katz (@not-an-aardvark)!
- Added: eslump can now explicitly be used as an npm module, exposing the
`generateRandomJS` function.
- Added: eslump can now explicitly be used as an npm module, exposing the `generateRandomJS` function.

### Version 1.5.1 (2017-03-26)

- Fixed: Random comments can now be inserted at the end of sequences of
whitespace.
- Fixed: Random comments can now be inserted at the end of sequences of whitespace.

### Version 1.5.0 (2017-03-25)

Expand All @@ -42,25 +33,22 @@

### Version 1.4.0 (2017-02-06)

- Improved: The probability of blank lines when using the `--comments` flag is
now greater.
- Improved: The probability of blank lines when using the `--comments` flag is now greater.

### Version 1.3.1 (2017-02-04)

- Fixed: A somewhat rare error message is now indented properly.

### Version 1.3.0 (2017-02-04)

- Improved: Artifacts are now written to disk when using the `--reproduce` flag.
This makes it easier to narrow down errors.
- Improved: Artifacts are now written to disk when using the `--reproduce` flag. This makes it easier to narrow down errors.

### Version 1.2.0 (2017-02-03)

- Improved: The `--comments` option:
- It is now much more robust.
- `/**/` comments can now contain newlines.
- Any sequence of whitespace is now randomized and possibly filled with
comments.
- Any sequence of whitespace is now randomized and possibly filled with comments.

### Version 1.1.1 (2017-01-30)

Expand Down
107 changes: 22 additions & 85 deletions README.md
Expand Up @@ -21,9 +21,6 @@ Inspired by [esfuzz]. Powered by [shift-fuzzer] and [shift-codegen].
- [Disclaimer](#disclaimer)
- [Examples](#examples)
- [Test files](#test-files)
- [Development](#development)
- [npm scripts](#npm-scripts)
- [Directories](#directories)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -116,34 +113,22 @@ Returns a string of random JavaScript code.

If you want, you can pass some options:

| Option | Type | Default | Description |
| ---------- | ------------------------ | ---------- | --------------------------------------------- |
| sourceType | `"module"` or `"script"` | `"module"` | The type of code to generate. |
| maxDepth | integer | 7 | How deeply nested AST:s to generate. |
| comments | boolean | false | Whether or not to generate random comments. |
| whitespace | boolean | false | Whether or not to generate random whitespace. |
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| sourceType | `"module"` or `"script"` | `"module"` | The type of code to generate. |
| maxDepth | integer | 7 | How deeply nested AST:s to generate. |
| comments | boolean | false | Whether or not to generate random comments. |
| whitespace | boolean | false | Whether or not to generate random whitespace. |

## Disclaimer

eslump was created from the need of finding edge cases in [Prettier]. It started
out as a bare-bones little script in a branch on my fork of that repo. As I
wanted more and more features, I extracted it and fleshed it out in its own
repo. Then I realized that it might be useful to others, so I put it on GitHub
and made the CLI installable from npm.
eslump was created from the need of finding edge cases in [Prettier]. It started out as a bare-bones little script in a branch on my fork of that repo. As I wanted more and more features, I extracted it and fleshed it out in its own repo. Then I realized that it might be useful to others, so I put it on GitHub and made the CLI installable from npm.

Initially, eslump basically just strung together [shift-fuzzer] and
[shift-codegen]. Then, I realized that no random comments were generated, so I
hacked that in (along with random whitespace) since comments are very difficult
to get right in Prettier. Then, random parentheses and semicolons where
requested, so I hacked that in as well.
Initially, eslump basically just strung together [shift-fuzzer] and [shift-codegen]. Then, I realized that no random comments were generated, so I hacked that in (along with random whitespace) since comments are very difficult to get right in Prettier. Then, random parentheses and semicolons where requested, so I hacked that in as well.

eslump has successfully found lots of little edge cases in Prettier, so it
evidently works. But there aren’t many tests. (I’ve mostly gone meta and
fuzz-tested it using itself basically.)
eslump has successfully found lots of little edge cases in Prettier, so it evidently works. But there aren’t many tests. (I’ve mostly gone meta and fuzz-tested it using itself basically.)

From the beginning eslump was only ever intended to be a CLI tool, but other
people have started to want to use eslump’s code generation as an npm module, so
these days it can also be used as a module. If you know what you’re doing.
From the beginning eslump was only ever intended to be a CLI tool, but other people have started to want to use eslump’s code generation as an npm module, so these days it can also be used as a module. If you know what you’re doing.

Here are some features I’d like to see from a proper random JS library:

Expand Down Expand Up @@ -206,53 +191,30 @@ module.exports = ({

- The main export is a function, called the _test function._

- The test function accepts a single argument, an object with the following
properties:
- The test function accepts a single argument, an object with the following properties:

- code: `String`. Randomly generated JavaScript, or the contents of
`OUTPUT_DIR/random.js` if using the `--reproduce` flag.
- code: `String`. Randomly generated JavaScript, or the contents of `OUTPUT_DIR/random.js` if using the `--reproduce` flag.

- sourceType: `String`. Either `"module"` or `"script"`. ES2015 can be parsed
in one of these modes, and parsers usually have an option for choosing
between the two.
- sourceType: `String`. Either `"module"` or `"script"`. ES2015 can be parsed in one of these modes, and parsers usually have an option for choosing between the two.

- reproductionData: `undefined` or anything that `JSON.parse` can return.
Normally, it is `undefined`. When using the `--reproduce` flag, this
property contains the result of running `JSON.parse` on the contents of
`OUTPUT_DIR/reproductionData.json`. This is used when the test function
itself generates random data, such as random options for a parser.
- reproductionData: `undefined` or anything that `JSON.parse` can return. Normally, it is `undefined`. When using the `--reproduce` flag, this property contains the result of running `JSON.parse` on the contents of `OUTPUT_DIR/reproductionData.json`. This is used when the test function itself generates random data, such as random options for a parser.

- If the test function is completely deterministic, ignore this property.
- Otherwise, generate random options if it is `undefined`.
- In all other cases, use its data to be able to reproduce a previous error.

- The test function returns nothing if the test succeeded. Then, eslump will run
it again with new random JavaScript code. If the `--reproduce` flag is used,
the test function will only be run once (and if nothing fails in that run
something is wrong).
- The test function returns nothing if the test succeeded. Then, eslump will run it again with new random JavaScript code. If the `--reproduce` flag is used, the test function will only be run once (and if nothing fails in that run something is wrong).

- The test function returns an object with the following properties if the test
fails:
- The test function returns an object with the following properties if the test fails:

- error: `Error`. The caught error. (Technically, this property can have any
value, since anything can be `throw`n.)
- error: `Error`. The caught error. (Technically, this property can have any value, since anything can be `throw`n.)

- reproductionData: Anything that `JSON.stringify` can handle. Optional. If
the test function isn’t completely deterministic, such as when generating
random options for a parser, the data needed to reproduce the error in the
future must be set here. eslump will write this data to
`OUTPUT_DIR/reproductionData.json`. That file will be read, parsed and
passed to the test function when using the `--reproduce` flag.
- reproductionData: Anything that `JSON.stringify` can handle. Optional. If the test function isn’t completely deterministic, such as when generating random options for a parser, the data needed to reproduce the error in the future must be set here. eslump will write this data to `OUTPUT_DIR/reproductionData.json`. That file will be read, parsed and passed to the test function when using the `--reproduce` flag.

- artifacts. `Object`. Optional. Sometimes it can be useful to see
intermediate values in addition to just the random JavaScript when a test
fails, such as the AST from a parser. Each key-value pair describes a file
to write:
- artifacts. `Object`. Optional. Sometimes it can be useful to see intermediate values in addition to just the random JavaScript when a test fails, such as the AST from a parser. Each key-value pair describes a file to write:

- The object keys are file paths relative to `OUTPUT_DIR`. The file will be
written at `OUTPUT_DIR/key`.
- The object values are the contents of the file. (The values will be passed
trough the `String` function before writing.)
- The object keys are file paths relative to `OUTPUT_DIR`. The file will be written at `OUTPUT_DIR/key`.
- The object values are the contents of the file. (The values will be passed trough the `String` function before writing.)

Example:

Expand All @@ -264,36 +226,12 @@ module.exports = ({
}
```

- The test function must not throw errors, so be sure to wrap everything in
try-catch. (eslump will catch uncaught errors, but it will not have a chance
to write `OUTPUT_DIR/reproductionData.json` or any artifacts.)

## Development

You need [Node.js] 10 and npm 7.

### npm scripts

- `npm run eslint`: Run [ESLint] \(including [Prettier]).
- `npm run eslint:fix`: Autofix [ESLint] errors.
- `npm run prettier`: Run [Prettier] for files other than JS.
- `npm run doctoc`: Run [doctoc] on README.md.
- `npm run jest`: Run unit tests. During development, `npm run jest -- --watch`
is nice, and `npm run jest -- --coverage` is interesting.
- `npm test`: Check that everything works.
- `npm publish`: Publish to [npm], but only if `npm test` passes.

### Directories

- `src/`: Source code.
- `examples/`: Examples, also used in tests.
- `test/`: [Jest] tests.
- The test function must not throw errors, so be sure to wrap everything in try-catch. (eslump will catch uncaught errors, but it will not have a chance to write `OUTPUT_DIR/reproductionData.json` or any artifacts.)

## License

[MIT](LICENSE).

<!-- prettier-ignore-start -->
[@babel/generator]: https://github.com/babel/babel/tree/master/packages/babel-generator
[@babel/parser]: https://babeljs.io/docs/en/babel-parser.html
[acorn]: https://github.com/acornjs/acorn
Expand All @@ -316,4 +254,3 @@ You need [Node.js] 10 and npm 7.
[shift-fuzzer]: https://github.com/shapesecurity/shift-fuzzer-js
[shift-parser]: https://github.com/shapesecurity/shift-parser-js
[typescript]: https://github.com/Microsoft/TypeScript
<!-- prettier-ignore-end -->

0 comments on commit 024e479

Please sign in to comment.