Skip to content

Commit

Permalink
refactor: use TypeScript and update to @babel 7 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay authored May 25, 2020
1 parent bba6079 commit 6d6c0c0
Show file tree
Hide file tree
Showing 15 changed files with 1,674 additions and 462 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/rollingversions-canary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Publish Canary

on:
push:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn install --frozen-lockfile
- run: yarn prettier:check
- run: yarn test

publish-canary:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12.x
registry-url: 'https://registry.npmjs.org'
- run: yarn install --frozen-lockfile
- run: yarn build
- run: npx rollingversions publish --canary $GITHUB_RUN_NUMBER
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
38 changes: 38 additions & 0 deletions .github/workflows/rollingversions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release
on:
repository_dispatch:
types: [rollingversions_publish_approved]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn install --frozen-lockfile
- run: yarn prettier:check
- run: yarn test

publish:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12.x
registry-url: 'https://registry.npmjs.org'
- run: yarn install --frozen-lockfile
- run: yarn build
- run: npx rollingversions publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test

on:
pull_request:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn install --frozen-lockfile
- run: yarn prettier:check
- run: yarn test
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Compile time `with` for strict mode JavaScript

[![build status](https://img.shields.io/travis/pugjs/with.svg)](http://travis-ci.org/pugjs/with)
[![Dependency Status](https://img.shields.io/david/pugjs/with.svg)](https://david-dm.org/pugjs/with)
[![NPM version](https://img.shields.io/npm/v/with.svg)](https://www.npmjs.com/package/with)
[![Build Status](https://img.shields.io/github/workflow/status/pugjs/with/Publish%20Canary/master?style=for-the-badge)](https://github.com/pugjs/with/actions?query=workflow%3A%22Publish+Canary%22)
[![Rolling Versions](https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen?style=for-the-badge)](https://rollingversions.com/pugjs/with)
[![NPM version](https://img.shields.io/npm/v/with?style=for-the-badge)](https://www.npmjs.com/package/with)

## Installation

Expand All @@ -13,17 +13,17 @@ Compile time `with` for strict mode JavaScript
## Usage

```js
var addWith = require('with')
var addWith = require('with');

addWith('obj', 'console.log(a)')
addWith('obj', 'console.log(a)');
// => ';(function (console, a) {
// console.log(a)
// }("console" in obj ? obj.console :
// typeof console!=="undefined" ? console : undefined,
// "a" in obj ? obj.a :
// typeof a !== "undefined" ? a : undefined));'

addWith('obj', 'console.log(a)', ['console'])
addWith('obj', 'console.log(a)', ['console']);
// => ';(function (console, a) {
// console.log(a)
// }("a" in obj ? obj.a :
Expand All @@ -38,26 +38,26 @@ The idea is that this is roughly equivallent to:

```js
with (obj) {
src
src;
}
```

There are a few differences though. For starters, assignments to variables will always remain contained within the with block.
There are a few differences though. For starters, assignments to variables will always remain contained within the with block.

e.g.

```js
var foo = 'foo'
var foo = 'foo';
with ({}) {
foo = 'bar'
foo = 'bar';
}
assert(foo === 'bar')// => This fails for compile time with but passes for native with
assert(foo === 'bar'); // => This fails for compile time with but passes for native with

var obj = {foo: 'foo'}
var obj = {foo: 'foo'};
with ({}) {
foo = 'bar'
foo = 'bar';
}
assert(obj.foo === 'bar')// => This fails for compile time with but passes for native with
assert(obj.foo === 'bar'); // => This fails for compile time with but passes for native with
```

It also makes everything be declared, so you can always do:
Expand All @@ -72,16 +72,16 @@ instead of
if (typeof foo === 'undefined')
```

This is not the case if foo is in `exclude`. If a variable is excluded, we ignore it entirely. This is useful if you know a variable will be global as it can lead to efficiency improvements.
This is not the case if foo is in `exclude`. If a variable is excluded, we ignore it entirely. This is useful if you know a variable will be global as it can lead to efficiency improvements.

It is also safe to use in strict mode (unlike `with`) and it minifies properly (`with` disables virtually all minification).

#### Parsing Errors

with internally uses babylon to parse code passed to `addWith`. If babylon throws an error, probably due to a syntax error, `addWith` returns an error wrapping the babylon error, so you can
retrieve location information. `error.component` is `"src"` if the error is in the body or `"obj"` if it's in the object part of the with expression. `error.babylonError` is
with internally uses babylon to parse code passed to `addWith`. If babylon throws an error, probably due to a syntax error, `addWith` returns an error wrapping the babylon error, so you can
retrieve location information. `error.component` is `"src"` if the error is in the body or `"obj"` if it's in the object part of the with expression. `error.babylonError` is
the error thrown from babylon.

## License

MIT
MIT
32 changes: 21 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"description": "Compile time `with` for strict mode JavaScript",
"main": "lib/index.js",
"scripts": {
"prepublish": "babel -d lib src",
"test": "babel-node node_modules/mocha/bin/_mocha test/index.js -R spec"
"build": "tsc",
"postbuild": "rimraf lib/**/__tests__",
"lint": "tslint './src/**/*.{ts,tsx}' -t verbose -p .",
"prettier:write": "prettier --ignore-path .gitignore --write './**/*.{md,json,yaml,js,jsx,ts,tsx}'",
"prettier:check": "prettier --ignore-path .gitignore --list-different './**/*.{md,json,yaml,js,jsx,ts,tsx}'",
"pretest": "yarn build",
"test": "mocha test/index.js -R spec"
},
"repository": {
"type": "git",
Expand All @@ -14,17 +19,22 @@
"author": "ForbesLindesay",
"license": "MIT",
"dependencies": {
"babel-runtime": "^6.11.6",
"babel-types": "^6.15.0",
"babylon": "^6.9.1",
"babylon-walk": "^1.0.2"
"@babel/parser": "^7.9.6",
"@babel/types": "^7.9.6",
"assert-never": "^1.2.1",
"babel-walk": "3.0.0-canary-5"
},
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015": "^6.14.0",
"@forbeslindesay/tsconfig": "^2.0.0",
"@types/node": "^14.0.5",
"mocha": "*",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"tslint": "^6.1.2",
"typescript": "^3.9.3",
"uglify-js": "^2.6.2"
},
"engines": {
"node": ">= 10.0.0"
}
}
}
1 change: 1 addition & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@forbeslindesay/tsconfig/prettier');
146 changes: 0 additions & 146 deletions src/globals.js

This file was deleted.

Loading

0 comments on commit 6d6c0c0

Please sign in to comment.