https://github.com/legend80s/es5-validator
Validate code against ES5
https://github.com/legend80s/es5-validator
Last synced: about 2 months ago
JSON representation
Validate code against ES5
- Host: GitHub
- URL: https://github.com/legend80s/es5-validator
- Owner: legend80s
- License: mit
- Created: 2020-07-11T14:32:37.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-03T06:07:07.000Z (almost 2 years ago)
- Last Synced: 2024-11-11T20:13:07.045Z (7 months ago)
- Language: JavaScript
- Size: 225 KB
- Stars: 23
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# es5-validator
> Validate JS source code against ES5 and report possible syntax errors before shipping to production to make your code safe on iOS 9, iOS 10 or other ES6+ incompatible platforms (IE11).
The earlier you find ES5 incompatibility errors, the earlier you can fix them.
## Use
```sh
npx es5-validator FILE_TO_VALIDATE1.js [FILE_TO_VALIDATE2.js] [https://some-cdn/es.min.js]
```### Or Use as Script
```sh
npm i es5-validator --save-dev
```package.json
```json
{
"scripts": {
"lint:es5": "es5-validator es6.js"
}
}
```es6.js
```js
function func(foo = '') {}
const bar = 1;
``````sh
npm run lint:es5
```Result:
```javascript
[es5-validator] Your code is not ES5 Compatible. It's not ready to ship to production, otherwise it will break you App on iOS 9 or iOS 10.Error: ECMAScript 5 validate failed when parsing es6.js.formatted (1, 18)
> 1 | function func(foo = '') {}
| ^ Invalid ECMAScript 5 syntax
2 | const bar = 1;
```
[Demo repo](https://github.com/legend80s/es5-validator-demo).
## Why it exits
To avoid breaking you App by shipping ES6+ code to production silently.
## How it works
Detect syntax error by parsing source code aganest ES5 using acorn. For more details, you can read the [code](https://github.com/legend80s/es5-validator/blob/master/index.js#L66).
> When encountering a syntax error, the parser will raise a `SyntaxError` object with a meaningful message. The error object will have a `pos` property that indicates the string offset at which the error occurred, and a `loc` object that contains a `{line, column}` object referring to that same position.
> - https://www.npmjs.com/package/acorn```js
acorn.parse(source, {
ecmaVersion: 5,
});
```💡 Notice: Only the syntax can be detected. Methods introduced by ES6+ wont be reported. For example `Object.assign` will not be reported but `spread operator` will.
## Features
- [x] Validate local JS file.
- [x] Validate remote JS file. For example: `npx es5-validator https://cdn.jsdelivr.net/npm/js-pinyin/index.js`.
- [x] Validate multiple JS files concurrently. For example: `npx es5-validator es6-1.js es6-2.js`.
- [x] Validate inline source code directly. For example: `npx es5-validator --inline "const bar = 1;"`.## Todo
- [x] Validate remote js. `npx es5-validator https://cdn.jsdelivr.net/npm/js-pinyin/index.js`.
*Give a star ❤️ if it helped you https://github.com/legend80s/es5-validator.*