1 | # fixclosure
|
2 |
|
3 | fixclosure is JavaScript dependency checker/fixer for Closure Library based on ECMAScript AST.
|
4 | It 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 |
|
19 | The 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)
|
23 | goog.provide("goog.foo.Bar");
|
24 |
|
25 | goog.require("goog.foo");
|
26 | goog.require("goog.unused");
|
27 |
|
28 | goog.foo.Bar = function () {
|
29 | goog.foo.baz();
|
30 | goog.missing.require();
|
31 | };
|
32 | ```
|
33 |
|
34 | Fix it !
|
35 |
|
36 | ```bash
|
37 | $ npx fixclosure --fix-in-place --namespaces=goog.foo,goog.missing foo.js
|
38 | File: foo.js
|
39 |
|
40 | Provided:
|
41 | - goog.foo.Bar
|
42 |
|
43 | Required:
|
44 | - goog.foo
|
45 | - goog.unused
|
46 |
|
47 | Missing Require:
|
48 | - goog.missing
|
49 |
|
50 | Unnecessary Require:
|
51 | - goog.unused
|
52 |
|
53 | FIXED!
|
54 |
|
55 | Total: 1 files
|
56 | Passed: 0 files
|
57 | Fixed: 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)
|
64 | goog.provide("goog.foo.Bar");
|
65 |
|
66 | goog.require("goog.foo");
|
67 | goog.require("goog.missing");
|
68 |
|
69 | goog.foo.Bar = function () {
|
70 | goog.foo.baz();
|
71 | goog.missing.require();
|
72 | };
|
73 | ```
|
74 |
|
75 | ### Rules fixclosure checked
|
76 |
|
77 | fixclosure 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 |
|
85 | The arguments are globbed by [globby](https://github.com/sindresorhus/globby).
|
86 | Directories are expanded as `**/*.js`.
|
87 |
|
88 | ```console
|
89 | $ fixclosure path/to/dir "foo/bar-*.js"
|
90 | ```
|
91 |
|
92 | ### Use with Grunt
|
93 |
|
94 | Use [grunt-fixclosure](https://github.com/teppeis/grunt-fixclosure "grunt-fixclosure") plugin.
|
95 |
|
96 | ## Configuration file
|
97 |
|
98 | fixclosure loads options from `.fixclosurerc` config file like:
|
99 |
|
100 | ```
|
101 | --provideRoots foo,bar
|
102 | --replaceMap foo.foobar:foo.foo
|
103 | --useForwardDeclare
|
104 | ```
|
105 |
|
106 | fixclosure 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 |
|
112 | If an invalid file is found, fixclosure fixes the file in place.
|
113 |
|
114 | ### `--config <file>`
|
115 |
|
116 | `.fixclosurerc` file path.
|
117 | Specify if your file is not in the search path.
|
118 | Default: `${process.cwd()}/.fixclosurerc`
|
119 |
|
120 | ### `--provideRoots <roots>`
|
121 |
|
122 | Specify your root namespaces to provide. Default is `goog`.
|
123 | Comma separated list.
|
124 |
|
125 | ### `--namespaces <namespaces>`
|
126 |
|
127 | Specify method or property exported as a namespace itself like `goog.dispose`.
|
128 | Comma separated list.
|
129 |
|
130 | ### `--replaceMap <map>`
|
131 |
|
132 | Replace method or property to namespace mapping like `goog.disposeAll:goog.dispose`.
|
133 | Comma separated list of colon separated pairs like `foo.bar1:foo.bar2,foo.bar3:foo.bar4`.
|
134 |
|
135 | ### `--useForwardDeclare`
|
136 |
|
137 | Use `goog.forwardDeclare()` instead of `goog.requireType()` for types used only in JSDoc.
|
138 | Default: `false`
|
139 |
|
140 | ### `--depsJs <files>`
|
141 |
|
142 | Load namespace methods from deps.js files separated by comma.
|
143 | You 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 |
|
147 | Show not only failed files but also passed files.
|
148 |
|
149 | ### `--no-color`
|
150 |
|
151 | Disable color output.
|
152 |
|
153 | ## Inline hint
|
154 |
|
155 | fixclosure reads "hint" for lint from special comments in your code.
|
156 |
|
157 | ### `ignore`
|
158 |
|
159 | fixclosure doesn't remove any `goog.provide` and `goog.require` with this hint.
|
160 |
|
161 | ```javascript
|
162 | goog.provide("goog.foo"); // fixclosure: ignore
|
163 |
|
164 | goog.require("goog.bar"); // fixclosure: ignore
|
165 | ```
|
166 |
|
167 | In the above, `goog.provide('goog.foo')` will not removed by fixclosure even if it isn't provided in the file.
|
168 | Also `goog.require('goog.bar')` will not removed if it isn't used.
|
169 | The hint affects only _same_ line.
|
170 | Useful in module declaration.
|
171 |
|
172 | ### `suppressRequire`
|
173 |
|
174 | Suppress `goog.require` auto insertion.
|
175 |
|
176 | ```javascript
|
177 | // fixclosure: suppressRequire
|
178 | goog.foo.bar();
|
179 | ```
|
180 |
|
181 | In the above, `goog.require('goog.foo')` will not inserted.
|
182 | The hint affects only _next_ line.
|
183 | This is useful to workaround cyclic reference.
|
184 |
|
185 | ### `suppressProvide`
|
186 |
|
187 | Suppress `goog.provide` auto insertion.
|
188 |
|
189 | ```javascript
|
190 | // fixclosure: suppressProvide
|
191 | goog.Foo = function () {};
|
192 | ```
|
193 |
|
194 | In the above, `goog.provide('goog.Foo')` will not inserted.
|
195 | The 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 |
|
207 | MIT 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
|