UNPKG

5.8 kBMarkdownView Raw
1# fixclosure
2
3fixclosure is JavaScript dependency checker/fixer for Closure Library based on ECMAScript AST.
4It finds namespaces used in a JavaScript file and insert/remove `goog.provide`, `goog.require`, `goog.requireType` and `goog.forwardDeclare` automatically.
5
6[![npm version][npm-image]][npm-url]
7![Node.js Version Support][node-version]
8[![Build Status][ci-image]][ci-url]
9![License][license]
10
11## Install
12
13```bash
14$ npm install fixclosure
15```
16
17## Usage
18
19The following code `goog.require()`s an unused namespace `goog.unused`, also `goog.missing` is used but not `goog.require()`d.
20
21```javascript
22// foo.js (before)
23goog.provide("goog.foo.Bar");
24
25goog.require("goog.foo");
26goog.require("goog.unused");
27
28goog.foo.Bar = function () {
29 goog.foo.baz();
30 goog.missing.require();
31};
32```
33
34Fix it !
35
36```bash
37$ npx fixclosure --fix-in-place --namespaces=goog.foo,goog.missing foo.js
38File: foo.js
39
40Provided:
41- goog.foo.Bar
42
43Required:
44- goog.foo
45- goog.unused
46
47Missing Require:
48- goog.missing
49
50Unnecessary Require:
51- goog.unused
52
53FIXED!
54
55Total: 1 files
56Passed: 0 files
57Fixed: 1 files
58```
59
60`goog.require('goog.unused')` is removed and `goog.require('goog.missing')` is inserted.
61
62```javascript
63// foo.js (fixed)
64goog.provide("goog.foo.Bar");
65
66goog.require("goog.foo");
67goog.require("goog.missing");
68
69goog.foo.Bar = function () {
70 goog.foo.baz();
71 goog.missing.require();
72};
73```
74
75### Rules fixclosure checked
76
77fixclosure checks and fixes:
78
79- Duplicated provide/require/requireType/forwardDeclare
80- Missing provide/require/requireType/forwardDeclare
81- Unnecessary provide/require/requireType/forwardDeclare
82
83### Globbing
84
85The arguments are globbed by [globby](https://github.com/sindresorhus/globby).
86Directories are expanded as `**/*.js`.
87
88```console
89$ fixclosure path/to/dir "foo/bar-*.js"
90```
91
92### Use with Grunt
93
94Use [grunt-fixclosure](https://github.com/teppeis/grunt-fixclosure "grunt-fixclosure") plugin.
95
96## Configuration file
97
98fixclosure loads options from `.fixclosurerc` config file like:
99
100```
101--provideRoots foo,bar
102--replaceMap foo.foobar:foo.foo
103--useForwardDeclare
104```
105
106fixclosure will find the file in the current directory and, if not found, will move one level up the directory tree all the way up to the filesystem root.
107
108## Options
109
110### `-f` or `--fix-in-place`
111
112If an invalid file is found, fixclosure fixes the file in place.
113
114### `--config <file>`
115
116`.fixclosurerc` file path.
117Specify if your file is not in the search path.
118Default: `${process.cwd()}/.fixclosurerc`
119
120### `--provideRoots <roots>`
121
122Specify your root namespaces to provide. Default is `goog`.
123Comma separated list.
124
125### `--namespaces <namespaces>`
126
127Specify method or property exported as a namespace itself like `goog.dispose`.
128Comma separated list.
129
130### `--replaceMap <map>`
131
132Replace method or property to namespace mapping like `goog.disposeAll:goog.dispose`.
133Comma separated list of colon separated pairs like `foo.bar1:foo.bar2,foo.bar3:foo.bar4`.
134
135### `--useForwardDeclare`
136
137Use `goog.forwardDeclare()` instead of `goog.requireType()` for types used only in JSDoc.
138Default: `false`
139
140### `--depsJs <files>`
141
142Load namespace methods from deps.js files separated by comma.
143You can generate deps.js with [google-closure-deps](https://www.npmjs.com/package/google-closure-deps) or [duck](https://www.npmjs.com/package/@teppeis/duck).
144
145### `--showSuccess`
146
147Show not only failed files but also passed files.
148
149### `--no-color`
150
151Disable color output.
152
153## Inline hint
154
155fixclosure reads "hint" for lint from special comments in your code.
156
157### `ignore`
158
159fixclosure doesn't remove any `goog.provide` and `goog.require` with this hint.
160
161```javascript
162goog.provide("goog.foo"); // fixclosure: ignore
163
164goog.require("goog.bar"); // fixclosure: ignore
165```
166
167In the above, `goog.provide('goog.foo')` will not removed by fixclosure even if it isn't provided in the file.
168Also `goog.require('goog.bar')` will not removed if it isn't used.
169The hint affects only _same_ line.
170Useful in module declaration.
171
172### `suppressRequire`
173
174Suppress `goog.require` auto insertion.
175
176```javascript
177// fixclosure: suppressRequire
178goog.foo.bar();
179```
180
181In the above, `goog.require('goog.foo')` will not inserted.
182The hint affects only _next_ line.
183This is useful to workaround cyclic reference.
184
185### `suppressProvide`
186
187Suppress `goog.provide` auto insertion.
188
189```javascript
190// fixclosure: suppressProvide
191goog.Foo = function () {};
192```
193
194In the above, `goog.provide('goog.Foo')` will not inserted.
195The hint affects only _next_ line.
196
197## Migration from v1 to v2
198
199- Old Node.js versions were no longer supported, use Node.js v10 or higher.
200- `--namespaceMethods` was deprecated, use `--namespaces`.
201- Deprecated `--roots` was removed, use `--provideRoots`.
202- `--requireRoots` was removed because fixclosure v2 no longer detects required namespaces heuristically. Use `--namespaces` or `--depsJs` to detect them. They can detect the namespaces correctly.
203- Types used only in JSDoc are reported as errors, while previously only types of `@extends` in `@interface` are reported. Add `goog.requireType()` or `goog.fowardDeclare()`.
204
205### License
206
207MIT License: Teppei Sato <teppeis@gmail.com>
208
209[npm-image]: https://badgen.net/npm/v/fixclosure?icon=npm&label=
210[npm-url]: https://npmjs.org/package/fixclosure
211[ci-image]: https://github.com/teppeis/fixclosure/workflows/ci/badge.svg
212[ci-url]: https://github.com/teppeis/fixclosure/actions?query=workflow%3A%22ci%22
213[deps-image]: https://badgen.net/david/dep/teppeis/fixclosure
214[deps-url]: https://david-dm.org/teppeis/fixclosure
215[node-version]: https://badgen.net/npm/node/fixclosure
216[coverage-image]: https://coveralls.io/repos/github/teppeis/fixclosure/badge.svg?branch=master
217[coverage-url]: https://coveralls.io/github/teppeis/fixclosure?branch=master
218[license]: https://badgen.net/npm/license/fixclosure